// app/model/stock_store.js 'use strict'; module.exports = app => { const { BIGINT, STRING, DECIMAL, INTEGER, DATE, ENUM, CHAR } = app.Sequelize; const StockStore = app.model.define('stock_store', { id: { type: BIGINT(20), primaryKey: true, autoIncrement: true, allowNull: false, comment: '库存商品ID', }, warehouse_code: { type: STRING(50), allowNull: false, validate: { notEmpty: true, len: [ 1, 50 ], }, comment: '仓库编码', }, owner_code: { type: STRING(50), allowNull: false, validate: { notEmpty: true, len: [ 1, 50 ], }, comment: '货主编码', }, app_key: { type: STRING(50), allowNull: true, validate: { len: [ 0, 50 ], }, comment: '应用', }, is_area_sale: { type: CHAR(1), allowNull: false, defaultValue: 'N', validate: { isIn: [[ 'Y', 'N' ]], }, comment: '是否区域销售', }, brand_name: { type: STRING(100), allowNull: true, validate: { len: [ 0, 50 ], }, }, item_code: { type: STRING(50), allowNull: false, validate: { notEmpty: true, len: [ 1, 50 ], }, comment: '商品编码', }, goods_code: { type: STRING(50), allowNull: true, comment: '货物编码', }, item_name: { type: STRING(255), allowNull: false, validate: { notEmpty: true, len: [ 1, 255 ], }, comment: '商品名称', }, sku_property: { type: STRING(100), allowNull: true, validate: { len: [ 0, 100 ], }, comment: 'SKU规格', }, stock_unit: { type: STRING(20), allowNull: true, validate: { len: [ 0, 20 ], }, comment: '库存单位', }, length: { type: DECIMAL(10, 2), allowNull: true, defaultValue: 0.00, validate: { min: 0, }, comment: '长度', }, width: { type: DECIMAL(10, 2), allowNull: true, defaultValue: 0.00, validate: { min: 0, }, comment: '宽度', }, height: { type: DECIMAL(10, 2), allowNull: true, defaultValue: 0.00, validate: { min: 0, }, comment: '高度', }, volume: { type: DECIMAL(10, 2), allowNull: true, defaultValue: 0.00, validate: { min: 0, }, comment: '体积', }, gross_weight: { type: DECIMAL(10, 2), allowNull: true, defaultValue: 0.00, validate: { min: 0, }, comment: '毛重', }, net_weight: { type: DECIMAL(10, 2), allowNull: true, defaultValue: 0.00, validate: { min: 0, }, comment: '净重', }, category_id: { type: INTEGER(11), allowNull: true, comment: '分类ID', }, category_name: { type: STRING(100), allowNull: true, validate: { len: [ 0, 100 ], }, comment: '分类名称', }, item_type: { type: STRING(10), allowNull: false, validate: { notEmpty: true, len: [ 1, 10 ], }, comment: '商品类型', }, tag_price: { type: DECIMAL(10, 2), allowNull: true, defaultValue: 0.00, validate: { min: 0, }, comment: '标价', }, retail_price: { type: DECIMAL(10, 2), allowNull: true, defaultValue: 0.00, validate: { min: 0, }, comment: '零售价', }, is_sn_mgmt: { type: CHAR(1), allowNull: false, defaultValue: 'N', validate: { isIn: [[ 'Y', 'N' ]], }, comment: '是否序列号管理', }, is_shelf_life_mgmt: { type: CHAR(1), allowNull: false, defaultValue: 'N', validate: { isIn: [[ 'Y', 'N' ]], }, comment: '是否保质期管理', }, is_batch_mgmt: { type: CHAR(1), allowNull: false, defaultValue: 'N', validate: { isIn: [[ 'Y', 'N' ]], }, comment: '是否批次管理', }, create_time: { type: DATE, allowNull: false, comment: '创建时间', }, update_time: { type: DATE, allowNull: false, comment: '更新时间', }, is_valid: { type: CHAR(1), allowNull: false, defaultValue: 'Y', validate: { isIn: [[ 'Y', 'N' ]], }, comment: '是否有效', }, barcode: { type: STRING(255), allowNull: true, validate: { len: [ 0, 255 ], }, comment: '条形码', }, stock_id: { type: BIGINT(20), allowNull: true, comment: '库存ID', }, warehouse_id: { type: BIGINT(20), allowNull: false, comment: '仓库ID', references: { model: 'store', key: 'id', }, }, store_id: { type: BIGINT(20), allowNull: false, comment: '商家ID', references: { model: 'store', key: 'id', }, }, create_from: { type: ENUM('qimen', 'api'), allowNull: true, comment: '创建来源', }, delete_at: { type: DATE, allowNull: true, comment: '删除时间', }, }, { tableName: 'wms_stock_store', freezeTableName: true, underscored: true, timestamps: false, // 使用自定义时间字段 comment: '库存商品信息表', indexes: [ { name: 'uk_warehouse_owner_item', unique: true, fields: [ 'warehouse_code', 'owner_code', 'item_code', 'app_key', 'warehouse_id', 'store_id' ], }, { name: 'idx_owner_code', fields: [ 'owner_code' ], }, { name: 'idx_goods_code', fields: [ 'goods_code' ], }, { name: 'idx_category_id', fields: [ 'category_id' ], }, { name: 'idx_create_time', fields: [ 'create_time' ], }, { name: 'idx_is_valid', fields: [ 'is_valid' ], }, { name: 'uk_item_code', fields: [ 'item_code' ], }, { name: 'idx_warehouse_id', fields: [ 'warehouse_id' ], }, { name: 'idx_store_id', fields: [ 'store_id' ], }, { name: 'idx_barcode', fields: [ 'barcode' ], }, { name: 'idx_warehouse_valid', fields: [ 'warehouse_id', 'is_valid' ], }, { name: 'idx_store_valid', fields: [ 'store_id', 'is_valid' ], }, { name: 'idx_item_valid', fields: [ 'item_code', 'is_valid' ], }, ], }); // 定义关联关系 StockStore.associate = function() { const { Store, StockWarehouse } = app.model; // 关联仓库 StockStore.belongsTo(Store, { foreignKey: 'warehouse_id', targetKey: 'id', as: 'warehouse', }); // 关联商家 StockStore.belongsTo(Store, { foreignKey: 'store_id', targetKey: 'id', as: 'store', }); StockStore.belongsTo(StockWarehouse, { foreignKey: 'stock_id', // 当前表的外键字段 targetKey: 'id', // 关联表的主键字段 as: 'warehouse_stock', }); }; // 实例方法 - 检查商品是否有效 StockStore.prototype.isValid = function() { return this.is_valid === 'Y' && !this.delete_at; }; // 实例方法 - 软删除 StockStore.prototype.softDelete = function() { return this.update({ is_valid: 'N', delete_at: new Date(), }); }; // 实例方法 - 计算体积 StockStore.prototype.calculateVolume = function() { if (this.length && this.width && this.height) { return (this.length * this.width * this.height).toFixed(2); } return this.volume; }; return StockStore; };