WMS_MAIN/app/model/order_delivery.js
liufumingren da7df4139d feat(order): 新增 need_oms_waybill 回 OMS 取号标记与本地取号分流
- 模型/迁移:wms_order_delivery 新增 need_oms_waybill TINYINT(4) NOT NULL DEFAULT 0,
  迁移覆盖基表与全部分表(含 _history/_dispatch/_outsourced),幂等且 down 可回滚
- 写入链路:openapi 请求根节点 need_oms_waybill(兼容 camelCase needOmsWaybill)→
  openapiMethods/deliveryorder.js 透传;delivery_order.js 仅 API 来源可能为 1,
  QM/MANUAL/STOCKOUT 一律强制 0(见相邻提交)
- 取号分流:need_oms_waybill=1 的订单不参与本地自动分配快递
  1. asyncProcessOrderCreated 不入本地取号队列,写 ORDER_NEED_OMS_WAYBILL_WAIT 操作日志
  2. express.createWaybillCodePipeline 按订单级标记过滤(同订单全部 business_key 一并跳过),
     返回值新增 skipped_need_oms_waybill_order_ids
  3. schedule/consume_waybill_allocate_queue 将对应队列行置终态 waiting_oms
     (不再走 pending 重试 → failed)
- waybill_allocate_queue.status 注释补充 waiting_oms 语义(STRING(20),无需迁移)
- test: delivery_order.asyncProcessOrderCreated.test.js 新增分流/保持本地取号用例,
  express.createWaybillCodePipeline.skipCancelled.test.js 补齐返回字段断言
2026-09-13 15:30:55 +08:00

625 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' ],
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: '是否紧急',
},
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: INTEGER(11),
allowNull: true,
comment: '来源键',
},
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)',
},
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: '打印状态机',
},
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;
};