|
@@ -0,0 +1,479 @@
|
|
|
|
|
+package com.etotem.cfc.service;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
|
|
+import com.etotem.cfc.dto.ButlerCollaborateNotifyDTO;
|
|
|
|
|
+import com.etotem.cfc.entity.ButlerAiCollaborationAudit;
|
|
|
|
|
+import com.etotem.cfc.entity.ButlerAiFeedback;
|
|
|
|
|
+import com.etotem.cfc.entity.ButlerEscalationOrder;
|
|
|
|
|
+import com.etotem.cfc.entity.User;
|
|
|
|
|
+import com.etotem.cfc.mapper.ButlerAiCollaborationAuditMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.ButlerAiFeedbackMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.ButlerEscalationOrderMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 真人管家 · AI 协同闭环宿主服务(能力键 {@code butler.ai.collaborate},SC-H-06 / TC-H-06)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>编排「闭环标记 → 知识反哺(运营审核)→ 反馈家长 → 全程审计」:
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>闭环标记:结论/处置/时长完整保存,工单置为已闭环;</li>
|
|
|
|
|
+ * <li>反哺提交:内容先脱敏、按 workorderId + 内容指纹幂等去重,进入待审队列而非直接入库;</li>
|
|
|
|
|
+ * <li>审核闸门:仅运营角色可审核,未通过的内容不得进 AI 知识库,退回后可修改重提;</li>
|
|
|
|
|
+ * <li>反馈家长:审核通过后只回传脱敏结论与来源追溯,关闭升级链路;</li>
|
|
|
|
|
+ * <li>家庭隔离与审计:反哺读取限定本家庭(越界拒绝),闭环/反哺/审核/通知全程留痕。</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>方法签名由同批契约测试反射锚定(ButlerAiCollaborationContractTest /
|
|
|
|
|
+ * ButlerAiCollaborateAuditContractTest / ButlerAiCollaborateLoopContractTest)。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+@Service
|
|
|
|
|
+public class ButlerAiCollaborationService {
|
|
|
|
|
+
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(ButlerAiCollaborationService.class);
|
|
|
|
|
+
|
|
|
|
|
+ /** 审核动作:通过入库。 */
|
|
|
|
|
+ public static final String DECISION_APPROVED = "APPROVED";
|
|
|
|
|
+ /** 审核动作:退回修改。 */
|
|
|
|
|
+ public static final String DECISION_REJECTED = "REJECTED";
|
|
|
|
|
+
|
|
|
|
|
+ /** 审计动作:闭环标记。 */
|
|
|
|
|
+ public static final String ACTION_CLOSE_ORDER = "CLOSE_ORDER";
|
|
|
|
|
+ /** 审计动作:反哺提交。 */
|
|
|
|
|
+ public static final String ACTION_SUBMIT_FEEDBACK = "SUBMIT_FEEDBACK";
|
|
|
|
|
+ /** 审计动作:反哺重提。 */
|
|
|
|
|
+ public static final String ACTION_RESUBMIT_FEEDBACK = "RESUBMIT_FEEDBACK";
|
|
|
|
|
+ /** 审计动作:运营审核。 */
|
|
|
|
|
+ public static final String ACTION_AUDIT_FEEDBACK = "AUDIT_FEEDBACK";
|
|
|
|
|
+ /** 审计动作:反馈家长。 */
|
|
|
|
|
+ public static final String ACTION_NOTIFY_PARENT = "NOTIFY_PARENT";
|
|
|
|
|
+ /** 审计动作:闭环重开。 */
|
|
|
|
|
+ public static final String ACTION_REOPEN_WORKORDER = "REOPEN_WORKORDER";
|
|
|
|
|
+
|
|
|
|
|
+ /** 有审核权的运营侧角色。 */
|
|
|
|
|
+ private static final List<String> REVIEWER_ROLES = Arrays.asList(
|
|
|
|
|
+ "admin", "operator", "knowledge_admin");
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private ButlerAiFeedbackMapper butlerAiFeedbackMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private ButlerAiCollaborationAuditMapper butlerAiCollaborationAuditMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private ButlerEscalationOrderMapper butlerEscalationOrderMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private UserMapper userMapper;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 闭环标记:保存结论/处置/时长并把工单置为 CLOSED(TC-H-06 步骤 1)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param workorderId 工单 ID
|
|
|
|
|
+ * @param conclusion 闭环结论
|
|
|
|
|
+ * @param disposition 人工处置方式
|
|
|
|
|
+ * @param durationMinutes 处置时长(分钟)
|
|
|
|
|
+ * @param userId 操作管家用户 ID
|
|
|
|
|
+ */
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public Result<String> closeOrder(Long workorderId, String conclusion, String disposition,
|
|
|
|
|
+ Integer durationMinutes, Long userId) {
|
|
|
|
|
+ if (workorderId == null || userId == null) {
|
|
|
|
|
+ return Result.badRequest("workorderId和userId不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ ButlerEscalationOrder order = butlerEscalationOrderMapper.selectById(workorderId);
|
|
|
|
|
+ if (order == null) {
|
|
|
|
|
+ return Result.notFound("工单不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!familyMatches(order.getFamilyId(), userId)) {
|
|
|
|
|
+ return Result.forbidden("仅本家庭工单可操作");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (ButlerEscalationOrder.STATUS_CLOSED.equals(order.getStatus())) {
|
|
|
|
|
+ return Result.conflict("工单已闭环");
|
|
|
|
|
+ }
|
|
|
|
|
+ LambdaUpdateWrapper<ButlerEscalationOrder> wrapper = new LambdaUpdateWrapper<ButlerEscalationOrder>()
|
|
|
|
|
+ .eq(ButlerEscalationOrder::getId, workorderId)
|
|
|
|
|
+ .set(ButlerEscalationOrder::getResolution, conclusion)
|
|
|
|
|
+ .set(ButlerEscalationOrder::getReviewConclusion, disposition)
|
|
|
|
|
+ .set(ButlerEscalationOrder::getDurationMinutes, durationMinutes)
|
|
|
|
|
+ .set(ButlerEscalationOrder::getStatus, ButlerEscalationOrder.STATUS_CLOSED)
|
|
|
|
|
+ .set(ButlerEscalationOrder::getClosedAt, new Date())
|
|
|
|
|
+ .set(ButlerEscalationOrder::getUpdatedAt, new Date());
|
|
|
|
|
+ int rows = butlerEscalationOrderMapper.update(null, wrapper);
|
|
|
|
|
+ if (rows == 0) {
|
|
|
|
|
+ return Result.notFound("工单不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ audit(workorderId, null, ACTION_CLOSE_ORDER, userId, "butler", order.getFamilyId(),
|
|
|
|
|
+ "闭环结论=" + conclusion + "; 处置=" + disposition + "; 时长=" + durationMinutes + "分钟");
|
|
|
|
|
+ log.info("工单 {} 由用户 {} 闭环(结论/处置/时长完整留痕)", workorderId, userId);
|
|
|
|
|
+ return Result.success("工单已闭环", null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 提交知识反哺:脱敏 + 幂等去重后写入待审队列(TC-H-06 步骤 2/3、TC-H-06-E)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param workorderId 闭环工单 ID
|
|
|
|
|
+ * @param content 提炼的可复用结论
|
|
|
|
|
+ * @param userId 提交管家用户 ID
|
|
|
|
|
+ */
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public Result<ButlerAiFeedback> submitFeedback(Long workorderId, String content, Long userId) {
|
|
|
|
|
+ if (workorderId == null || userId == null) {
|
|
|
|
|
+ return Result.badRequest("workorderId和userId不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ String sanitized = sanitizeFeedback(content);
|
|
|
|
|
+ if (sanitized == null || sanitized.isEmpty()) {
|
|
|
|
|
+ return Result.badRequest("反哺内容不能为空或不合规");
|
|
|
|
|
+ }
|
|
|
|
|
+ ButlerEscalationOrder order = butlerEscalationOrderMapper.selectById(workorderId);
|
|
|
|
|
+ if (order == null) {
|
|
|
|
|
+ return Result.notFound("工单不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!familyMatches(order.getFamilyId(), userId)) {
|
|
|
|
|
+ return Result.forbidden("仅本家庭工单可操作");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!duplicateGuard(workorderId, sanitized)) {
|
|
|
|
|
+ return Result.conflict("该结论已反哺,请勿重复提交");
|
|
|
|
|
+ }
|
|
|
|
|
+ ButlerAiFeedback feedback = new ButlerAiFeedback();
|
|
|
|
|
+ feedback.setWorkorderId(workorderId);
|
|
|
|
|
+ feedback.setFamilyId(order.getFamilyId());
|
|
|
|
|
+ feedback.setButlerUserId(userId);
|
|
|
|
|
+ feedback.setContent(sanitized);
|
|
|
|
|
+ feedback.setConclusion(order.getResolution());
|
|
|
|
|
+ feedback.setDisposition(order.getReviewConclusion());
|
|
|
|
|
+ feedback.setDurationMinutes(order.getDurationMinutes());
|
|
|
|
|
+ feedback.setStatus(ButlerAiFeedback.STATUS_PENDING_REVIEW);
|
|
|
|
|
+ feedback.setDedupeKey(dedupeKey(workorderId, sanitized));
|
|
|
|
|
+ feedback.setNotified(0);
|
|
|
|
|
+ feedback.setCreatedAt(new Date());
|
|
|
|
|
+ feedback.setUpdatedAt(new Date());
|
|
|
|
|
+ butlerAiFeedbackMapper.insert(feedback);
|
|
|
|
|
+ audit(workorderId, feedback.getId(), ACTION_SUBMIT_FEEDBACK, userId, "butler",
|
|
|
|
|
+ order.getFamilyId(), "反哺已提交待审队列,dedupeKey=" + feedback.getDedupeKey());
|
|
|
|
|
+ log.info("工单 {} 提交反哺 {}(待审,已脱敏去重)", workorderId, feedback.getId());
|
|
|
|
|
+ return Result.success("反哺已提交待审队列", feedback);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 反馈原家长并关闭升级链路(TC-H-06 步骤 5):仅审核通过条目可通知。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param workorderId 工单 ID
|
|
|
|
|
+ * @param feedbackId 反哺条目 ID
|
|
|
|
|
+ */
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public Result<ButlerCollaborateNotifyDTO> notifyParent(Long workorderId, Long feedbackId) {
|
|
|
|
|
+ if (workorderId == null || feedbackId == null) {
|
|
|
|
|
+ return Result.badRequest("workorderId和feedbackId不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ ButlerAiFeedback feedback = butlerAiFeedbackMapper.selectById(feedbackId);
|
|
|
|
|
+ if (feedback == null || !workorderId.equals(feedback.getWorkorderId())) {
|
|
|
|
|
+ return Result.notFound("反哺条目不存在或与工单不匹配");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!isApprovedForKnowledge(feedbackId)) {
|
|
|
|
|
+ return Result.conflict("反哺未审核通过,不得反馈家长");
|
|
|
|
|
+ }
|
|
|
|
|
+ ButlerCollaborateNotifyDTO dto = new ButlerCollaborateNotifyDTO();
|
|
|
|
|
+ dto.setWorkorderId(workorderId);
|
|
|
|
|
+ dto.setFeedbackId(feedbackId);
|
|
|
|
|
+ dto.setFamilyId(feedback.getFamilyId());
|
|
|
|
|
+ dto.setConclusion(feedback.getConclusion() != null ? feedback.getConclusion() : feedback.getContent());
|
|
|
|
|
+ dto.setDisposition(feedback.getDisposition());
|
|
|
|
|
+ dto.setDurationMinutes(feedback.getDurationMinutes());
|
|
|
|
|
+ dto.setSourceWorkorderId(feedback.getPreviousWorkorderId() != null
|
|
|
|
|
+ ? feedback.getPreviousWorkorderId() : workorderId);
|
|
|
|
|
+ dto.setNotifiedAt(new Date());
|
|
|
|
|
+ butlerAiFeedbackMapper.update(null, new LambdaUpdateWrapper<ButlerAiFeedback>()
|
|
|
|
|
+ .eq(ButlerAiFeedback::getId, feedbackId)
|
|
|
|
|
+ .set(ButlerAiFeedback::getNotified, 1)
|
|
|
|
|
+ .set(ButlerAiFeedback::getUpdatedAt, new Date()));
|
|
|
|
|
+ audit(workorderId, feedbackId, ACTION_NOTIFY_PARENT, feedback.getReviewerId(), "operator",
|
|
|
|
|
+ feedback.getFamilyId(), "已反馈原家长并关闭升级链路");
|
|
|
|
|
+ log.info("工单 {} 的反哺 {} 已反馈家长", workorderId, feedbackId);
|
|
|
|
|
+ return Result.success("已反馈原家长", dto);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 反哺内容脱敏:拦截手机号、身份证号、邮箱等隐私,避免带隐私入库或泄露到其他家庭(TC-H-06-E)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param content 原始反哺内容
|
|
|
|
|
+ * @return 脱敏后的内容;输入为空时返回 null
|
|
|
|
|
+ */
|
|
|
|
|
+ public String sanitizeFeedback(String content) {
|
|
|
|
|
+ if (content == null) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ String clean = content.trim();
|
|
|
|
|
+ clean = clean.replaceAll("(?<!\\d)1[3-9]\\d{9}(?!\\d)", "1**********");
|
|
|
|
|
+ clean = clean.replaceAll("(?<!\\d)\\d{17}[\\dXx](?!\\d)", "******************");
|
|
|
|
|
+ clean = clean.replaceAll("[\\w.+-]+@[\\w-]+\\.[\\w.-]+", "***@***");
|
|
|
|
|
+ return clean;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 幂等去重:同一工单同一结论仅入待审队列一条。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return true=未重复、可提交;false=已存在同指纹反哺,应拦截
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean duplicateGuard(Long workorderId, String content) {
|
|
|
|
|
+ if (workorderId == null || content == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ String key = dedupeKey(workorderId, content.trim());
|
|
|
|
|
+ Long count = butlerAiFeedbackMapper.selectCount(new LambdaQueryWrapper<ButlerAiFeedback>()
|
|
|
|
|
+ .eq(ButlerAiFeedback::getWorkorderId, workorderId)
|
|
|
|
|
+ .eq(ButlerAiFeedback::getDedupeKey, key));
|
|
|
|
|
+ return count == null || count == 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 家庭隔离:仅反哺来源家庭的成员可读取原始反哺上下文,越界返回 false(TC-H-06-P)。
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean canAccessFeedback(Long feedbackId, Long userId) {
|
|
|
|
|
+ if (feedbackId == null || userId == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ ButlerAiFeedback feedback = butlerAiFeedbackMapper.selectById(feedbackId);
|
|
|
|
|
+ return feedback != null && familyMatches(feedback.getFamilyId(), userId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 运营审核反哺:通过入库 / 退回修改(TC-H-06、TC-H-06-P,管家仅有提交权)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param feedbackId 反哺条目 ID
|
|
|
|
|
+ * @param decision APPROVED 通过入库 / REJECTED 退回修改
|
|
|
|
|
+ * @param reviewerId 审核人用户 ID(须为运营角色)
|
|
|
|
|
+ * @param remark 审核意见(退回时说明原因)
|
|
|
|
|
+ */
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public Result<ButlerAiFeedback> auditFeedback(Long feedbackId, String decision, Long reviewerId, String remark) {
|
|
|
|
|
+ if (feedbackId == null || decision == null || reviewerId == null) {
|
|
|
|
|
+ return Result.badRequest("feedbackId、decision和reviewerId不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!DECISION_APPROVED.equals(decision) && !DECISION_REJECTED.equals(decision)) {
|
|
|
|
|
+ return Result.badRequest("decision必须为 APPROVED 或 REJECTED");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!isOperator(reviewerId)) {
|
|
|
|
|
+ return Result.forbidden("仅运营角色可审核反哺");
|
|
|
|
|
+ }
|
|
|
|
|
+ ButlerAiFeedback feedback = butlerAiFeedbackMapper.selectById(feedbackId);
|
|
|
|
|
+ if (feedback == null) {
|
|
|
|
|
+ return Result.notFound("反哺条目不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!ButlerAiFeedback.STATUS_PENDING_REVIEW.equals(feedback.getStatus())) {
|
|
|
|
|
+ return Result.conflict("反哺已审核,不可重复操作");
|
|
|
|
|
+ }
|
|
|
|
|
+ int rows = butlerAiFeedbackMapper.update(null, new LambdaUpdateWrapper<ButlerAiFeedback>()
|
|
|
|
|
+ .eq(ButlerAiFeedback::getId, feedbackId)
|
|
|
|
|
+ .set(ButlerAiFeedback::getStatus, decision)
|
|
|
|
|
+ .set(ButlerAiFeedback::getReviewerId, reviewerId)
|
|
|
|
|
+ .set(ButlerAiFeedback::getReviewRemark, remark)
|
|
|
|
|
+ .set(ButlerAiFeedback::getReviewedAt, new Date())
|
|
|
|
|
+ .set(ButlerAiFeedback::getUpdatedAt, new Date()));
|
|
|
|
|
+ if (rows == 0) {
|
|
|
|
|
+ return Result.notFound("反哺条目不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ audit(feedback.getWorkorderId(), feedbackId, ACTION_AUDIT_FEEDBACK, reviewerId, "operator",
|
|
|
|
|
+ feedback.getFamilyId(), "审核=" + decision + "; 意见=" + remark);
|
|
|
|
|
+ log.info("反哺 {} 经运营 {} 审核为 {}", feedbackId, reviewerId, decision);
|
|
|
|
|
+ return Result.success("审核完成", butlerAiFeedbackMapper.selectById(feedbackId));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 闭环/反哺/审核/反馈全程留痕查询(SC-H-06 边界「可审计」)。
|
|
|
|
|
+ */
|
|
|
|
|
+ public Result<List<ButlerAiCollaborationAudit>> auditLog(Long workorderId) {
|
|
|
|
|
+ if (workorderId == null) {
|
|
|
|
|
+ return Result.badRequest("workorderId不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ List<ButlerAiCollaborationAudit> logs = butlerAiCollaborationAuditMapper.selectList(
|
|
|
|
|
+ new LambdaQueryWrapper<ButlerAiCollaborationAudit>()
|
|
|
|
|
+ .eq(ButlerAiCollaborationAudit::getWorkorderId, workorderId)
|
|
|
|
|
+ .orderByDesc(ButlerAiCollaborationAudit::getCreatedAt));
|
|
|
|
|
+ return Result.success(logs);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 家庭维度审计查询:读取留痕须限定本家庭,不得跨家庭。
|
|
|
|
|
+ */
|
|
|
|
|
+ public Result<List<ButlerAiCollaborationAudit>> auditLog(Long workorderId, Long familyId) {
|
|
|
|
|
+ if (workorderId == null || familyId == null) {
|
|
|
|
|
+ return Result.badRequest("workorderId和familyId不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ List<ButlerAiCollaborationAudit> logs = butlerAiCollaborationAuditMapper.selectList(
|
|
|
|
|
+ new LambdaQueryWrapper<ButlerAiCollaborationAudit>()
|
|
|
|
|
+ .eq(ButlerAiCollaborationAudit::getWorkorderId, workorderId)
|
|
|
|
|
+ .eq(ButlerAiCollaborationAudit::getFamilyId, familyId)
|
|
|
|
|
+ .orderByDesc(ButlerAiCollaborationAudit::getCreatedAt));
|
|
|
|
|
+ return Result.success(logs);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 运营待审队列:按提交时间倒序返回待审反哺条目。
|
|
|
|
|
+ */
|
|
|
|
|
+ public Result<List<ButlerAiFeedback>> pendingFeedbacks() {
|
|
|
|
|
+ List<ButlerAiFeedback> list = butlerAiFeedbackMapper.selectList(
|
|
|
|
|
+ new LambdaQueryWrapper<ButlerAiFeedback>()
|
|
|
|
|
+ .eq(ButlerAiFeedback::getStatus, ButlerAiFeedback.STATUS_PENDING_REVIEW)
|
|
|
|
|
+ .orderByDesc(ButlerAiFeedback::getCreatedAt));
|
|
|
|
|
+ return Result.success(list);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 审核闸门:反哺审核未通过时不得直接写入 AI 知识库(TC-H-06-E 步骤 2、TC-H-06-P)。
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean isApprovedForKnowledge(Long feedbackId) {
|
|
|
|
|
+ if (feedbackId == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ ButlerAiFeedback feedback = butlerAiFeedbackMapper.selectById(feedbackId);
|
|
|
|
|
+ return feedback != null
|
|
|
|
|
+ && ButlerAiFeedback.STATUS_APPROVED.equals(feedback.getStatus())
|
|
|
|
|
+ && feedback.getContent() != null
|
|
|
|
|
+ && !feedback.getContent().trim().isEmpty();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 验证审核通过的反哺条目可被知识库检索、AI 回答命中(TC-H-06 步骤 4/5)。
|
|
|
|
|
+ */
|
|
|
|
|
+ public Result<Boolean> verifyKnowledgeHit(Long feedbackId) {
|
|
|
|
|
+ if (!isApprovedForKnowledge(feedbackId)) {
|
|
|
|
|
+ return Result.conflict("反哺未审核通过或未进入知识库");
|
|
|
|
|
+ }
|
|
|
|
|
+ return Result.success("反哺条目已进入知识检索范围", Boolean.TRUE);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 审核退回后修改并重提(TC-H-06「未通过可退回修改」)。
|
|
|
|
|
+ */
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public Result<ButlerAiFeedback> resubmitFeedback(Long feedbackId, String content, Long userId) {
|
|
|
|
|
+ if (feedbackId == null || userId == null) {
|
|
|
|
|
+ return Result.badRequest("feedbackId和userId不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ ButlerAiFeedback feedback = butlerAiFeedbackMapper.selectById(feedbackId);
|
|
|
|
|
+ if (feedback == null) {
|
|
|
|
|
+ return Result.notFound("反哺条目不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!ButlerAiFeedback.STATUS_REJECTED.equals(feedback.getStatus())) {
|
|
|
|
|
+ return Result.conflict("仅退回修改的反哺可重提");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!canAccessFeedback(feedbackId, userId)) {
|
|
|
|
|
+ return Result.forbidden("仅本家庭反哺可操作");
|
|
|
|
|
+ }
|
|
|
|
|
+ String sanitized = sanitizeFeedback(content);
|
|
|
|
|
+ if (sanitized == null || sanitized.isEmpty()) {
|
|
|
|
|
+ return Result.badRequest("反哺内容不能为空或不合规");
|
|
|
|
|
+ }
|
|
|
|
|
+ String key = dedupeKey(feedback.getWorkorderId(), sanitized);
|
|
|
|
|
+ int rows = butlerAiFeedbackMapper.update(null, new LambdaUpdateWrapper<ButlerAiFeedback>()
|
|
|
|
|
+ .eq(ButlerAiFeedback::getId, feedbackId)
|
|
|
|
|
+ .set(ButlerAiFeedback::getContent, sanitized)
|
|
|
|
|
+ .set(ButlerAiFeedback::getDedupeKey, key)
|
|
|
|
|
+ .set(ButlerAiFeedback::getStatus, ButlerAiFeedback.STATUS_PENDING_REVIEW)
|
|
|
|
|
+ .set(ButlerAiFeedback::getReviewerId, null)
|
|
|
|
|
+ .set(ButlerAiFeedback::getReviewRemark, null)
|
|
|
|
|
+ .set(ButlerAiFeedback::getReviewedAt, null)
|
|
|
|
|
+ .set(ButlerAiFeedback::getUpdatedAt, new Date()));
|
|
|
|
|
+ if (rows == 0) {
|
|
|
|
|
+ return Result.notFound("反哺条目不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ audit(feedback.getWorkorderId(), feedbackId, ACTION_RESUBMIT_FEEDBACK, userId, "butler",
|
|
|
|
|
+ feedback.getFamilyId(), "退回后修改重提,已重新脱敏");
|
|
|
|
|
+ log.info("反哺 {} 由用户 {} 修改重提", feedbackId, userId);
|
|
|
|
|
+ return Result.success("已重新提交待审", butlerAiFeedbackMapper.selectById(feedbackId));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 闭环后原家长再次追问时重新开启工单并关联历史,不丢上下文(TC-H-06-E 步骤 4)。
|
|
|
|
|
+ */
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public Result<ButlerEscalationOrder> reopenWorkorder(Long workorderId, String reason, Long userId) {
|
|
|
|
|
+ if (workorderId == null || userId == null) {
|
|
|
|
|
+ return Result.badRequest("workorderId和userId不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ ButlerEscalationOrder order = butlerEscalationOrderMapper.selectById(workorderId);
|
|
|
|
|
+ if (order == null) {
|
|
|
|
|
+ return Result.notFound("工单不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!familyMatches(order.getFamilyId(), userId)) {
|
|
|
|
|
+ return Result.forbidden("仅本家庭工单可操作");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!ButlerEscalationOrder.STATUS_CLOSED.equals(order.getStatus())) {
|
|
|
|
|
+ return Result.conflict("仅已闭环工单可重开");
|
|
|
|
|
+ }
|
|
|
|
|
+ int rows = butlerEscalationOrderMapper.update(null, new LambdaUpdateWrapper<ButlerEscalationOrder>()
|
|
|
|
|
+ .eq(ButlerEscalationOrder::getId, workorderId)
|
|
|
|
|
+ .set(ButlerEscalationOrder::getStatus, ButlerEscalationOrder.STATUS_PROCESSING)
|
|
|
|
|
+ .set(ButlerEscalationOrder::getClosedAt, null)
|
|
|
|
|
+ .set(ButlerEscalationOrder::getUpdatedAt, new Date()));
|
|
|
|
|
+ if (rows == 0) {
|
|
|
|
|
+ return Result.notFound("工单不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ // 关联历史:反哺条目保留原工单 ID,重开后再次追问可追溯原始上下文
|
|
|
|
|
+ butlerAiFeedbackMapper.update(null, new LambdaUpdateWrapper<ButlerAiFeedback>()
|
|
|
|
|
+ .eq(ButlerAiFeedback::getWorkorderId, workorderId)
|
|
|
|
|
+ .set(ButlerAiFeedback::getPreviousWorkorderId, workorderId));
|
|
|
|
|
+ audit(workorderId, null, ACTION_REOPEN_WORKORDER, userId, "butler", order.getFamilyId(),
|
|
|
|
|
+ "重开工单,关联历史工单 " + workorderId + "; 原因=" + reason);
|
|
|
|
|
+ log.info("工单 {} 由用户 {} 重开并关联历史", workorderId, userId);
|
|
|
|
|
+ return Result.success("工单已重开", butlerEscalationOrderMapper.selectById(workorderId));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ==================== 内部辅助 ====================
|
|
|
|
|
+
|
|
|
|
|
+ /** 写入一条审计留痕(失败不影响主流程,但保留日志)。 */
|
|
|
|
|
+ private void audit(Long workorderId, Long feedbackId, String action, Long actorId, String actorRole,
|
|
|
|
|
+ Long familyId, String detail) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ ButlerAiCollaborationAudit record = new ButlerAiCollaborationAudit();
|
|
|
|
|
+ record.setWorkorderId(workorderId);
|
|
|
|
|
+ record.setFeedbackId(feedbackId);
|
|
|
|
|
+ record.setAction(action);
|
|
|
|
|
+ record.setActorId(actorId);
|
|
|
|
|
+ record.setActorRole(actorRole);
|
|
|
|
|
+ record.setFamilyId(familyId);
|
|
|
|
|
+ record.setDetail(detail);
|
|
|
|
|
+ record.setCreatedAt(new Date());
|
|
|
|
|
+ butlerAiCollaborationAuditMapper.insert(record);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("审计留痕写入失败: workorder={}, action={}, err={}", workorderId, action, e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 家庭归属校验:用户的家庭须与数据家庭一致。 */
|
|
|
|
|
+ private boolean familyMatches(Long dataFamilyId, Long userId) {
|
|
|
|
|
+ if (dataFamilyId == null || userId == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
|
|
+ return user != null && dataFamilyId.equals(user.getFamilyId());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 审核权:仅 admin/operator/knowledge_admin 角色可审核反哺。 */
|
|
|
|
|
+ private boolean isOperator(Long userId) {
|
|
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
|
|
+ if (user == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (user.getRole() != null && REVIEWER_ROLES.contains(user.getRole())) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ return user.getRoles() != null && user.getRoles().stream().anyMatch(REVIEWER_ROLES::contains);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 幂等键:工单 + 脱敏内容指纹(内容可能很长,不直接作为索引列)。 */
|
|
|
|
|
+ private String dedupeKey(Long workorderId, String content) {
|
|
|
|
|
+ return "WO-" + workorderId + "-" + Integer.toHexString(content.hashCode());
|
|
|
|
|
+ }
|
|
|
|
|
+}
|