WMS_MAIN/app/service/inbox/v2/functionSync.js
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

85 lines
3.2 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 仓库可配置回信引擎)。
*
* 职责:启动时把 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: 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 / sort_order。
await ctx.model.AfterSaleFunction.bulkCreate(rows, {
updateOnDuplicate: [ 'name', 'description' ],
});
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;