import request from '@/utils/http' import type { QcTask } from '@/api/qc-task' export type QcInspectionBatch = { id: string workOrderId?: string batchNo: string batchName: string orderNum?: number assigneeUserIds: string[] assigneeUserNames?: string hasLab?: boolean hasThirdParty?: boolean labItemIds?: string[] thirdPartyItemIds?: string[] taskCount?: number completedTaskCount?: number tasks?: QcTask[] } export type QcInspectionBatchConfigureItem = { id?: string batchName: string assigneeUserIds: string[] hasLab: boolean hasThirdParty: boolean labItemIds: string[] thirdPartyItemIds: string[] } export function fetchQcInspectionBatches(workOrderId: string) { return request.get({ url: `/web/admin/qc-work-orders/${workOrderId}/inspection-batches` }) } export function fetchQcInspectionBatchesBatch(workOrderIds: string[]) { return request.post>({ url: '/web/admin/qc-work-orders/inspection-batches/batch', data: { workOrderIds: workOrderIds.filter(Boolean) } }) } /** 优先使用详情内嵌批次,否则独立 GET */ export async function loadQcInspectionBatches( workOrderId: string, embedded?: QcInspectionBatch[] | null ): Promise { if (embedded?.length) return embedded if (!workOrderId) return [] try { return (await fetchQcInspectionBatches(workOrderId)) || [] } catch { return [] } } type QcBatchTaskLike = { id?: string batchId?: string taskStatus?: string assigneeUserNames?: string } export type QcBatchTaskSection = { key: string batchId: string batchNo: string batchName: string assigneeUserNames: string taskCount: number completedTaskCount: number tasks: T[] } export function batchTasksPanelSlot(batchKey: string) { return `batch-tasks-${batchKey}` } function resolveBatchSectionTasks( batch: QcInspectionBatch, fallbackTasks?: T[] ): T[] { const enrichedById = new Map() if (fallbackTasks?.length) { for (const t of fallbackTasks) { if (t.id) enrichedById.set(t.id, t) } } const rawFromBatch = (batch.tasks || []) as T[] if (enrichedById.size && rawFromBatch.length) { return rawFromBatch.map((t) => (t.id && enrichedById.get(t.id)) || t) } if (enrichedById.size && batch.id) { return fallbackTasks!.filter((t) => (t.batchId || '').trim() === batch.id) } return rawFromBatch } export function buildBatchSectionsFromBatches( batches: QcInspectionBatch[], fallbackTasks?: T[] ): QcBatchTaskSection[] { if (batches.length) { return batches.map((b) => { const tasks = resolveBatchSectionTasks(b, fallbackTasks) const completed = tasks.filter((t) => t.taskStatus === 'ywc').length return { key: b.id || b.batchNo, batchId: b.id, batchNo: b.batchNo, batchName: b.batchName || b.batchNo || '检测批次', assigneeUserNames: b.assigneeUserNames || '', taskCount: b.taskCount ?? tasks.length, completedTaskCount: b.completedTaskCount ?? completed, tasks } }) } if (fallbackTasks?.length) { return buildBatchSectionsFromTasks(fallbackTasks) } return [] } export function buildBatchSectionsFromTasks( tasks: T[] ): QcBatchTaskSection[] { const map = new Map() const order: string[] = [] for (const t of tasks) { const bid = (t.batchId || '').trim() || '__legacy__' if (!map.has(bid)) { map.set(bid, []) order.push(bid) } map.get(bid)!.push(t) } return order.map((bid) => { const group = map.get(bid)! const completed = group.filter((t) => t.taskStatus === 'ywc').length return { key: bid, batchId: bid === '__legacy__' ? '' : bid, batchNo: '', batchName: bid === '__legacy__' ? '历史任务' : '检测批次', assigneeUserNames: group[0]?.assigneeUserNames || '', taskCount: group.length, completedTaskCount: completed, tasks: group } }) } export function resolveBatchSections( batches: QcInspectionBatch[] | undefined | null, tasks: T[] | undefined | null ): QcBatchTaskSection[] { const fromBatches = buildBatchSectionsFromBatches(batches || [], tasks || undefined) if (fromBatches.length) return fromBatches if (tasks?.length) return buildBatchSectionsFromTasks(tasks) return [] } export function createEmptyInspectionBatch(defaultAssigneeUserIds?: string[]): QcInspectionBatchConfigureItem { const today = new Date() const y = today.getFullYear() const m = String(today.getMonth() + 1).padStart(2, '0') const d = String(today.getDate()).padStart(2, '0') const assigneeUserIds = (defaultAssigneeUserIds || []) .map((id) => String(id || '').trim()) .filter(Boolean) return { batchName: `${y}${m}${d}检测`, assigneeUserIds, hasLab: false, hasThirdParty: false, labItemIds: [], thirdPartyItemIds: [] } }