feat(M08-B): 补保洁照片上传和自动建单
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { ResultSetHeader, RowDataPacket } from 'mysql2/promise';
|
||||
import type { PoolConnection, ResultSetHeader, RowDataPacket } from 'mysql2/promise';
|
||||
import type { AccessProfile } from '../auth/rbac-repository.js';
|
||||
import type { MySqlPool } from '../db/mysql.js';
|
||||
|
||||
@@ -47,6 +47,12 @@ interface CountRow extends RowDataPacket { total: number }
|
||||
interface StatusCountRow extends RowDataPacket { status: CleaningTaskStatus; total: number }
|
||||
interface AmountRow extends RowDataPacket { amount: number | null }
|
||||
interface CurrentStatusRow extends RowDataPacket { status: CleaningTaskStatus; cleanerUserId: string | null }
|
||||
interface FinishedOrderRow extends RowDataPacket {
|
||||
id: string;
|
||||
orderNo: string;
|
||||
storeId: string;
|
||||
roomId: string;
|
||||
}
|
||||
|
||||
export class CleaningTaskRepository {
|
||||
constructor(private readonly pool: MySqlPool) {}
|
||||
@@ -98,17 +104,84 @@ export class CleaningTaskRepository {
|
||||
return this.moveMine(input, 'CLAIMED', 'STARTED', 'START', 'started_at', '');
|
||||
}
|
||||
|
||||
async rework(input: CleaningActor & { taskId: string }) {
|
||||
return this.moveMine(input, 'REJECTED', 'STARTED', 'REWORK', 'started_at', '', {
|
||||
photo_urls_json: JSON.stringify([]),
|
||||
reject_reason: ''
|
||||
});
|
||||
}
|
||||
|
||||
async submit(input: CleaningActor & { taskId: string; photoUrls: string[]; note?: string }) {
|
||||
const task = await this.moveMine(
|
||||
input,
|
||||
'STARTED',
|
||||
'SUBMITTED',
|
||||
'SUBMIT',
|
||||
'submitted_at',
|
||||
input.note ?? '',
|
||||
{ photo_urls_json: JSON.stringify(input.photoUrls.slice(0, 9)) }
|
||||
this.assertCleaner(input.access, 'write');
|
||||
if (input.photoUrls.length === 0) throw new CleaningTaskError('CLEANING_PHOTO_REQUIRED');
|
||||
const [rows] = await this.pool.execute<CurrentStatusRow[]>(
|
||||
`SELECT status, cleaner_user_id AS cleanerUserId
|
||||
FROM qipai_cleaning_tasks
|
||||
WHERE tenant_id = ? AND id = ? AND cleaner_user_id = ? AND deleted_at IS NULL`,
|
||||
[input.tenantId, input.taskId, input.userId]
|
||||
);
|
||||
return task;
|
||||
const current = rows[0];
|
||||
if (!current || !['STARTED', 'REJECTED'].includes(current.status)) {
|
||||
throw new CleaningTaskError('CLEANING_TASK_STATUS_CONFLICT');
|
||||
}
|
||||
const [result] = await this.pool.execute<ResultSetHeader>(
|
||||
`UPDATE qipai_cleaning_tasks
|
||||
SET status = 'SUBMITTED', submitted_at = UTC_TIMESTAMP(3),
|
||||
photo_urls_json = ?, reject_reason = ''
|
||||
WHERE tenant_id = ? AND id = ? AND cleaner_user_id = ? AND status = ?
|
||||
AND deleted_at IS NULL`,
|
||||
[JSON.stringify(input.photoUrls.slice(0, 9)), input.tenantId, input.taskId,
|
||||
input.userId, current.status]
|
||||
);
|
||||
if (result.affectedRows !== 1) throw new CleaningTaskError('CLEANING_TASK_STATUS_CONFLICT');
|
||||
await this.recordEvent(input, input.taskId, current.status, 'SUBMITTED', 'SUBMIT', input.note ?? '');
|
||||
return this.getMineTask(input, input.taskId);
|
||||
}
|
||||
|
||||
async assertCanUploadPhoto(input: CleaningActor & { taskId: string }) {
|
||||
this.assertCleaner(input.access, 'write');
|
||||
const [rows] = await this.pool.execute<CurrentStatusRow[]>(
|
||||
`SELECT status, cleaner_user_id AS cleanerUserId
|
||||
FROM qipai_cleaning_tasks
|
||||
WHERE tenant_id = ? AND id = ? AND cleaner_user_id = ? AND status IN ('STARTED', 'REJECTED')
|
||||
AND deleted_at IS NULL`,
|
||||
[input.tenantId, input.taskId, input.userId]
|
||||
);
|
||||
if (!rows[0]) throw new CleaningTaskError('CLEANING_TASK_STATUS_CONFLICT');
|
||||
}
|
||||
|
||||
async createForFinishedOrder(
|
||||
connection: PoolConnection,
|
||||
input: { tenantId: string; orderId: string; actorId: string; traceId: string }
|
||||
) {
|
||||
const [orders] = await connection.execute<FinishedOrderRow[]>(
|
||||
`SELECT id, order_no AS orderNo, store_id AS storeId, room_id AS roomId
|
||||
FROM qipai_orders
|
||||
WHERE tenant_id = ? AND id = ? AND status = 'FINISHED' AND deleted_at IS NULL
|
||||
LIMIT 1`,
|
||||
[input.tenantId, input.orderId]
|
||||
);
|
||||
const order = orders[0];
|
||||
if (!order) throw new CleaningTaskError('CLEANING_ORDER_NOT_FINISHED');
|
||||
const taskNo = `CLN-${order.orderNo}`;
|
||||
const [result] = await connection.execute<ResultSetHeader>(
|
||||
`INSERT IGNORE INTO qipai_cleaning_tasks
|
||||
(tenant_id, store_id, room_id, order_id, task_no, status, priority,
|
||||
reward_cents, requirement, photo_urls_json)
|
||||
VALUES (?, ?, ?, ?, ?, 'WAITING', 5, 0, '订单结束后保洁', JSON_ARRAY())`,
|
||||
[input.tenantId, order.storeId, order.roomId, input.orderId, taskNo]
|
||||
);
|
||||
if (result.affectedRows === 1) {
|
||||
await connection.execute(
|
||||
`INSERT IGNORE INTO qipai_cleaning_task_events
|
||||
(tenant_id, task_id, from_status, to_status, action, actor_id, trace_id, note, metadata)
|
||||
SELECT tenant_id, id, NULL, 'WAITING', 'AUTO_CREATE', ?, ?, '订单结束自动创建保洁任务',
|
||||
JSON_OBJECT('orderId', ?)
|
||||
FROM qipai_cleaning_tasks
|
||||
WHERE tenant_id = ? AND order_id = ?`,
|
||||
[input.actorId, input.traceId, input.orderId, input.tenantId, input.orderId]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async stats(input: CleaningActor) {
|
||||
@@ -181,19 +254,26 @@ export class CleaningTaskRepository {
|
||||
action: string,
|
||||
timestampColumn: 'started_at' | 'submitted_at',
|
||||
note: string,
|
||||
extra?: { photo_urls_json: string }
|
||||
extra?: { photo_urls_json?: string; reject_reason?: string }
|
||||
) {
|
||||
this.assertCleaner(input.access, 'write');
|
||||
const setExtra = extra ? ', photo_urls_json = ?' : '';
|
||||
const params: Array<string | number> = extra
|
||||
? [extra.photo_urls_json, input.tenantId, input.taskId, input.userId, from]
|
||||
: [input.tenantId, input.taskId, input.userId, from];
|
||||
const extraAssignments: string[] = [];
|
||||
const extraParams: Array<string | number> = [];
|
||||
if (extra?.photo_urls_json !== undefined) {
|
||||
extraAssignments.push('photo_urls_json = ?');
|
||||
extraParams.push(extra.photo_urls_json);
|
||||
}
|
||||
if (extra?.reject_reason !== undefined) {
|
||||
extraAssignments.push('reject_reason = ?');
|
||||
extraParams.push(extra.reject_reason);
|
||||
}
|
||||
const setExtra = extraAssignments.length > 0 ? `, ${extraAssignments.join(', ')}` : '';
|
||||
const [result] = await this.pool.execute<ResultSetHeader>(
|
||||
`UPDATE qipai_cleaning_tasks
|
||||
SET status = ?, ${timestampColumn} = UTC_TIMESTAMP(3)${setExtra}
|
||||
WHERE tenant_id = ? AND id = ? AND cleaner_user_id = ? AND status = ?
|
||||
AND deleted_at IS NULL`,
|
||||
[to, ...params]
|
||||
[to, ...extraParams, input.tenantId, input.taskId, input.userId, from]
|
||||
);
|
||||
if (result.affectedRows !== 1) throw new CleaningTaskError('CLEANING_TASK_STATUS_CONFLICT');
|
||||
await this.recordEvent(input, input.taskId, from, to, action, note);
|
||||
|
||||
Reference in New Issue
Block a user