跑通consumer下单之后派单给delivery2

This commit is contained in:
2026-05-27 19:09:17 +08:00
parent c26f2c5431
commit 1bf9d11c35
13 changed files with 2554 additions and 436 deletions

View File

@@ -645,7 +645,16 @@ async function listWorkerTaskIds(): Promise<Array<string>> {
return result
}
}
const legacyResponse = await supa.from('hss_service_orders').select('id').eq('current_staff_id', userId).order('created_at', { ascending: false }).execute()
let staffProfileId = ''
const staffResponse = await supa.from('ml_delivery_staff').select('id').eq('uid', userId).limit(1).execute()
if (staffResponse.error == null && staffResponse.data != null) {
const staffRows = staffResponse.data as Array<any>
if (staffRows.length > 0) {
staffProfileId = readString(staffRows[0], 'id')
}
}
const legacyStaffId = staffProfileId != '' ? staffProfileId : userId
const legacyResponse = await supa.from('hss_service_orders').select('id').eq('current_staff_id', legacyStaffId).order('created_at', { ascending: false }).execute()
if (legacyResponse.error != null || legacyResponse.data == null) {
return [] as Array<string>
}

View File

@@ -27,6 +27,8 @@ export type CreateServiceOrderParams = {
remark: string
}
const HOMECARE_DISPATCH_CANDIDATE_RPC = 'rpc_homecare_dispatch_candidate'
function nowText(): string {
return new Date().toISOString().replace('T', ' ').substring(0, 19)
}
@@ -254,36 +256,16 @@ function getStaffPriority(staff: any): number {
}
async function getAutoAssignableStaff(): Promise<any | null> {
const staffResponse = await supa
.from('ml_delivery_staff')
.select('id, uid, station_id, status, deleted_at, is_active, online_status, updated_at, created_at')
.eq('status', 1)
.eq('online_status', 'online')
.execute()
if (staffResponse.error != null || staffResponse.data == null) {
try {
const rpcResponse: any = await supa.rpc(HOMECARE_DISPATCH_CANDIDATE_RPC, {} as any)
if (rpcResponse == null || rpcResponse.error != null || rpcResponse.data == null) {
return null
}
return rpcResponse.data
} catch (error) {
console.warn('getAutoAssignableStaff rpc failed', error)
return null
}
const rawStaffList = staffResponse.data as Array<any>
let selected: any = null
let bestScore = -1
let bestTime = ''
for (let i = 0; i < rawStaffList.length; i++) {
const staff = rawStaffList[i]
if (!isStaffActive(staff)) {
continue
}
if (readString(staff, 'uid') == '') {
continue
}
const score = getStaffPriority(staff)
const timeMark = readFirstString(staff, ['updated_at', 'created_at'])
if (selected == null || score > bestScore || (score == bestScore && timeMark > bestTime)) {
selected = staff
bestScore = score
bestTime = timeMark
}
}
return selected
}
function buildEcServiceRequestPayload(params: CreateServiceOrderParams, userId: string, requestId: string, createdAt: string, appointmentTime: string | null, useAddressSnapshot: boolean): any {