'use strict'; module.exports = app => { const { STRING, BIGINT, DECIMAL, TEXT, DATE, CHAR, INTEGER, ENUM, TINYINT } = app.Sequelize; const StockWarehouse = app.model.define('stock_warehouse', { id: { type: BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键ID', }, item_code: { type: STRING(50), allowNull: false, comment: '商品编码', }, goods_code: { type: STRING(50), allowNull: true, comment: '货物编码', }, store_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '货主ID', }, item_name: { type: STRING(255), allowNull: false, comment: '商品名称', }, sku_property: { type: STRING(100), comment: 'SKU规格', }, stock_unit: { type: STRING(20), allowNull: false, comment: '库存单位', }, packing_requirements: { type: TEXT, comment: '打包要求', }, brand_name: { type: STRING(100), allowNull: true, validate: { len: [ 0, 50 ], }, }, length: { type: DECIMAL(10, 2), defaultValue: 0.00, comment: '长度', }, width: { type: DECIMAL(10, 2), defaultValue: 0.00, comment: '宽度', }, height: { type: DECIMAL(10, 2), defaultValue: 0.00, comment: '高度', }, volume: { type: INTEGER(11).UNSIGNED, defaultValue: 0, comment: '体积', }, gross_weight: { type: DECIMAL(10, 2), defaultValue: 0.00, comment: '毛重', }, net_weight: { type: DECIMAL(10, 2), defaultValue: 0.00, comment: '净重', }, item_type: { type: STRING(10), allowNull: false, comment: '商品类型', }, tag_price: { type: DECIMAL(10, 2), defaultValue: 0.00, comment: '标价', }, retail_price: { type: DECIMAL(10, 2), defaultValue: 0.00, comment: '零售价', }, is_sn_mgmt: { type: CHAR(1), defaultValue: 'N', comment: '是否序列号管理', }, is_shelf_life_mgmt: { type: CHAR(1), defaultValue: 'N', comment: '是否保质期管理', }, is_batch_mgmt: { type: CHAR(1), defaultValue: 'N', comment: '是否批次管理', }, create_at: { type: DATE, allowNull: false, comment: '创建时间', }, level: { type: ENUM('A', 'B', 'C'), allowNull: true, defaultValue: 'N', comment: '是否需要发票', }, update_at: { type: DATE, defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), comment: '更新时间', }, is_valid: { type: CHAR(1), defaultValue: 'Y', comment: '是否有效', }, is_images: { type: CHAR(1), defaultValue: 'Y', comment: '是否有照片', }, warehouse_id: { type: BIGINT(20).UNSIGNED, comment: '仓库ID', }, delete_at: { type: DATE, comment: '删除时间', }, quantity: { type: INTEGER(11), defaultValue: 0, comment: '库存数量', }, locked_quantity: { type: INTEGER(11), defaultValue: 0, comment: '锁定数量', }, prohibited_id: { type: INTEGER.UNSIGNED, comment: '违禁品类型id', }, enable_box_spec: { type: TINYINT(1), defaultValue: 0, }, quantity_per_box: { type: INTEGER(11), defaultValue: 0, }, box_gross_weight: { type: INTEGER(11), defaultValue: 0, }, box_net_weight: { type: INTEGER(11), defaultValue: 0, }, box_height: { type: DECIMAL(10, 2), defaultValue: 0.00, }, box_length: { type: DECIMAL(10, 2), defaultValue: 0.00, }, box_width: { type: DECIMAL(10, 2), defaultValue: 0.00, }, box_volume: { type: INTEGER(11).UNSIGNED, defaultValue: 0, }, inbound_min_days: { type: INTEGER(11), defaultValue: 0, comment: '入库最小天数要求', }, outbound_min_days: { type: INTEGER(11), defaultValue: 0, comment: '出库最小天数要求', }, shelf_life_days: { type: INTEGER(11), defaultValue: 0, comment: '保质期天数', }, remaining_threshold_days: { type: INTEGER(11), defaultValue: 0, comment: '剩余有效期阈值天数', }, deleted_at: { type: DATE, defaultValue: null, }, }, { tableName: 'wms_stock_warehouse', freezeTableName: true, timestamps: false, paranoid: true, comment: '仓库商品库存主表', indexes: [ { name: 'uk_item_code', unique: true, fields: [ 'item_code', 'warehouse_id', 'store_id', 'deleted_at' ], }, { name: 'idx_create_time', fields: [ 'create_at' ], }, { name: 'idx_is_valid', fields: [ 'is_valid' ], }, { name: 'warehouse_id', fields: [ 'warehouse_id' ], }, ], }); StockWarehouse.associate = function() { const { StoreWarehouse, StockBarcode, ProhibitedType, StockStore, Storage } = app.model; // store_stock 关联依赖 store_id + warehouse_id 复合语义,单列关联仅作为 include 入口。 StockWarehouse.belongsTo(StoreWarehouse, { foreignKey: 'store_id', // 当前表的外键字段 targetKey: 'store_id', // 关联表业务键(非主键) as: 'store_stock', constraints: false, }); StockWarehouse.hasMany(StockBarcode, { foreignKey: 'stock_id', // StockBarcodes表中的外键字段 as: 'barcodes', }); StockWarehouse.belongsTo(ProhibitedType, { foreignKey: 'prohibited_id', // 当前表的外键字段 targetKey: 'id', // 关联表的主键字段 as: 'prohibited', }); StockWarehouse.hasMany(StockStore, { foreignKey: 'stock_id', // 当前表的外键字段 as: 'store_out_stock', }); StockWarehouse.hasMany(Storage, { foreignKey: 'stock_id', // 当前表的外键字段 as: 'storage', }); const { StockTransaction } = app.model; StockWarehouse.hasMany(StockTransaction, { foreignKey: 'stock_id', as: 'transactions', }); }; return StockWarehouse; };