WMS_MAIN/app/model/stock_warehouse.js
liufumingren 19c3fe09a7 refactor(weight): 重量字段统一为 INTEGER(g),取代 DECIMAL(kg)
- 8 个 model 重量字段类型 DECIMAL→INTEGER,注释 kg→g

- order_express 称重校验改为正整数(g)

- delivery_order 手动单查 StockStore + warehouse 称重毛重

- openapi.json 毛重/净重描述 kg→g

- 迁移: 20260725120000-unify-weight-fields-to-int-g

- 脚本: update-stock-warehouse-weight.cjs
2026-07-27 03:46:07 +08:00

326 lines
7.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'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: INTEGER(11),
defaultValue: 0,
comment: '毛重(g)',
},
net_weight: {
type: INTEGER(11),
defaultValue: 0,
comment: '净重(g)',
},
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,
comment: '箱毛重(g)',
},
box_net_weight: {
type: INTEGER(11),
defaultValue: 0,
comment: '箱净重(g)',
},
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' ],
},
],
});
/**
* 商品详情缓存 key 前缀。
* 与 app/service/systems/stock_warehouse.js 中保持一致。
*/
const STOCK_DETAIL_CACHE_PREFIX = 'cache:stock_detail';
/**
* 构建商品详情缓存 key。
* @param {number} warehouseId 仓库ID
* @param {number} stockId 仓库商品ID
* @return {string} 缓存 key
*/
function buildStockDetailCacheKey(warehouseId, stockId) {
return `${STOCK_DETAIL_CACHE_PREFIX}:${warehouseId}:${stockId}`;
}
/**
* 资料变更后自动清除商品详情缓存。
* 直接在 afterUpdate hook 中使用 async/await,缓存删除很快不会阻塞主流程。
* @param {Object} instance Sequelize 模型实例
* @return {Promise<void>}
*/
async function invalidateStockDetailCacheAfterUpdate(instance) {
try {
const warehouseId = instance.get('warehouse_id');
const stockId = instance.get('id');
if (warehouseId && stockId) {
const cacheKey = buildStockDetailCacheKey(warehouseId, stockId);
if (typeof app.delCacheAsync === 'function') {
await app.delCacheAsync(cacheKey);
app.logger.info(`[StockWarehouse.afterUpdate] 已清除商品详情缓存: ${cacheKey}`);
}
}
} catch (err) {
app.logger.error(`[StockWarehouse.afterUpdate] 清除商品详情缓存失败: ${err.message}`);
}
}
// 注册 afterUpdate hook:商品资料更新时自动清除详情缓存
StockWarehouse.addHook('afterUpdate', 'invalidateStockDetailCache', invalidateStockDetailCacheAfterUpdate);
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;
};