新增功能: - AI智能助手: DeepSeek多轮推理、函数调用(searchStock/createOrder等) - silent动作: AI识别无需回复的消息(好的/谢谢/再见)自动跳过回执 - ai_enabled灰度开关: 群级别控制AI启停,支持逐群放量 - 消息列表增强: storeId/groupId/groupName/userName/content/sourceType多条件筛选,时间格式化 代码优化: - processWithAI: 多轮推理循环(MAX_ROUNDS=5)、对话历史不含[#N]前缀防AI污染 - 回执记录补齐group_id/user_id/group_name/user_name上下文 - 移除未使用的target_channel字段及迁移 - bindCode文档修正: store_warehouse → store 接口文档: - system.json v0.3.18→0.3.19: 新增aiEnabled参数、消息列表响应扩充 - changes/system.json: 新增bindCode变更记录 杂项: - store_warehouse.js JSDoc补全 - wave.js waveBatchSource修复
251 lines
8.5 KiB
JavaScript
251 lines
8.5 KiB
JavaScript
'use strict';
|
||
|
||
const Service = require('egg').Service;
|
||
|
||
/**
|
||
* 站内信群管理业务逻辑层。
|
||
* 提供群列表查询、禁用、修改等核心能力。
|
||
*/
|
||
class InboxGroupService extends Service {
|
||
/**
|
||
* 拉取当前仓库的群列表。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @param {number} data.warehouseId 仓库ID(必填,由 controller 注入)。
|
||
* @param {string} [data.groupType] 按群类型筛选:store / warehouse / express。
|
||
* @param {string} [data.keyword] 按群名称模糊搜索。
|
||
* @param {number} [data.page=1] 页码。
|
||
* @param {number} [data.pageSize=20] 每页条数。
|
||
* @return {Promise<Object>} { total, list }。
|
||
*/
|
||
async list(data = {}) {
|
||
const { ctx } = this;
|
||
const { InboxGroup } = ctx.model;
|
||
const { Op } = ctx.app.Sequelize;
|
||
|
||
const warehouseId = Number(data.warehouseId);
|
||
const groupType = String(data.groupType || '').trim();
|
||
const keyword = String(data.keyword || '').trim();
|
||
const page = Math.max(1, Number(data.page) || 1);
|
||
const pageSize = Math.min(100, Math.max(1, Number(data.pageSize) || 20));
|
||
const offset = (page - 1) * pageSize;
|
||
|
||
if (!warehouseId) throw new Error('warehouseId 无效');
|
||
|
||
const where = { warehouse_id: warehouseId };
|
||
if (groupType) where.group_type = groupType;
|
||
if (keyword) where.group_name = { [Op.like]: `%${keyword}%` };
|
||
|
||
const { count, rows } = await InboxGroup.findAndCountAll({
|
||
attributes: [
|
||
'id', 'group_name', 'group_id', 'warehouse_id', 'store_id',
|
||
'group_type', 'sub_type', 'source_type', 'source_extra',
|
||
'is_blocked', 'ai_enabled', 'express_interfaces', 'system_express_code',
|
||
'created_at', 'updated_at',
|
||
],
|
||
where,
|
||
order: [[ 'created_at', 'DESC' ]],
|
||
limit: pageSize,
|
||
offset,
|
||
raw: true,
|
||
});
|
||
|
||
// express_interfaces 输出为数组
|
||
const list = rows.map(row => ({
|
||
...row,
|
||
express_interfaces: row.express_interfaces
|
||
? row.express_interfaces.split(',').map(Number).filter(Boolean)
|
||
: [],
|
||
}));
|
||
|
||
return { total: count, list };
|
||
}
|
||
|
||
/**
|
||
* 禁用 / 解禁群。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @param {number} data.id 群记录主键ID(必填)。
|
||
* @param {number} data.warehouseId 仓库ID(必填,由 controller 注入)。
|
||
* @param {number} data.isBlocked 是否屏蔽 1=禁用 0=启用。
|
||
* @return {Promise<void>}
|
||
*/
|
||
async disable(data = {}) {
|
||
const { ctx } = this;
|
||
const { InboxGroup } = ctx.model;
|
||
|
||
const id = Number(data.id);
|
||
const warehouseId = Number(data.warehouseId);
|
||
const isBlocked = data.isBlocked === 1 ? 1 : 0;
|
||
|
||
if (!id) throw new Error('群ID无效');
|
||
if (!warehouseId) throw new Error('warehouseId 无效');
|
||
|
||
const [ affected ] = await InboxGroup.update(
|
||
{ is_blocked: isBlocked },
|
||
{ where: { id, warehouse_id: warehouseId } }
|
||
);
|
||
|
||
if (!affected) {
|
||
throw new Error('群不存在或无权限操作');
|
||
}
|
||
|
||
ctx.logger.info(
|
||
`[inbox group] ${isBlocked ? '禁用' : '启用'}群 id=${id} warehouse_id=${warehouseId}`
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 修改群信息。
|
||
* 支持修改 group_type / sub_type / store_id / express_interfaces / system_express_code。
|
||
* 群类型切为 express 时 system_express_code 必填,
|
||
* express_interfaces 仅在群类型为 express 时生效(非 express 类型忽略)。
|
||
*
|
||
* @param {Object} data 请求数据。
|
||
* @param {number} data.id 群记录主键ID(必填)。
|
||
* @param {number} data.warehouseId 仓库ID(必填,由 controller 注入)。
|
||
* @param {string} [data.groupType] 群类型:store / warehouse / express。
|
||
* @param {string} [data.subType] 群子类型。
|
||
* @param {number} [data.storeId] 关联货主ID。
|
||
* @param {number[]} [data.expressInterfaces] 快递接口ID数组。
|
||
* @param {string} [data.systemExpressCode] 系统快递编码。
|
||
* @return {Promise<void>}
|
||
*/
|
||
async update(data = {}) {
|
||
const { ctx } = this;
|
||
const { InboxGroup } = ctx.model;
|
||
|
||
const id = Number(data.id);
|
||
const warehouseId = Number(data.warehouseId);
|
||
|
||
if (!id) throw new Error('群ID无效');
|
||
if (!warehouseId) throw new Error('warehouseId 无效');
|
||
|
||
// 查群是否存在
|
||
const group = await InboxGroup.findOne({
|
||
attributes: [ 'id', 'group_type', 'group_id', 'warehouse_id' ],
|
||
where: { id, warehouse_id: warehouseId },
|
||
raw: true,
|
||
});
|
||
if (!group) throw new Error('群不存在或无权限操作');
|
||
|
||
// 目标群类型(优先新值,否则保留原值)
|
||
const targetGroupType = data.groupType !== undefined
|
||
? String(data.groupType).trim()
|
||
: group.group_type || '';
|
||
const isExpress = targetGroupType === 'express';
|
||
|
||
// express_interfaces 处理
|
||
let expressInterfaces = null;
|
||
if (data.expressInterfaces !== undefined) {
|
||
if (Array.isArray(data.expressInterfaces) && data.expressInterfaces.length > 0) {
|
||
// 校验快递接口ID有效性
|
||
await this.validateExpressInterfaces(data.expressInterfaces);
|
||
expressInterfaces = data.expressInterfaces.join(',');
|
||
} else if (Array.isArray(data.expressInterfaces) && data.expressInterfaces.length === 0) {
|
||
// 空数组 = 清空 = 表示全部快递类型
|
||
expressInterfaces = '';
|
||
} else {
|
||
expressInterfaces = null; // 未传入 = 不修改
|
||
}
|
||
}
|
||
|
||
// system_express_code 校验
|
||
let systemExpressCode;
|
||
if (data.systemExpressCode !== undefined) {
|
||
const code = String(data.systemExpressCode).trim();
|
||
if (code) {
|
||
await this.validateSystemExpressCode(code);
|
||
systemExpressCode = code;
|
||
} else {
|
||
systemExpressCode = null;
|
||
}
|
||
}
|
||
|
||
// 切换为 express 类型时 system_express_code 不能为空
|
||
if (isExpress && data.groupType !== undefined) {
|
||
const finalCode = systemExpressCode !== undefined ? systemExpressCode : group.system_express_code;
|
||
if (!finalCode) {
|
||
throw new Error('群类型为快递群时,系统快递编码不能为空');
|
||
}
|
||
}
|
||
|
||
// 构建更新字段
|
||
const updateData = {};
|
||
if (data.groupType !== undefined) updateData.group_type = targetGroupType || null;
|
||
if (data.subType !== undefined) updateData.sub_type = String(data.subType || '').trim() || null;
|
||
if (data.storeId !== undefined) {
|
||
updateData.store_id = Number.isFinite(Number(data.storeId)) && Number(data.storeId) > 0
|
||
? Number(data.storeId) : null;
|
||
}
|
||
// 非 express 群类型时忽略 express 相关字段
|
||
if (isExpress && expressInterfaces !== null) {
|
||
updateData.express_interfaces = expressInterfaces || null;
|
||
}
|
||
if (isExpress && systemExpressCode !== undefined) {
|
||
updateData.system_express_code = systemExpressCode || null;
|
||
}
|
||
// AI 开关(所有群类型均支持)
|
||
if (data.aiEnabled !== undefined) {
|
||
updateData.ai_enabled = data.aiEnabled === 1 ? 1 : 0;
|
||
}
|
||
|
||
if (Object.keys(updateData).length === 0) {
|
||
throw new Error('未提供任何需要修改的字段');
|
||
}
|
||
|
||
await InboxGroup.update(updateData, { where: { id, warehouse_id: warehouseId } });
|
||
ctx.logger.info(
|
||
`[inbox group] 修改群 id=${id} fields=${Object.keys(updateData).join(',')}`
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 校验快递接口ID数组的有效性。
|
||
* 所有ID必须在 wms_express_interface 表中存在。
|
||
*
|
||
* @param {number[]} ids 快递接口ID数组。
|
||
* @return {Promise<void>} 无效时抛出异常。
|
||
*/
|
||
async validateExpressInterfaces(ids) {
|
||
const { ctx } = this;
|
||
if (!Array.isArray(ids) || ids.length === 0) return;
|
||
|
||
const numericIds = ids.map(Number).filter(id => Number.isFinite(id) && id > 0);
|
||
if (numericIds.length !== ids.length) {
|
||
throw new Error('快递接口ID包含无效值');
|
||
}
|
||
|
||
const count = await ctx.model.ExpressInterface.count({
|
||
where: { id: { [ctx.app.Sequelize.Op.in]: numericIds } },
|
||
});
|
||
|
||
if (count !== numericIds.length) {
|
||
throw new Error('部分快递接口ID不存在');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 校验系统快递编码有效性。
|
||
* 必须在 wms_express 表中存在对应的 code。
|
||
*
|
||
* @param {string} code 系统快递编码。
|
||
* @return {Promise<void>} 无效时抛出异常。
|
||
*/
|
||
async validateSystemExpressCode(code) {
|
||
const { ctx } = this;
|
||
const trimmed = String(code).trim();
|
||
if (!trimmed) throw new Error('系统快递编码不能为空');
|
||
|
||
const exists = await ctx.model.Express.count({
|
||
where: { code: trimmed },
|
||
});
|
||
|
||
if (!exists) {
|
||
throw new Error(`系统快递编码 "${trimmed}" 不存在`);
|
||
}
|
||
}
|
||
}
|
||
|
||
module.exports = InboxGroupService;
|