// app/model/receiving_detail.js module.exports = app => { const { STRING, INTEGER, DATE, DECIMAL, TEXT, ENUM, BIGINT } = app.Sequelize; const ReceivingDetail = app.model.define('wms_receiving_detail', { id: { type: INTEGER, primaryKey: true, autoIncrement: true, comment: '明细ID', }, receiving_id: { type: INTEGER, allowNull: false, comment: '收货单ID', }, product_name: { type: STRING(200), allowNull: false, comment: '商品名称', }, barcode: { type: STRING(100), comment: '商品条码', }, product_snapshot: { type: TEXT, comment: '商品信息快照(JSON格式)', }, system_product_id: { type: INTEGER, comment: '关联的系统商品ID(可以为空)', }, actual_quantity: { type: INTEGER, allowNull: false, comment: '实际收货数量', }, box_quantity: { type: INTEGER, defaultValue: 0, comment: '箱数', }, boxes_per_pallet: { type: INTEGER, defaultValue: 1, comment: '每托盘箱数(箱规)', }, pallet_quantity: { type: INTEGER, defaultValue: 0, comment: '托盘数', }, receiving_time: { type: DATE, comment: '收货时间(该商品的收货时间)', }, receiving_status: { type: ENUM('pending', 'receiving', 'completed', 'cancelled'), defaultValue: 'pending', comment: '收货状态', }, storage_status: { type: ENUM('none', 'partial', 'all'), defaultValue: 'none', comment: '入库状态:none-未入库, partial-部分入库, all-全部入库', }, stored_quantity: { type: INTEGER, defaultValue: 0, comment: '已入库数量', }, batch_number: { type: STRING(100), comment: '批次号', }, production_date: { type: DATE, comment: '生产日期', }, expiry_date: { type: DATE, comment: '保质期/到期日期', }, remark: { type: TEXT, comment: '备注', }, space_id: { type: BIGINT.UNSIGNED, defaultValue: 1, comment: '存放库位', }, creator_id: { type: BIGINT.UNSIGNED, allowNull: false, }, box_width: { type: DECIMAL(10, 2), }, box_length: { type: DECIMAL(10, 2), }, box_height: { type: DECIMAL(10, 2), }, weight: { type: INTEGER, }, box_remainder: { type: INTEGER, allowNull: false, defaultValue: 0, comment: '箱余数', }, box_weight: { type: DECIMAL(10, 2), allowNull: false, defaultValue: '0.00', comment: '箱重量(kg)', }, }, { tableName: 'wms_receiving_detail', timestamps: true, createdAt: 'created_at', updatedAt: 'updated_at', paranoid: false, indexes: [ { fields: [ 'receiving_id' ], }, { fields: [ 'barcode' ], }, { fields: [ 'system_product_id' ], }, { fields: [ 'receiving_status' ], }, { fields: [ 'storage_status' ], }, { fields: [ 'matched_status' ], }, { fields: [ 'batch_number' ], }, { fields: [ 'receiving_id', 'barcode' ], // 添加这个复合索引 }, ], }); ReceivingDetail.associate = function() { const { Receiving, StockWarehouse, LocationSpace } = app.model; // 关联主表 ReceivingDetail.belongsTo(Receiving, { foreignKey: 'receiving_id', as: 'receiving', }); // 关联系统商品 ReceivingDetail.belongsTo(StockWarehouse, { foreignKey: 'system_product_id', as: 'system_product', }); // 关联系统商品 ReceivingDetail.belongsTo(LocationSpace, { foreignKey: 'space_id', as: 'receiving_space', }); }; return ReceivingDetail; };