'use strict'; module.exports = app => { const { STRING, INTEGER, BIGINT, ENUM, DATE, TEXT } = app.Sequelize; const StockTransaction = app.model.define('stock_transaction', { id: { type: BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键ID', }, transaction_type: { type: ENUM('INBOUND', 'OUTBOUND'), allowNull: false, comment: '出入库类型', }, warehouse_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID', }, store_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '货主ID', }, stock_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '商品库存ID,关联 wms_stock_warehouse.id', }, quantity: { type: INTEGER(12), allowNull: false, comment: '变动数量(正数)', }, before_quantity: { type: INTEGER(12), allowNull: true, comment: '变更前总库存数量', }, after_quantity: { type: INTEGER(12), allowNull: true, comment: '变更后总库存数量', }, before_locked_quantity: { type: INTEGER(12), allowNull: true, comment: '变更前锁定数量', }, after_locked_quantity: { type: INTEGER(12), allowNull: true, comment: '变更后锁定数量', }, batch_no: { type: STRING(100), allowNull: true, comment: '批次号', }, order_type: { type: STRING(50), allowNull: true, comment: '关联单据类型(如 SCRK=收货入库, MATCH=匹配入库, CGTH=采购退货等)', }, order_no: { type: TEXT, allowNull: true, comment: '关联单据号', }, order_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '关联单据ID', }, receiving_id: { type: INTEGER, allowNull: true, comment: '关联收货单ID', }, entry_order_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '关联入库单ID', }, operator_id: { type: STRING(100), allowNull: true, comment: '操作人ID', }, operator_name: { type: STRING(100), allowNull: true, comment: '操作人名称', }, remark: { type: STRING(500), allowNull: true, comment: '备注', }, created_at: { type: DATE, allowNull: false, defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, }, { tableName: 'wms_stock_transaction', freezeTableName: true, timestamps: false, comment: '出入库流水记录表', indexes: [ { name: 'idx_stock_id', fields: [ 'stock_id' ], }, { name: 'idx_warehouse_time', fields: [ 'warehouse_id', 'created_at' ], }, { name: 'idx_order', fields: [ 'order_type', 'order_id' ], }, { name: 'idx_receiving_id', fields: [ 'receiving_id' ], }, { name: 'idx_entry_order_id', fields: [ 'entry_order_id' ], }, { name: 'idx_created_at', fields: [ 'created_at' ], }, { name: 'idx_store_transaction_type', fields: [ 'store_id', 'transaction_type' ], }, ], }); StockTransaction.associate = function() { const { StockWarehouse } = app.model; StockTransaction.belongsTo(StockWarehouse, { foreignKey: 'stock_id', as: 'stockItem', }); }; return StockTransaction; };