修复订单显示bug
This commit is contained in:
@@ -12,28 +12,11 @@ import {
|
||||
startDepartById,
|
||||
startServiceById
|
||||
} from '@/api/delivery.uts'
|
||||
import { saveServiceRecord as saveRealServiceRecord } from '@/services/serviceOrderService.uts'
|
||||
import {
|
||||
acceptCareOrder,
|
||||
checkInCareOrder,
|
||||
completeCareOrder,
|
||||
getCareOrderDetail,
|
||||
getCareRecords,
|
||||
getDeliveryCareDashboard,
|
||||
getDeliveryCareProfile,
|
||||
getHistoryCareOrders,
|
||||
getPendingCareOrders,
|
||||
getTodayCareOrders,
|
||||
markCareOrderArrived,
|
||||
markCareOrderDeparted,
|
||||
rejectCareOrder,
|
||||
startCareService,
|
||||
submitCareAbnormalReport,
|
||||
submitCareServiceRecord,
|
||||
updateCareOrderStatus,
|
||||
updateDeliveryCareOnlineStatus
|
||||
} from '@/mock/delivery-care.mock.uts'
|
||||
import { requireDeliveryAuth } from '@/utils/deliveryAuth.uts'
|
||||
getOrderDetail as getDirectServiceOrderDetail,
|
||||
getOrdersByTab as getDirectOrdersByTab
|
||||
} from '@/services/serviceOrderService.uts'
|
||||
import { getUserInfo, requireDeliveryAuth, setDeliveryInfo } from '@/utils/deliveryAuth.uts'
|
||||
import type {
|
||||
DeliveryAbnormalReportType,
|
||||
DeliveryCheckinPayloadType,
|
||||
@@ -53,6 +36,87 @@ import type {
|
||||
DeliveryServiceRecordType
|
||||
} from '@/types/delivery.uts'
|
||||
|
||||
function nowText(): string {
|
||||
return new Date().toISOString().replace('T', ' ').substring(0, 19)
|
||||
}
|
||||
|
||||
function emptyDashboard(): DeliveryDashboardType {
|
||||
return {
|
||||
pendingAssignmentCount: 0,
|
||||
pendingAcceptCount: 0,
|
||||
todayOrderCount: 0,
|
||||
pendingDepartCount: 0,
|
||||
servingCount: 0,
|
||||
completedCount: 0,
|
||||
exceptionCount: 0,
|
||||
expectedIncome: 0,
|
||||
onlineStatus: 'resting',
|
||||
nextOrder: null,
|
||||
recentOrders: [] as Array<DeliveryOrderType>
|
||||
} as DeliveryDashboardType
|
||||
}
|
||||
|
||||
async function getCurrentStaffIdOrEmpty(): Promise<string> {
|
||||
const authResult = await requireDeliveryAuth({ redirectOnFail: false, toastOnFail: false })
|
||||
if (!authResult.ok || authResult.deliveryInfo == null) {
|
||||
return ''
|
||||
}
|
||||
return authResult.deliveryInfo.id
|
||||
}
|
||||
|
||||
function hasOrderCoreInfo(order: DeliveryOrderType | null): boolean {
|
||||
if (order == null) {
|
||||
return false
|
||||
}
|
||||
return order.serviceName != '' || order.elderName != '' || order.address != '' || order.contactName != ''
|
||||
}
|
||||
|
||||
function filterOrdersWithCoreInfo(orders: Array<DeliveryOrderType>): Array<DeliveryOrderType> {
|
||||
const result = [] as Array<DeliveryOrderType>
|
||||
for (let i = 0; i < orders.length; i++) {
|
||||
if (hasOrderCoreInfo(orders[i])) {
|
||||
result.push(orders[i])
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
function shouldFallbackOrders(orders: Array<DeliveryOrderType>): boolean {
|
||||
if (orders.length == 0) {
|
||||
return false
|
||||
}
|
||||
for (let i = 0; i < orders.length; i++) {
|
||||
if (hasOrderCoreInfo(orders[i])) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
function mapOrderToRecord(order: DeliveryOrderType): DeliveryRecordType {
|
||||
const isAccepted = order.status == 'completed' || order.status == 'pending_acceptance' || order.status == 'settled' || order.status == 'archived'
|
||||
return {
|
||||
id: order.id,
|
||||
orderId: order.id,
|
||||
orderNo: order.orderNo,
|
||||
serviceName: order.serviceName,
|
||||
elderName: order.fullElderName != '' ? order.fullElderName : order.elderName,
|
||||
elderNameMasked: order.elderNameMasked != '' ? order.elderNameMasked : order.elderName,
|
||||
status: order.status,
|
||||
statusText: order.statusText,
|
||||
appointmentStartTime: order.appointmentStartTime,
|
||||
appointmentTime: order.appointmentTime,
|
||||
actualStartTime: order.actualStartTime,
|
||||
actualEndTime: order.actualEndTime != '' ? order.actualEndTime : order.finishTime,
|
||||
staffIncome: order.staffIncome,
|
||||
settlementStatus: order.settlementStatus != '' ? order.settlementStatus : '待结算',
|
||||
acceptanceStatus: order.status,
|
||||
exceptionDesc: order.exceptionDesc,
|
||||
ratingText: isAccepted ? '已验收' : (order.status == 'pending_acceptance' ? '待验收' : '待补充'),
|
||||
hasServiceRecord: order.serviceRecord != null
|
||||
} as DeliveryRecordType
|
||||
}
|
||||
|
||||
export async function loginDelivery(payload: DeliveryLoginPayloadType): Promise<DeliveryLoginResultType> {
|
||||
return await loginDeliveryApi(payload)
|
||||
}
|
||||
@@ -228,19 +292,63 @@ export async function getDeliveryDashboardStats(): Promise<DeliveryDashboardType
|
||||
}
|
||||
|
||||
export async function getPendingServiceOrders(): Promise<Array<DeliveryOrderType>> {
|
||||
return await getDeliveryOrders({ tab: 'pending', keyword: '' } as DeliveryOrderQueryType)
|
||||
const orders = await getDeliveryOrders({ tab: 'pending', keyword: '' } as DeliveryOrderQueryType)
|
||||
const validOrders = filterOrdersWithCoreInfo(orders)
|
||||
if (validOrders.length > 0) {
|
||||
return validOrders
|
||||
}
|
||||
if (shouldFallbackOrders(orders)) {
|
||||
console.warn('[deliveryService] pending orders missing core info, fallback to direct query')
|
||||
const fallbackOrders = await getDirectOrdersByTab('pending')
|
||||
if (fallbackOrders.length > 0) {
|
||||
return fallbackOrders
|
||||
}
|
||||
}
|
||||
return orders
|
||||
}
|
||||
|
||||
export async function getTodayServiceOrders(): Promise<Array<DeliveryOrderType>> {
|
||||
return await getDeliveryOrders({ tab: 'today', keyword: '' } as DeliveryOrderQueryType)
|
||||
const orders = await getDeliveryOrders({ tab: 'today', keyword: '' } as DeliveryOrderQueryType)
|
||||
const validOrders = filterOrdersWithCoreInfo(orders)
|
||||
if (validOrders.length > 0) {
|
||||
return validOrders
|
||||
}
|
||||
if (shouldFallbackOrders(orders)) {
|
||||
console.warn('[deliveryService] today orders missing core info, fallback to direct query')
|
||||
const fallbackOrders = await getDirectOrdersByTab('today')
|
||||
if (fallbackOrders.length > 0) {
|
||||
return fallbackOrders
|
||||
}
|
||||
}
|
||||
return orders
|
||||
}
|
||||
|
||||
export async function getHistoryServiceOrders(): Promise<Array<DeliveryOrderType>> {
|
||||
return await getDeliveryOrders({ tab: 'history', keyword: '' } as DeliveryOrderQueryType)
|
||||
const orders = await getDeliveryOrders({ tab: 'history', keyword: '' } as DeliveryOrderQueryType)
|
||||
const validOrders = filterOrdersWithCoreInfo(orders)
|
||||
if (validOrders.length > 0) {
|
||||
return validOrders
|
||||
}
|
||||
if (shouldFallbackOrders(orders)) {
|
||||
console.warn('[deliveryService] history orders missing core info, fallback to direct query')
|
||||
const fallbackOrders = await getDirectOrdersByTab('history')
|
||||
if (fallbackOrders.length > 0) {
|
||||
return fallbackOrders
|
||||
}
|
||||
}
|
||||
return orders
|
||||
}
|
||||
|
||||
export async function getServiceOrderDetail(orderId: string): Promise<DeliveryOrderType | null> {
|
||||
return await getDeliveryOrderDetail(orderId)
|
||||
const order = await getDeliveryOrderDetailById(orderId)
|
||||
if (!hasOrderCoreInfo(order)) {
|
||||
console.warn('[deliveryService] order detail missing core info, fallback to direct query:', orderId)
|
||||
const fallbackOrder = await getDirectServiceOrderDetail(orderId)
|
||||
if (hasOrderCoreInfo(fallbackOrder)) {
|
||||
return fallbackOrder
|
||||
}
|
||||
}
|
||||
return order
|
||||
}
|
||||
|
||||
export async function acceptServiceOrder(orderId: string): Promise<DeliveryOrderType | null> {
|
||||
@@ -294,4 +402,4 @@ export async function updateOrderStatus(orderId: string, nextStatus: DeliveryOrd
|
||||
export async function getAbnormalReport(orderId: string): Promise<DeliveryAbnormalReportType | null> {
|
||||
const order = getCareOrderDetail(orderId)
|
||||
return order != null ? order.abnormalReport : null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -739,7 +739,24 @@ async function listWorkerTaskIds(): Promise<Array<string>> {
|
||||
if (userId == '') {
|
||||
return [] as Array<string>
|
||||
}
|
||||
const careTaskResponse = await supa.from('ec_care_tasks').select('id').eq('assigned_to', userId).order('created_at', { ascending: false }).execute()
|
||||
|
||||
// FIX: userId 是 auth.uid(),但 assigned_to / current_staff_id 存的是 ml_delivery_staff.id
|
||||
// 需要先查出当前用户对应的 staff_id
|
||||
const staffResponse = await supa.from('ml_delivery_staff')
|
||||
.select('id')
|
||||
.eq('uid', userId)
|
||||
.single()
|
||||
.execute()
|
||||
|
||||
const staffId = (staffResponse.error == null && staffResponse.data != null)
|
||||
? readString(staffResponse.data as any, 'id')
|
||||
: ''
|
||||
|
||||
if (staffId == '') {
|
||||
return [] as Array<string>
|
||||
}
|
||||
|
||||
const careTaskResponse = await supa.from('ec_care_tasks').select('id').eq('assigned_to', staffId).order('created_at', { ascending: false }).execute()
|
||||
if (careTaskResponse.error == null && careTaskResponse.data != null) {
|
||||
const result = [] as Array<string>
|
||||
const rows = careTaskResponse.data as Array<any>
|
||||
@@ -753,16 +770,7 @@ async function listWorkerTaskIds(): Promise<Array<string>> {
|
||||
return result
|
||||
}
|
||||
}
|
||||
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()
|
||||
const legacyResponse = await supa.from('hss_service_orders').select('id').eq('current_staff_id', staffId).order('created_at', { ascending: false }).execute()
|
||||
if (legacyResponse.error != null || legacyResponse.data == null) {
|
||||
return [] as Array<string>
|
||||
}
|
||||
@@ -783,7 +791,8 @@ async function isCareTask(taskId: string): Promise<boolean> {
|
||||
}
|
||||
|
||||
async function completeWorkerTask(taskId: string): Promise<HomeServiceTaskType | null> {
|
||||
// LEGACY/TODO: 已切换为调用 rpc_delivery_finish_service。
|
||||
// LEGACY/TODO: 本函数旧逻辑为前端直接 update ec_care_tasks + insert hc_work_order_events。
|
||||
// 已切换为调用 rpc_delivery_finish_service(delivery 端统一动作 RPC)。
|
||||
const { data, error } = await supa.rpc('rpc_delivery_finish_service', {
|
||||
p_order_id: taskId,
|
||||
p_payload: {} as any
|
||||
@@ -953,7 +962,8 @@ export async function advanceWorkerTask(taskId: string): Promise<HomeServiceTask
|
||||
}
|
||||
|
||||
export async function submitWorkerCheckIn(taskId: string, note: string): Promise<HomeServiceTaskType | null> {
|
||||
// LEGACY/TODO: 已切换为调用 rpc_delivery_checkin_order + rpc_delivery_start_service。
|
||||
// LEGACY/TODO: 本函数旧逻辑为前端直接 insert ec_care_records + update ec_care_tasks + insert hc_work_order_events。
|
||||
// 已切换为调用 rpc_delivery_checkin_order 与 rpc_delivery_start_service。
|
||||
const checkinResult = await supa.rpc('rpc_delivery_checkin_order', {
|
||||
p_order_id: taskId,
|
||||
p_payload: {
|
||||
@@ -975,7 +985,8 @@ export async function submitWorkerCheckIn(taskId: string, note: string): Promise
|
||||
}
|
||||
|
||||
export async function submitWorkerServiceRecord(taskId: string, summary: string): Promise<HomeServiceTaskType | null> {
|
||||
// LEGACY/TODO: 已切换为调用 rpc_delivery_save_progress。
|
||||
// LEGACY/TODO: 本函数旧逻辑为前端直接 upsert ec_care_records + update ec_care_tasks + insert hc_work_order_events。
|
||||
// 已切换为调用 rpc_delivery_save_progress。
|
||||
const { data, error } = await supa.rpc('rpc_delivery_save_progress', {
|
||||
p_order_id: taskId,
|
||||
p_payload: {
|
||||
@@ -991,7 +1002,8 @@ export async function submitWorkerServiceRecord(taskId: string, summary: string)
|
||||
}
|
||||
|
||||
export async function submitWorkerException(taskId: string, exceptionType: string, description: string): Promise<HomeServiceTaskType | null> {
|
||||
// LEGACY/TODO: 已切换为调用 rpc_delivery_submit_exception。
|
||||
// LEGACY/TODO: 本函数旧逻辑为前端直接 insert hc_work_order_exceptions + update ec_care_tasks + insert hc_work_order_events。
|
||||
// 已切换为调用 rpc_delivery_submit_exception。
|
||||
const { data, error } = await supa.rpc('rpc_delivery_submit_exception', {
|
||||
p_order_id: taskId,
|
||||
p_payload: {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user