feat(selfop): multi-owner turnover filters, CSV export and percentage

This commit is contained in:
2026-10-15 11:14:35 +08:00
parent 11e348a29f
commit 50b06dca10
9 changed files with 196 additions and 30 deletions
@@ -2,7 +2,12 @@ import type { TurnoverQuery, TurnoverSort } from './turnoverTypes'
import { turnoverSortOptions } from './turnoverSorting'
export const turnoverSorts: TurnoverSort[] = turnoverSortOptions.map(option => option.value)
const detailOnly = ['salesWindowDays', 'salesWindowEndDate', 'attention', 'coverageSelection', 'coverageBucket', 'bucketRuleVersion']
const turnoverOnly = ['hideZeroInventory', 'groupBy', 'baseWindowDays', 'focus', 'turnoverStatus', 'snapshotContext']
const turnoverOnly = ['customerCodes', 'hideZeroInventory', 'groupBy', 'baseWindowDays', 'focus', 'turnoverStatus', 'snapshotContext']
export function normalizeTurnoverOwners(value: unknown): string[] {
if (value == null) return []
if (!Array.isArray(value) || value.length > 100 || value.some(code => typeof code !== 'string' || !code.trim() || code.length > 64)) throw new Error('请选择有效货主,最多100个')
return [...new Set(value as string[])].sort()
}
function scalar(q: Record<string, unknown>, key: string) {
const v = q[key]
if (v == null) return undefined
@@ -24,6 +29,7 @@ export function yesterdayBeijing(now = new Date()) {
return new Date(Date.parse(`${date}T00:00:00Z`) - 86400000).toISOString().slice(0, 10)
}
export function validateTurnover(q: TurnoverQuery, latestDate = yesterdayBeijing()) {
normalizeTurnoverOwners(q.customerCodes)
if (q.hideZeroInventory != null && typeof q.hideZeroInventory !== 'boolean') throw new Error('隐藏零库存参数无效')
if (!q.warehouseCode.trim()) throw new Error('请选择可用仓库')
if (!/^\d{4}-\d{2}-\d{2}$/.test(q.endDate) || !Number.isFinite(Date.parse(q.endDate)) || new Date(q.endDate).toISOString().slice(0, 10) !== q.endDate || q.endDate > latestDate) throw new Error('截止日必须是已结束的北京时间日期')
@@ -39,10 +45,14 @@ export function validateTurnover(q: TurnoverQuery, latestDate = yesterdayBeijing
}
export function turnoverRoute(q: Record<string, unknown>): Partial<TurnoverQuery> {
if (inventoryView(q) !== 'TURNOVER') throw new Error('当前为库存明细入口')
const allowed = new Set(['view', 'warehouseCode', 'hideZeroInventory', 'sku', 'customerCode', 'endDate', 'baseWindowDays', 'groupBy', 'focus', 'coverageDaysThreshold', 'turnoverStatus', 'sortBy', 'snapshotContext', 'current', 'size'])
const allowed = new Set(['view', 'warehouseCode', 'hideZeroInventory', 'sku', 'customerCodes', 'endDate', 'baseWindowDays', 'groupBy', 'focus', 'coverageDaysThreshold', 'turnoverStatus', 'sortBy', 'snapshotContext', 'current', 'size'])
for (const key of Object.keys(q)) if (!allowed.has(key)) throw new Error(`库存周转不接受参数 ${key}`)
const result: Record<string, string | number | boolean> = {}
const result: Record<string, string | number | boolean | string[]> = {}
for (const key of allowed) {
if (key === 'customerCodes') {
result[key] = normalizeTurnoverOwners(q[key] == null ? [] : Array.isArray(q[key]) ? q[key] : [q[key]])
continue
}
const value = scalar(q, key)
if (key === 'hideZeroInventory' && value != null) {
if (!['true', 'false'].includes(value)) throw new Error('隐藏零库存参数无效')