WMS_MAIN/app/model/order_delivery.js
liufumingren 765bb2c0f7 feat(order): from_key 改为承载上游应用 appKey 字符串
背景:from_key 是「回原通道」的应用标识,出库单回传与取号业务分支都依赖它。
历史只存过奇门/ERP 的纯数字 appKey(聚水潭 23060081、吉客云 24869601),
OpenAPI 创建来源的 appKey 是字符串(wms_api_app.self_app_key,如
WMSD08A3696BBA9551F261C88BBF7F462BD),INTEGER 列无法承载,
normalizeOrderFromKey 会把字符串归一化成 null,导致 API 渠道订单 from_key 全为空。

改动:
- from_key 列 INTEGER(11) -> VARCHAR(100),迁移覆盖基表与全部分表
  (current/history/dispatch/outsourced),整数改字符串需重建表,
  迁移在改列后校验并恢复 AUTO_INCREMENT 水位,避免分表 id 区间重叠
- normalizeOrderFromKey 不再按整数归一化,改为去空白 + 截断 100 落库
- DY 平台聚水潭判断改为字符串比较,兼容改造后的存量数据

落库口径:QM 取 wms_qimen_apps.app_key,API 取 wms_api_app.self_app_key,
MANUAL/STOCKOUT 为空。

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-04 04:46:38 +08:00

644 lines
15 KiB
JavaScript
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.

