Pure functions for copy-first-row attrs and same-box start/end slot pick. Co-authored-by: Cursor <cursoragent@cursor.com>
174 lines
5.2 KiB
TypeScript
174 lines
5.2 KiB
TypeScript
export type ProductLineAttrRow = {
|
|
specName?: string
|
|
expectedCellCount?: number
|
|
viability?: number
|
|
[key: string]: unknown
|
|
}
|
|
|
|
export type CopyFirstAttrsResult =
|
|
| { ok: true; filledCount: number }
|
|
| { ok: false; message: string }
|
|
|
|
export function copyFirstProductLineAttrs(lines: ProductLineAttrRow[]): CopyFirstAttrsResult {
|
|
if (!lines || lines.length < 2) {
|
|
return { ok: false, message: '至少两行明细才能复制' }
|
|
}
|
|
const first = lines[0]
|
|
for (let i = 1; i < lines.length; i++) {
|
|
lines[i].specName = first.specName ?? ''
|
|
lines[i].expectedCellCount = first.expectedCellCount
|
|
lines[i].viability = first.viability
|
|
}
|
|
return { ok: true, filledCount: lines.length - 1 }
|
|
}
|
|
|
|
export type RangeFillSlotRow = Record<string, unknown> & {
|
|
id?: string
|
|
facilityNodeId?: string
|
|
facility_node_id?: string
|
|
orderNum?: number
|
|
order_num?: number
|
|
slotStatus?: string
|
|
slot_status?: string
|
|
occupiedCount?: number
|
|
occupied_count?: number
|
|
slotCode?: string
|
|
slot_code?: string
|
|
fullSlotCode?: string
|
|
full_slot_code?: string
|
|
}
|
|
|
|
export type ResolveRangeFillInput = {
|
|
slots: RangeFillSlotRow[]
|
|
startSlotId: string
|
|
endSlotId: string
|
|
emptyLineCount: number
|
|
formUsedSlotIds: string[]
|
|
}
|
|
|
|
export type ResolveRangeFillResult =
|
|
| {
|
|
ok: true
|
|
slotIds: string[]
|
|
options: Array<{ id: string; name: string; facilityNodeId?: string }>
|
|
}
|
|
| { ok: false; message: string }
|
|
|
|
function slotFacilityNodeId(slot: RangeFillSlotRow): string {
|
|
return String(slot.facilityNodeId ?? slot.facility_node_id ?? '').trim()
|
|
}
|
|
|
|
function slotDisplayName(slot: RangeFillSlotRow): string {
|
|
return String(
|
|
slot.fullSlotCode ?? slot.full_slot_code ?? slot.slotCode ?? slot.slot_code ?? slot.id ?? ''
|
|
)
|
|
}
|
|
|
|
function slotOrderNum(slot: RangeFillSlotRow): number {
|
|
const raw = slot.orderNum ?? slot.order_num ?? 0
|
|
const parsed = Number(raw)
|
|
return Number.isFinite(parsed) ? parsed : 0
|
|
}
|
|
|
|
/** 与 storage-slot-options.isStorageSlotAvailable 一致 */
|
|
function isSlotAvailable(slot: RangeFillSlotRow): boolean {
|
|
const status = String(slot.slotStatus ?? slot.slot_status ?? '')
|
|
const occupied = Number(slot.occupiedCount ?? slot.occupied_count ?? 0)
|
|
return status === '空' || occupied === 0
|
|
}
|
|
|
|
/** 盒内按 order_num 升序(同盒批量填充足够;与货架布局 order 一致) */
|
|
function sortBoxSlots(slots: RangeFillSlotRow[]): RangeFillSlotRow[] {
|
|
return [...slots].sort((a, b) => {
|
|
const orderDiff = slotOrderNum(a) - slotOrderNum(b)
|
|
if (orderDiff !== 0) return orderDiff
|
|
return String(a.id ?? '').localeCompare(String(b.id ?? ''))
|
|
})
|
|
}
|
|
|
|
export function resolveStorageSlotRangeFill(
|
|
input: ResolveRangeFillInput
|
|
): ResolveRangeFillResult {
|
|
const startId = String(input.startSlotId || '').trim()
|
|
const endId = String(input.endSlotId || '').trim()
|
|
const need = Math.max(0, Number(input.emptyLineCount) || 0)
|
|
if (!startId || !endId) {
|
|
return { ok: false, message: '请选择起始与结束储物格' }
|
|
}
|
|
if (need <= 0) {
|
|
return { ok: false, message: '没有可填充的空行' }
|
|
}
|
|
|
|
const byId = new Map(
|
|
(input.slots || [])
|
|
.map((s) => [String(s.id || '').trim(), s] as const)
|
|
.filter(([id]) => !!id)
|
|
)
|
|
const start = byId.get(startId)
|
|
const end = byId.get(endId)
|
|
if (!start || !end) {
|
|
return { ok: false, message: '起始或结束储物格不在当前盒子中' }
|
|
}
|
|
const startNode = slotFacilityNodeId(start)
|
|
const endNode = slotFacilityNodeId(end)
|
|
if (!startNode || !endNode || startNode !== endNode) {
|
|
return { ok: false, message: '起始与结束须在同一盒子内' }
|
|
}
|
|
|
|
const sameBox = (input.slots || []).filter((s) => slotFacilityNodeId(s) === startNode)
|
|
const sorted = sortBoxSlots(sameBox)
|
|
const iStart = sorted.findIndex((s) => String(s.id) === startId)
|
|
const iEnd = sorted.findIndex((s) => String(s.id) === endId)
|
|
if (iStart < 0 || iEnd < 0) {
|
|
return { ok: false, message: '起始或结束储物格不在当前盒子中' }
|
|
}
|
|
const from = Math.min(iStart, iEnd)
|
|
const to = Math.max(iStart, iEnd)
|
|
const range = sorted.slice(from, to + 1)
|
|
|
|
for (const slot of range) {
|
|
if (!isSlotAvailable(slot)) {
|
|
return { ok: false, message: '区间内存在已占用储物格,请调整起止范围' }
|
|
}
|
|
}
|
|
|
|
const used = new Set(
|
|
(input.formUsedSlotIds || []).map((id) => String(id || '').trim()).filter(Boolean)
|
|
)
|
|
const available = range.filter((s) => {
|
|
const id = String(s.id || '').trim()
|
|
return id && !used.has(id)
|
|
})
|
|
if (available.length < need) {
|
|
return {
|
|
ok: false,
|
|
message: `区间内可用储物格不足(需要 ${need} 个,实际 ${available.length} 个)`
|
|
}
|
|
}
|
|
|
|
const picked = available.slice(0, need)
|
|
return {
|
|
ok: true,
|
|
slotIds: picked.map((s) => String(s.id)),
|
|
options: picked.map((s) => ({
|
|
id: String(s.id),
|
|
name: slotDisplayName(s),
|
|
facilityNodeId: startNode
|
|
}))
|
|
}
|
|
}
|
|
|
|
/** 将 slotIds 依次写入空行(原地修改),返回填充行数 */
|
|
export function applySlotIdsToEmptyLines(
|
|
lines: Array<{ storageSlotId?: string }>,
|
|
slotIds: string[]
|
|
): number {
|
|
let k = 0
|
|
for (const line of lines) {
|
|
if (String(line.storageSlotId || '').trim()) continue
|
|
if (k >= slotIds.length) break
|
|
line.storageSlotId = slotIds[k++]
|
|
}
|
|
return k
|
|
}
|