WEB/src/views/storage/report/report-config.ts
2026-08-10 18:13:39 +08:00

293 lines
9.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import dayjs from 'dayjs'
import type { StockReportListItem } from '@/api/stock.report/stock.report'
export const reportFieldLabels: Record<string, string> = {
store_id: '货主ID',
store_name: '货主',
stock_id: 'SKU',
item_code: '商品编码',
barcode: '条码',
item_name: '商品名称',
begin_total: '期初数量',
inbound_total: '区间内入库',
outbound_total: '区间内出库',
end_total: '期末数量',
start_time: '统计区间开始',
end_time: '统计区间结束',
warehouse_id: '仓库ID',
warehouse_name: '仓库名称',
}
const DETAIL_KEY_ORDER = [
'store_id',
'store_name',
'warehouse_id',
'warehouse_name',
'stock_id',
'item_code',
'barcode',
'item_name',
'begin_total',
'inbound_total',
'outbound_total',
'end_total',
'start_time',
'end_time'
]
export function sortReportDetailKeys(keys: string[]) {
const head = DETAIL_KEY_ORDER.filter((k) => keys.includes(k))
const tail = keys.filter((k) => !DETAIL_KEY_ORDER.includes(k)).sort()
return [...head, ...tail]
}
export function formatReportNumber(val: unknown): string {
if (val === null || val === undefined || val === '') return '--'
const n = Number(val)
if (Number.isNaN(n)) return '--'
return String(Math.trunc(n) === n ? n.toLocaleString('zh-CN') : n.toLocaleString('zh-CN', { maximumFractionDigits: 4 }))
}
export function formatReportDateTime(val: unknown): string {
if (val === null || val === undefined || val === '') return '--'
const d = dayjs(val as string | number | Date)
return d.isValid() ? d.format('YYYY-MM-DD HH:mm:ss') : String(val)
}
export function formatReportCell(val: unknown): string {
if (val === null || val === undefined || val === '') return '--'
if (typeof val === 'object') return JSON.stringify(val)
return String(val)
}
export type ReportColumn = {
prop: string
label: string
width?: string | number
minWidth?: string | number
align?: 'left' | 'center' | 'right'
sortable?: boolean | 'custom'
sortOrders?: string[]
copyAction?: 'copy' | 'copyQr'
formatter?: (row: StockReportListItem) => string
}
/** 主表格固定列(与真实接口字段一致);按库位时后端可额外返回 space_*,由 buildReportTableColumns 追加 */
export const fixedReportColumns: ReportColumn[] = [
{ prop: 'store_name', label: '货主', minWidth: 120, formatter: (row) => row.store_name || '--' },
{ prop: 'item_name', label: '商品名称', minWidth: 140, formatter: (row) => row.item_name || '--' },
{ prop: 'item_code', label: '商品编码', minWidth: 120, copyAction: 'copy' as const, formatter: (row) => row.item_code || '--' },
{ prop: 'barcode', label: '条码', minWidth: 140, copyAction: 'copy' as const, formatter: (row) => row.barcode || '--' },
{ prop: 'stock_id', label: 'SKU', width: 96, copyAction: 'copy' as const, formatter: (row) => formatReportCell(row.stock_id) },
{
prop: 'begin_total',
label: '期初数量',
width: 104,
sortable: 'custom',
formatter: (row) => formatReportNumber(row.begin_total)
},
{
prop: 'inbound_total',
label: '区间内入库',
width: 112,
sortable: 'custom',
formatter: (row) => formatReportNumber(row.inbound_total)
},
{
prop: 'outbound_total',
label: '区间内出库',
width: 112,
sortable: 'custom',
formatter: (row) => formatReportNumber(row.outbound_total)
},
{
prop: 'end_total',
label: '期末数量',
width: 104,
sortable: 'custom',
formatter: (row) => formatReportNumber(row.end_total)
},
{
prop: 'start_time',
label: '区间开始',
width: 168,
formatter: (row) => formatReportDateTime(row.start_time)
},
{
prop: 'end_time',
label: '区间结束',
width: 168,
formatter: (row) => formatReportDateTime(row.end_time)
},
{ prop: 'warehouse_name', label: '仓库', minWidth: 120, formatter: (row) => (row as any).warehouse_name || '--' }
]
const FIXED_PROPS = new Set(fixedReportColumns.map((c) => c.prop))
export function buildReportTableColumns(rows: StockReportListItem[]) {
const cols = fixedReportColumns.map((c) => ({ ...c }))
const extra = new Set<string>()
for (const row of rows) {
if (!row || typeof row !== 'object') continue
for (const k of Object.keys(row)) {
if (k === '__rid' || k === 'store_id' || k === 'warehouse_id' || FIXED_PROPS.has(k)) continue
extra.add(k)
}
}
for (const prop of [...extra].sort()) {
cols.push({
prop,
label: reportFieldLabels[prop] || prop,
minWidth: 110,
formatter: (row) => formatReportCell((row as any)[prop])
})
}
return cols
}
function normalizeReportDateParam(value: unknown): string | undefined {
if (value == null || value === '') return undefined
const d = dayjs(value as string | number | Date)
return d.isValid() ? d.format('YYYY-MM-DD HH:mm:ss') : String(value)
}
function normalizeReportStartDateParam(value: unknown): string | undefined {
if (value == null || value === '') return undefined
const d = dayjs(value as string | number | Date)
return d.isValid() ? d.startOf('day').format('YYYY-MM-DD HH:mm:ss') : String(value)
}
function normalizeReportEndDateParam(value: unknown): string | undefined {
if (value == null || value === '') return undefined
const d = dayjs(value as string | number | Date)
return d.isValid() ? d.endOf('day').format('YYYY-MM-DD HH:mm:ss') : String(value)
}
/** ScanBox 筛选字段分组(库存报表模块专用) */
export const REPORT_SCAN_FIELD_GROUPS = {
basic: '基础信息',
time: '时间筛选',
} as const
type ReportScanFieldGroupKey = keyof typeof REPORT_SCAN_FIELD_GROUPS
export function reportScanField<G extends ReportScanFieldGroupKey>(
group: G,
field: Record<string, any>
) {
return { ...field, group: REPORT_SCAN_FIELD_GROUPS[group] }
}
/** ScanBox 默认 props(与波次模块 scan-box 侧栏一致) */
export const reportScanBoxConfig = {
width: '250px',
treeProps: { children: 'children', label: 'label' } as const,
autoSubmit: false
}
/** ScanBox 侧栏可用字段(与 storage.report.search 的 where 对应) */
export const reportScanBoxAvailableFields = [
reportScanField('basic', {
value: 'keyword',
label: '关键字',
type: 'text',
placeholder: '搜索商品名称/编码/条码'
}),
reportScanField('basic', {
value: 'store_id',
label: '货主',
type: 'select',
multiple: true,
filterable: true,
placeholder: '请选择货主',
options: [] as { label: string; value: number }[]
}),
reportScanField('basic', {
value: 'by_space',
label: '按库位统计',
type: 'boolean',
placeholder: '开启后按商品+库位+货主返回;关闭则按商品+货主聚合'
}),
reportScanField('time', {
value: 'report_period',
label: '统计时间',
type: 'daterange',
startPlaceholder: '开始日期',
endPlaceholder: '结束日期'
}),
]
/** 将 ScanBox 条件转为接口扁平 where;无有效条件时返回 {} */
export function normalizeScanConditionToReportWhere(condition: any): Record<string, unknown> {
const where: Record<string, unknown> = {}
const conds = condition?.conditions && typeof condition.conditions === 'object'
? condition.conditions
: condition && typeof condition === 'object'
? condition
: null
if (!conds) {
return where
}
const period = conds.report_period?.value ?? conds.report_period
const startTime = conds.start_time?.value ?? conds.start_time
const endTime = conds.end_time?.value ?? conds.end_time
const dateRangeValue = Array.isArray(period)
? period
: Array.isArray(startTime) && startTime.length >= 2
? startTime
: Array.isArray(endTime) && endTime.length >= 2
? endTime
: null
if (dateRangeValue && dateRangeValue.length >= 2) {
const a = normalizeReportStartDateParam(dateRangeValue[0])
const b = normalizeReportEndDateParam(dateRangeValue[1])
if (a) where.start_time = a
if (b) where.end_time = b
} else {
const a = normalizeReportStartDateParam(startTime)
const b = normalizeReportEndDateParam(endTime)
if (a) where.start_time = a
if (b) where.end_time = b
}
const sid = conds.store_id?.value ?? conds.store_id
if (sid != null && sid !== '') {
const raw = sid
const arr = (Array.isArray(raw) ? raw : [raw])
.map((x: unknown) => Number(x))
.filter((n) => Number.isFinite(n))
if (arr.length) where.store_ids = arr
}
if (Object.prototype.hasOwnProperty.call(conds, 'by_space')) {
const bySpaceRaw = conds.by_space?.value ?? conds.by_space
if (bySpaceRaw === true || bySpaceRaw === 'true' || bySpaceRaw === 1 || bySpaceRaw === '1') {
where.by_space = true
} else if (
bySpaceRaw === false ||
bySpaceRaw === 'false' ||
bySpaceRaw === 0 ||
bySpaceRaw === '0'
) {
where.by_space = false
}
}
// 关键字搜索
const keyword = conds.keyword?.value ?? conds.keyword
if (keyword != null && String(keyword).trim() !== '') {
where.keyword = String(keyword).trim()
}
// 仓库筛选
const warehouseId = conds.warehouse_id?.value ?? conds.warehouse_id
if (warehouseId != null && warehouseId !== '' && warehouseId !== undefined) {
where.warehouse_id = warehouseId
}
return where
}