'use strict';
module.exports = app => {
const { Sequelize, model } = app;
const { STRING, BIGINT, DECIMAL, TEXT, ENUM, DATE, INTEGER, CHAR, SMALLINT, TINYINT, JSON } = Sequelize;
// 状态机枚举定义
const StatusEnum = {
STATUS: [ 'pedding', 'created', 'cancel', 'error', 'finished' ],
SYNC_STATUS: [ 'padding', 'fin', 'error' ],
POST_STATUS: [ 'padding', 'fin', 'timeout', 'error', 'none' ],
STORAGE_STATUS: [ 'padding', 'fin', 'error', 'none' ],
PICK_STATUS: [ 'padding', 'fin', 'error', 'none' ],
PACK_STATUS: [ 'none', 'padding', 'fin', 'error' ],
SENDED_STATUS: [ 'none', 'padding', 'fin', 'error' ],
INSPECTION_STATUS: [ 'none', 'padding', 'running', 'fin', 'error' ],
PRINT_STATUS: [ 'none', 'padding', 'fin', 'error' ],
CONFIRM_PRINT_STATUS: [ 'none', 'fin' ],
PRINT_LOCK_STATUS: [ 'none', 'warehouse' ],
CLAIM_STATUS: [ 'none', 'padding', 'fin', 'error' ],
OUTBOUND_STATUS: [ 'none', 'padding', 'fin', 'timeout', 'error' ],
LOCK_STATUS: [ 'none', 'warehouse', 'store' ],
INTERCEPT_STATUS: [
'none', 'requesting', 'requested', 'intercepting',
'intercepted_in_transit', 'intercepted_at_warehouse',
'returning', 'returned', 'delivered_by_mistake',
'timeout', 'cancelled', 'failed', 'exception',
],
URGENCY: [ 'Y', 'N' ],
INVOICE: [ 'Y', 'N' ],
};
// 基础模型定义
const baseModel = {
id: {
type: BIGINT(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
},
delivery_order_code: {
type: STRING(50),
allowNull: true,
comment: '发货单号',
},
pre_delivery_order_code: {
type: STRING(50),
allowNull: true,
comment: '预发货单号',
},
pre_delivery_order_id: {
type: STRING(50),
allowNull: true,
comment: '预发货单ID',
},
order_type: {
type: STRING(50),
allowNull: true,
comment: '订单类型',
},
warehouse_code: {
type: STRING(50),
allowNull: true,
comment: '仓库编码',
},
order_flag: {
type: TEXT('long'),
allowNull: true,
comment: '订单标记',
},
extend_props: {
type: JSON,
allowNull: true,
comment: '订单扩展属性',
},
source_platform_code: {
type: STRING(50),
allowNull: true,
comment: '来源平台编码',
},
source_platform_name: {
type: STRING(200),
allowNull: true,
comment: '来源平台名称',
},
oaid_order_source_code: {
type: STRING(50),
allowNull: true,
comment: 'OAID订单来源编码',
},
create_channel: {
type: STRING(20),
allowNull: false,
defaultValue: 'MANUAL',
comment: '创建来源渠道',
},
need_oms_waybill: {
type: TINYINT(4),
allowNull: false,
defaultValue: 0,
comment: '是否需要回OMS取号:0不需要 1需要(仅openapi创建来源使用,其他默认0)',
},
source_order_codes: {
type: TEXT,
allowNull: true,
comment: '来源订单号集合',
},
sys_platform_code: {
type: STRING(50),
allowNull: true,
comment: '系统平台编码',
},
sys_platform_name: {
type: STRING(200),
allowNull: true,
comment: '系统平台名称',
},
create_at: {
type: DATE,
allowNull: true,
comment: '创建时间',
},
delete_at: {
type: DATE,
allowNull: true,
comment: '删除时间',
},
updated_at: {
type: DATE,
allowNull: true,
comment: '更新时间',
},
place_order_time: {
type: DATE,
allowNull: true,
comment: '店铺创建订单时间',
},
pay_time: {
type: DATE,
allowNull: true,
comment: '支付时间',
},
pay_no: {
type: TEXT,
allowNull: true,
comment: '支付单号',
},
service_code: {
type: STRING(50),
allowNull: true,
comment: '集包服务编码',
},
operator_name: {
type: STRING(50),
allowNull: true,
comment: 'ERP审核人',
},
operate_time: {
type: DATE,
allowNull: true,
comment: 'ERP审核时间',
},
delivery_time: {
type: DATE,
allowNull: true,
comment: '发货时间',
},
shop_nick: {
type: STRING(200),
allowNull: true,
comment: '店铺名称',
},
buyer_message: {
type: STRING(500),
allowNull: true,
comment: '买家留言',
},
seller_message: {
type: STRING(500),
allowNull: true,
comment: '卖家留言',
},
remark: {
type: STRING(500),
allowNull: true,
comment: '备注',
},
seller_nick: {
type: STRING(200),
allowNull: true,
comment: '卖家名称',
},
buyer_nick: {
type: STRING(200),
allowNull: true,
comment: '买家名称',
},
total_amount: {
type: DECIMAL(18, 2),
allowNull: true,
comment: '订单总金额',
},
item_amount: {
type: DECIMAL(18, 2),
allowNull: true,
comment: '商品总金额',
},
discount_amount: {
type: DECIMAL(18, 2),
allowNull: true,
comment: '订单折扣金额',
},
freight: {
type: DECIMAL(18, 2),
allowNull: true,
comment: '快递费用',
},
ar_amount: {
type: DECIMAL(18, 2),
allowNull: true,
comment: '应收金额',
},
got_amount: {
type: DECIMAL(18, 2),
allowNull: true,
comment: '已收金额',
},
service_fee: {
type: DECIMAL(18, 2),
allowNull: true,
comment: 'COD服务费',
},
logistics_code: {
type: STRING(50),
allowNull: true,
comment: '物流公司编码',
},
logistics_name: {
type: STRING(200),
allowNull: true,
comment: '物流公司名称',
},
sys_logistics_code: {
type: STRING(50),
allowNull: true,
comment: '系统物流公司编码',
},
sys_logistics_name: {
type: STRING(200),
allowNull: true,
comment: '系统物流公司名称',
},
basket_number: {
type: STRING(50),
allowNull: true,
comment: '筐号',
},
express_code: {
type: STRING(50),
allowNull: true,
comment: '面单号前置',
},
logistics_area_code: {
type: STRING(50),
allowNull: true,
comment: '快递区域编码, 大头笔信息',
},
delivery_type: {
type: STRING(255),
allowNull: true,
comment: '发货服务类型',
},
weight_type: {
type: INTEGER(11),
allowNull: false,
defaultValue: 0,
comment: '重量类型:0常规、1轻货、2重货、3冷链',
},
is_urgency: {
type: ENUM(...StatusEnum.URGENCY),
allowNull: true,
defaultValue: 'N',
comment: '是否紧急',
},
is_b2b: {
type: ENUM('Y', 'N'),
allowNull: true,
defaultValue: 'N',
comment: '是否B2B:Y是 N否',
},
invoice_flag: {
type: ENUM(...StatusEnum.INVOICE),
allowNull: true,
defaultValue: 'N',
comment: '是否需要发票',
},
status: {
type: ENUM(...StatusEnum.STATUS),
allowNull: true,
defaultValue: 'created',
comment: '订单状态',
},
name: {
type: TEXT,
allowNull: true,
comment: '收件人姓名',
},
zip_code: {
type: STRING(50),
allowNull: true,
comment: '收件人邮编',
},
tel: {
type: TEXT,
allowNull: true,
comment: '收件人电话',
},
mobile: {
type: TEXT,
allowNull: true,
comment: '收件人移动电话',
},
email: {
type: TEXT,
allowNull: true,
comment: '收件人邮箱',
},
country_code: {
type: STRING(255),
allowNull: true,
comment: '国家编码',
},
province: {
type: STRING(255),
allowNull: true,
comment: '省份',
},
city: {
type: STRING(255),
allowNull: true,
comment: '城市',
},
area: {
type: STRING(255),
allowNull: true,
comment: '区',
},
town: {
type: STRING(255),
allowNull: true,
comment: '镇',
},
detail_address: {
type: TEXT,
allowNull: true,
comment: '详细地址',
},
id_type: {
type: STRING(255),
allowNull: true,
comment: '证件类型',
},
id_number: {
type: INTEGER(11),
allowNull: true,
comment: '证件号码',
},
oaid: {
type: TEXT,
allowNull: true,
comment: 'OAID',
},
sync_status: {
type: ENUM(...StatusEnum.SYNC_STATUS),
allowNull: true,
defaultValue: 'padding',
comment: '同步状态',
},
store_id: {
type: BIGINT(20).UNSIGNED,
allowNull: true,
comment: '货主ID',
},
warehouse_id: {
type: BIGINT(20).UNSIGNED,
allowNull: true,
comment: '仓库ID',
},
from_key: {
type: STRING(100),
allowNull: true,
comment: '来源键(上游对接应用 appKey:QM=wms_qimen_apps.app_key,API=wms_api_app.self_app_key)',
},
items: {
type: TEXT,
allowNull: true,
comment: '商品JSON字符串',
},
items_count: {
type: SMALLINT(5).UNSIGNED,
allowNull: false,
defaultValue: 0,
comment: '商品种类数',
},
items_total: {
type: INTEGER(10).UNSIGNED,
allowNull: false,
defaultValue: 0,
comment: '商品总数',
},
items_weight: {
type: INTEGER(11),
allowNull: false,
defaultValue: 0,
comment: '总重量(g)',
},
packages_weight: {
type: INTEGER(11),
allowNull: false,
defaultValue: 0,
comment: '所有包裹总重量(g)',
},
items_hash: {
type: CHAR(32),
allowNull: false,
defaultValue: '',
comment: 'MD5哈希值(32位)',
},
items_crc: {
type: INTEGER(10).UNSIGNED,
allowNull: false,
defaultValue: 0,
comment: 'CRC32校验码',
},
wave_id: {
type: BIGINT(20),
allowNull: false,
defaultValue: 0,
comment: '波次ID,0表示未分配',
},
wave_no: {
type: STRING(50),
allowNull: false,
defaultValue: '',
comment: '波次号,空字符串表示未分配',
},
wave_scope: {
type: TINYINT(4),
allowNull: false,
defaultValue: 0,
comment: '波次范围:0未分配波次 1大团购 2散单 3小团购 4单品单件',
},
grab_operator_id: {
type: BIGINT(20),
allowNull: true,
defaultValue: null,
comment: '抓取人工号ID',
},
grab_operator: {
type: STRING(50),
allowNull: true,
defaultValue: null,
comment: '抓取人',
},
claim_operator_id: {
type: BIGINT(20),
allowNull: true,
defaultValue: null,
comment: '领料人工号ID',
},
claim_operator: {
type: STRING(50),
allowNull: true,
defaultValue: null,
comment: '领料人',
},
wave_strategy_version: {
type: INTEGER(10),
allowNull: false,
defaultValue: 0,
comment: '波次策略版本号',
},
wave_scan_result: {
type: INTEGER(10),
allowNull: false,
defaultValue: 0,
comment: '波次扫描结果,0未扫描,1扫描符合,2扫描没有符合',
},
// 状态机字段
post_status: {
type: ENUM(...StatusEnum.POST_STATUS),
allowNull: true,
defaultValue: 'none',
comment: '分配快递号状态机',
},
storage_status: {
type: ENUM(...StatusEnum.STORAGE_STATUS),
allowNull: true,
defaultValue: 'none',
comment: '分配储位状态机',
},
pick_status: {
type: ENUM(...StatusEnum.PICK_STATUS),
allowNull: true,
defaultValue: 'none',
comment: '拣货状态机',
},
pack_status: {
type: ENUM(...StatusEnum.PACK_STATUS),
allowNull: true,
defaultValue: 'none',
comment: '打包状态机',
},
sended_status: {
type: ENUM(...StatusEnum.SENDED_STATUS),
allowNull: true,
defaultValue: 'none',
comment: '发货状态机',
},
inspection_status: {
type: ENUM(...StatusEnum.INSPECTION_STATUS),
allowNull: true,
defaultValue: 'none',
comment: '检验状态机',
},
recheck_status: {
type: STRING(20),
allowNull: true,
defaultValue: 'none',
comment: '复核状态',
},
recheck_time: {
type: DATE,
allowNull: true,
comment: '复核时间',
},
step: {
type: INTEGER(10),
allowNull: false,
defaultValue: 0,
comment: '订单步骤:0未审核,1已审核,2处理中,3驳回/异常,4配送中,5已完成,9已关闭',
},
print_status: {
type: ENUM(...StatusEnum.PRINT_STATUS),
allowNull: true,
defaultValue: 'none',
comment: '打印状态机',
},
confirm_print_status: {
type: ENUM(...StatusEnum.CONFIRM_PRINT_STATUS),
allowNull: true,
defaultValue: 'none',
comment: '确认打印状态机(打印完成后真实扫描面单确认)',
},
claim_status: {
type: ENUM(...StatusEnum.CLAIM_STATUS),
allowNull: true,
defaultValue: 'none',
comment: '领取状态机',
},
outbound_status: {
type: ENUM(...StatusEnum.OUTBOUND_STATUS),
allowNull: true,
defaultValue: 'none',
comment: '出库状态机',
},
lock_status: {
type: ENUM(...StatusEnum.LOCK_STATUS),
allowNull: true,
defaultValue: 'none',
comment: '锁定状态机',
},
print_lock_status: {
type: ENUM(...StatusEnum.PRINT_LOCK_STATUS),
allowNull: true,
defaultValue: 'none',
comment: '打印锁定状态机',
},
intercept_status: {
type: ENUM(...StatusEnum.INTERCEPT_STATUS),
allowNull: true,
defaultValue: 'none',
comment: '拦截状态机',
},
intercept_time: {
type: DATE,
allowNull: true,
defaultValue: null,
comment: '拦截时间(进入拦截流程/库内拦截确认时写入)',
},
};
// 主表模型
const OrderDelivery = model.define('order_delivery', baseModel, {
tableName: 'wms_order_delivery',
timestamps: false,
comment: '仓库发货单常用表',
indexes: [
{
name: 'idx_current_store_id',
fields: [ 'store_id' ],
},
{
name: 'idx_current_warehouse_id',
fields: [ 'warehouse_id' ],
},
{
name: 'idx_current_delivery_code',
unique: true,
fields: [ 'delivery_order_code', 'store_id', 'warehouse_id' ],
},
{
name: 'idx_items',
fields: [{ attribute: 'items', length: 50 }],
},
{
name: 'idx_items_hash',
fields: [ 'items_hash' ],
},
{
name: 'idx_hash_crc',
fields: [ 'items_hash', 'items_crc', 'warehouse_id' ],
},
{
name: 'wave_id',
fields: [ 'status', 'wave_id' ],
},
// 状态机索引
{
name: 'idx_statuses_composite',
fields: [
'status',
'post_status',
'pick_status',
'pack_status',
'outbound_status',
],
},
],
});
return OrderDelivery;
};