WMS_MAIN/app/service/systemMethods/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

211 lines
6.4 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';
const MainService = require('../main');
/**
* 店铺-快递绑定策略 methods 出口。
*
* 支持以下 method 调用(通过 /system?method=storeExpressBindingPolicy.xxx):
* - storeExpressBindingPolicy.list 分页查询策略列表
* - storeExpressBindingPolicy.add 新增策略
* - storeExpressBindingPolicy.edit 编辑策略
* - storeExpressBindingPolicy.del 软删除策略
*/
class StoreExpressBindingPolicyMethodService extends MainService {
/**
* 初始化鉴权配置。
* @param {Egg.Context} ctx 上下文对象
*/
constructor(ctx) {
super(ctx);
const _noNeedLogin = '';
const _noNeedRight = '';
const _noNeedLog = '*';
const _noNeedAuth = '';
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
}
/**
* 统一调用业务服务。
* @param {string} methodName 业务方法名
* @param {Object} data 请求参数
* @param {string} fallbackMessage 默认错误文案
* @return {Promise<Object>} methods 标准返回
*/
async callSystem(methodName, data, fallbackMessage) {
const { service } = this;
try {
const result = await service.systems.storeExpressBindingPolicy[methodName](data);
return this.success('ok', { data: result, code: 200 });
} catch (error) {
return this.fail(error.message || fallbackMessage, {
code: Number(error.code) || 500,
});
}
}
/**
* storeExpressBindingPolicy.list:分页查询策略列表。
* @param {Object} [data={}] 请求参数
* @return {Promise<Object>} 查询结果
*/
async list(data = {}) {
const payload = {
...(data && typeof data === 'object' ? data : {}),
};
this.prepareWhereContext(payload, {
specialFieldMap: this.service.systems.storeExpressBindingPolicy.getListWhereSpecialFieldMap(),
});
return await this.callSystem('list', payload, '查询策略列表失败');
}
/**
* storeExpressBindingPolicy.add:新增策略。
* @param {Object} [data={}] 请求参数
* @return {Promise<Object>} 新增结果
*/
async add(data = {}) {
const {
custom_name: customName,
customName: customNameCamel,
express_code: expressCode,
expressCode: expressCodeCamel,
priority,
is_enabled: isEnabled,
isEnabled: isEnabledCamel,
regions,
shop_name: shopName,
store_ids: storeIds,
exclude_regions: excludeRegions,
min_weight_g: minWeightG,
max_weight_g: maxWeightG,
weight_type: weightType,
prohibited_type_id: prohibitedTypeId,
prohibitedTypeId: prohibitedTypeIdCamel,
remark,
creator_id: creatorId,
creatorId: creatorIdCamel,
} = data || {};
return await this.callSystem('add', {
custom_name: customName !== undefined ? customName : customNameCamel,
express_code: expressCode !== undefined ? expressCode : expressCodeCamel,
priority,
is_enabled: isEnabled !== undefined ? isEnabled : isEnabledCamel,
regions,
shop_name: shopName,
store_ids: storeIds,
exclude_regions: excludeRegions,
min_weight_g: minWeightG,
max_weight_g: maxWeightG,
weight_type: weightType,
prohibited_type_id: prohibitedTypeId !== undefined ? prohibitedTypeId : prohibitedTypeIdCamel,
remark,
creator_id: creatorId !== undefined ? creatorId : creatorIdCamel,
}, '新增策略失败');
}
/**
* storeExpressBindingPolicy.edit:编辑策略。
* @param {Object} [data={}] 请求参数
* @return {Promise<Object>} 编辑结果
*/
async edit(data = {}) {
const {
id,
policy_id: policyId,
policyId: policyIdCamel,
custom_name: customName,
customName: customNameCamel,
express_code: expressCode,
expressCode: expressCodeCamel,
priority,
is_enabled: isEnabled,
isEnabled: isEnabledCamel,
regions,
shop_name: shopName,
store_ids: storeIds,
exclude_regions: excludeRegions,
min_weight_g: minWeightG,
max_weight_g: maxWeightG,
weight_type: weightType,
prohibited_type_id: prohibitedTypeId,
prohibitedTypeId: prohibitedTypeIdCamel,
remark,
creator_id: creatorId,
creatorId: creatorIdCamel,
} = data || {};
const payload = {
id: id !== undefined ? id : (policyId !== undefined ? policyId : policyIdCamel),
};
if (customName !== undefined || customNameCamel !== undefined) {
payload.custom_name = customName !== undefined ? customName : customNameCamel;
}
if (expressCode !== undefined || expressCodeCamel !== undefined) {
payload.express_code = expressCode !== undefined ? expressCode : expressCodeCamel;
}
if (priority !== undefined) {
payload.priority = priority;
}
if (isEnabled !== undefined || isEnabledCamel !== undefined) {
payload.is_enabled = isEnabled !== undefined ? isEnabled : isEnabledCamel;
}
if (regions !== undefined) {
payload.regions = regions;
}
if (shopName !== undefined) {
payload.shop_name = shopName;
}
if (storeIds !== undefined) {
payload.store_ids = storeIds;
}
if (excludeRegions !== undefined) {
payload.exclude_regions = excludeRegions;
}
if (minWeightG !== undefined) {
payload.min_weight_g = minWeightG;
}
if (maxWeightG !== undefined) {
payload.max_weight_g = maxWeightG;
}
if (weightType !== undefined) {
payload.weight_type = weightType;
}
if (prohibitedTypeId !== undefined || prohibitedTypeIdCamel !== undefined) {
payload.prohibited_type_id = prohibitedTypeId !== undefined ? prohibitedTypeId : prohibitedTypeIdCamel;
}
if (remark !== undefined) {
payload.remark = remark;
}
if (creatorId !== undefined || creatorIdCamel !== undefined) {
payload.creator_id = creatorId !== undefined ? creatorId : creatorIdCamel;
}
return await this.callSystem('edit', payload, '编辑策略失败');
}
/**
* storeExpressBindingPolicy.del:软删除策略。
* @param {Object} [data={}] 请求参数
* @return {Promise<Object>} 删除结果
*/
async del(data = {}) {
const {
id,
policy_id: policyId,
policyId: policyIdCamel,
} = data || {};
return await this.callSystem('del', {
id: id !== undefined ? id : (policyId !== undefined ? policyId : policyIdCamel),
}, '删除策略失败');
}
}
module.exports = StoreExpressBindingPolicyMethodService;