48 lines
2.0 KiB
TypeScript
48 lines
2.0 KiB
TypeScript
import { getLoadingAssociationSummaryByInboundId, type LoadingAssociationSummary } from '@/shared/api/inboundCodeRelations'
|
|||
|
|
import { groupSmartLoadingItems } from './smartLoadingItems'
|
||
|
|
import type { SmartLoadingPlan } from './types'
|
||
|
|
|
||
|
|
export interface SmartLoadingPlanCoLoadGroup {
|
||
|
|
groupId: string
|
||
|
|
groupNo: string
|
||
|
|
lockedPlanId: string
|
||
|
|
memberOrderCount: number
|
||
|
|
inboundFragmentCount: number
|
||
|
|
fullPackageCount: number
|
||
|
|
}
|
||
|
|
|
||
|
|
type AssociationLookup = (inboundId: string) => Promise<LoadingAssociationSummary>
|
||
|
|
|
||
|
|
export async function loadSmartLoadingPlanCoLoadGroups(
|
||
|
|
plan: SmartLoadingPlan,
|
||
|
|
lookup: AssociationLookup = getLoadingAssociationSummaryByInboundId,
|
||
|
|
) {
|
||
|
|
const inboundRows = groupSmartLoadingItems(plan.items || [])
|
||
|
|
if (!inboundRows.length) return new Map<string, SmartLoadingPlanCoLoadGroup>()
|
||
|
|
|
||
|
|
const associations = await Promise.all(inboundRows.map(async (row) => ({
|
||
|
|
row,
|
||
|
|
association: await lookup(row.inboundId),
|
||
|
|
})))
|
||
|
|
const membersByGroupId = new Map<string, typeof associations>()
|
||
|
|
for (const entry of associations) {
|
||
|
|
const groupId = String(entry.association.coLoadGroupId || '')
|
||
|
|
if (!groupId) continue
|
||
|
|
membersByGroupId.set(groupId, [...(membersByGroupId.get(groupId) || []), entry])
|
||
|
|
}
|
||
|
|
|
||
|
|
const groupsByInboundId = new Map<string, SmartLoadingPlanCoLoadGroup>()
|
||
|
|
for (const [groupId, members] of membersByGroupId) {
|
||
|
|
const group = {
|
||
|
|
groupId,
|
||
|
|
groupNo: members.find((entry) => entry.association.coLoadGroupNo)?.association.coLoadGroupNo || '同柜不可拆组',
|
||
|
|
lockedPlanId: members.find((entry) => entry.association.loadingPlanId)?.association.loadingPlanId || plan.id,
|
||
|
|
memberOrderCount: new Set(members.map((entry) => entry.row.orderId)).size,
|
||
|
|
inboundFragmentCount: members.length,
|
||
|
|
fullPackageCount: members.reduce((sum, entry) => sum + entry.row.packageCount, 0),
|
||
|
|
} satisfies SmartLoadingPlanCoLoadGroup
|
||
|
|
for (const { row } of members) groupsByInboundId.set(row.inboundId, group)
|
||
|
|
}
|
||
|
|
return groupsByInboundId
|
||
|
|
}
|