oms/backend/database/migrations/20260915000100-create-order-delivery-items.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

88 lines
4.7 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_delivery_items
*
* 对应奇门 `deliveryorder.create` 的 `request.orderLines.orderLine`(子表商品明细),
* 字段口径对齐 WMS 侧明细落库结构
* (D:\wms\app\service\systems\delivery_order.js#addOrderLines 的 lineData):
* order_line_no / item_id / item_code / item_name / plan_qty / actual_price / retail_price /
* ext_code / inventory_type / owner_code / source_order_code / sub_source_order_code /
* extend_props / stock_id
*
* 口径要点(与 WMS 保持一致):
* - plan_qty 必须 > 0(WMS 侧 `数量必须大于0`)
* - item_id(WMS 内部商品ID,取自 warehouse_products.wms_item_id)与 item_code 至少给一个;
* WMS 侧 itemId 主键查不到时会降级按 itemCode 查找
* - order_line_no 缺省按行序号(WMS 为 `${lineIndex + 1}`)
* - inventory_type:OMS 落 ZP 正品 / CC 残次,推送 WMS 时映射为 ZC / CC
* - stock_id:WMS 仓库库存ID(手工建单直传场景,WMS 会校验库存归属货主)
*/
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(
'order_delivery_items',
{
id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, autoIncrement: true, primaryKey: true },
principal_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '所属主体ID',
references: { model: 'principals', key: 'id' },
},
order_delivery_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '所属发货单ID(order_deliveries.id)',
references: { model: 'order_deliveries', key: 'id' },
onDelete: 'CASCADE',
},
order_line_no: { type: Sequelize.STRING(20), allowNull: false, defaultValue: '', comment: '行号(orderLineNo,缺省按行序号)' },
item_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: 'WMS 内部商品ID(itemId,取自 warehouse_products.wms_item_id)' },
item_code: { type: Sequelize.STRING(100), allowNull: true, comment: '商品编码(itemCode)' },
item_name: { type: Sequelize.STRING(255), allowNull: true, comment: '商品名称(itemName)' },
plan_qty: { type: Sequelize.DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '计划数量(planQty,必须 > 0)' },
actual_price: { type: Sequelize.DECIMAL(12, 2), allowNull: true, comment: '实际单价(actualPrice)' },
retail_price: { type: Sequelize.DECIMAL(12, 2), allowNull: true, comment: '零售价(retailPrice)' },
ext_code: { type: Sequelize.STRING(100), allowNull: true, comment: '外部编码(extCode)' },
inventory_type: {
type: Sequelize.STRING(10),
allowNull: false,
defaultValue: 'ZP',
comment: '库存类型:ZP=正品 CC=残次(对齐 WMS 行级 inventoryType,推送时映射 ZC/CC)',
},
owner_code: { type: Sequelize.STRING(50), allowNull: true, comment: '货主编码(ownerCode)' },
source_order_code: { type: Sequelize.STRING(50), allowNull: true, comment: '来源订单编码(sourceOrderCode)' },
sub_source_order_code: { type: Sequelize.STRING(50), allowNull: true, comment: '子来源订单编码(subSourceOrderCode)' },
stock_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: 'WMS 仓库库存ID(stockId,手工建单直传)' },
extend_props: { type: Sequelize.JSON, allowNull: true, comment: '扩展属性(extendProps)' },
created_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
},
},
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '发货单明细(子表商品明细)' }
);
await queryInterface.addIndex('order_delivery_items', { fields: [ 'order_delivery_id' ], name: 'idx_od_items_order' });
await queryInterface.addIndex('order_delivery_items', { fields: [ 'principal_id' ], name: 'idx_od_items_principal' });
await queryInterface.addIndex('order_delivery_items', { fields: [ 'item_code' ], name: 'idx_od_items_item_code' });
// 同一发货单内行号唯一,避免明细重复或覆盖
await queryInterface.addIndex('order_delivery_items', {
fields: [ 'order_delivery_id', 'order_line_no' ],
unique: true,
name: 'uk_od_items_line',
});
},
async down(queryInterface) {
await queryInterface.dropTable('order_delivery_items');
},
};