WMS_MAIN/app/model/stock_barcode.js
liufumingren 569a25390f 1
2026-06-24 07:36:43 +08:00

130 lines
3.6 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, DATE } = app.Sequelize;
const StockBarcode = app.model.define('stock_barcodes', {
id: {
type: BIGINT(20),
primaryKey: true,
autoIncrement: true,
comment: '主键ID',
},
stock_id: {
type: BIGINT(20),
allowNull: false,
comment: '主表ID',
references: {
model: 'wms_stock_warehouse',
key: 'id',
},
onDelete: 'CASCADE',
onUpdate: 'NO ACTION',
},
warehouse_id: {
type: BIGINT.UNSIGNED,
allowNull: false,
comment: '仓库id',
},
barcode: {
type: STRING(100),
allowNull: false,
comment: '条码',
charset: 'utf8mb4',
collate: 'utf8mb4_unicode_ci',
},
description: {
type: STRING(255),
allowNull: true,
comment: '条码描述',
charset: 'utf8mb4',
collate: 'utf8mb4_unicode_ci',
},
create_time: {
type: DATE,
allowNull: true,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
comment: '创建时间',
},
}, {
tableName: 'wms_stock_barcodes',
timestamps: false, // 不使用Sequelize的自动时间戳
charset: 'utf8mb4',
collate: 'utf8mb4_unicode_ci',
paranoid: false,
// 明确指定不使用 deletedAt 字段
deletedAt: false,
comment: 'WMS商品条码表',
indexes: [
{
name: 'idx_stock_id',
fields: [ 'stock_id' ],
},
{
name: 'uk_barcode',
unique: true,
fields: [ 'barcode' ],
},
],
});
/**
* 商品详情缓存 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}`;
}
/**
* 条码变更后自动清除关联的商品详情缓存。
* 直接在 hook 中使用 async/await,缓存删除很快不会阻塞主流程。
* @param {Object} instance Sequelize 模型实例
* @return {Promise<void>}
*/
async function invalidateStockDetailCacheForBarcode(instance) {
try {
const warehouseId = instance.get('warehouse_id');
const stockId = instance.get('stock_id');
if (warehouseId && stockId) {
const cacheKey = buildStockDetailCacheKey(warehouseId, stockId);
if (typeof app.delCacheAsync === 'function') {
await app.delCacheAsync(cacheKey);
app.logger.info(`[StockBarcode] 已清除商品详情缓存: ${cacheKey}`);
}
}
} catch (err) {
app.logger.error(`[StockBarcode] 清除商品详情缓存失败: ${err.message}`);
}
}
// 注册 hooks:条码新增/更新/删除时自动清除关联商品详情缓存
StockBarcode.addHook('afterCreate', 'invalidateStockDetailCache', invalidateStockDetailCacheForBarcode);
StockBarcode.addHook('afterUpdate', 'invalidateStockDetailCache', invalidateStockDetailCacheForBarcode);
StockBarcode.addHook('afterDestroy', 'invalidateStockDetailCache', invalidateStockDetailCacheForBarcode);
// 定义关联关系
StockBarcode.associate = function() {
const { StockWarehouse } = app.model;
console.log('StockWarehouse', StockWarehouse, StockBarcode);
StockBarcode.belongsTo(
app.model.StockWarehouse,
{
foreignKey: 'stock_id',
targetKey: 'id',
as: 'stock',
}
);
};
return StockBarcode;
};