oms/backend/database/migrations/20260909000107-create-orders.js

80 lines
3.5 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';
/**
* 8. 订单表:归属于具体主体;
* 包含本系统订单主状态、外部 WMS 回传状态、以及"上交"(上报/交接给上级或平台)状态。
*/
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(
'orders',
{
id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
order_no: { type: Sequelize.STRING(50), allowNull: false, comment: '业务订单号' },
principal_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '所属主体ID',
references: { model: 'principals', key: 'id' },
},
order_type: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '订单类型' },
// OMS 本系统主状态
status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '主状态:1待处理 2处理中 3已完成 4已取消' },
// 外部 WMS 相关状态
wms_status: {
type: Sequelize.STRING(50),
allowNull: true,
comment: '外部WMS状态(如 CREATED/PICKING/SHIPPED,由回调回写)',
},
wms_order_no: { type: Sequelize.STRING(50), allowNull: true, comment: '外部WMS单号' },
wms_synced_at: { type: Sequelize.DATE, allowNull: true, comment: '最近一次WMS同步时间' },
// 上交(上报/交接)相关状态
submit_status: {
type: Sequelize.TINYINT,
allowNull: false,
defaultValue: 0,
comment: '上交状态:0未上交 1上交中 2已上交 3上交失败',
},
submit_order_no: { type: Sequelize.STRING(50), allowNull: true, comment: '上交后返回的单号' },
submit_at: { type: Sequelize.DATE, allowNull: true, comment: '上交时间' },
submit_msg: { type: Sequelize.STRING(255), allowNull: true, comment: '上交结果/失败原因' },
// 金额与收货信息
total_amount: { type: Sequelize.DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '订单金额' },
pay_amount: { type: Sequelize.DECIMAL(12, 2), allowNull: true, comment: '实付金额' },
receiver: {
type: Sequelize.JSON,
allowNull: true,
comment: '收货人信息 JSON:{ name, phone, address }',
},
remark: { type: Sequelize.STRING(255), allowNull: true, comment: '备注' },
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('orders', { fields: [ 'order_no' ], unique: true, name: 'uk_orders_order_no' });
await queryInterface.addIndex('orders', { fields: [ 'principal_id' ], name: 'idx_orders_principal' });
await queryInterface.addIndex('orders', { fields: [ 'principal_id', 'status' ], name: 'idx_orders_principal_status' });
await queryInterface.addIndex('orders', { fields: [ 'wms_status' ], name: 'idx_orders_wms_status' });
await queryInterface.addIndex('orders', { fields: [ 'submit_status' ], name: 'idx_orders_submit_status' });
},
async down(queryInterface) {
await queryInterface.dropTable('orders');
},
};