WMS_MAIN/app/model/store_express_binding_policy.js
liufumingren 0b781a7e70 feat: 店铺快递绑定策略模块 + 订单创建链路集成 + 运单号分配优化
【新增模块】
- app/model/store_express_binding_policy.js: 策略数据模型(17字段,JSON数组)
- app/service/systems/store_express_binding_policy.js: CRUD+列表(含名称批量解析)
- app/service/systems/express_policy.js: 缓存优先策略查询 + 6维漏斗匹配引擎
- app/service/systemMethods/store_express_binding_policy.js: method路由层
- database/designs/store-express-binding-policy-design.md: 设计文档

【策略漏斗匹配逻辑】
- 按priority DESC逐条匹配: store_ids→shop_name→prohibited→regions→exclude→weight
- NULL/空数组=不限, 未命中自动continue到下一条
- 地区/店铺匹配使用子串包含(省/市/区/详细地址均生效)
- 违禁品匹配使用数组交集
- 输出快递编码+重量类型, 未命中返回null

【订单创建链路集成】
- addOrder→addOrderLines后触发matchPolicy(weight可用)
- 命中: 写入logistics/sys_logistics 4字段+weight_type+操作日志
- 未命中: 降级到resolveExpressSnapshot默认值+兜底DEFAULT_STORE_SUBJECT_CONFIG(STO)
- 系统快递4字段一致, 自定义映射(express_setting)分流写入

【运单号分配优化】
- pickWaybillExpressInterface两步兜底:
  Step1: logistics_code仅匹配express_code(自定义编码精确)
  Step2: sys_logistics_code匹配全候选码(系统编码兜底)
- 新增weight_type过滤器(接口NULL=通配, 设限须精确匹配)
- groupedOrder.options新增sys_logistics_code/weight_type传递

【操作码翻译】
- ORDER_EXPRESS_POLICY_MATCH: '策略命中'
- ORDER_EXPRESS_FALLBACK: '快递降级'
- 无策略命中时写入快递降级日志

【API文档】
- methods/system.json: 策略模块4接口+响应字段(express_name/数组类型等)

【数据库迁移】
- 13个migration: 建表→字段迭代→TEXT改JSON→prohibited改数组(dev+prod均执行)
2026-08-04 08:18:43 +08:00

145 lines
4.0 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';
/**
* 订单自定义店铺-快递绑定策略模型。
*
* 支持在仓库维度下,为用户自定义店铺标签配置快递绑定策略,
* 按优先级匹配,可附加商品、地区、重量等条件。
*
* @param {import('egg').Application} app Egg 应用实例。
* @return {import('sequelize').ModelCtor<import('sequelize').Model>} Sequelize 模型。
*/
module.exports = app => {
const { BIGINT, STRING, INTEGER, TINYINT, DATE, TEXT, JSON: JSON_TYPE } = app.Sequelize;
const StoreExpressBindingPolicy = app.model.define('store_express_binding_policy', {
id: {
type: BIGINT.UNSIGNED,
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '主键ID',
},
warehouse_id: {
type: BIGINT.UNSIGNED,
allowNull: false,
comment: '仓库ID,策略所属仓库',
},
custom_name: {
type: STRING(100),
allowNull: false,
comment: '策略名称,便于人工识别',
},
shop_name: {
type: JSON_TYPE,
allowNull: true,
comment: '包含店铺名称,字符串数组,如["旗舰店","奥莱店"],NULL=不限制',
},
store_ids: {
type: JSON_TYPE,
allowNull: true,
comment: '限定货主ID,数字数组,如[1000000,1000001],NULL=不限制',
},
express_code: {
type: STRING(50),
allowNull: false,
comment: '绑定的快递编码(wms_express.code)',
},
priority: {
type: INTEGER(11),
allowNull: false,
defaultValue: 0,
comment: '优先级,数值越大越优先匹配',
},
is_enabled: {
type: TINYINT(1),
allowNull: false,
defaultValue: 1,
validate: {
isIn: {
args: [[ 0, 1 ]],
msg: '启用状态不合法,0=停用,1=启用',
},
},
comment: '是否启用:0=停用,1=启用',
},
regions: {
type: JSON_TYPE,
allowNull: true,
comment: '限定地区,字符串数组,如["广东省","广州市"],NULL=不限制',
},
exclude_regions: {
type: JSON_TYPE,
allowNull: true,
comment: '排除地区,字符串数组,如["新疆维吾尔自治区"],NULL=不排除',
},
min_weight_g: {
type: INTEGER,
allowNull: true,
comment: '最小重量(克),NULL=不限制',
},
max_weight_g: {
type: INTEGER,
allowNull: true,
comment: '最大重量(克),NULL=不限制',
},
weight_type: {
type: TINYINT(1),
allowNull: false,
defaultValue: 0,
validate: { isIn: { args: [[ 0, 1, 2, 3 ]], msg: '重量类型不合法,0=常规/1=轻货/2=重货/3=冷链' } },
comment: '重量段类型:0=常规/1=轻货/2=重货/3=冷链',
},
prohibited_type_id: {
type: JSON_TYPE,
allowNull: true,
comment: '违禁品类型ID数组(关联 wms_prohibited_type.id),如[1,2],NULL=不限制违禁品',
},
remark: {
type: STRING(500),
allowNull: true,
comment: '备注说明',
},
creator_id: {
type: BIGINT.UNSIGNED,
allowNull: true,
comment: '创建人账户ID(accounts.id)',
},
created_at: {
type: DATE,
allowNull: false,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
comment: '创建时间',
},
updated_at: {
type: DATE,
allowNull: false,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
comment: '更新时间',
},
deleted_at: {
type: DATE,
allowNull: true,
comment: '软删除时间',
},
}, {
tableName: 'wms_store_express_binding_policy',
freezeTableName: true,
underscored: true,
timestamps: false,
comment: '订单自定义店铺-快递绑定策略表',
indexes: [
{
name: 'idx_warehouse_enabled',
fields: [ 'warehouse_id', 'is_enabled', 'deleted_at' ],
},
{
name: 'idx_warehouse_express',
fields: [ 'warehouse_id', 'express_code', 'deleted_at' ],
},
],
});
return StoreExpressBindingPolicy;
};