import type { QcWorkOrder } from '@/api/qc-work-order' import { fetchQcWorkOrderDetail } from '@/api/qc-work-order' import { fetchMyQcTaskList, type QcTask } from '@/api/qc-task' import { groupInboundQcWorkOrders, matchesTemporaryQcSourceFilter } from '@/utils/qc-work-order-groups' import type { QcWorkOrderTabKey } from '@/utils/qc-work-order-tabs' import { resolveTabKeyFromWorkOrder, tabUsesInfusionTimeFilter, getQcWorkOrderTab, toWorkOrderListQuery } from '@/utils/qc-work-order-tabs' export type QcTaskMineFilters = { keyword?: string taskType?: string taskStatus?: string tabKey: QcWorkOrderTabKey } export type MineWorkOrderSearchFilters = { keyword?: string workOrderStatus?: string triggerSource?: string createTimeStart?: string createTimeEnd?: string infusionTimeStart?: string infusionTimeEnd?: string tabKey: QcWorkOrderTabKey } export type MineWorkOrderEntry = { workOrderId: string workOrder: QcWorkOrder tabKey: QcWorkOrderTabKey tasks: QcTask[] } const MINE_TASK_FETCH_PAGE_SIZE = 100 function dateInRange(value: string | undefined, start?: string, end?: string): boolean { const v = (value || '').trim().slice(0, 10) if (!v) return !start && !end if (start && v < start) return false if (end && v > end) return false return true } function keywordMatchesWorkOrder(row: QcWorkOrder, keyword: string, tabKey: QcWorkOrderTabKey): boolean { const kw = keyword.trim().toLowerCase() if (!kw) return true const tab = getQcWorkOrderTab(tabKey)! const fields = tabKey === 'temporary' ? [row.workOrderNo, row.productionWorkOrderNo] : tabKey === 'outbound_basic_experiment' ? [row.workOrderNo, row.applicationNo, row.projectTitle, row.customerName, row.applicationName] : tab.group === 'outbound' ? [row.workOrderNo, row.applicationNo, row.applicationName, row.patientName] : [row.workOrderNo, row.sampleCode, row.labStorageCode] return fields.some((f) => (f || '').toLowerCase().includes(kw)) } export function filterMineEntriesByWorkOrderSearch( entries: MineWorkOrderEntry[], filters: MineWorkOrderSearchFilters ): MineWorkOrderEntry[] { const tabKey = filters.tabKey const tab = getQcWorkOrderTab(tabKey)! return entries.filter((entry) => { const row = entry.workOrder if (resolveTabKeyFromWorkOrder(row) !== tabKey) return false if (filters.workOrderStatus && row.workOrderStatus !== filters.workOrderStatus) return false if (filters.triggerSource) { if (tabKey === 'temporary') { if (!matchesTemporaryQcSourceFilter(row, filters.triggerSource)) return false } else if (tab.listLayout !== 'standalone' && row.triggerSource !== filters.triggerSource) { return false } } if (!keywordMatchesWorkOrder(row, filters.keyword || '', tabKey)) return false if (tabUsesInfusionTimeFilter(tab)) { if (!dateInRange(row.infusionTime, filters.infusionTimeStart, filters.infusionTimeEnd)) { return false } } else if (!dateInRange(row.createTime, filters.createTimeStart, filters.createTimeEnd)) { return false } return true }) } export async function fetchAllMineTasks(filters: QcTaskMineFilters): Promise { const tab = getQcWorkOrderTab(filters.tabKey)! const listQuery = toWorkOrderListQuery(tab) const pageSize = MINE_TASK_FETCH_PAGE_SIZE let page = 1 const all: QcTask[] = [] let total = 0 while (true) { const res = await fetchMyQcTaskList({ page, pageSize, keyword: filters.keyword || undefined, taskType: filters.taskType || undefined, taskStatus: filters.taskStatus || undefined, qcBizType: tab.workOrderType, receptionSource: listQuery.receptionSource, bizType: listQuery.bizType }) const list = res.list || [] all.push(...list) total = res.total || 0 if (!list.length || all.length >= total) break page += 1 } return all } export async function enrichMineEntriesWithWorkOrders( entries: MineWorkOrderEntry[] ): Promise { const cache = new Map() const uniqueIds = [...new Set(entries.map((entry) => entry.workOrderId))] await Promise.all( uniqueIds.map(async (id) => { try { const detail = await fetchQcWorkOrderDetail(id) cache.set(id, detail) } catch { // 保留任务聚合的工单摘要 } }) ) return entries.map((entry) => { const detail = cache.get(entry.workOrderId) if (!detail) return entry return { ...entry, workOrder: { ...detail, hasLab: detail.hasLab ?? entry.workOrder.hasLab, hasThirdParty: detail.hasThirdParty ?? entry.workOrder.hasThirdParty } } }) } export function paginateMineEntries(items: T[], page: number, pageSize: number) { const safePage = Math.max(1, page) const safeSize = Math.max(1, pageSize) const start = (safePage - 1) * safeSize return { total: items.length, items: items.slice(start, start + safeSize) } } export function buildMineTableRows(pageEntries: MineWorkOrderEntry[]): { rows: QcWorkOrder[] presetTasksByWorkOrderId: Record } { return { rows: pageEntries.map((e) => e.workOrder), presetTasksByWorkOrderId: buildPresetTasksMap(pageEntries) } } export function resolveWorkOrderTabKey(task: QcTask): QcWorkOrderTabKey { const woType = task.qcBizType || task.orderSource || 'inbound' if (woType === 'outbound') { return resolveTabKeyFromWorkOrder({ workOrderType: 'outbound', bizType: task.receptionSource }) } if (woType === 'temporary') { return 'temporary' } return resolveTabKeyFromWorkOrder({ workOrderType: 'inbound', receptionSource: task.receptionSource }) } export function buildWorkOrderFromTasks(tasks: QcTask[]): QcWorkOrder { const t = tasks[0] const tabKey = resolveWorkOrderTabKey(t) const woType = getQcWorkOrderTab(tabKey)?.workOrderType || 'inbound' return { id: t.workOrderId, workOrderNo: t.workOrderNo || '—', workOrderType: woType, workOrderStatus: t.workOrderStatus || '', receptionId: '', protocolSampleId: '', collectionProtocolId: '', protocolNo: t.protocolNo || '', sampleCode: t.sampleCode || '', receptionSource: t.receptionSource, receptionSourceLabel: t.receptionSourceLabel, qcBizType: t.qcBizType || woType, qcBizTypeLabel: t.qcBizTypeLabel, cellProductId: t.cellProductId, cellProductCode: t.cellProductCode, cellProductName: t.cellProductName, cellProductTypeName: t.cellProductTypeName, cellProductPassage: t.cellProductPassage, cellProductSpecName: t.cellProductSpecName, sampleTypeName: t.sampleTypeName, receptionTime: t.receptionTime, handlerName: t.handlerName, hasLab: tasks.some((x) => x.taskType === 'sysz'), hasThirdParty: tasks.some((x) => x.taskType === 'dsfjc'), createTime: t.createTime || '', bizType: woType === 'outbound' ? t.receptionSource : undefined, bizTypeLabel: woType === 'outbound' ? t.receptionSourceLabel : undefined, protocolStatus: t.protocolStatus, protocolStatusLabel: t.protocolStatusLabel } } /** 按工单聚合任务,保持 API 返回顺序 */ export function groupMineTasksByWorkOrder(tasks: QcTask[]): MineWorkOrderEntry[] { const map = new Map() const order: string[] = [] for (const row of tasks) { const key = row.workOrderId || row.workOrderNo || row.id if (!map.has(key)) { map.set(key, []) order.push(key) } map.get(key)!.push(row) } return order.map((workOrderId) => { const groupTasks = map.get(workOrderId)! return { workOrderId, workOrder: buildWorkOrderFromTasks(groupTasks), tabKey: resolveWorkOrderTabKey(groupTasks[0]), tasks: groupTasks } }) } export function buildPresetTasksMap(entries: MineWorkOrderEntry[]): Record { const map: Record = {} for (const entry of entries) { map[entry.workOrder.id] = entry.tasks } return map } export type MineCardListModel = { inboundGroups: ReturnType standaloneEntries: MineWorkOrderEntry[] presetTasksByWorkOrderId: Record hasItems: boolean } /** 入库走 SampleGroup,出库/临时走 SingleCard standalone */ export function buildMineCardListModel( tasks: QcTask[], tabKey: QcWorkOrderTabKey ): MineCardListModel { const entries = groupMineTasksByWorkOrder(tasks).filter((e) => e.tabKey === tabKey) const presetTasksByWorkOrderId = buildPresetTasksMap(entries) const tab = getQcWorkOrderTab(tabKey)! const useInboundGroups = tab.listLayout === 'inbound-grouped' const inboundGroups = useInboundGroups ? groupInboundQcWorkOrders(entries.map((e) => e.workOrder)) : [] const standaloneEntries = useInboundGroups ? [] : entries return { inboundGroups, standaloneEntries, presetTasksByWorkOrderId, hasItems: entries.length > 0 } }