WMS_MAIN/app/model/stock_warehouse.js
liufumingren 9af7dcf8fb feat(storeWarehouse): 签约关系接口配置按货主开放接口落库且回调地址统一取配置
- storeWarehouse.store.saveApiConfig:新增 resolveContractApiInterfaceType,
  interface_type_id=3(OPEN_API_WAREHOUSE 仓库开放接口)自动映射为 2(OPEN_API 货主开放接口)落库,
  不再要求当前主体为仓库(货主可维护自身签约关系下的接口配置)
- callback_url 不再取入参(含 callbackUrl 别名),统一写入 config.openapi.defaultCallbackUrl(新增配置项,默认空)
- storeWarehouse.store.listApiConfig:interface_type_id 过滤同步映射(传 3 按 2 查询)
- 文档:methods/system.json 升级 0.3.350、methods/changes/system.json 变更记录、docs/openapi-callback-events.md 回调地址来源说明
- 测试:saveApiConfig 新增 4 条用例(3→2 落库、忽略入参回调地址、默认值未配置、货主主体可保存、缺失类型 404),listApiConfig 新增 1 条过滤映射用例

同时提交工作区其余在制品改动:
- waybill:移除 wms_order_express_trace 模型并新增 drop 迁移,轨迹查询/取号/打印链路调整
- order:订单 SN 编码(order_item_sn / order_item_sn_log / sn_code_rule、has_sn_item 回填迁移),pendingOrder/recheckOrder/wave/base 调整
- express_platform、order_async、order_export、order_storage_allocation、storage.report、stock_store 配套修复
- lib:新增 jd_client,调整 qimen_client、callback_functions 及配套测试

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-14 17:14:04 +08:00

357 lines
8.8 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, JSON } = 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: '打包要求',
},
pick_requirements: {
type: TEXT,
comment: '拣货要求',
},
recheck_requirements: {
type: TEXT,
comment: '眼球复核要求',
},
merge_requirements: {
type: CHAR(1),
allowNull: false,
defaultValue: 'Y',
comment: '要求是否合并:Y=合并(拣货/复核要求统一输出打包要求),N=不合并',
},
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: '是否序列号管理',
},
sn_code_rules: {
type: JSON,
allowNull: true,
defaultValue: null,
comment: 'SN码规则(JSON数组):match_type=prefix|suffix、match_value=前缀/后缀、keep_mode=full|strip',
},
is_shelf_life_mgmt: {
type: CHAR(1),
defaultValue: 'N',
comment: '是否保质期管理',
},
is_batch_mgmt: {
type: CHAR(1),
defaultValue: 'N',
comment: '是否批次管理',
},
need_recheck: {
type: CHAR(1),
allowNull: false,
defaultValue: 'Y',
comment: '是否需要复核:Y=需要复核,N=不需要复核',
},
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: '锁定数量',
},
warning_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;
};