'use strict'; module.exports = app => { const { STRING, INTEGER, BIGINT, ENUM, TEXT, TINYINT, DATE } = app.Sequelize; const EntryOrder = app.model.define('wms_entry_order', { id: { type: BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, comment: '入库单ID', }, entry_order_code: { type: STRING(50), allowNull: false, comment: '入库单号', validate: { notEmpty: true, }, }, order_type: { type: ENUM('SCRK', 'LYRK', 'CCRK', 'CGRK', 'DBRK', 'QTRK', 'B2BRK', 'XNRK'), allowNull: false, comment: '业务类型', }, purchase_order_code: { type: STRING(50), allowNull: true, comment: '采购单号', }, total_order_lines: { type: INTEGER(11), allowNull: false, defaultValue: 0, comment: '单据总行数', }, order_create_time: { type: DATE, allowNull: true, comment: '订单创建时间', }, expect_start_time: { type: DATE, allowNull: true, comment: '预期到货时间', }, expect_end_time: { type: DATE, allowNull: true, comment: '最迟预期到货时间', }, logistics_code: { type: STRING(50), allowNull: true, comment: '物流公司编码', }, logistics_name: { type: STRING(200), allowNull: true, comment: '物流公司名称', }, express_code: { type: STRING(50), allowNull: true, comment: '运单号', }, supplier_code: { type: STRING(50), allowNull: true, comment: '供应商编码', }, supplier_name: { type: STRING(200), allowNull: true, comment: '供应商名称', }, source_type: { type: ENUM('WMS', 'API', 'QM'), allowNull: false, defaultValue: 'API', comment: '来源类型', }, order_status: { type: ENUM('PENDING', 'CONFIRMED', 'PARTIAL_RECEIVED', 'COMPLETED', 'CANCELLED', 'CREATED', 'CREATING'), allowNull: false, defaultValue: 'PENDING', comment: '订单状态', }, sync_status: { type: ENUM('PENDING', 'SUCCESS', 'FAILED'), allowNull: false, defaultValue: 'PENDING', comment: '同步状态', }, sync_message: { type: TEXT, allowNull: true, comment: '同步返回信息', }, is_enabled: { type: TINYINT(1), allowNull: false, defaultValue: 1, comment: '是否启用', }, created_at: { type: DATE, allowNull: true, defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, updated_at: { type: DATE, allowNull: true, comment: '更新时间', }, warehouse_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID', references: { model: 'wms_store', key: 'id', }, }, store_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '货主ID', references: { model: 'wms_store', key: 'id', }, }, remark: { type: STRING(500), allowNull: true, comment: 'ERP备注', }, expense_record: { type: TINYINT(1), allowNull: false, defaultValue: 0, comment: '费用是否记录', }, store_name: { type: STRING(200), allowNull: true, comment: '货主名称快照', }, warehouse_name: { type: STRING(200), allowNull: true, comment: '仓库名称快照', }, deleted_at: { type: DATE, allowNull: true, comment: '删除时间', }, qimen_owner_code: { type: STRING(100), allowNull: true, comment: '奇门货主code', }, qimen_warehouse_code: { type: STRING(100), allowNull: true, comment: '奇门仓库code', }, }, { tableName: 'wms_entry_order', comment: '入库单主表', timestamps: false, indexes: [ { name: 'uk_entry_order_code', unique: true, fields: [ 'entry_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', fields: [ 'source_type' ], }, { name: 'idx_create_time', fields: [ 'order_create_time' ], }, { name: 'idx_warehouse_store', fields: [ 'warehouse_id', 'store_id' ], }, { name: 'idx_is_enabled', fields: [ 'is_enabled' ], }, { name: 'idx_deleted_at', fields: [ 'deleted_at' ], }, ], }); // 关联关系 EntryOrder.associate = function() { const { EntryOrder, Store, EntryItems } = app.model; // 关联仓库 EntryOrder.belongsTo(Store, { foreignKey: 'warehouse_id', as: 'warehouse', }); // 关联货主 EntryOrder.belongsTo(Store, { foreignKey: 'store_id', as: 'store', }); // 关联入库明细 EntryOrder.hasMany(EntryItems, { foreignKey: 'order_id', as: 'items', }); }; // 实例方法 EntryOrder.prototype.isPending = function() { return this.order_status === 'PENDING'; }; EntryOrder.prototype.isConfirmed = function() { return this.order_status === 'CONFIRMED'; }; EntryOrder.prototype.isCompleted = function() { return this.order_status === 'COMPLETED'; }; EntryOrder.prototype.isCancelled = function() { return this.order_status === 'CANCELLED'; }; EntryOrder.prototype.isPartialReceived = function() { return this.order_status === 'PARTIAL_RECEIVED'; }; EntryOrder.prototype.canConfirm = function() { return [ 'PENDING', 'CREATED' ].includes(this.order_status); }; EntryOrder.prototype.canCancel = function() { return [ 'PENDING', 'CONFIRMED', 'CREATED' ].includes(this.order_status); }; EntryOrder.prototype.canComplete = function() { return [ 'CONFIRMED', 'PARTIAL_RECEIVED' ].includes(this.order_status); }; EntryOrder.prototype.isSyncSuccess = function() { return this.sync_status === 'SUCCESS'; }; EntryOrder.prototype.isSyncFailed = function() { return this.sync_status === 'FAILED'; }; EntryOrder.prototype.isEnabled = function() { return this.is_enabled === 1 && !this.deleted_at; }; return EntryOrder; };