liufumingren c2a761ca26 feat(inbox): 新增开门/到货/进场/离场车牌登记与 extend_props 持久化
- functions 新增 openDoor/registerLicensePlate/enter/departure(规则表+权限分组)
- 到货登记表 wms_inbox_arrival_register(含消费字段,离场闭环,货主收货群优先推送)
- 回执 extend_props 持久化落库与定时补发送下发(重推兜底回读)
- 车牌提取正则兼容异体字(粤/粵)
- 售后类型/关键字/函数/回复 v2 模型与迁移
- INBOX V2 HTTP 测试报告与指南
2026-09-17 02:41:43 +08:00

205 lines
7.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 Service = require('egg').Service;
/** v2 配置缓存键前缀(单仓库一份配置,TTL 300s)。 */
const CACHE_KEY_PREFIX = 'inbox:v2:cfg:';
/** 配置缓存 TTL(秒)。 */
const CACHE_TTL = 300;
/** 缓存结构版本号,结构变更时递增,避免读到旧结构。 */
const CACHE_VERSION = 1;
/**
* v2 仓库可配置回信引擎 —— 配置加载服务。
*
* 职责:
* - load(warehouseId):加载"系统级(warehouse_id IS NULL) + 仓库级(warehouse_id=当前仓库)"合并后的配置,
* 缓存于单 key inbox:v2:cfg:{warehouseId}(TTL 300s),缓存 miss 时从 DB 拼装;
* - invalidate(warehouseId):删除指定仓库配置缓存(仅管理接口调用,启动不清缓存防雪崩)。
*
* 配置结构(纯对象,可安全进缓存/Redis 序列化,不含 Map/Set/RegExp 实例):
* {
* _v: 1,
* subTypes: { [id]: {id, type, sub_type, name, is_system, is_enabled} },
* keywords: [ {keyword, sub_type_id, sort_order} ],
* groupWhitelist: { [sub_type_id]: [ {group_type, sub_group} ] },
* subTypeFunctions: { [sub_type_id]: [ {function_code, sort_order} ] },
* functionEnabled: { [function_code]: true },
* subTypeRules: { [sub_type_id]: [ {target, regexp, sort_order} ] },
* replies: { [sub_type_id]: {reply, is_reply, no_number_hint, warehouse_id} } // 仓库级覆盖系统级
* }
*/
class InboxV2ConfigService extends Service {
/**
* 加载仓库配置(缓存优先)。
* @param {number} warehouseId 仓库ID。
* @return {Promise<Object>} 配置对象。
*/
async load(warehouseId) {
const { app } = this;
const wid = Number(warehouseId) || 0;
const key = CACHE_KEY_PREFIX + wid;
if (typeof app.getCacheAsync === 'function') {
const cached = await app.getCacheAsync(key);
if (cached && cached._v === CACHE_VERSION) return cached;
}
const cfg = await this.build(wid);
if (typeof app.setCacheAsync === 'function') {
await app.setCacheAsync(key, cfg, CACHE_TTL);
}
return cfg;
}
/**
* 失效仓库配置缓存(管理接口增删改配置后调用)。
* @param {number} warehouseId 仓库ID。
* @return {Promise<void>} 无返回值。
*/
async invalidate(warehouseId) {
const { app } = this;
const wid = Number(warehouseId) || 0;
if (typeof app.delCacheAsync === 'function') {
await app.delCacheAsync(CACHE_KEY_PREFIX + wid);
}
}
/**
* 从 DB 拼装配置(全量小表查询 + 内存关联)。
* @param {number} wid 仓库ID(0=未绑定仓库,只加载系统级)。
* @return {Promise<Object>} 配置对象。
*/
async build(wid) {
const { ctx } = this;
const { Op } = ctx.app.Sequelize;
const wh = { [Op.or]: [{ warehouse_id: null }, { warehouse_id: wid }] };
// 1. 子类型(启用)
const subTypes = await ctx.model.AfterSaleSubType.findAll({
where: { ...wh, is_enabled: 1 },
raw: true,
});
const subTypeMap = {};
const validSubTypeIds = new Set();
for (const s of subTypes) {
subTypeMap[s.id] = {
id: s.id, type: s.type, sub_type: s.sub_type, name: s.name,
is_system: s.is_system, is_enabled: s.is_enabled,
};
validSubTypeIds.add(s.id);
}
// 2. 关键词(启用)
const keywords = await ctx.model.AfterSaleKeyword.findAll({
where: { ...wh, is_enabled: 1 },
attributes: [ 'keyword', 'sub_type_id', 'sort_order' ],
raw: true,
});
const kwList = keywords
.filter(k => validSubTypeIds.has(k.sub_type_id))
.sort((a, b) => (a.sort_order - b.sort_order) || String(a.keyword).length - String(b.keyword).length);
// 3. 群类型白名单(全局表)
const groupRows = await ctx.model.AfterSaleSubTypeGroup.findAll({
attributes: [ 'sub_type_id', 'group_type', 'sub_group' ],
raw: true,
});
const groupWhitelist = {};
for (const g of groupRows) {
if (!validSubTypeIds.has(g.sub_type_id)) continue;
if (!groupWhitelist[g.sub_type_id]) groupWhitelist[g.sub_type_id] = [];
groupWhitelist[g.sub_type_id].push({
group_type: String(g.group_type || '').trim(),
sub_group: String(g.sub_group || '').trim(),
});
}
// 4. 子类型-函数关系(全局表)
const funcRelRows = await ctx.model.AfterSaleSubTypeFunction.findAll({
attributes: [ 'sub_type_id', 'function_id', 'sort_order' ],
raw: true,
});
const funcRows = await ctx.model.AfterSaleFunction.findAll({
attributes: [ 'id', 'function_code', 'is_enabled' ],
raw: true,
});
const funcIdMap = {};
const functionEnabled = {};
for (const f of funcRows) {
funcIdMap[f.id] = f.function_code;
if (Number(f.is_enabled) === 1) functionEnabled[f.function_code] = true;
}
const subTypeFunctions = {};
for (const r of funcRelRows) {
const code = funcIdMap[r.function_id];
if (!code || !validSubTypeIds.has(r.sub_type_id)) continue;
if (!subTypeFunctions[r.sub_type_id]) subTypeFunctions[r.sub_type_id] = [];
subTypeFunctions[r.sub_type_id].push({ function_code: code, sort_order: r.sort_order });
}
for (const key of Object.keys(subTypeFunctions)) {
subTypeFunctions[key].sort((a, b) => a.sort_order - b.sort_order);
}
// 5. 子类型-取号规则关系(全局表)+ 取号规则本体
const ruleRelRows = await ctx.model.AfterSaleSubTypeRule.findAll({
attributes: [ 'sub_type_id', 'number_rule_id', 'sort_order' ],
raw: true,
});
const ruleRows = await ctx.model.InboxNumberRule.findAll({
attributes: [ 'id', 'target', 'regexp', 'sort_order' ],
raw: true,
});
const ruleIdMap = {};
for (const n of ruleRows) ruleIdMap[n.id] = n;
const subTypeRules = {};
for (const r of ruleRelRows) {
const rule = ruleIdMap[r.number_rule_id];
if (!rule || !validSubTypeIds.has(r.sub_type_id)) continue;
if (!subTypeRules[r.sub_type_id]) subTypeRules[r.sub_type_id] = [];
subTypeRules[r.sub_type_id].push({
target: String(rule.target || '').trim(),
regexp: String(rule.regexp || ''),
sort_order: r.sort_order || rule.sort_order || 0,
});
}
for (const key of Object.keys(subTypeRules)) {
subTypeRules[key].sort((a, b) => a.sort_order - b.sort_order);
}
// 6. 回复表:仓库级覆盖系统级(同子类型取 warehouse_id 非空者优先)
const replyRows = await ctx.model.AfterSaleReply.findAll({
where: { ...wh, is_enabled: 1 },
attributes: [ 'warehouse_id', 'sub_type_id', 'reply', 'is_reply', 'no_number_hint' ],
raw: true,
});
const replies = {};
for (const r of replyRows) {
if (!validSubTypeIds.has(r.sub_type_id)) continue;
const isWh = r.warehouse_id !== null && r.warehouse_id !== undefined;
const existing = replies[r.sub_type_id];
if (!existing || (isWh && existing.warehouse_id === null)) {
replies[r.sub_type_id] = {
reply: String(r.reply || ''),
is_reply: Number(r.is_reply) === 1 ? 1 : 0,
no_number_hint: r.no_number_hint ? String(r.no_number_hint) : '',
warehouse_id: isWh ? r.warehouse_id : null,
};
}
}
return {
_v: CACHE_VERSION,
subTypes: subTypeMap,
keywords: kwList,
groupWhitelist,
subTypeFunctions,
functionEnabled,
subTypeRules,
replies,
};
}
}
module.exports = InboxV2ConfigService;