310 lines
9.3 KiB
JavaScript
310 lines
9.3 KiB
JavaScript
'use strict';
|
||
|
||
const MAIN_TABLE = 'wms_order_stockout';
|
||
const ITEMS_TABLE = 'wms_order_stockout_items';
|
||
|
||
/**
|
||
* 创建奇门出库单主表与明细表。
|
||
*
|
||
* - wms_order_stockout:出库单主表(对应奇门 stockout.create 的 <deliveryOrder> 节点)
|
||
* - wms_order_stockout_items:出库明细表(对应 <orderLine> 节点)
|
||
*
|
||
* 幂等:如果表已存在则跳过创建。
|
||
*/
|
||
module.exports = {
|
||
up: async (queryInterface, Sequelize) => {
|
||
const tables = await queryInterface.showAllTables();
|
||
|
||
const exists = name =>
|
||
tables.some(t => (typeof t === 'string' ? t : t.tableName) === name);
|
||
|
||
// ----------------------- 主表 -----------------------
|
||
if (!exists(MAIN_TABLE)) {
|
||
await queryInterface.createTable(MAIN_TABLE, {
|
||
id: {
|
||
type: Sequelize.BIGINT(20).UNSIGNED,
|
||
primaryKey: true,
|
||
autoIncrement: true,
|
||
allowNull: false,
|
||
comment: '主键ID',
|
||
},
|
||
stockout_order_code: {
|
||
type: Sequelize.STRING(50),
|
||
allowNull: false,
|
||
comment: '出库单号(对应奇门 deliveryOrderCode)',
|
||
},
|
||
order_type: {
|
||
type: Sequelize.STRING(50),
|
||
allowNull: false,
|
||
comment: '业务类型(JYCK/HHCK/BFCK/QTCK 等)',
|
||
},
|
||
total_order_lines: {
|
||
type: Sequelize.INTEGER(11),
|
||
allowNull: false,
|
||
defaultValue: 0,
|
||
comment: '单据总行数',
|
||
},
|
||
order_create_time: {
|
||
type: Sequelize.DATE,
|
||
allowNull: true,
|
||
comment: '订单创建时间(对应奇门 createTime)',
|
||
},
|
||
source_type: {
|
||
type: Sequelize.ENUM('WMS', 'API', 'QM'),
|
||
allowNull: false,
|
||
defaultValue: 'QM',
|
||
comment: '来源类型',
|
||
},
|
||
order_status: {
|
||
type: Sequelize.ENUM('CREATING', 'CREATED', 'PENDING', 'COMPLETED', 'CANCELLED'),
|
||
allowNull: false,
|
||
defaultValue: 'CREATED',
|
||
comment: '订单状态',
|
||
},
|
||
sync_status: {
|
||
type: Sequelize.ENUM('PENDING', 'SUCCESS', 'FAILED'),
|
||
allowNull: false,
|
||
defaultValue: 'PENDING',
|
||
comment: '同步状态',
|
||
},
|
||
sync_message: {
|
||
type: Sequelize.TEXT,
|
||
allowNull: true,
|
||
comment: '同步返回信息',
|
||
},
|
||
warehouse_id: {
|
||
type: Sequelize.BIGINT(20).UNSIGNED,
|
||
allowNull: false,
|
||
comment: '仓库ID',
|
||
},
|
||
store_id: {
|
||
type: Sequelize.BIGINT(20).UNSIGNED,
|
||
allowNull: false,
|
||
comment: '货主ID',
|
||
},
|
||
store_name: {
|
||
type: Sequelize.STRING(200),
|
||
allowNull: true,
|
||
comment: '货主名称快照',
|
||
},
|
||
warehouse_name: {
|
||
type: Sequelize.STRING(200),
|
||
allowNull: true,
|
||
comment: '仓库名称快照',
|
||
},
|
||
qimen_owner_code: {
|
||
type: Sequelize.STRING(100),
|
||
allowNull: true,
|
||
comment: '奇门货主code(对应 ownerCode)',
|
||
},
|
||
qimen_warehouse_code: {
|
||
type: Sequelize.STRING(100),
|
||
allowNull: true,
|
||
comment: '奇门仓库code(对应 warehouseCode)',
|
||
},
|
||
remark: {
|
||
type: Sequelize.STRING(500),
|
||
allowNull: true,
|
||
comment: '备注',
|
||
},
|
||
extend_props: {
|
||
type: Sequelize.JSON,
|
||
allowNull: true,
|
||
comment: '扩展属性',
|
||
},
|
||
is_enabled: {
|
||
type: Sequelize.TINYINT(1),
|
||
allowNull: false,
|
||
defaultValue: 1,
|
||
comment: '是否启用',
|
||
},
|
||
created_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
|
||
comment: '创建时间',
|
||
},
|
||
updated_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
|
||
comment: '更新时间',
|
||
},
|
||
deleted_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: true,
|
||
comment: '软删除时间',
|
||
},
|
||
});
|
||
|
||
// 唯一索引:(stockout_order_code, warehouse_id, store_id)
|
||
await queryInterface.addIndex(MAIN_TABLE, [ 'stockout_order_code', 'warehouse_id', 'store_id' ], {
|
||
unique: true,
|
||
name: 'uk_stockout_order_code',
|
||
});
|
||
|
||
// 辅助查询索引
|
||
await queryInterface.addIndex(MAIN_TABLE, [ 'order_type' ], {
|
||
name: 'idx_order_type',
|
||
});
|
||
await queryInterface.addIndex(MAIN_TABLE, [ 'order_status' ], {
|
||
name: 'idx_order_status',
|
||
});
|
||
await queryInterface.addIndex(MAIN_TABLE, [ 'sync_status' ], {
|
||
name: 'idx_sync_status',
|
||
});
|
||
await queryInterface.addIndex(MAIN_TABLE, [ 'source_type' ], {
|
||
name: 'idx_source_type',
|
||
});
|
||
await queryInterface.addIndex(MAIN_TABLE, [ 'warehouse_id', 'store_id' ], {
|
||
name: 'idx_warehouse_store',
|
||
});
|
||
await queryInterface.addIndex(MAIN_TABLE, [ 'order_create_time' ], {
|
||
name: 'idx_order_create_time',
|
||
});
|
||
await queryInterface.addIndex(MAIN_TABLE, [ 'is_enabled' ], {
|
||
name: 'idx_is_enabled',
|
||
});
|
||
await queryInterface.addIndex(MAIN_TABLE, [ 'deleted_at' ], {
|
||
name: 'idx_deleted_at',
|
||
});
|
||
}
|
||
|
||
// ----------------------- 明细表 -----------------------
|
||
if (!exists(ITEMS_TABLE)) {
|
||
await queryInterface.createTable(ITEMS_TABLE, {
|
||
id: {
|
||
type: Sequelize.BIGINT(20).UNSIGNED,
|
||
primaryKey: true,
|
||
autoIncrement: true,
|
||
allowNull: false,
|
||
comment: '主键ID',
|
||
},
|
||
order_id: {
|
||
type: Sequelize.BIGINT(20).UNSIGNED,
|
||
allowNull: false,
|
||
comment: '出库单ID,外键关联 wms_order_stockout.id',
|
||
},
|
||
out_biz_code: {
|
||
type: Sequelize.STRING(255),
|
||
allowNull: true,
|
||
comment: '外部业务编码(幂等键)',
|
||
},
|
||
order_line_no: {
|
||
type: Sequelize.STRING(50),
|
||
allowNull: false,
|
||
comment: '订单行号(对应奇门 orderLineNo)',
|
||
},
|
||
owner_code: {
|
||
type: Sequelize.STRING(50),
|
||
allowNull: true,
|
||
comment: '货主编码(对应奇门 ownerCode)',
|
||
},
|
||
item_code: {
|
||
type: Sequelize.STRING(50),
|
||
allowNull: true,
|
||
comment: '商品编码(对应奇门 itemCode)',
|
||
},
|
||
item_id: {
|
||
type: Sequelize.BIGINT(20).UNSIGNED,
|
||
allowNull: true,
|
||
comment: 'WMS商品ID(对应奇门 itemId)',
|
||
},
|
||
item_name: {
|
||
type: Sequelize.STRING(200),
|
||
allowNull: true,
|
||
comment: '商品名称',
|
||
},
|
||
plan_qty: {
|
||
type: Sequelize.INTEGER(12),
|
||
allowNull: true,
|
||
comment: '计划数量(对应奇门 planQty)',
|
||
},
|
||
actual_qty: {
|
||
type: Sequelize.INTEGER(12),
|
||
allowNull: false,
|
||
defaultValue: 0,
|
||
comment: '实发数量',
|
||
},
|
||
inventory_type: {
|
||
type: Sequelize.STRING(50),
|
||
allowNull: true,
|
||
comment: '库存类型(ZP正品/CC残次等)',
|
||
},
|
||
stock_id: {
|
||
type: Sequelize.BIGINT(20),
|
||
allowNull: true,
|
||
comment: '库存ID,外键关联 wms_stock_warehouse.id',
|
||
},
|
||
extend_props: {
|
||
type: Sequelize.JSON,
|
||
allowNull: true,
|
||
comment: '扩展属性',
|
||
},
|
||
created_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
|
||
comment: '创建时间',
|
||
},
|
||
updated_at: {
|
||
type: Sequelize.DATE,
|
||
allowNull: false,
|
||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
|
||
comment: '更新时间',
|
||
},
|
||
});
|
||
|
||
// 唯一索引:(order_id, order_line_no)
|
||
await queryInterface.addIndex(ITEMS_TABLE, [ 'order_id', 'order_line_no' ], {
|
||
unique: true,
|
||
name: 'uk_order_line_no',
|
||
});
|
||
|
||
// 辅助索引
|
||
await queryInterface.addIndex(ITEMS_TABLE, [ 'order_id' ], {
|
||
name: 'idx_order_id',
|
||
});
|
||
await queryInterface.addIndex(ITEMS_TABLE, [ 'stock_id' ], {
|
||
name: 'idx_stock_id',
|
||
});
|
||
await queryInterface.addIndex(ITEMS_TABLE, [ 'item_code' ], {
|
||
name: 'idx_item_code',
|
||
});
|
||
await queryInterface.addIndex(ITEMS_TABLE, [ 'out_biz_code' ], {
|
||
name: 'idx_out_biz_code',
|
||
});
|
||
|
||
// 外键约束
|
||
await queryInterface.addConstraint(ITEMS_TABLE, {
|
||
fields: [ 'order_id' ],
|
||
type: 'foreign key',
|
||
name: 'fk_items_order_id',
|
||
references: {
|
||
table: MAIN_TABLE,
|
||
field: 'id',
|
||
},
|
||
onDelete: 'CASCADE',
|
||
onUpdate: 'CASCADE',
|
||
});
|
||
|
||
await queryInterface.addConstraint(ITEMS_TABLE, {
|
||
fields: [ 'stock_id' ],
|
||
type: 'foreign key',
|
||
name: 'fk_items_stock_id',
|
||
references: {
|
||
table: 'wms_stock_warehouse',
|
||
field: 'id',
|
||
},
|
||
onDelete: 'RESTRICT',
|
||
onUpdate: 'CASCADE',
|
||
});
|
||
}
|
||
},
|
||
|
||
down: async queryInterface => {
|
||
await queryInterface.dropTable(ITEMS_TABLE);
|
||
await queryInterface.dropTable(MAIN_TABLE);
|
||
},
|
||
};
|