262 lines
6.4 KiB
JavaScript
262 lines
6.4 KiB
JavaScript
'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'),
|
||
allowNull: false,
|
||
defaultValue: 'CREATED',
|
||
comment: '订单状态:CREATING=创建中, CREATED=已创建待审核, PENDING=已审核, COMPLETED=已完成, CANCELLED=已作废',
|
||
},
|
||
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: '备注',
|
||
},
|
||
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.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;
|
||
};
|