merge(clearance): 合入运营导入月份修复到主线

This commit is contained in:
居居 2026-11-25 16:46:35 +08:00
commit e9acf82ebf
6 changed files with 101 additions and 10 deletions

View File

@ -7,6 +7,7 @@ import styles from './clearance-price-push.module.css'
import { CLEARANCE_TASK_IMPORT_TEMPLATE_HEADERS } from '@/lib/clearance-task-manual-import-headers'
type ImportResult = {
periodKey: string
importedCount: number
createdCount: number
updatedCount: number

View File

@ -17,7 +17,10 @@ import {
} from '@/lib/clearance-period-lifecycle'
import { isClearanceDiscountBlockedPriority } from '@/lib/clearance-discount-restrictions'
import { formatClearanceDailySales } from '@/lib/clearance-number-format'
import { resolveClearanceTaskPeriod } from '@/lib/clearance-task-period'
import {
resolveClearanceTaskPeriod,
resolveClearanceTaskViewPeriod,
} from '@/lib/clearance-task-period'
import {
shouldPollClearanceProgressSync,
shouldRefreshClearanceProgressData,
@ -1002,9 +1005,12 @@ export function ClearanceView({
setTaskLoading(true)
setTaskError('')
if (tab === 'tasks') setTasks(null)
const summaryPeriod = tab === 'pool'
? pool?.periodKey ?? taskPeriod
: taskPeriod || currentPeriodKey()
const summaryPeriod = resolveClearanceTaskViewPeriod({
view: tab === 'tasks' ? 'tasks' : 'pool',
poolPeriod: pool?.periodKey ?? '',
taskPeriod,
currentPeriod: currentPeriodKey(),
})
const taskParams = new URLSearchParams({ period: summaryPeriod })
taskParams.set('includeFreight', '0')
taskParams.set('liveProgress', '0')
@ -1019,7 +1025,7 @@ export function ClearanceView({
if (!isCurrentRequest()) return
setTasks(data)
const resolvedTaskPeriod = resolveClearanceTaskPeriod({
requestedPeriod: taskPeriod,
requestedPeriod: summaryPeriod,
responsePeriod: data.periodKey,
availablePeriods: data.periods ?? [],
currentPeriod: currentPeriodKey(),
@ -2518,6 +2524,7 @@ export function ClearanceView({
aria-label="清货月份"
onChange={(event) => {
setPoolPeriod(event.target.value)
setTaskPeriod(event.target.value)
setPoolReviewStatus('ALL')
setPoolPage(1)
setPoolFilterDraft([])
@ -3895,7 +3902,12 @@ export function ClearanceView({
{taskImportOpen && (
<ClearanceTaskManualImportDialog
period={taskPeriod || pool?.periodKey || currentPeriodKey()}
period={resolveClearanceTaskViewPeriod({
view: tab === 'tasks' ? 'tasks' : 'pool',
poolPeriod: pool?.periodKey ?? '',
taskPeriod,
currentPeriod: currentPeriodKey(),
})}
onClose={() => setTaskImportOpen(false)}
onImported={(result) => {
setTaskImportOpen(false)
@ -3904,7 +3916,7 @@ export function ClearanceView({
setTaskRefreshKey((value) => value + 1)
setPoolRefreshKey((value) => value + 1)
setToast(
`已导入 ${result.importedCount} 条清货任务并进入待审核`
`已导入到${monthLabel(result.periodKey)} ${result.importedCount} 条清货任务并进入待审核`
+ `(新增 ${result.createdCount} 条,更新 ${result.updatedCount} 条)`
+ (result.adjustedToStockCount
? `;${result.adjustedToStockCount} 条任务量已按简尚库存截断:${result.adjustedBarcodes.slice(0, 8).join('、')}`
@ -3915,7 +3927,12 @@ export function ClearanceView({
)}
{taskDirectImportOpen && (
<ClearanceTaskDirectImportDialog
period={taskPeriod || currentPeriodKey()}
period={resolveClearanceTaskViewPeriod({
view: tab === 'tasks' ? 'tasks' : 'pool',
poolPeriod: pool?.periodKey ?? '',
taskPeriod,
currentPeriod: currentPeriodKey(),
})}
onClose={() => setTaskDirectImportOpen(false)}
onImported={(result) => {
setTaskDirectImportOpen(false)

View File

@ -1,6 +1,27 @@
import { resolveClearanceTaskPeriod } from './clearance-task-period'
import {
resolveClearanceTaskPeriod,
resolveClearanceTaskViewPeriod,
} from './clearance-task-period'
describe('resolveClearanceTaskPeriod', () => {
it('uses the selected pool period on pool pages instead of stale task state', () => {
expect(resolveClearanceTaskViewPeriod({
view: 'pool',
poolPeriod: '2026-12',
taskPeriod: '2026-11',
currentPeriod: '2026-11',
})).toBe('2026-12')
})
it('uses the task page period when viewing the task list', () => {
expect(resolveClearanceTaskViewPeriod({
view: 'tasks',
poolPeriod: '2026-12',
taskPeriod: '2026-11',
currentPeriod: '2026-11',
})).toBe('2026-11')
})
it('keeps a requested period when the task response includes it', () => {
expect(resolveClearanceTaskPeriod({
requestedPeriod: '2026-11',

View File

@ -16,3 +16,14 @@ export function resolveClearanceTaskPeriod(input: {
if (response && periods.includes(response)) return response
return periods[0] || response || requested || current
}
export function resolveClearanceTaskViewPeriod(input: {
view: 'pool' | 'tasks'
poolPeriod: string
taskPeriod: string
currentPeriod: string
}) {
const poolPeriod = input.poolPeriod.trim()
if (input.view === 'pool' && poolPeriod) return poolPeriod
return input.taskPeriod.trim() || poolPeriod || input.currentPeriod.trim()
}

View File

@ -73,6 +73,13 @@ function repository() {
{ operationOwner: '运营G', profitCenter: '运营中心G' },
]),
latestTaskRound: jest.fn().mockResolvedValue(1),
findApplicationWindow: jest.fn().mockResolvedValue({
periodKey: '2026-11',
round: 1,
startAt: new Date('2026-11-01T00:00:00.000Z'),
endAt: new Date('2026-11-30T23:59:59.000Z'),
status: 'CONFIRMED',
}),
findReviewArchive: jest.fn().mockResolvedValue(null),
writeTasksWithEvents: jest.fn().mockResolvedValue([]),
}
@ -92,6 +99,24 @@ describe('SupplyChainClearanceTaskImportService', () => {
expect(repo.writeTasksWithEvents).not.toHaveBeenCalled()
})
it('rejects operator imports outside the selected period application window', async () => {
const repo = repository()
repo.findApplicationWindow.mockResolvedValue(null)
const service = new SupplyChainClearanceTaskImportService(
repo as never,
() => new Date('2026-11-11T00:00:00.000Z'),
)
await expect(service.importTasks({
periodKey: '2026-11',
user,
buffer: await workbookBuffer([
['SKU-A', '铁心兰', 100, '', ''],
]),
})).rejects.toThrow('当前不在 2026-11 第 1 期清货任务申报窗口内')
expect(repo.writeTasksWithEvents).not.toHaveBeenCalled()
})
it('imports pending tasks, clamps to Jianshang stock and updates existing tasks', async () => {
const repo = repository()
const service = new SupplyChainClearanceTaskImportService(

View File

@ -19,6 +19,7 @@ type TaskImportRepository = Pick<
| 'listTasksForManualImport'
| 'listOwnerProfitCenters'
| 'latestTaskRound'
| 'findApplicationWindow'
| 'findReviewArchive'
| 'writeTasksWithEvents'
>
@ -160,11 +161,27 @@ export class SupplyChainClearanceTaskImportService {
}
const importCodes = Array.from(new Set(rows.map((row) => row.barcode)))
const importedAt = this.now()
const [poolItems, latestRound, ownerCenters] = await Promise.all([
this.repository.listPoolItemsForTaskPeriodByCodes(periodKey, importCodes),
this.repository.latestTaskRound(periodKey),
this.repository.listOwnerProfitCenters(),
])
const applicationWindow = await this.repository.findApplicationWindow(
periodKey,
latestRound,
)
if (
!applicationWindow
|| applicationWindow.status !== 'CONFIRMED'
|| importedAt.getTime() < applicationWindow.startAt.getTime()
|| importedAt.getTime() > applicationWindow.endAt.getTime()
) {
throw new AppError(
ErrorCode.VALIDATION_INVALID_FORMAT,
`当前不在 ${periodKey} 第 ${latestRound} 期清货任务申报窗口内,不能导入任务`,
)
}
const importCodeSet = new Set(importCodes)
const poolItemsByImportCode = new Map<string, typeof poolItems>()
for (const item of poolItems) {
@ -342,7 +359,6 @@ export class SupplyChainClearanceTaskImportService {
)
}
const importedAt = this.now()
const writes: Parameters<TaskImportRepository['writeTasksWithEvents']>[0] = preparedRows.map((row) => {
const existing = existingByKey.get(itemKey(
periodKey,