267 lines
8.3 KiB
TypeScript
267 lines
8.3 KiB
TypeScript
export type PackageMaterialRecord = {
|
||
id?: number
|
||
material_code?: string
|
||
material_name?: string
|
||
material_type?: string
|
||
spec?: string
|
||
length?: number
|
||
width?: number
|
||
height?: number
|
||
weight?: number
|
||
cost_price?: number
|
||
status?: number
|
||
created_at?: string
|
||
updated_at?: string
|
||
}
|
||
|
||
export const packageMaterialColumns = [
|
||
{ prop: 'id', label: 'ID', width: '80', sortable: true },
|
||
{ prop: 'material_code', label: '包材编码', minWidth: '130', showOverflowTooltip: true },
|
||
{ prop: 'material_name', label: '包材名称', minWidth: '150', showOverflowTooltip: true },
|
||
{ prop: 'material_type', label: '包材类型', minWidth: '110', showOverflowTooltip: true },
|
||
{ prop: 'spec', label: '规格', minWidth: '120', showOverflowTooltip: true },
|
||
{ prop: 'dimensions', label: '尺寸(L×W×H)', minWidth: '140', align: 'center' },
|
||
{ prop: 'weight', label: '重量', width: '90', align: 'right' },
|
||
{ prop: 'cost_price', label: '成本价', width: '100', align: 'right' },
|
||
{ prop: 'status', label: '状态', width: '90', align: 'center' },
|
||
{ prop: 'actions', label: '操作', width: '150', align: 'center', fixed: 'right' }
|
||
]
|
||
|
||
export const packageMaterialScanFields = [
|
||
{
|
||
value: 'quickWhere',
|
||
label: '关键词',
|
||
type: 'text',
|
||
placeholder: '编码/名称/类型',
|
||
operators: ['contains'],
|
||
defaultOperator: 'contains'
|
||
},
|
||
{
|
||
value: 'material_code',
|
||
label: '包材编码',
|
||
type: 'text',
|
||
placeholder: '请输入包材编码',
|
||
operators: ['eq', 'contains', 'like'],
|
||
defaultOperator: 'contains'
|
||
},
|
||
{
|
||
value: 'material_name',
|
||
label: '包材名称',
|
||
type: 'text',
|
||
placeholder: '请输入包材名称',
|
||
operators: ['eq', 'contains', 'like'],
|
||
defaultOperator: 'contains'
|
||
},
|
||
{
|
||
value: 'material_type',
|
||
label: '包材类型',
|
||
type: 'text',
|
||
placeholder: '请输入包材类型',
|
||
operators: ['eq', 'contains', 'like'],
|
||
defaultOperator: 'contains'
|
||
},
|
||
{
|
||
value: 'status',
|
||
label: '状态',
|
||
type: 'select',
|
||
clearable: true,
|
||
options: [
|
||
{ label: '启用', value: '1' },
|
||
{ label: '停用', value: '0' }
|
||
]
|
||
},
|
||
{
|
||
value: 'id',
|
||
label: 'ID',
|
||
type: 'number',
|
||
placeholder: '包材 ID',
|
||
operators: ['eq', 'ne'],
|
||
defaultOperator: 'eq'
|
||
}
|
||
]
|
||
|
||
function getFieldConfig(key: string, fields: typeof packageMaterialScanFields) {
|
||
return fields.find((f) => f.value === key)
|
||
}
|
||
|
||
function mapFieldType(type: string, value: unknown) {
|
||
if (type === 'daterange' || Array.isArray(value)) return 'daterange'
|
||
if (type === 'number' || typeof value === 'number') return 'number'
|
||
return 'text'
|
||
}
|
||
|
||
function processConditionValue(key: string, raw: any, fields: typeof packageMaterialScanFields) {
|
||
if (key === 'quickWhere') {
|
||
const val = raw?.value !== undefined ? raw.value : raw
|
||
const text = val == null ? '' : String(val).trim()
|
||
return text ? text : null
|
||
}
|
||
|
||
const config = getFieldConfig(key, fields)
|
||
const fieldType = config?.type || 'text'
|
||
|
||
if (raw && typeof raw === 'object' && !Array.isArray(raw)) {
|
||
const op = raw.operator || (fieldType === 'select' ? 'eq' : fieldType === 'number' ? 'eq' : 'contains')
|
||
const type = raw.type || mapFieldType(fieldType, raw.value)
|
||
|
||
if (['isnull', 'isnotnull'].includes(op)) {
|
||
return { operator: op, value: null, type }
|
||
}
|
||
|
||
const val = raw.value !== undefined ? raw.value : raw
|
||
if (val === null || val === undefined || val === '' || (Array.isArray(val) && val.length === 0)) {
|
||
return null
|
||
}
|
||
|
||
if (fieldType === 'select') {
|
||
return { operator: op || 'eq', value: val, type: 'text' }
|
||
}
|
||
|
||
return { operator: op, value: val, type }
|
||
}
|
||
|
||
if (raw === null || raw === undefined || raw === '' || (Array.isArray(raw) && raw.length === 0)) {
|
||
return null
|
||
}
|
||
|
||
const simpleOp = fieldType === 'select' || fieldType === 'number' ? 'eq' : 'contains'
|
||
return { operator: simpleOp, value: raw, type: mapFieldType(fieldType, raw) }
|
||
}
|
||
|
||
export function normalizePackageMaterialScanConditionToWhere(
|
||
condition: any,
|
||
fields: typeof packageMaterialScanFields = packageMaterialScanFields
|
||
): Record<string, unknown> {
|
||
if (!condition) return {}
|
||
|
||
const conds =
|
||
condition?.conditions && typeof condition.conditions === 'object'
|
||
? condition.conditions
|
||
: condition && typeof condition === 'object'
|
||
? condition
|
||
: null
|
||
|
||
if (!conds) return {}
|
||
|
||
const where: Record<string, unknown> = {}
|
||
for (const key of Object.keys(conds)) {
|
||
const result = processConditionValue(key, conds[key], fields)
|
||
if (result == null) continue
|
||
if (key === 'quickWhere' && typeof result === 'string') {
|
||
where.quickWhere = result
|
||
} else {
|
||
where[key] = result
|
||
}
|
||
}
|
||
return where
|
||
}
|
||
|
||
export function formatPackageMaterialStatus(status: string | number | undefined) {
|
||
const raw = String(status ?? '').trim()
|
||
if (!raw) return { text: '--', type: 'info' as const }
|
||
if (['1', 'ACTIVE', 'ENABLED', 'ENABLE'].includes(raw)) {
|
||
return { text: '启用', type: 'success' as const }
|
||
}
|
||
if (['0', 'INACTIVE', 'DISABLED', 'DISABLE'].includes(raw)) {
|
||
return { text: '停用', type: 'danger' as const }
|
||
}
|
||
return { text: String(status), type: 'info' as const }
|
||
}
|
||
|
||
export function formatDimensions(row: PackageMaterialRecord): string {
|
||
const parts = [row.length, row.width, row.height].map((v) => {
|
||
if (v === null || v === undefined) return null
|
||
return String(v)
|
||
})
|
||
const valid = parts.filter(Boolean)
|
||
return valid.length ? valid.join(' × ') : '--'
|
||
}
|
||
|
||
export function formatNumber(value: unknown, digits = 2): string {
|
||
if (value === null || value === undefined || value === '') return '--'
|
||
const num = Number(value)
|
||
if (!Number.isFinite(num)) return String(value)
|
||
return Number.isInteger(num) ? String(num) : num.toFixed(digits)
|
||
}
|
||
|
||
export function buildPackageMaterialListPayload(options: {
|
||
page: number
|
||
pageSize: number
|
||
where?: Record<string, unknown>
|
||
}) {
|
||
const payload: Record<string, unknown> = {
|
||
page: options.page,
|
||
pageSize: options.pageSize,
|
||
pagination: {
|
||
page: options.page,
|
||
pageSize: options.pageSize
|
||
}
|
||
}
|
||
if (options.where && Object.keys(options.where).length) {
|
||
payload.where = options.where
|
||
}
|
||
return payload
|
||
}
|
||
|
||
/** 兼容 setting.packageMaterial.get 扁平响应与 data 嵌套响应 */
|
||
export function parsePackageMaterialListResponse(res: any): {
|
||
list: PackageMaterialRecord[]
|
||
total: number
|
||
page?: number
|
||
pageSize?: number
|
||
} {
|
||
const pick = (obj: any) => {
|
||
if (Array.isArray(obj)) {
|
||
return { list: obj as PackageMaterialRecord[], total: obj.length, page: undefined, pageSize: undefined }
|
||
}
|
||
if (!obj || typeof obj !== 'object') {
|
||
return { list: [] as PackageMaterialRecord[], total: 0, page: undefined, pageSize: undefined }
|
||
}
|
||
const list = Array.isArray(obj.list)
|
||
? obj.list
|
||
: Array.isArray(obj.records)
|
||
? obj.records
|
||
: Array.isArray(obj.items)
|
||
? obj.items
|
||
: []
|
||
const total = Number(obj.pagination?.total ?? obj.total ?? obj.count ?? 0)
|
||
const page = Number(obj.page ?? obj.pagination?.page)
|
||
const pageSize = Number(obj.page_size ?? obj.pageSize ?? obj.pagination?.pageSize)
|
||
return {
|
||
list,
|
||
total: Number.isFinite(total) ? total : list.length,
|
||
page: Number.isFinite(page) && page > 0 ? page : undefined,
|
||
pageSize: Number.isFinite(pageSize) && pageSize > 0 ? pageSize : undefined
|
||
}
|
||
}
|
||
|
||
const nested = res?.data
|
||
if (Array.isArray(nested)) return pick(nested)
|
||
if (
|
||
nested &&
|
||
typeof nested === 'object' &&
|
||
(Array.isArray(nested.list) ||
|
||
nested.total !== undefined ||
|
||
nested.page !== undefined ||
|
||
nested.page_size !== undefined)
|
||
) {
|
||
return pick(nested)
|
||
}
|
||
|
||
return pick(res)
|
||
}
|
||
|
||
/** setting.packageMaterial.search 响应解析(data 为数组,最多 50 条) */
|
||
export function parsePackageMaterialSearchResponse(res: unknown): PackageMaterialRecord[] {
|
||
const root = (res as any)?.data ?? res
|
||
if (Array.isArray(root)) return root as PackageMaterialRecord[]
|
||
if (Array.isArray(root?.data)) return root.data as PackageMaterialRecord[]
|
||
if (Array.isArray(root?.list)) return root.list as PackageMaterialRecord[]
|
||
return []
|
||
}
|
||
|
||
export function isPackageMaterialApiSuccess(res: unknown) {
|
||
const r = res as { code?: number; flag?: string } | null | undefined
|
||
return Boolean(r && (r.code === 1 || r.code === 200 || r.flag === 'success'))
|
||
}
|