diff --git a/furniture-3d-system/src/app/supply-chain/management/clearance/clearance-table-columns.ts b/furniture-3d-system/src/app/supply-chain/management/clearance/clearance-table-columns.ts index 5c878c965..1e838ecb7 100644 --- a/furniture-3d-system/src/app/supply-chain/management/clearance/clearance-table-columns.ts +++ b/furniture-3d-system/src/app/supply-chain/management/clearance/clearance-table-columns.ts @@ -1,4 +1,5 @@ import { compactClearanceDepartmentName } from '@/lib/clearance-department-name' +import { groupClearanceComponentTasks } from '@/lib/clearance-task-component-group' export type ClearanceColumnKind = | 'image' @@ -151,36 +152,8 @@ export function groupClearanceTaskRowsByDuplicateCode( rows: readonly T[], ) { - const grouped = new Map() - for (const row of rows) { - const code = row.clearanceCode.trim().toUpperCase() - const barcode = row.barcode.trim().toUpperCase() - if (!code || code === barcode) { - grouped.set(`single\u0000${row.id}`, [row]) - continue - } - const key = [ - code, - row.ownerName.replace(/\s+/g, ' ').trim(), - compactClearanceDepartmentName(row.department), - row.profitCenter.replace(/\s+/g, ' ').trim(), - row.status, - String(row.round), - ].join('\u0000') - const groupRows = grouped.get(key) ?? [] - groupRows.push(row) - grouped.set(key, groupRows) - } - return Array.from(grouped.values()).flatMap((groupRows) => { + return groupClearanceComponentTasks(rows).map((groupRows) => { if (groupRows.length === 1) return groupRows[0] - const barcodeCounts = new Map() - for (const row of groupRows) { - const barcode = row.barcode.trim().toUpperCase() - barcodeCounts.set(barcode, (barcodeCounts.get(barcode) ?? 0) + 1) - } - if (Array.from(barcodeCounts.values()).some((count) => count > 1)) { - return groupRows - } const first = groupRows[0] const minimum = (values: number[]) => Math.min(...values) const minimumNullable = (values: Array) => { diff --git a/furniture-3d-system/src/lib/clearance-department-name.test.ts b/furniture-3d-system/src/lib/clearance-department-name.test.ts index 0036227f5..2df6f0368 100644 --- a/furniture-3d-system/src/lib/clearance-department-name.test.ts +++ b/furniture-3d-system/src/lib/clearance-department-name.test.ts @@ -19,8 +19,8 @@ describe('compact clearance department name', () => { it('keeps short business suffixes', () => { expect(compactClearanceDepartmentName('运营二部(HZ)')).toBe('运营二部(HZ)') expect(compactClearanceDepartmentName('运营一部(CZ)')).toBe('运营一部(CZ)') - expect(clearanceDepartmentPathFilters('运营二部(HZ)')).toEqual([]) - expect(clearanceDepartmentPathFilters('运营一部(CZ)')).toEqual([]) + expect(compactClearanceDepartmentName('运营二部(HZ)(运营中心C > 管理组)')).toBe('运营二部(HZ)') + expect(compactClearanceDepartmentName('运营一部(CZ) (公司 > 运营中心C)')).toBe('运营一部(CZ)') }) it('uses the same organization markers for display and database filter aliases', () => { diff --git a/furniture-3d-system/src/lib/clearance-department-name.ts b/furniture-3d-system/src/lib/clearance-department-name.ts index 0cfd3d0fd..4d0b0a253 100644 --- a/furniture-3d-system/src/lib/clearance-department-name.ts +++ b/furniture-3d-system/src/lib/clearance-department-name.ts @@ -4,21 +4,33 @@ function isOrganizationPathSuffix(suffix: string) { return CLEARANCE_DEPARTMENT_ORGANIZATION_MARKERS.some((marker) => suffix.includes(marker)) } -export function compactClearanceDepartmentName(value: unknown) { +export function splitClearanceDepartmentName(value: unknown) { const text = String(value ?? '').replace(/\s+/g, ' ').trim() - const bracketIndex = text.indexOf('(') >= 0 ? text.indexOf('(') : text.indexOf('(') - if (bracketIndex > 0) { - const suffix = text.slice(bracketIndex) - if (isOrganizationPathSuffix(suffix)) { - return text.slice(0, bracketIndex).trim() + const bracketIndex = text.search(/[((]/u) + if (bracketIndex <= 0) return { name: text, organizationPath: '' } + + // Keep the business suffix before the separate organization suffix. + const tail = text.slice(bracketIndex) + const businessSuffix = /^[((](?:HZ|CZ)[))]/iu.exec(tail)?.[0] + const pathStart = bracketIndex + (businessSuffix?.length ?? 0) + const organization = text.slice(pathStart).trim() + if (/^[((]/u.test(organization) && isOrganizationPathSuffix(organization)) { + return { + name: text.slice(0, pathStart).trim(), + organizationPath: organization.slice(1).replace(/[))]$/u, '').trim() + .replace(/\s*>\s*/g, ' > '), } } - return text + return { name: text, organizationPath: '' } +} + +export function compactClearanceDepartmentName(value: unknown) { + return splitClearanceDepartmentName(value).name } export function clearanceDepartmentPathFilters(value: unknown) { const department = compactClearanceDepartmentName(value) - if (!department || /[((]/u.test(department)) return [] + if (!department) return [] return [ `${department}(`, `${department}(`, @@ -28,3 +40,14 @@ export function clearanceDepartmentPathFilters(value: unknown) { CLEARANCE_DEPARTMENT_ORGANIZATION_MARKERS.map((marker) => ({ prefix, marker })) )) } + +// A department without a suffix must not match a separate HZ/CZ department. +export function clearanceDepartmentBusinessSuffixPrefixes(name: string) { + if (!name || /[((]/u.test(name)) return [] + return ['', ' '].flatMap((gap) => ( + ['HZ', 'CZ'].flatMap((suffix) => [ + name + gap + '(' + suffix + ')', + name + gap + '(' + suffix + ')', + ]) + )) +} diff --git a/furniture-3d-system/src/lib/clearance-task-component-group.test.ts b/furniture-3d-system/src/lib/clearance-task-component-group.test.ts new file mode 100644 index 000000000..4b450cab5 --- /dev/null +++ b/furniture-3d-system/src/lib/clearance-task-component-group.test.ts @@ -0,0 +1,29 @@ +import { groupClearanceComponentTasks } from './clearance-task-component-group' + +const task = { clearanceCode: 'ZH-A-B', barcode: 'A', ownerName: '负责人', department: '运营一部', + profitCenter: '运营中心C', periodKey: '2026-11', status: 'APPROVED', round: 1 } + +describe('clearance component task groups', () => { + it('keeps every task separate when any component barcode repeats', () => { + const rows = [{ ...task, id: 'a' }, { ...task, id: 'a2', barcode: ' a ' }, + { ...task, id: 'b', barcode: 'B' }] + expect(groupClearanceComponentTasks(rows)).toEqual(rows.map((row) => [row])) + }) + + it('matches a short name to one known organization path', () => { + const rows = [task, { ...task, barcode: 'B', department: '运营一部(运营中心C > 管理组)' }] + expect(groupClearanceComponentTasks(rows)).toEqual([rows]) + }) + + it('separates different full paths when departments have the same short name', () => { + const rows = [task, { ...task, barcode: 'B', department: '运营一部(甲公司 > 运营中心C)' }, + { ...task, barcode: 'C', department: '运营一部(乙公司 > 运营中心C)' }] + expect(groupClearanceComponentTasks(rows)).toEqual(rows.map((row) => [row])) + }) + + it('separates HZ and CZ when both departments have an organization path', () => { + const rows = [{ ...task, department: '运营一部(HZ)(运营中心C > 管理组)' }, + { ...task, barcode: 'B', department: '运营一部(CZ)(运营中心C > 管理组)' }] + expect(groupClearanceComponentTasks(rows)).toEqual(rows.map((row) => [row])) + }) +}) diff --git a/furniture-3d-system/src/lib/clearance-task-component-group.ts b/furniture-3d-system/src/lib/clearance-task-component-group.ts new file mode 100644 index 000000000..236dae35c --- /dev/null +++ b/furniture-3d-system/src/lib/clearance-task-component-group.ts @@ -0,0 +1,53 @@ +import { splitClearanceDepartmentName } from './clearance-department-name' + +type ClearanceComponentTask = { + barcode: string + clearanceCode: string + department: string + ownerName: string + profitCenter: string + periodKey?: string + status: string + round: number +} + +export function groupClearanceComponentTasks(rows: readonly T[]): T[][] { + type Group = { rows: T[]; barcodes: Set; organizationPaths: Set; repeated: boolean } + const groups = new Map() + const ordered: Array<{ single: T } | Group> = [] + for (const row of rows) { + const code = row.clearanceCode.trim().toUpperCase() + const barcode = row.barcode.trim().toUpperCase() + if (!code || !barcode || code === barcode) { + ordered.push({ single: row }) + continue + } + const department = splitClearanceDepartmentName(row.department) + const key = JSON.stringify([ + row.periodKey ?? '', + code, + row.ownerName.replace(/\s+/g, ' ').trim(), + department.name, + row.profitCenter.replace(/\s+/g, ' ').trim(), + row.status, + row.round, + ]) + let group = groups.get(key) + if (!group) { + group = { rows: [], barcodes: new Set(), organizationPaths: new Set(), repeated: false } + groups.set(key, group) + ordered.push(group) + } + group.repeated ||= group.barcodes.has(barcode) + group.barcodes.add(barcode) + if (department.organizationPath) group.organizationPaths.add(department.organizationPath) + group.rows.push(row) + } + // An incomplete department path can match a unique full path, but cannot choose between two. + return ordered.flatMap((group) => { + if ('single' in group) return [[group.single]] + return group.repeated || group.organizationPaths.size > 1 + ? group.rows.map((row) => [row]) + : [group.rows] + }) +} diff --git a/furniture-3d-system/src/server/repositories/supply-chain-clearance-filter.test.ts b/furniture-3d-system/src/server/repositories/supply-chain-clearance-filter.test.ts index 83c8b45a9..282ef0a67 100644 --- a/furniture-3d-system/src/server/repositories/supply-chain-clearance-filter.test.ts +++ b/furniture-3d-system/src/server/repositories/supply-chain-clearance-filter.test.ts @@ -15,18 +15,19 @@ describe('clearance pool department filters', () => { expect(serialized).toContain('"contains":"公司"') }) - it('keeps HZ and CZ as distinct department names', () => { - for (const department of ['运营二部(HZ)', '运营一部(CZ)']) { - const where = clearancePoolFilterWhere({ - key: 'deptOwner', - operator: 'equals', - value: department, - }) - const serialized = JSON.stringify(where) - - expect(serialized).toContain(`"equals":"${department}"`) - expect(serialized).not.toContain('"contains":">"') - expect(serialized).not.toContain('"contains":"公司"') + it('matches the selected business suffix and excludes it from the plain short name', () => { + const filter = (value: string) => clearancePoolFilterWhere({ key: 'deptOwner', operator: 'equals', value }) + for (const department of ['运营一部(HZ)', '运营一部(CZ)']) { + expect(filter(department)?.OR).toEqual(expect.arrayContaining([ + { department: { equals: department, mode: 'insensitive' } }, + { AND: [ + { department: { startsWith: department + '(', mode: 'insensitive' } }, + { department: { contains: '>', mode: 'insensitive' } }, + ] }, + ])) + expect(filter('运营一部')?.NOT).toEqual(expect.arrayContaining([ + { department: { startsWith: department, mode: 'insensitive' } }, + ])) } }) }) diff --git a/furniture-3d-system/src/server/repositories/supply-chain-clearance.repository.ts b/furniture-3d-system/src/server/repositories/supply-chain-clearance.repository.ts index f1e7a34bd..390650d9b 100644 --- a/furniture-3d-system/src/server/repositories/supply-chain-clearance.repository.ts +++ b/furniture-3d-system/src/server/repositories/supply-chain-clearance.repository.ts @@ -6,6 +6,7 @@ import { type ClearanceFilterCondition, } from '@/lib/clearance-filters' import { + clearanceDepartmentBusinessSuffixPrefixes, clearanceDepartmentPathFilters, compactClearanceDepartmentName, } from '@/lib/clearance-department-name' @@ -106,7 +107,13 @@ export function clearancePoolFilterWhere( const department = compactClearanceDepartmentName(filter.value) if (!department) return { id: '__invalid_filter_value__' } const organizationPathFilters = clearanceDepartmentPathFilters(department) + const otherDepartments = clearanceDepartmentBusinessSuffixPrefixes(department) return { + ...(otherDepartments.length ? { + NOT: otherDepartments.map((prefix) => ({ + department: { startsWith: prefix, mode: 'insensitive' }, + })), + } : {}), OR: [ { department: { equals: department, mode: 'insensitive' } }, ...organizationPathFilters.map(({ prefix, marker }) => ({ diff --git a/furniture-3d-system/src/server/services/__tests__/supply-chain-clearance.service.test.ts b/furniture-3d-system/src/server/services/__tests__/supply-chain-clearance.service.test.ts index 517509118..615f84127 100644 --- a/furniture-3d-system/src/server/services/__tests__/supply-chain-clearance.service.test.ts +++ b/furniture-3d-system/src/server/services/__tests__/supply-chain-clearance.service.test.ts @@ -6061,6 +6061,28 @@ describe('supply-chain-clearance workflow', () => { }) }) + it('keeps two A components and one B component separate in the default tasks response', async () => { + const tasks = [ + { ...pendingTask, id: 'task-a', barcode: 'DUP-CASE-A', department: '运营一部' }, + { ...pendingTask, id: 'task-a2', barcode: 'DUP-CASE-A', department: '运营一部(运营中心C > 管理组)' }, + { ...pendingTask, id: 'task-b', barcode: 'DUP-CASE-B', department: '运营一部' }, + ].map((task) => ({ ...task, status: 'APPROVED', ownerName: '负责人', profitCenter: '运营中心C' })) + repository.listTasks.mockResolvedValue(tasks as never) + repository.listReviewMonths.mockResolvedValue([]) + repository.listPoolItemsForTaskPeriod.mockResolvedValue(['DUP-CASE-A', 'DUP-CASE-B'].map((barcode) => ({ + barcode, costPrice: '30.00', stockJianshang: 100, stockFactory: 0, totalStock: 100, + extra: { clearanceCode: 'ZH-DUP-CASE-A-1-DUP-CASE-B-1', profitCenter: '运营中心C' }, + })) as never) + + const result = await supplyChainClearanceService.getTasks('2026-10', { + includeFreight: false, liveProgress: false, + }) + + expect(result.tasks.map((row) => row.taskIds)).toEqual([['task-a'], ['task-a2'], ['task-b']]) + expect(result.tasks.map((row) => row.costPrice)).toEqual([30, 30, 30]) + expect(repository.listTasks).toHaveBeenCalledTimes(1) + }) + it('combines component tasks into one clearance code using the minimum execution values', async () => { repository.listTasks.mockResolvedValue([ { diff --git a/furniture-3d-system/src/server/services/supply-chain-clearance.service.ts b/furniture-3d-system/src/server/services/supply-chain-clearance.service.ts index 66d70f816..bb5ab1706 100644 --- a/furniture-3d-system/src/server/services/supply-chain-clearance.service.ts +++ b/furniture-3d-system/src/server/services/supply-chain-clearance.service.ts @@ -5,6 +5,7 @@ import type { Prisma } from '@prisma/client' import { supplyChainClearanceRepository } from '@/server/repositories/supply-chain-clearance.repository' import { normalizeClearanceTaskImage } from '@/lib/clearance-task-image' import { compactClearanceDepartmentName } from '@/lib/clearance-department-name' +import { groupClearanceComponentTasks } from '@/lib/clearance-task-component-group' import { supplyChainClearanceReviewSourceRepository } from '@/server/repositories/supply-chain-clearance-review-source.repository' import { supplyChainDiscontinuationSourceRepository } from '@/server/repositories/supply-chain-discontinuation-source.repository' import { dingtalkWorkflowMonitorRepository } from '@/server/repositories/dingtalk-workflow-monitor.repository' @@ -4615,25 +4616,8 @@ class SupplyChainClearanceService { } = row return visibleRow } - const grouped = Array.from( - mapped.reduce((groups, task) => { - const key = [ - task.clearanceCode.trim().toUpperCase(), - task.ownerName.replace(/\s+/g, ' ').trim(), - compactClearanceDepartmentName(task.department), - task.profitCenter.replace(/\s+/g, ' ').trim(), - task.status, - task.round, - ].join('\u0000') - const rows = groups.get(key) ?? [] - rows.push(task) - groups.set(key, rows) - return groups - }, new Map>()), - ).flatMap(([, rows]) => { - if (rows.length === 1 || new Set(rows.map((row) => row.barcode)).size === 1) { - return rows.map(stripInternalTaskRow) - } + const grouped = groupClearanceComponentTasks(mapped).flatMap((rows) => { + if (rows.length === 1) return rows.map(stripInternalTaskRow) const minValue = (values: number[]) => values.length ? Math.min(...values) : 0 const minNullable = (values: Array) => { const finite = values.filter((value): value is number => value != null)