WMS_MAIN/app/service/inbox/v2/functionSync.js
liufumingren 806e739267 feat(inbox): v2 规则引擎完善(help 指令/建单开关/系统回复)并修复拦截日志误写仓库 ID
- functions.js: 拦截/取消拦截波次感知写订单日志 businessId 误用仓库 ID,改为订单 ID
  (生产订单 JY2026091813795 已发起拦截但订单详情查不到拦截日志的根因)
- rules/v2/processor/config/functionSync: 新增 help 指令、create_work_order=0 时跳过
  工单登记、系统级回复与无单号提示、白名单范围修正
- after_sale_reply 模型 + 6 个迁移: create_work_order 字段与相关配置种子
- return_unpack: 列表 count 统计修复、分页排序追加主键防重漏行、视频附加 camera_name
- methods/system.json + changes + sync 脚本: 接口文档与权限同步
2026-09-18 22:38:57 +08:00

131 lines
4.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';
const Service = require('egg').Service;
/**
* function_code → 中文名称映射(管理界面展示用)。
* 未列出的函数回退到 function_code(英文注册名)。
*/
const FUNCTION_NAME_MAP = {
hello: '问候',
help: '帮助',
queryPrintBalance: '查询面单余额',
createWaybill: '创建运单号',
searchStock: '搜索货主商品',
searchWarehouseStock: '搜索仓库商品',
queryOrder: '查询订单',
recallHistory: '回顾对话历史',
createOrder: '创建发货单',
blockUser: '屏蔽用户',
interceptWaybill: '拦截运单',
requestPickup: '揽收处理',
queryReturn: '查询退回单',
cancelIntercept: '取消拦截',
handleStoreFeedback: '问题反馈建单',
changeAddr: '改地址/催件处理',
weishoudao: '核实签收',
changeExpress: '改快递',
createPostnum: '分配快递号',
queryPackVideo: '查询打包视频',
queryWeight: '查询重量',
handleExpressFeedback: '快递反馈处理',
queryWarehouseAddress: '查询仓库地址',
listGroups: '查询群列表',
openDoor: '开门',
registerLicensePlate: '到货登记',
departure: '离场登记',
enter: '进场登记',
};
/**
* 售后函数表同步服务(v2 仓库可配置回信引擎)。
*
* 职责:启动时把 inbox functions 注册表(代码权威源)幂等 upsert 到
* wms_after_sale_function,供管理界面展示与开关控制。
*
* 原则:
* - 只增改、不删:代码中已删除的函数保留 DB 行(避免关联行悬空,管理端可见并可停用);
* - 不覆盖管理端字段:is_enabled / sort_order 由管理端维护,同步仅刷新 name/description;
* - 失败仅告警:本服务不影响任何消息处理链路,运行时执行始终以代码注册表为准。
*/
class InboxV2FunctionSyncService extends Service {
/**
* 同步注册表到售后函数表(幂等,可重复执行)。
* @return {Promise<{ok:boolean, created:number, updated:number, total:number, skipped?:boolean, error?:string}>} 同步统计结果,skipped 表示未执行同步(模型缺失/注册表为空)。
*/
async sync() {
const { ctx } = this;
const stats = { ok: false, created: 0, updated: 0, total: 0 };
// 模型缺失场景(如部分单测):直接跳过,不报错。
if (!ctx.model || !ctx.model.AfterSaleFunction) {
return { ...stats, ok: true, skipped: true };
}
let registry;
try {
const functionsSvc = ctx.service.inbox.functions;
registry = functionsSvc && functionsSvc.registry;
} catch (e) {
ctx.logger.warn(`[inbox v2] 函数服务不可用,跳过函数表同步: ${e.message}`);
return { ...stats, ok: true, skipped: true };
}
if (!(registry instanceof Map) || registry.size === 0) {
return { ...stats, ok: true, skipped: true };
}
// 收集待同步行(function_code 即注册名)。
const rows = [];
const seen = new Set();
for (const [ code, def ] of registry) {
if (!code || seen.has(code)) continue;
seen.add(code);
rows.push({
function_code: code,
name: FUNCTION_NAME_MAP[code] || code,
description: (typeof def.description === 'string' ? def.description : '').slice(0, 500) || null,
});
}
stats.total = rows.length;
try {
// 同步前查现有集合,用于统计新增/更新。
const existing = await ctx.model.AfterSaleFunction.findAll({
attributes: [ 'function_code' ],
raw: true,
});
const existingSet = new Set(existing.map(r => r.function_code));
stats.created = rows.filter(r => !existingSet.has(r.function_code)).length;
stats.updated = rows.length - stats.created;
// bulkCreate + updateOnDuplicate:新行插入(is_enabled 默认 1、sort_order 默认 0),
// 已有行刷新 name/description;启用状态硬编码——注册表存在即启用(is_enabled 同步为 1),
// sort_order 由接口配置,不同步覆盖。
await ctx.model.AfterSaleFunction.bulkCreate(rows, {
updateOnDuplicate: [ 'name', 'description', 'is_enabled' ],
});
// 代码已删除的函数:保留行但标记下线(is_enabled=0),不删除。
if (rows.length > 0) {
const codes = rows.map(r => r.function_code);
const { Op } = ctx.app.Sequelize;
await ctx.model.AfterSaleFunction.update(
{ is_enabled: 0 },
{ where: { function_code: { [Op.notIn]: codes } } }
);
}
stats.ok = true;
return stats;
} catch (error) {
ctx.logger.warn(
`[inbox v2] 售后函数表同步失败(不影响启动): ${error && error.message ? error.message : error}`
);
return { ...stats, error: error && error.message ? error.message : String(error) };
}
}
}
module.exports = InboxV2FunctionSyncService;