WEB/src/views/order/orderManagement/utils/order-index-helpers.ts
2026-06-30 18:19:51 +08:00

436 lines
15 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 type { OrderExportMode, UpdateExpressWeighingWeightResponse } from '@/api/order.index/orderindex'
export type { OrderExportMode }
export {
isExportApiSuccess as isOrderIndexApiSuccess,
parseExportTaskPayload as parseOrderExportTaskPayload,
normalizeExportDownloadUrl as normalizeOrderDownloadUrl,
downloadExportFile as downloadOrderExportFile
} from '@/utils/asyncExportHelpers'
/** order.export 导出模式选项 */
export const ORDER_EXPORT_MODE_OPTIONS: Array<{ label: string; value: OrderExportMode }> = [
{ label: '不含明细', value: 'waybill' },
{ label: '含明细', value: 'item' }
]
export type OrderExportScope = {
table_type?: string
tableType?: string
module?: string
module_name?: string
}
export type OrderListPagination = {
page?: number
pageSize?: number
total?: number
totalPages?: number
hasPrevious?: boolean
hasNext?: boolean
}
/** order.list 及各子模块 list 接口统一响应结构 */
export type OrderWorkbenchListData = {
list: any[]
pagination: OrderListPagination
/** 复核状态:none=未复核,fin=已复核 */
recheck_status?: string
}
export type ParsedOrderListResponse = {
list: any[]
total: number
page: number
pageSize: number
recheck_status?: string
}
/** 各订单子页与 order.export 入参映射 */
export const ORDER_INDEX_EXPORT_SCOPES: Record<string, OrderExportScope> = {
reviewOrder: { table_type: 'revieworder', module: 'reviewOrder' },
pendingOrder: { table_type: 'current', module: 'pendingOrder' },
exceptionOrder: { table_type: 'current', module: 'expectionOrder' },
outboundOrder: { table_type: 'current', module: 'outboundOrder' },
entrustWarehouse: { table_type: 'outsourced', module: 'entrustOrder' },
deliveryOrder: { table_type: 'dispatch', module: 'deliveryOrder' },
deliveryException: { table_type: 'dispatch', module: 'deliveryExpectionOrder' },
archiveOrder: { table_type: 'history', module: 'archiveOrder' }
}
export function parseOrderListResponse(res: any): ParsedOrderListResponse {
const pick = (obj: any) => {
if (!obj || typeof obj !== 'object') {
return { list: [] as any[], total: 0, page: 1, pageSize: 20, recheck_status: undefined as string | undefined }
}
const rawList = Array.isArray(obj.list)
? obj.list
: Array.isArray(obj.records)
? obj.records
: []
const list = rawList.map(normalizeOrderListRow)
const pagination = obj.pagination && typeof obj.pagination === 'object' ? obj.pagination : {}
const total = Number(pagination.total ?? obj.total ?? obj.count ?? list.length)
const page = Number(pagination.page ?? obj.page ?? 1)
const pageSize = Number(pagination.pageSize ?? obj.page_size ?? 20)
const recheckStatusRaw = obj.recheck_status ?? obj.recheckStatus
const recheck_status =
recheckStatusRaw === undefined || recheckStatusRaw === null || recheckStatusRaw === ''
? undefined
: String(recheckStatusRaw)
return {
list,
total: Number.isFinite(total) ? total : list.length,
page: Number.isFinite(page) && page > 0 ? page : 1,
pageSize: Number.isFinite(pageSize) && pageSize > 0 ? pageSize : 20,
recheck_status
}
}
const nested = res?.data
if (nested && typeof nested === 'object') {
if (Array.isArray(nested.list) || nested.pagination || nested.total !== undefined) {
return pick(nested)
}
if (nested.data && typeof nested.data === 'object') {
return pick(nested.data)
}
}
return pick(res)
}
export function parseOrderDetailPayload(res: any): Record<string, any> {
if (!res || typeof res !== 'object') return {}
const nested = res.data
if (nested && typeof nested === 'object' && !Array.isArray(nested)) {
if (nested.order || nested.detail || nested.expresses || nested.orderItems || nested.items) {
return nested
}
if (nested.data && typeof nested.data === 'object') {
return nested.data
}
}
return nested && typeof nested === 'object' ? nested : res
}
/** order.detail 商品明细:优先 orderItems,兼容 items 及嵌套字段 */
export function normalizeOrderDetailItems(data: Record<string, any>): any[] {
if (Array.isArray(data.orderItems)) return data.orderItems
if (Array.isArray(data.items)) return data.items
const rawItems = data.order?.items || data.detail?.items
if (Array.isArray(rawItems)) return rawItems
if (typeof rawItems === 'string') {
try {
const parsed = JSON.parse(rawItems)
return Array.isArray(parsed) ? parsed : []
} catch {
return []
}
}
return []
}
/** order.detail 运单列表:优先 expresses,兼容 waybill_codes / 单运单字段 */
export function normalizeOrderExpressList(data: Record<string, any>) {
const expresses = Array.isArray(data.expresses) ? data.expresses : []
if (expresses.length) {
return expresses
.map((item: any) => ({
waybill_code: String(item.waybill_code || item.express_code || item.code || '').trim(),
weighing_weight: item.weighing_weight ?? null,
sys_logistics_code: item.sys_logistics_code || '',
sys_logistics_name: item.sys_logistics_name || '',
logistics_code: item.logistics_code || '',
logistics_name: item.logistics_name || item.sys_logistics_name || item.logisticsName || '',
logistics_interface_id: item.logistics_interface_id ?? null,
business_key: item.business_key ?? '',
created_at: item.created_at ?? item.create_at ?? null,
is_main: String(item.business_key) === '1',
package_material_code: item.package_material_code,
package_material_name_snapshot: item.package_material_name_snapshot
}))
.filter((item: { waybill_code: string }) => item.waybill_code)
}
const codes = Array.isArray(data.waybill_codes)
? data.waybill_codes
: Array.isArray(data.order?.waybill_codes)
? data.order.waybill_codes
: Array.isArray(data.detail?.waybill_codes)
? data.detail.waybill_codes
: []
if (codes.length) {
const logisticsName = data.order?.logistics_name || data.detail?.logistics_name || ''
return codes.map((code: any) => ({
waybill_code: String(code).trim(),
weighing_weight: null,
logistics_name: logisticsName,
sys_logistics_name: '',
logistics_code: '',
sys_logistics_code: '',
logistics_interface_id: null,
business_key: '',
created_at: null,
is_main: false,
package_material_code: '',
package_material_name_snapshot: ''
})).filter((item: { waybill_code: string }) => item.waybill_code)
}
const expressCode = data.order?.express_code || data.detail?.express_code
if (expressCode) {
return [{
waybill_code: String(expressCode).trim(),
weighing_weight: null,
logistics_name: data.order?.logistics_name || data.detail?.logistics_name || '',
sys_logistics_name: '',
logistics_code: '',
sys_logistics_code: '',
logistics_interface_id: null,
business_key: '',
created_at: null,
is_main: false,
package_material_code: '',
package_material_name_snapshot: ''
}]
}
return []
}
/** order.detail 运单号汇总:优先 waybill_codes,否则从 expresses 提取 */
export function normalizeOrderWaybillCodes(data: Record<string, any>, expressList: any[] = []) {
if (Array.isArray(data.waybill_codes)) return data.waybill_codes
if (Array.isArray(data.order?.waybill_codes)) return data.order.waybill_codes
if (Array.isArray(data.detail?.waybill_codes)) return data.detail.waybill_codes
return expressList
.map((item) => item?.waybill_code)
.filter((item) => item !== null && item !== undefined && `${item}`.trim() !== '')
}
/** order.updateExpressWeighingWeight 响应 data 解析 */
export function parseUpdateExpressWeighingWeightResponse(
res: any
): Partial<UpdateExpressWeighingWeightResponse> {
if (!res || typeof res !== 'object') return {}
const nested = res.data
if (nested && typeof nested === 'object' && !Array.isArray(nested)) {
if (
nested.weighing_weight !== undefined ||
nested.waybill_code !== undefined ||
nested.order_id !== undefined ||
nested.wave_id !== undefined ||
nested.wave_no !== undefined ||
nested.business_key !== undefined ||
nested.total_weight !== undefined ||
nested.split_count !== undefined ||
nested.splits !== undefined ||
nested.store_name !== undefined ||
nested.sys_logistics_name !== undefined
) {
return nested as UpdateExpressWeighingWeightResponse
}
if (nested.data && typeof nested.data === 'object') {
return nested.data as UpdateExpressWeighingWeightResponse
}
}
if (nested && typeof nested === 'object') {
return nested as UpdateExpressWeighingWeightResponse
}
return (res as UpdateExpressWeighingWeightResponse) ?? {}
}
export function parseOrderLogsResponse(res: any): { list: any[]; total: number } {
const pick = (obj: any) => {
if (!obj || typeof obj !== 'object') return { list: [], total: 0 }
const list = Array.isArray(obj.list)
? obj.list
: Array.isArray(obj.records)
? obj.records
: []
const pagination = obj.pagination && typeof obj.pagination === 'object' ? obj.pagination : {}
const total = Number(pagination.total ?? obj.total ?? list.length)
return {
list,
total: Number.isFinite(total) ? total : list.length
}
}
const level1 = res?.data
if (level1 && typeof level1 === 'object') {
if (Array.isArray(level1.list) || level1.pagination) return pick(level1)
if (level1.data && typeof level1.data === 'object') return pick(level1.data)
}
if (Array.isArray(res?.list)) {
return pick(res)
}
return pick(level1)
}
export function sanitizeOrderListWhere(where: Record<string, unknown>) {
const next = { ...where }
delete next.warehouse_id
delete next.warehouseId
return next
}
const DATE_RANGE_OPERATORS = new Set(['between', 'notbetween'])
const NULLISH_OPERATORS = new Set(['isnull', 'isnotnull'])
const MULTI_VALUE_OPERATORS = new Set(['in', 'notin', 'contains'])
function isEmptyConditionValue(value: unknown): boolean {
if (value === undefined || value === null || value === '') return true
if (Array.isArray(value) && value.length === 0) return true
return false
}
/** 归一化 daterange 值:支持 `[start, end]` 或 `{ start/from/begin, end/to/finish }` */
export function normalizeDateRangeValue(value: unknown): [string, string] | null {
if (Array.isArray(value)) {
const start = String(value[0] ?? '').trim()
const end = String(value[1] ?? '').trim()
if (!start || !end) return null
return [start, end]
}
if (value && typeof value === 'object') {
const obj = value as Record<string, unknown>
const start = String(obj.start ?? obj.from ?? obj.begin ?? '').trim()
const end = String(obj.end ?? obj.to ?? obj.finish ?? '').trim()
if (!start || !end) return null
return [start, end]
}
return null
}
function normalizeWhereConditionEntry(condition: Record<string, any>): Record<string, any> | null {
const operator = String(condition.operator ?? 'eq')
const type = condition.type ? String(condition.type) : undefined
const value = condition.value
if (NULLISH_OPERATORS.has(operator)) {
return { operator, type: type || 'text', value: null }
}
if (isEmptyConditionValue(value)) {
return null
}
if (
type === 'daterange'
|| type === 'datetimerange'
|| (DATE_RANGE_OPERATORS.has(operator) && (Array.isArray(value) || (value && typeof value === 'object')))
) {
const rangeValue = normalizeDateRangeValue(value)
if (!rangeValue) return null
const rangeType = type === 'datetimerange' ? 'datetimerange' : 'daterange'
return {
operator: DATE_RANGE_OPERATORS.has(operator) ? operator : 'between',
type: rangeType,
value: rangeValue
}
}
if (type === 'date') {
return { operator, type: 'date', value }
}
if (MULTI_VALUE_OPERATORS.has(operator)) {
return { operator, type: type || 'text', value }
}
if (condition.isMultiple && Array.isArray(value)) {
return {
operator: operator && operator !== 'eq' ? operator : 'in',
type: type || 'text',
value
}
}
if (DATE_RANGE_OPERATORS.has(operator) && Array.isArray(value) && value.length >= 2) {
return {
operator,
type: type || 'number',
value: [value[0], value[1]]
}
}
return {
operator,
type: type || condition.conditionType || 'text',
value
}
}
/** 将 ScanBox getConditions() 转为 order.list 标准 where 条件对象 */
export function buildOrderWhereConditions(rawConditions: Record<string, any>) {
const where: Record<string, any> = {}
Object.keys(rawConditions || {}).forEach((key) => {
const condition = rawConditions[key]
const normalizedKey = key === 'order_code' ? 'delivery_order_code' : key
if (condition && typeof condition === 'object' && 'operator' in condition) {
const normalized = normalizeWhereConditionEntry(condition)
if (normalized) {
where[normalizedKey] = normalized
}
return
}
if (isEmptyConditionValue(condition)) {
return
}
if (Array.isArray(condition)) {
const rangeValue = normalizeDateRangeValue(condition)
if (rangeValue) {
where[normalizedKey] = {
operator: 'between',
type: 'daterange',
value: rangeValue
}
}
return
}
where[normalizedKey] = {
operator: 'eq',
value: condition,
type: 'text'
}
})
return where
}
/** 列表行字段归一化(snake_case 优先,兼容 camelCase) */
export function normalizeOrderListRow(row: any) {
if (!row || typeof row !== 'object') return row
const normalized = { ...row }
if (normalized.intercept_status == null && normalized.interceptStatus != null) {
normalized.intercept_status = normalized.interceptStatus
}
if (normalized.recheck_status == null && normalized.recheckStatus != null) {
normalized.recheck_status = normalized.recheckStatus
}
if (normalized.store_remark == null && normalized.storeRemark != null) {
normalized.store_remark = normalized.storeRemark
}
return normalized
}
/** 行级拦截状态(优先 intercept_status,兼容 interceptStatus) */
export function resolveOrderInterceptStatus(row?: Record<string, any> | null): string {
if (!row || typeof row !== 'object') return 'none'
const value = row.intercept_status ?? row.interceptStatus
if (value === undefined || value === null || value === '') return 'none'
return String(value).trim()
}
/** 是否为有效拦截状态(非 none / 空) */
export function isActiveInterceptStatus(value: unknown): boolean {
const normalized = String(value ?? 'none').trim().toLowerCase()
return normalized !== '' && normalized !== 'none'
}
/** 行级复核状态(优先 recheck_status,兼容历史 inspection_status) */
export function resolveOrderRecheckStatus(row?: Record<string, any> | null): string {
if (!row || typeof row !== 'object') return ''
const value = row.recheck_status ?? row.recheckStatus ?? row.inspection_status
return value === undefined || value === null ? '' : String(value)
}