oms/backend/database/migrations/20260915000000-create-order-deliveries.js
liufumingren 11e7b26ced feat(backend): 发货单模块与 WMS 定时推送队列
- 发货单主表/明细表(order_deliveries / order_delivery_items)控制器与模型
- 表头金额与明细派生字段重算(utils/recalcOrderDelivery.js)
- 状态流转:审核/驳回/取消/恢复/作废、批量审核、批量切换仓库
- 审核入队 + 定时推送:service/orderDeliveryPush.js(乐观锁 CAS 防重、失败重试上限 3 次、业务失败退回待审核)+ schedule/orderDeliveryPush.js
- 收件人 AI 智能识别(service/ai.js,DeepSeek)
- 相关迁移:建表、站内字段、明细表、订单类型默认值、快递字典、菜单种子、历史数据搬迁
2026-09-13 02:23:47 +08:00

184 lines
12 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';
/**
* 发货单(订单)表:order_deliveries
*
* 定位:独立于出库单(outbound_orders)的一张发货单/订单主表,字段口径对齐
* WMS 侧 `wms_order_delivery`(模型 D:\wms\app\model\order_delivery.js)
* 与奇门 `deliveryorder.create` 的 request.deliveryOrder + request.deliveryOrder.receiverInfo
* 落库映射 D:\wms\app\service\systems\delivery_order.js#prepareOrderData
*
* 口径要点(与 WMS 保持一致,勿改):
* - deliveryOrderCode → delivery_order_code,必填;去重键对齐 WMS 的三元唯一
* (delivery_order_code, store_id, warehouse_id),OMS 侧为 (delivery_order_code, principal_id, warehouse_id)
* - 奇门 camelCase 字段一律 snake_case 落库(WMS 同名):order_flag / pre_delivery_order_code /
* pre_delivery_order_id / is_urgency / invoice_flag / source_platform_code / source_platform_name /
* shop_nick / seller_nick / buyer_nick / buyer_message / seller_message / place_order_time / pay_time /
* pay_no / operate_time / operator_name / total_amount / item_amount / discount_amount / freight /
* ar_amount / got_amount / logistics_code / express_code / logistics_area_code /
* oaid_order_source_code / create_channel / need_oms_waybill / extend_props
* - receiverInfo 在 WMS 侧是**扁平落主表**(不单独建表),此处同样扁平为 12 列,列名与 WMS 相同
* - createTime **不落库**(WMS 显式忽略,取服务器时间),OMS 用 created_at
* - sourceOrderCode 不单独落 WMS 的 source_order_code,仅作 pay_no 缺省回退;
* 东侧多来源场景用 source_order_codes(对齐 WMS 列名)存来源订单号集合
* - create_channel:MANUAL 人工 / QM 奇门 / API 开放接口(OMS 调用 WMS openapi 时即 API)
* - need_oms_waybill:仅 createChannel=API 时生效,其余渠道恒 0
*
* OMS 自有字段:principal_id(货主,对应 WMS store_id)、warehouse_id、shop_id、warehouse_code
* 快照、status、created_by、pushed_at、wms_push_result(对齐出入库单的推送记录口径)。
*/
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(
'order_deliveries',
{
id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, autoIncrement: true, primaryKey: true },
// ---------- OMS 归属与键 ----------
principal_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '所属主体ID(货主,对应 WMS store_id)',
references: { model: 'principals', key: 'id' },
},
warehouse_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: true,
comment: '仓库ID(关联 warehouses,对应 WMS warehouse_id)',
references: { model: 'warehouses', key: 'id' },
onDelete: 'SET NULL',
},
shop_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: true,
comment: '店铺ID(关联 shops)',
references: { model: 'shops', key: 'id' },
onDelete: 'SET NULL',
},
warehouse_code: { type: Sequelize.STRING(50), allowNull: true, comment: '仓库编码(报文 warehouseCode 快照)' },
// ---------- 主键 / 类型 / 标记 ----------
delivery_order_code: { type: Sequelize.STRING(50), allowNull: false, comment: '发货单号(必填,唯一去重键)' },
order_type: { type: Sequelize.STRING(50), allowNull: true, comment: '订单类型(WMS 编码,如 JYCK/HHCK/BFCK/PTCK...)' },
order_flag: { type: Sequelize.TEXT, allowNull: true, comment: '订单标记' },
pre_delivery_order_code: { type: Sequelize.STRING(50), allowNull: true, comment: '预发货单号' },
pre_delivery_order_id: { type: Sequelize.STRING(50), allowNull: true, comment: '预发货单ID' },
is_urgency: { type: Sequelize.ENUM('Y', 'N'), allowNull: true, defaultValue: 'N', comment: '是否紧急:Y是 N否(默认 N)' },
invoice_flag: { type: Sequelize.ENUM('Y', 'N'), allowNull: true, defaultValue: 'N', comment: '是否需要发票:Y是 N否(默认 N)' },
// ---------- 平台 / 店铺 ----------
source_platform_code: { type: Sequelize.STRING(50), allowNull: true, comment: '来源平台编码' },
source_platform_name: { type: Sequelize.STRING(200), allowNull: true, comment: '来源平台名称' },
shop_nick: { type: Sequelize.STRING(200), allowNull: true, comment: '店铺名称' },
seller_nick: { type: Sequelize.STRING(200), allowNull: true, comment: '卖家名称' },
buyer_nick: { type: Sequelize.STRING(200), allowNull: true, comment: '买家名称' },
// ---------- 留言 / 备注 ----------
buyer_message: { type: Sequelize.STRING(500), allowNull: true, comment: '买家留言' },
seller_message: { type: Sequelize.STRING(500), allowNull: true, comment: '卖家留言' },
remark: { type: Sequelize.STRING(500), allowNull: true, comment: '备注' },
// ---------- 时间(createTime 被 WMS 显式忽略,此处不设列) ----------
place_order_time: { type: Sequelize.DATE, allowNull: true, comment: '店铺创建订单时间' },
pay_time: { type: Sequelize.DATE, allowNull: true, comment: '支付时间' },
pay_no: { type: Sequelize.STRING(50), allowNull: true, comment: '支付单号(缺省回退取 sourceOrderCode)' },
operate_time: { type: Sequelize.DATE, allowNull: true, comment: 'ERP 审核时间' },
// ---------- 金额 ----------
total_amount: { type: Sequelize.DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '订单总金额' },
item_amount: { type: Sequelize.DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '商品总金额' },
discount_amount: { type: Sequelize.DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '订单折扣金额' },
freight: { type: Sequelize.DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '快递费用' },
ar_amount: { type: Sequelize.DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '应收金额' },
got_amount: { type: Sequelize.DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '已收金额' },
// ---------- 快递 ----------
logistics_code: { type: Sequelize.STRING(50), allowNull: true, comment: '物流公司编码(WMS 标准编码,如 SF/YTO)' },
logistics_name: { type: Sequelize.STRING(200), allowNull: true, comment: '物流公司名称' },
express_code: { type: Sequelize.STRING(50), allowNull: true, comment: '运单号(面单号)' },
logistics_area_code: { type: Sequelize.STRING(50), allowNull: true, comment: '快递区域编码(大头笔信息)' },
// ---------- 操作人 / 其他 ----------
operator_name: { type: Sequelize.STRING(50), allowNull: true, comment: 'ERP 审核人(奇门 operatorName)' },
oaid_order_source_code: { type: Sequelize.STRING(50), allowNull: true, comment: 'OAID 订单来源编码' },
source_order_codes: { type: Sequelize.TEXT, allowNull: true, comment: '来源订单号集合(对齐 WMS 列名 source_order_codes)' },
extend_props: { type: Sequelize.JSON, allowNull: true, comment: '请求级扩展属性(如 ewaybill_order_code)' },
// ---------- 渠道 / 取号 ----------
create_channel: {
type: Sequelize.STRING(20),
allowNull: false,
defaultValue: 'MANUAL',
comment: '创建渠道:MANUAL 人工 / QM 奇门 / API 开放接口',
},
need_oms_waybill: {
type: Sequelize.TINYINT,
allowNull: false,
defaultValue: 0,
comment: '是否需要回 OMS 取号:0不需要 1需要(仅 create_channel=API 生效)',
},
// ---------- 收件人(奇门 receiverInfo 扁平落库,列名与 WMS 一致) ----------
name: { type: Sequelize.STRING(100), allowNull: true, comment: '收件人姓名(receiverInfo.name)' },
tel: { type: Sequelize.STRING(50), allowNull: true, comment: '收件人电话(receiverInfo.tel)' },
mobile: { type: Sequelize.STRING(50), allowNull: true, comment: '收件人移动电话(receiverInfo.mobile)' },
email: { type: Sequelize.STRING(100), allowNull: true, comment: '收件人邮箱(receiverInfo.email)' },
country_code: { type: Sequelize.STRING(50), allowNull: true, comment: '国家编码(receiverInfo.countryCode)' },
province: { type: Sequelize.STRING(100), allowNull: true, comment: '省份(receiverInfo.province)' },
city: { type: Sequelize.STRING(100), allowNull: true, comment: '城市(receiverInfo.city)' },
area: { type: Sequelize.STRING(100), allowNull: true, comment: '区县(receiverInfo.area)' },
town: { type: Sequelize.STRING(100), allowNull: true, comment: '街道/镇(receiverInfo.town)' },
detail_address: { type: Sequelize.STRING(500), allowNull: true, comment: '详细地址(receiverInfo.detailAddress)' },
zip_code: { type: Sequelize.STRING(20), allowNull: true, comment: '邮编(receiverInfo.zipCode)' },
oaid: { type: Sequelize.STRING(100), allowNull: true, comment: 'OAID(receiverInfo.oaid)' },
// ---------- OMS 状态与推送记录 ----------
status: {
type: Sequelize.TINYINT,
allowNull: false,
defaultValue: 1,
comment: '状态:1待处理 2已审核 3已推送 4已完成 5已作废 9已取消',
},
created_by: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: true,
comment: '创建人ID(关联 users)',
references: { model: 'users', key: 'id' },
onDelete: 'SET NULL',
},
pushed_at: { type: Sequelize.DATE, allowNull: true, comment: 'WMS 推送时间' },
wms_push_result: { type: Sequelize.JSON, allowNull: true, comment: 'WMS 推送结果(外部单号/返回码/消息/原始报文)' },
created_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
comment: '创建时间(对应 WMS create_at,取服务器时间)',
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
},
},
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '发货单(订单)表' }
);
// 去重键:对齐 WMS (delivery_order_code, store_id, warehouse_id) 唯一索引
await queryInterface.addIndex('order_deliveries', {
fields: [ 'delivery_order_code', 'principal_id', 'warehouse_id' ],
unique: true,
name: 'uk_order_deliveries_code',
});
await queryInterface.addIndex('order_deliveries', { fields: [ 'principal_id' ], name: 'idx_order_deliveries_principal' });
await queryInterface.addIndex('order_deliveries', { fields: [ 'warehouse_id' ], name: 'idx_order_deliveries_warehouse' });
await queryInterface.addIndex('order_deliveries', { fields: [ 'shop_id' ], name: 'idx_order_deliveries_shop' });
await queryInterface.addIndex('order_deliveries', { fields: [ 'status' ], name: 'idx_order_deliveries_status' });
await queryInterface.addIndex('order_deliveries', { fields: [ 'place_order_time' ], name: 'idx_order_deliveries_place_time' });
},
async down(queryInterface) {
await queryInterface.dropTable('order_deliveries');
},
};