'use strict'; /** * 出库单主表 Model。 * * 对应数据库表 `wms_order_stockout`,存储奇门 stockout.create 接口下发的出库单信息。 * * 业务类型枚举值: * - JYCK:一般交易出库 * - HHCK:换货出库 * - BFCK:补发出库 * - QTCK:其他出库 * @param {import('egg').Application} app - Egg 应用实例 */ module.exports = app => { const { STRING, INTEGER, BIGINT, ENUM, TEXT, TINYINT, DATE, JSON: JSON_TYPE } = app.Sequelize; const OrderStockout = app.model.define('wms_order_stockout', { id: { type: BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键ID', }, stockout_order_code: { type: STRING(50), allowNull: false, comment: '出库单号(对应奇门 deliveryOrderCode)', validate: { notEmpty: true, }, }, order_type: { type: STRING(50), allowNull: false, comment: '业务类型(JYCK一般交易出库/HHCK换货出库/BFCK补发出库/QTCK其他出库等)', validate: { notEmpty: true, }, }, total_order_lines: { type: INTEGER(11), allowNull: false, defaultValue: 0, comment: '单据总行数', }, order_create_time: { type: DATE, allowNull: true, comment: '订单创建时间(对应奇门 createTime)', }, source_type: { type: ENUM('WMS', 'API', 'QM'), allowNull: false, defaultValue: 'QM', comment: '来源类型', }, order_status: { type: ENUM('CREATING', 'CREATED', 'PENDING', 'COMPLETED', 'CANCELLED', 'TRANSFERRED'), allowNull: false, defaultValue: 'CREATED', comment: '订单状态:CREATING=创建中, CREATED=已创建待审核, PENDING=已审核, COMPLETED=已完成, CANCELLED=已作废, TRANSFERRED=已转订单', }, sync_status: { type: ENUM('PENDING', 'SUCCESS', 'FAILED'), allowNull: false, defaultValue: 'PENDING', comment: '同步状态', }, sync_message: { type: TEXT, allowNull: true, comment: '同步返回信息', }, warehouse_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID', }, store_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '货主ID', }, store_name: { type: STRING(200), allowNull: true, comment: '货主名称快照', }, warehouse_name: { type: STRING(200), allowNull: true, comment: '仓库名称快照', }, qimen_owner_code: { type: STRING(100), allowNull: true, comment: '奇门货主code(对应 ownerCode)', }, qimen_warehouse_code: { type: STRING(100), allowNull: true, comment: '奇门仓库code(对应 warehouseCode)', }, remark: { type: STRING(500), allowNull: true, comment: '备注', }, receiver_company: { type: STRING(200), allowNull: true, comment: '收件人公司名称(receiverInfo.company)', }, receiver_name: { type: STRING(50), allowNull: true, comment: '收件人姓名(receiverInfo.name)', }, receiver_zip_code: { type: STRING(50), allowNull: true, comment: '收件人邮编(receiverInfo.zipCode)', }, receiver_tel: { type: STRING(50), allowNull: true, comment: '收件人固定电话(receiverInfo.tel)', }, receiver_mobile: { type: STRING(50), allowNull: true, comment: '收件人移动电话(receiverInfo.mobile)', }, receiver_email: { type: STRING(50), allowNull: true, comment: '收件人电子邮箱(receiverInfo.email)', }, receiver_country_code: { type: STRING(50), allowNull: true, comment: '收件人国家二字码(receiverInfo.countryCode)', }, receiver_province: { type: STRING(50), allowNull: true, comment: '收件人省份(receiverInfo.province)', }, receiver_city: { type: STRING(50), allowNull: true, comment: '收件人城市(receiverInfo.city)', }, receiver_area: { type: STRING(50), allowNull: true, comment: '收件人区域(receiverInfo.area)', }, receiver_town: { type: STRING(50), allowNull: true, comment: '收件人村镇(receiverInfo.town)', }, receiver_detail_address: { type: STRING(200), allowNull: true, comment: '收件人详细地址(receiverInfo.detailAddress)', }, receiver_id: { type: STRING(50), allowNull: true, comment: '收件人证件号(receiverInfo.id)', }, source_platform_code: { type: STRING(50), allowNull: true, comment: '来源平台编码(转订单时优先使用,为空时兜底平台)', }, source_platform_name: { type: STRING(200), allowNull: true, comment: '来源平台名称快照', }, express_code: { type: STRING(50), allowNull: true, comment: '快递公司编码(如 SF、STO,用于出站回传 logisticsCode)', }, express_name: { type: STRING(200), allowNull: true, comment: '快递公司名称(由 express_code 自动解析,如 顺丰、申通)', }, waybill_code: { type: STRING(50), allowNull: true, comment: '运单号', }, waybill_codes: { type: JSON_TYPE, allowNull: true, comment: '运单号列表(JSON 数组,支持多包裹场景)', }, express_codes: { type: JSON_TYPE, allowNull: true, comment: '@deprecated 运单号列表,请使用 waybill_codes', }, extend_props: { type: JSON_TYPE, allowNull: true, comment: '扩展属性', }, is_enabled: { type: TINYINT(1), allowNull: false, defaultValue: 1, comment: '是否启用', }, created_at: { type: DATE, allowNull: false, comment: '创建时间', }, updated_at: { type: DATE, allowNull: false, comment: '更新时间', }, deleted_at: { type: DATE, allowNull: true, comment: '软删除时间', }, }, { tableName: 'wms_order_stockout', comment: '出库单主表', timestamps: false, indexes: [ { name: 'uk_stockout_order_code', unique: true, fields: [ 'stockout_order_code', 'warehouse_id', 'store_id' ], }, { name: 'idx_order_type', fields: [ 'order_type' ], }, { name: 'idx_order_status', fields: [ 'order_status' ], }, { name: 'idx_sync_status', fields: [ 'sync_status' ], }, { name: 'idx_source_type', fields: [ 'source_type' ], }, { name: 'idx_warehouse_store', fields: [ 'warehouse_id', 'store_id' ], }, { name: 'idx_order_create_time', fields: [ 'order_create_time' ], }, { name: 'idx_is_enabled', fields: [ 'is_enabled' ], }, { name: 'idx_deleted_at', fields: [ 'deleted_at' ], }, ], }); /** * 关联关系定义。 * - 关联货主 / 仓库(均通过 Store 表) * - 关联出库明细表 */ OrderStockout.associate = function() { const { OrderStockout, Store, OrderStockoutItems } = app.model; // 关联仓库 OrderStockout.belongsTo(Store, { foreignKey: 'warehouse_id', as: 'warehouse', }); // 关联货主 OrderStockout.belongsTo(Store, { foreignKey: 'store_id', as: 'store', }); // 关联出库明细 OrderStockout.hasMany(OrderStockoutItems, { foreignKey: 'order_id', as: 'items', }); }; // ---------- 状态机辅助方法 ---------- /** 判断订单是否处于"已创建"状态 */ OrderStockout.prototype.isCreated = function() { return this.order_status === 'CREATED'; }; /** 判断订单是否处于"已审核"状态 */ OrderStockout.prototype.isPending = function() { return this.order_status === 'PENDING'; }; /** 判断订单是否已完成 */ OrderStockout.prototype.isCompleted = function() { return this.order_status === 'COMPLETED'; }; /** 判断订单是否已作废 */ OrderStockout.prototype.isCancelled = function() { return this.order_status === 'CANCELLED'; }; /** 判断订单是否已转销售订单 */ OrderStockout.prototype.isTransferred = function() { return this.order_status === 'TRANSFERRED'; }; /** 当前订单是否允许审核 */ OrderStockout.prototype.canConfirm = function() { return [ 'CREATED', 'PENDING' ].includes(this.order_status); }; /** 当前订单是否允许取消 */ OrderStockout.prototype.canCancel = function() { return [ 'CREATED', 'PENDING' ].includes(this.order_status); }; /** 当前订单是否允许完成 */ OrderStockout.prototype.canComplete = function() { return [ 'PENDING' ].includes(this.order_status); }; /** 同步是否成功 */ OrderStockout.prototype.isSyncSuccess = function() { return this.sync_status === 'SUCCESS'; }; /** 同步是否失败 */ OrderStockout.prototype.isSyncFailed = function() { return this.sync_status === 'FAILED'; }; /** 是否启用(未软删除) */ OrderStockout.prototype.isEnabled = function() { return this.is_enabled === 1 && !this.deleted_at; }; return OrderStockout; };