|
@@ -2,8 +2,14 @@ package com.etotem.cfc.service;
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.etotem.cfc.common.Result;
|
|
import com.etotem.cfc.common.Result;
|
|
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
|
|
+import com.etotem.cfc.entity.HealthReport;
|
|
|
|
|
+import com.etotem.cfc.entity.HealthReportDraft;
|
|
|
import com.etotem.cfc.entity.ReportFeedback;
|
|
import com.etotem.cfc.entity.ReportFeedback;
|
|
|
import com.etotem.cfc.entity.User;
|
|
import com.etotem.cfc.entity.User;
|
|
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.HealthReportDraftMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.HealthReportMapper;
|
|
|
import com.etotem.cfc.mapper.ReportFeedbackMapper;
|
|
import com.etotem.cfc.mapper.ReportFeedbackMapper;
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -22,6 +28,10 @@ public class ReportFeedbackService {
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(ReportFeedbackService.class);
|
|
private static final Logger log = LoggerFactory.getLogger(ReportFeedbackService.class);
|
|
|
|
|
|
|
|
|
|
+ /** TC-P-83-E:反馈附件上限 10MB(10 * 1024 * 1024 = 10485760 字节)。 */
|
|
|
|
|
+ private static final long MAX_ATTACHMENT_SIZE = 10 * 1024 * 1024;
|
|
|
|
|
+ private static final String MSG_ATTACHMENT_TOO_LARGE = "附件不超过 10MB";
|
|
|
|
|
+
|
|
|
@Resource
|
|
@Resource
|
|
|
private ReportFeedbackMapper reportFeedbackMapper;
|
|
private ReportFeedbackMapper reportFeedbackMapper;
|
|
|
|
|
|
|
@@ -37,11 +47,28 @@ public class ReportFeedbackService {
|
|
|
@Resource
|
|
@Resource
|
|
|
private RedisTemplate<String, Object> redisTemplate;
|
|
private RedisTemplate<String, Object> redisTemplate;
|
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private HealthReportMapper healthReportMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private HealthReportDraftMapper healthReportDraftMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private FamilyMemberMapper familyMemberMapper;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 提交报告反馈(持久化 + AI 解答)。
|
|
* 提交报告反馈(持久化 + AI 解答)。
|
|
|
* 免费版每日 1 次,付费版不限/多轮(沿用报告快速分析策略)。
|
|
* 免费版每日 1 次,付费版不限/多轮(沿用报告快速分析策略)。
|
|
|
*/
|
|
*/
|
|
|
public Result<?> submit(Long userId, Long memberId, Long reportId, Long draftId, String type, String content) {
|
|
public Result<?> submit(Long userId, Long memberId, Long reportId, Long draftId, String type, String content) {
|
|
|
|
|
+ return submit(userId, memberId, reportId, draftId, type, content, null, null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 提交报告反馈(含附件载体)。敏感信息脱敏后再送 AI / 落库(TC-P-83-E)。
|
|
|
|
|
+ */
|
|
|
|
|
+ public Result<?> submit(Long userId, Long memberId, Long reportId, Long draftId, String type, String content,
|
|
|
|
|
+ String attachmentUrl, Long attachmentSize) {
|
|
|
if (content == null || content.trim().isEmpty()) {
|
|
if (content == null || content.trim().isEmpty()) {
|
|
|
return Result.error("反馈内容不能为空");
|
|
return Result.error("反馈内容不能为空");
|
|
|
}
|
|
}
|
|
@@ -49,15 +76,24 @@ public class ReportFeedbackService {
|
|
|
if (content.length() > 500) {
|
|
if (content.length() > 500) {
|
|
|
return Result.error("反馈内容不能超过500字");
|
|
return Result.error("反馈内容不能超过500字");
|
|
|
}
|
|
}
|
|
|
|
|
+ Result<?> attachmentError = checkAttachmentSize(attachmentSize);
|
|
|
|
|
+ if (attachmentError != null) {
|
|
|
|
|
+ return attachmentError;
|
|
|
|
|
+ }
|
|
|
// 归一化类型:question=疑问 / suggestion=建议,其他默认 question
|
|
// 归一化类型:question=疑问 / suggestion=建议,其他默认 question
|
|
|
String normalizedType = "suggestion".equals(type) ? "suggestion" : "question";
|
|
String normalizedType = "suggestion".equals(type) ? "suggestion" : "question";
|
|
|
|
|
|
|
|
// 1. 会员级别检查
|
|
// 1. 会员级别检查
|
|
|
User user = userService.getUserInfo(userId);
|
|
User user = userService.getUserInfo(userId);
|
|
|
- String memberLevel = user != null ? user.getMemberLevel() : "FREE";
|
|
|
|
|
- boolean isFree = memberLevel == null || "FREE".equals(memberLevel);
|
|
|
|
|
|
|
|
|
|
- // 2. 免费版 Redis 计数(每日 1 次;Redis 异常时 fail-open 放行)
|
|
|
|
|
|
|
+ // 2. 越权校验:reportId/draftId/memberId 非本用户归属 → 403 拒绝、不落库、不泄露(TC-P-83-P)
|
|
|
|
|
+ Result<?> ownershipError = checkOwnership(userId, memberId, reportId, draftId, user);
|
|
|
|
|
+ if (ownershipError != null) {
|
|
|
|
|
+ return ownershipError;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 免费版 Redis 计数(每日 1 次;Redis 异常时 fail-open 放行)
|
|
|
|
|
+ boolean isFree = user == null || user.getMemberLevel() == null || "FREE".equals(user.getMemberLevel());
|
|
|
if (isFree) {
|
|
if (isFree) {
|
|
|
try {
|
|
try {
|
|
|
String key = "report:feedback:free:" + userId + ":" + LocalDate.now();
|
|
String key = "report:feedback:free:" + userId + ":" + LocalDate.now();
|
|
@@ -73,7 +109,7 @@ public class ReportFeedbackService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 3. 组装 AI 上下文(指定报告/草稿时注入对应数据)
|
|
|
|
|
|
|
+ // 4. 组装 AI 上下文(指定报告/草稿时注入对应数据)
|
|
|
Map<String, Object> inputs;
|
|
Map<String, Object> inputs;
|
|
|
if (reportId != null) {
|
|
if (reportId != null) {
|
|
|
inputs = familyContextService.buildContext(userId, reportId);
|
|
inputs = familyContextService.buildContext(userId, reportId);
|
|
@@ -83,20 +119,24 @@ public class ReportFeedbackService {
|
|
|
inputs = familyContextService.buildContext(userId);
|
|
inputs = familyContextService.buildContext(userId);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 4. 组装提问(单轮问答;conversationId 留待后续多轮扩展)
|
|
|
|
|
- String query = "【" + ("suggestion".equals(normalizedType) ? "建议" : "疑问") + "】" + content;
|
|
|
|
|
|
|
+ // 5. 敏感信息脱敏(身份证/手机号)后再送 AI,原始敏感串不进入外部链路(TC-P-83-E)
|
|
|
|
|
+ String safeContent = maskSensitiveInfo(content);
|
|
|
|
|
|
|
|
- // 5. 调 LangGraph(经 AiGateway,统一熔断/配额/脱敏)
|
|
|
|
|
|
|
+ // 6. 组装提问(单轮问答;conversationId 留待后续多轮扩展)
|
|
|
|
|
+ String query = "【" + ("suggestion".equals(normalizedType) ? "建议" : "疑问") + "】" + safeContent;
|
|
|
|
|
+
|
|
|
|
|
+ // 7. 调 LangGraph(经 AiGateway,统一熔断/配额/脱敏)
|
|
|
Map<String, Object> resp = aiGateway.chat(query, userId, null, inputs);
|
|
Map<String, Object> resp = aiGateway.chat(query, userId, null, inputs);
|
|
|
|
|
|
|
|
- // 6. 组装记录(AI 失败也落库,ai_status=failed)
|
|
|
|
|
|
|
+ // 8. 组装记录(AI 失败也落库,ai_status=failed;content 已脱敏)
|
|
|
ReportFeedback record = new ReportFeedback();
|
|
ReportFeedback record = new ReportFeedback();
|
|
|
record.setUserId(userId);
|
|
record.setUserId(userId);
|
|
|
record.setMemberId(memberId);
|
|
record.setMemberId(memberId);
|
|
|
record.setReportId(reportId);
|
|
record.setReportId(reportId);
|
|
|
record.setDraftId(draftId);
|
|
record.setDraftId(draftId);
|
|
|
record.setFeedbackType(normalizedType);
|
|
record.setFeedbackType(normalizedType);
|
|
|
- record.setContent(content);
|
|
|
|
|
|
|
+ record.setContent(safeContent);
|
|
|
|
|
+ record.setAttachmentUrl(attachmentUrl);
|
|
|
if (resp == null) {
|
|
if (resp == null) {
|
|
|
record.setAiStatus("failed");
|
|
record.setAiStatus("failed");
|
|
|
record.setAiAnswer(null);
|
|
record.setAiAnswer(null);
|
|
@@ -110,6 +150,44 @@ public class ReportFeedbackService {
|
|
|
return Result.success(record);
|
|
return Result.success(record);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 转人工客服 / 工单接入(反馈域在线客服,缺口 G-4 · SC-P-83)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>与 AI 自动解答并行的第二条路径:仅落库为待人工受理(feedback_type=consult、
|
|
|
|
|
+ * ai_status=pending),不调用 AI;同样执行敏感信息脱敏、附件 10MB 上限与越权校验。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+ public Result<?> handoffToConsultant(Long userId, Long memberId, String content,
|
|
|
|
|
+ String attachmentUrl, Long attachmentSize) {
|
|
|
|
|
+ if (content == null || content.trim().isEmpty()) {
|
|
|
|
|
+ return Result.error("反馈内容不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ content = content.trim();
|
|
|
|
|
+ if (content.length() > 500) {
|
|
|
|
|
+ return Result.error("反馈内容不能超过500字");
|
|
|
|
|
+ }
|
|
|
|
|
+ Result<?> attachmentError = checkAttachmentSize(attachmentSize);
|
|
|
|
|
+ if (attachmentError != null) {
|
|
|
|
|
+ return attachmentError;
|
|
|
|
|
+ }
|
|
|
|
|
+ User user = userService.getUserInfo(userId);
|
|
|
|
|
+ Result<?> ownershipError = checkOwnership(userId, memberId, null, null, user);
|
|
|
|
|
+ if (ownershipError != null) {
|
|
|
|
|
+ return ownershipError;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ReportFeedback record = new ReportFeedback();
|
|
|
|
|
+ record.setUserId(userId);
|
|
|
|
|
+ record.setMemberId(memberId);
|
|
|
|
|
+ record.setFeedbackType("consult");
|
|
|
|
|
+ record.setContent(maskSensitiveInfo(content));
|
|
|
|
|
+ record.setAttachmentUrl(attachmentUrl);
|
|
|
|
|
+ record.setAiStatus("pending");
|
|
|
|
|
+ record.setCreatedAt(new Date());
|
|
|
|
|
+ reportFeedbackMapper.insert(record);
|
|
|
|
|
+ log.info("转人工客服工单已创建: id={}, userId={}", record.getId(), userId);
|
|
|
|
|
+ return Result.success(record);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 我的报告反馈列表(含 AI 解答),按创建时间倒序,最多 50 条。
|
|
* 我的报告反馈列表(含 AI 解答),按创建时间倒序,最多 50 条。
|
|
|
*/
|
|
*/
|
|
@@ -123,4 +201,56 @@ public class ReportFeedbackService {
|
|
|
List<ReportFeedback> list = reportFeedbackMapper.selectList(wrapper);
|
|
List<ReportFeedback> list = reportFeedbackMapper.selectList(wrapper);
|
|
|
return Result.success(list);
|
|
return Result.success(list);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /** 附件 >10MB 拒绝(400『附件不超过 10MB』,TC-P-83-E)。 */
|
|
|
|
|
+ private Result<?> checkAttachmentSize(Long attachmentSize) {
|
|
|
|
|
+ if (attachmentSize != null && attachmentSize > MAX_ATTACHMENT_SIZE) {
|
|
|
|
|
+ return Result.badRequest(MSG_ATTACHMENT_TOO_LARGE);
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 归属校验(TC-P-83-P):reportId/draftId 必须属于当前 userId,memberId 必须属于当前用户家庭;
|
|
|
|
|
+ * 否则统一 403 拒绝、不落库、不泄露他人数据(「越权」访问)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>user.familyId 为空表示当前上下文无法确定家庭(由 JwtInterceptor 按 userId+familyId 解析
|
|
|
|
|
+ * currentMemberId),此时跳过跨家庭判定,保持既有放行语义。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+ private Result<?> checkOwnership(Long userId, Long memberId, Long reportId, Long draftId, User user) {
|
|
|
|
|
+ if (reportId != null) {
|
|
|
|
|
+ HealthReport report = healthReportMapper.selectById(reportId);
|
|
|
|
|
+ if (report != null && !userId.equals(report.getUserId())) {
|
|
|
|
|
+ return Result.forbidden("越权访问:报告不属于当前用户");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (draftId != null) {
|
|
|
|
|
+ HealthReportDraft draft = healthReportDraftMapper.selectById(draftId);
|
|
|
|
|
+ if (draft != null && !userId.equals(draft.getUserId())) {
|
|
|
|
|
+ return Result.forbidden("越权访问:草稿不属于当前用户");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (memberId != null && user != null && user.getFamilyId() != null) {
|
|
|
|
|
+ FamilyMember member = familyMemberMapper.selectById(memberId);
|
|
|
|
|
+ if (member == null || !user.getFamilyId().equals(member.getFamilyId())) {
|
|
|
|
|
+ return Result.forbidden("越权访问:家庭成员不属于当前用户家庭");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 敏感信息脱敏(SC-P-83 / TC-P-83-E):18 位身份证与 11 位手机号仅保留前 3/6 后 4 位,
|
|
|
|
|
+ * 落库与 AI 请求中均不得出现原始明文。
|
|
|
|
|
+ */
|
|
|
|
|
+ private String maskSensitiveInfo(String content) {
|
|
|
|
|
+ if (content == null) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 身份证:18 位(末位可为 X),保留前 6 后 4
|
|
|
|
|
+ String masked = content.replaceAll("(?<!\\d)(\\d{6})\\d{8}([\\dXx]{4})(?!\\d)", "$1********$2");
|
|
|
|
|
+ // 手机号:1 开头的 11 位数字,保留前 3 后 4
|
|
|
|
|
+ masked = masked.replaceAll("(?<!\\d)(1\\d{2})\\d{4}(\\d{4})(?!\\d)", "$1****$2");
|
|
|
|
|
+ return masked;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|