237 lines
7.0 KiB
TypeScript
237 lines
7.0 KiB
TypeScript
export type PackageSupplierRecord = {
|
|
id?: number
|
|
supplier_code?: string
|
|
supplier_name?: string
|
|
contact_name?: string
|
|
contact_phone?: string
|
|
status?: number
|
|
created_at?: string
|
|
updated_at?: string
|
|
}
|
|
|
|
export const packageSupplierColumns = [
|
|
{ prop: 'id', label: 'ID', width: '80', sortable: true },
|
|
{ prop: 'supplier_code', label: '供应商编码', minWidth: '130', showOverflowTooltip: true },
|
|
{ prop: 'supplier_name', label: '供应商名称', minWidth: '160', showOverflowTooltip: true },
|
|
{ prop: 'contact_name', label: '联系人', minWidth: '120', showOverflowTooltip: true },
|
|
{ prop: 'contact_phone', label: '联系电话', minWidth: '140', showOverflowTooltip: true },
|
|
{ prop: 'status', label: '状态', width: '90', align: 'center' },
|
|
{ prop: 'actions', label: '操作', width: '150', align: 'center', fixed: 'right' }
|
|
]
|
|
|
|
export const packageSupplierScanFields = [
|
|
{
|
|
value: 'quickWhere',
|
|
label: '关键词',
|
|
type: 'text',
|
|
placeholder: '编码/名称/联系人/电话',
|
|
operators: ['contains'],
|
|
defaultOperator: 'contains'
|
|
},
|
|
{
|
|
value: 'supplier_code',
|
|
label: '供应商编码',
|
|
type: 'text',
|
|
placeholder: '请输入供应商编码',
|
|
operators: ['eq', 'contains', 'like'],
|
|
defaultOperator: 'contains'
|
|
},
|
|
{
|
|
value: 'supplier_name',
|
|
label: '供应商名称',
|
|
type: 'text',
|
|
placeholder: '请输入供应商名称',
|
|
operators: ['eq', 'contains', 'like'],
|
|
defaultOperator: 'contains'
|
|
},
|
|
{
|
|
value: 'contact_name',
|
|
label: '联系人',
|
|
type: 'text',
|
|
placeholder: '请输入联系人',
|
|
operators: ['eq', 'contains', 'like'],
|
|
defaultOperator: 'contains'
|
|
},
|
|
{
|
|
value: 'contact_phone',
|
|
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 packageSupplierScanFields) {
|
|
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 packageSupplierScanFields) {
|
|
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 normalizePackageSupplierScanConditionToWhere(
|
|
condition: any,
|
|
fields: typeof packageSupplierScanFields = packageSupplierScanFields
|
|
): 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 formatPackageSupplierStatus(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 buildPackageSupplierListPayload(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.packageSupplier.get 扁平响应与 data 嵌套响应 */
|
|
export function parsePackageSupplierListResponse(res: any): {
|
|
list: PackageSupplierRecord[]
|
|
total: number
|
|
page?: number
|
|
pageSize?: number
|
|
} {
|
|
const pick = (obj: any) => {
|
|
if (Array.isArray(obj)) {
|
|
return { list: obj as PackageSupplierRecord[], total: obj.length, page: undefined, pageSize: undefined }
|
|
}
|
|
if (!obj || typeof obj !== 'object') {
|
|
return { list: [] as PackageSupplierRecord[], 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)
|
|
}
|