|
@@ -10,6 +10,7 @@ import com.etotem.cfc.entity.FamilyMember;
|
|
|
import com.etotem.cfc.entity.FiveDimensionSelfCheck;
|
|
import com.etotem.cfc.entity.FiveDimensionSelfCheck;
|
|
|
import com.etotem.cfc.entity.HealthPlan;
|
|
import com.etotem.cfc.entity.HealthPlan;
|
|
|
import com.etotem.cfc.entity.HealthReport;
|
|
import com.etotem.cfc.entity.HealthReport;
|
|
|
|
|
+import com.etotem.cfc.entity.HealthStatus;
|
|
|
import com.etotem.cfc.entity.Product;
|
|
import com.etotem.cfc.entity.Product;
|
|
|
import com.etotem.cfc.entity.Task;
|
|
import com.etotem.cfc.entity.Task;
|
|
|
import com.etotem.cfc.entity.Family;
|
|
import com.etotem.cfc.entity.Family;
|
|
@@ -20,6 +21,7 @@ import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
import com.etotem.cfc.mapper.FiveDimensionSelfCheckMapper;
|
|
import com.etotem.cfc.mapper.FiveDimensionSelfCheckMapper;
|
|
|
import com.etotem.cfc.mapper.HealthPlanMapper;
|
|
import com.etotem.cfc.mapper.HealthPlanMapper;
|
|
|
import com.etotem.cfc.mapper.HealthReportMapper;
|
|
import com.etotem.cfc.mapper.HealthReportMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.HealthStatusMapper;
|
|
|
import com.etotem.cfc.mapper.ProductMapper;
|
|
import com.etotem.cfc.mapper.ProductMapper;
|
|
|
import com.etotem.cfc.mapper.TaskMapper;
|
|
import com.etotem.cfc.mapper.TaskMapper;
|
|
|
import com.etotem.cfc.mapper.UserMapper;
|
|
import com.etotem.cfc.mapper.UserMapper;
|
|
@@ -30,6 +32,7 @@ import com.etotem.cfc.service.CartService;
|
|
|
import com.etotem.cfc.service.HealthPlanService;
|
|
import com.etotem.cfc.service.HealthPlanService;
|
|
|
import com.etotem.cfc.service.ProductRecommendationService;
|
|
import com.etotem.cfc.service.ProductRecommendationService;
|
|
|
import com.etotem.cfc.service.api.WechatServiceInterface;
|
|
import com.etotem.cfc.service.api.WechatServiceInterface;
|
|
|
|
|
+import com.etotem.cfc.util.MedicalRedlineUtil;
|
|
|
import com.etotem.cfc.util.PlanContentSafetyUtil;
|
|
import com.etotem.cfc.util.PlanContentSafetyUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@@ -84,6 +87,10 @@ public class HealthPlanServiceImpl implements HealthPlanService {
|
|
|
@Resource
|
|
@Resource
|
|
|
private HealthReportMapper healthReportMapper;
|
|
private HealthReportMapper healthReportMapper;
|
|
|
|
|
|
|
|
|
|
+ /** §三 P1-1 / §8.1:用户已有诊断、在服药物(注入 health_plan_graph 的 medical_constraints) */
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private HealthStatusMapper healthStatusMapper;
|
|
|
|
|
+
|
|
|
@Resource
|
|
@Resource
|
|
|
private DanAssessmentResultMapper danAssessmentResultMapper;
|
|
private DanAssessmentResultMapper danAssessmentResultMapper;
|
|
|
|
|
|
|
@@ -2002,6 +2009,7 @@ public class HealthPlanServiceImpl implements HealthPlanService {
|
|
|
* SC-T-28 AI 方案内容安全复核(命中即拦截,fail-closed):
|
|
* SC-T-28 AI 方案内容安全复核(命中即拦截,fail-closed):
|
|
|
* <ol>
|
|
* <ol>
|
|
|
* <li>内置敏感词/违规医疗主张检测(基础闸门,无条件执行);</li>
|
|
* <li>内置敏感词/违规医疗主张检测(基础闸门,无条件执行);</li>
|
|
|
|
|
+ * <li>§八 8.1 医疗红线正则审计({@link MedicalRedlineUtil#matchRedline(String)},R1~R4 命中即拦截);</li>
|
|
|
* <li>外部内容安全原语 {@link WechatServiceInterface#checkText(String)} 叠加复核
|
|
* <li>外部内容安全原语 {@link WechatServiceInterface#checkText(String)} 叠加复核
|
|
|
* (可用时执行,true=安全、false=拦截;wechatService 未注入时跳过,不阻碍内置闸门)。</li>
|
|
* (可用时执行,true=安全、false=拦截;wechatService 未注入时跳过,不阻碍内置闸门)。</li>
|
|
|
* </ol>
|
|
* </ol>
|
|
@@ -2019,6 +2027,11 @@ public class HealthPlanServiceImpl implements HealthPlanService {
|
|
|
if (hit != null) {
|
|
if (hit != null) {
|
|
|
return "命中违规词「" + hit + "」";
|
|
return "命中违规词「" + hit + "」";
|
|
|
}
|
|
}
|
|
|
|
|
+ // §三 P1-1 + §八 8.1:医疗红线输出正则审计(R1~R4),命中即拦截发布(fail-closed)
|
|
|
|
|
+ String redline = MedicalRedlineUtil.matchRedline(text);
|
|
|
|
|
+ if (redline != null) {
|
|
|
|
|
+ return "命中医疗红线(§8.1 " + redline + ")";
|
|
|
|
|
+ }
|
|
|
if (wechatService != null && !wechatService.checkText(text)) {
|
|
if (wechatService != null && !wechatService.checkText(text)) {
|
|
|
return "第三方内容安全检测未通过";
|
|
return "第三方内容安全检测未通过";
|
|
|
}
|
|
}
|
|
@@ -2204,6 +2217,8 @@ public class HealthPlanServiceImpl implements HealthPlanService {
|
|
|
inputs.put("familyId", user.getFamilyId());
|
|
inputs.put("familyId", user.getFamilyId());
|
|
|
inputs.put("dimensions", String.join(",", lowDims));
|
|
inputs.put("dimensions", String.join(",", lowDims));
|
|
|
inputs.put("goal", "基于五维自检结果,针对低分维度制定改善计划");
|
|
inputs.put("goal", "基于五维自检结果,针对低分维度制定改善计划");
|
|
|
|
|
+ // §三 P1-1 / §8.1:Java 调用侧注入用户已有诊断、在服药物,供 prompt 约束
|
|
|
|
|
+ inputs.put("medical_constraints", buildMedicalConstraints(userId, null));
|
|
|
String planContent = aiGateway.generateHealthPlan(inputs);
|
|
String planContent = aiGateway.generateHealthPlan(inputs);
|
|
|
if (planContent == null) return null;
|
|
if (planContent == null) return null;
|
|
|
HealthPlan plan = new HealthPlan();
|
|
HealthPlan plan = new HealthPlan();
|
|
@@ -2315,6 +2330,8 @@ public class HealthPlanServiceImpl implements HealthPlanService {
|
|
|
body.put("member_ids", memberIds);
|
|
body.put("member_ids", memberIds);
|
|
|
body.put("dimensions", dimensions);
|
|
body.put("dimensions", dimensions);
|
|
|
body.put("goal", goal);
|
|
body.put("goal", goal);
|
|
|
|
|
+ // §三 P1-1 / §8.1:Java 调用侧注入用户已有诊断、在服药物,供 health_plan_graph 作 prompt 约束
|
|
|
|
|
+ body.put("medical_constraints", buildMedicalConstraints(null, memberIds));
|
|
|
String url = langgraphBaseUrl + "/api/v1/health/plan/generate";
|
|
String url = langgraphBaseUrl + "/api/v1/health/plan/generate";
|
|
|
ResponseEntity<String> response = restTemplate.postForEntity(url, body, String.class);
|
|
ResponseEntity<String> response = restTemplate.postForEntity(url, body, String.class);
|
|
|
if (response.getBody() != null && response.getStatusCode().is2xxSuccessful()) {
|
|
if (response.getBody() != null && response.getStatusCode().is2xxSuccessful()) {
|
|
@@ -2329,6 +2346,78 @@ public class HealthPlanServiceImpl implements HealthPlanService {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * §三 P1-1 / §8.1:构造调用侧输入字段 {@code medical_constraints}(用户已有诊断、在服药物)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>数据来源为 {@code health_status.disease_history / medications}(账号级优先,其次首个方案成员);
|
|
|
|
|
+ * 任何数据源不可用或读取失败时降级为空列表,保证请求体始终携带该字段(输入契约 fail-closed)。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+ private Map<String, Object> buildMedicalConstraints(Long userId, String memberIds) {
|
|
|
|
|
+ Map<String, Object> constraints = new LinkedHashMap<>();
|
|
|
|
|
+ List<String> diagnoses = new ArrayList<>();
|
|
|
|
|
+ List<String> medications = new ArrayList<>();
|
|
|
|
|
+ StringBuilder notes = new StringBuilder();
|
|
|
|
|
+ try {
|
|
|
|
|
+ HealthStatus status = null;
|
|
|
|
|
+ if (healthStatusMapper != null) {
|
|
|
|
|
+ if (userId != null) {
|
|
|
|
|
+ status = healthStatusMapper.selectOne(
|
|
|
|
|
+ new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<HealthStatus>()
|
|
|
|
|
+ .eq(HealthStatus::getUserId, userId)
|
|
|
|
|
+ .orderByDesc(HealthStatus::getUpdatedAt)
|
|
|
|
|
+ .last("LIMIT 1"));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (status == null && memberIds != null && !memberIds.trim().isEmpty()) {
|
|
|
|
|
+ String first = memberIds.split(",")[0].trim();
|
|
|
|
|
+ if (!first.isEmpty()) {
|
|
|
|
|
+ Long memberId = Long.valueOf(first);
|
|
|
|
|
+ status = healthStatusMapper.selectOne(
|
|
|
|
|
+ new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<HealthStatus>()
|
|
|
|
|
+ .eq(HealthStatus::getMemberId, memberId)
|
|
|
|
|
+ .orderByDesc(HealthStatus::getUpdatedAt)
|
|
|
|
|
+ .last("LIMIT 1"));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (status != null) {
|
|
|
|
|
+ diagnoses.addAll(parseMedicalNames(status.getDiseaseHistory()));
|
|
|
|
|
+ medications.addAll(parseMedicalNames(status.getMedications()));
|
|
|
|
|
+ if (status.getNotes() != null && !status.getNotes().trim().isEmpty()) {
|
|
|
|
|
+ notes.append(status.getNotes().trim());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("读取用户医疗约束失败(降级为空约束): {}", e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ constraints.put("diagnoses", diagnoses);
|
|
|
|
|
+ constraints.put("medications", medications);
|
|
|
|
|
+ constraints.put("notes", notes.toString());
|
|
|
|
|
+ return constraints;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 解析 health_status 中的 [{name,note}] JSON 为名称列表(兼容纯字符串数组),异常返回空列表。 */
|
|
|
|
|
+ private List<String> parseMedicalNames(String json) {
|
|
|
|
|
+ List<String> names = new ArrayList<>();
|
|
|
|
|
+ if (json == null || json.trim().isEmpty()) {
|
|
|
|
|
+ return names;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ JsonNode arr = objectMapper.readTree(json);
|
|
|
|
|
+ if (arr != null && arr.isArray()) {
|
|
|
|
|
+ for (JsonNode node : arr) {
|
|
|
|
|
+ String name = node.isTextual() ? node.asText()
|
|
|
|
|
+ : (node.has("name") ? node.get("name").asText() : null);
|
|
|
|
|
+ if (name != null && !name.trim().isEmpty() && !names.contains(name.trim())) {
|
|
|
|
|
+ names.add(name.trim());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("解析医疗约束JSON失败: {}", e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ return names;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private String buildPlanJsonFromData(JsonNode data) {
|
|
private String buildPlanJsonFromData(JsonNode data) {
|
|
|
try {
|
|
try {
|
|
|
// 多人方案:原样透传 common + individual 结构(与前端保存的 planJson 一致),
|
|
// 多人方案:原样透传 common + individual 结构(与前端保存的 planJson 一致),
|