|
|
@@ -78,6 +78,68 @@ public class DifyService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Invoke Dify Chatflow for relation analysis (v20260604.2 redesign).
|
|
|
+ * Uses POST /v1/chat-messages with member_snapshot as input context.
|
|
|
+ * Falls back to mock response when Dify is unavailable.
|
|
|
+ */
|
|
|
+ public String invokeRelationChatflow(String memberSnapshot, String query, String userId) {
|
|
|
+ if (isMockMode()) {
|
|
|
+ return generateMockRelationChatflowResponse(memberSnapshot, query);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String chatflowAppId = configService.getValue("dify.chatflow.relation_app_id", "");
|
|
|
+ JsonObject body = new JsonObject();
|
|
|
+ body.addProperty("query", query);
|
|
|
+ body.addProperty("user", userId);
|
|
|
+ Map<String, Object> inputs = new HashMap<>();
|
|
|
+ inputs.put("member_snapshot", memberSnapshot);
|
|
|
+ body.add("inputs", gson.toJsonTree(inputs));
|
|
|
+
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.set("Authorization", "Bearer " + apiKey);
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ HttpEntity<String> entity = new HttpEntity<>(body.toString(), headers);
|
|
|
+
|
|
|
+ String response = restTemplate.postForObject(baseUrl + "/chat-messages", entity, String.class);
|
|
|
+ JsonObject json = gson.fromJson(response, JsonObject.class);
|
|
|
+ return json.get("answer").getAsString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("invokeRelationChatflow failed, falling back to mock: {}", e.getMessage());
|
|
|
+ return generateMockRelationChatflowResponse(memberSnapshot, query);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Generate mock response for relation analysis chatflow.
|
|
|
+ */
|
|
|
+ private String generateMockRelationChatflowResponse(String memberSnapshot, String query) {
|
|
|
+ try {
|
|
|
+ JsonArray members = gson.fromJson(memberSnapshot, JsonArray.class);
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append("🔮 关系能量分析\n\n");
|
|
|
+ sb.append("参与成员:\n");
|
|
|
+ for (int i = 0; i < members.size(); i++) {
|
|
|
+ JsonObject member = members.get(i).getAsJsonObject();
|
|
|
+ String name = getString(member, "name", "未知");
|
|
|
+ String relLabel = getString(member, "relationTypeLabel", "");
|
|
|
+ sb.append("• ").append(name);
|
|
|
+ if (!relLabel.isEmpty() && !"本人".equals(relLabel)) {
|
|
|
+ sb.append("(").append(relLabel).append(")");
|
|
|
+ }
|
|
|
+ sb.append("\n");
|
|
|
+ }
|
|
|
+ sb.append("\n您的问题:").append(query != null ? query : "").append("\n\n");
|
|
|
+ sb.append("根据成员能量盘分析,各方数字能量存在一定的互补关系。");
|
|
|
+ sb.append("主性格数字之间的互动将影响关系发展走向。");
|
|
|
+ sb.append("建议关注数字能量的平衡与协调,充分发挥各自优势。\n\n");
|
|
|
+ sb.append("💡 以上为系统初步分析,详细解读请使用完整功能。");
|
|
|
+ return sb.toString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ return "🔮 关系能量分析已启动。请继续提问获取更详细的解读。";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Invoke Dify Workflow (for structured interpretation).
|
|
|
* Uses POST /v1/workflows/run endpoint.
|
|
|
@@ -263,6 +325,156 @@ public class DifyService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // ── Relationship Analysis (EPIC 10) ──────────────────────────────────────────
|
|
|
+
|
|
|
+ public String invokeRelationWorkflow(Map<String, Object> inputs, String userId) {
|
|
|
+ if (isMockMode()) {
|
|
|
+ return generateMockRelationInterpretation(inputs);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String workflowAppId = configService.getValue("dify.workflow.relation_app_id", "");
|
|
|
+ JsonObject body = new JsonObject();
|
|
|
+ body.addProperty("app_id", workflowAppId);
|
|
|
+ body.add("inputs", gson.toJsonTree(inputs));
|
|
|
+ body.addProperty("user", userId);
|
|
|
+
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.set("Authorization", "Bearer " + apiKey);
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ HttpEntity<String> entity = new HttpEntity<>(body.toString(), headers);
|
|
|
+
|
|
|
+ String response = restTemplate.postForObject(baseUrl + "/workflows/run", entity, String.class);
|
|
|
+ JsonObject json = gson.fromJson(response, JsonObject.class);
|
|
|
+ JsonObject data = json.getAsJsonObject("data");
|
|
|
+ if (data != null && data.has("outputs")) {
|
|
|
+ return data.get("outputs").toString();
|
|
|
+ }
|
|
|
+ return json.toString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("invokeRelationWorkflow failed, falling back to mock: {}", e.getMessage());
|
|
|
+ return generateMockRelationInterpretation(inputs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ String generateMockRelationInterpretation(Map<String, Object> inputs) {
|
|
|
+ try {
|
|
|
+ String ctx1 = (String) inputs.get("chart_context_user1");
|
|
|
+ String ctx2 = (String) inputs.get("chart_context_user2");
|
|
|
+ String relType = (String) inputs.get("relation_type");
|
|
|
+
|
|
|
+ JsonObject chart1 = gson.fromJson(ctx1, JsonObject.class);
|
|
|
+ JsonObject chart2 = gson.fromJson(ctx2, JsonObject.class);
|
|
|
+
|
|
|
+ JsonObject pos1 = chart1.getAsJsonObject("positions");
|
|
|
+ if (pos1 == null) pos1 = chart1;
|
|
|
+ JsonObject pos2 = chart2.getAsJsonObject("positions");
|
|
|
+ if (pos2 == null) pos2 = chart2;
|
|
|
+
|
|
|
+ int O1 = getInt(pos1, "O", 7);
|
|
|
+ int O2 = getInt(pos2, "O", 7);
|
|
|
+
|
|
|
+ String relLabel = relType != null ? relType : "合作";
|
|
|
+
|
|
|
+ JsonObject result = new JsonObject();
|
|
|
+ result.addProperty("compatibility",
|
|
|
+ "双方命盘有较强的互补性,主性格数字" + O1 + "和" + O2 + "形成" + (O1 + O2) + "组合," +
|
|
|
+ "在" + relLabel + "关系中能够相互促进、共同成长。");
|
|
|
+ JsonArray strengths = new JsonArray();
|
|
|
+ strengths.add("沟通顺畅");
|
|
|
+ strengths.add("价值观相近");
|
|
|
+ result.add("strengths", strengths);
|
|
|
+ JsonArray challenges = new JsonArray();
|
|
|
+ challenges.add("决策方式不同");
|
|
|
+ result.add("challenges", challenges);
|
|
|
+ result.addProperty("advice", "建议在重大决策时多倾听对方意见,发挥各自优势,实现共赢。");
|
|
|
+ return result.toString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ JsonObject fallback = new JsonObject();
|
|
|
+ fallback.addProperty("compatibility", "双方命盘有较强的互补性,请使用完整功能获取详细关系解读。");
|
|
|
+ fallback.addProperty("advice", "建议在重大决策时多倾听对方意见。");
|
|
|
+ return fallback.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ── Number Analysis (EPIC 10) ───────────────────────────────────────────────
|
|
|
+
|
|
|
+ public String invokeNumberAnalysisWorkflow(Map<String, Object> inputs, String userId) {
|
|
|
+ if (isMockMode()) {
|
|
|
+ return generateMockNumberAnalysisInterpretation(inputs);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String workflowAppId = configService.getValue("dify.workflow.number_analysis_app_id", "");
|
|
|
+ JsonObject body = new JsonObject();
|
|
|
+ body.addProperty("app_id", workflowAppId);
|
|
|
+ body.add("inputs", gson.toJsonTree(inputs));
|
|
|
+ body.addProperty("user", userId);
|
|
|
+
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.set("Authorization", "Bearer " + apiKey);
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ HttpEntity<String> entity = new HttpEntity<>(body.toString(), headers);
|
|
|
+
|
|
|
+ String response = restTemplate.postForObject(baseUrl + "/workflows/run", entity, String.class);
|
|
|
+ JsonObject json = gson.fromJson(response, JsonObject.class);
|
|
|
+ JsonObject data = json.getAsJsonObject("data");
|
|
|
+ if (data != null && data.has("outputs")) {
|
|
|
+ return data.get("outputs").toString();
|
|
|
+ }
|
|
|
+ return json.toString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("invokeNumberAnalysisWorkflow failed, falling back to mock: {}", e.getMessage());
|
|
|
+ return generateMockNumberAnalysisInterpretation(inputs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ String generateMockNumberAnalysisInterpretation(Map<String, Object> inputs) {
|
|
|
+ try {
|
|
|
+ String phone = (String) inputs.get("phone_number");
|
|
|
+ String starCombosStr = (String) inputs.get("star_combinations");
|
|
|
+
|
|
|
+ String phoneDisplay = phone != null ? phone : "未知号码";
|
|
|
+
|
|
|
+ JsonObject result = new JsonObject();
|
|
|
+ result.addProperty("overall", "该号码" + phoneDisplay + "整体能量偏向中和,磁场分布较为均衡。");
|
|
|
+ result.addProperty("suitability", "适合从事与人沟通、服务相关的行业。");
|
|
|
+ result.addProperty("recommendation", "号码整体较好,但需注意负面组合的影响,建议结合个人命盘综合判断。");
|
|
|
+
|
|
|
+ if (starCombosStr != null && !starCombosStr.isEmpty()) {
|
|
|
+ try {
|
|
|
+ JsonArray combos = gson.fromJson(starCombosStr, JsonArray.class);
|
|
|
+ result.add("starCombinations", combos);
|
|
|
+ } catch (Exception e) {
|
|
|
+ JsonArray defaultCombos = new JsonArray();
|
|
|
+ JsonObject sample = new JsonObject();
|
|
|
+ sample.addProperty("pair", "13");
|
|
|
+ sample.addProperty("starType", "TIAN_YI");
|
|
|
+ sample.addProperty("level", 1);
|
|
|
+ sample.addProperty("meaning", "天医一级,主财富与姻缘,正能量的数字组合。");
|
|
|
+ defaultCombos.add(sample);
|
|
|
+ result.add("starCombinations", defaultCombos);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ JsonArray defaultCombos = new JsonArray();
|
|
|
+ JsonObject sample = new JsonObject();
|
|
|
+ sample.addProperty("pair", "13");
|
|
|
+ sample.addProperty("starType", "TIAN_YI");
|
|
|
+ sample.addProperty("level", 1);
|
|
|
+ sample.addProperty("meaning", "天医一级,主财富与姻缘,正能量的数字组合。");
|
|
|
+ defaultCombos.add(sample);
|
|
|
+ result.add("starCombinations", defaultCombos);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result.toString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ JsonObject fallback = new JsonObject();
|
|
|
+ fallback.addProperty("overall", "该号码整体能量偏向中和,请使用完整功能获取详细号码分析。");
|
|
|
+ fallback.addProperty("recommendation", "建议结合个人命盘综合判断。");
|
|
|
+ return fallback.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private String generateAcademicTalent(int O, int I, int J, int K, int L) {
|
|
|
String mainDesc = NUMBER_DESC.getOrDefault(O > 9 ? 0 : O,
|
|
|
O > 9 ? "卓越数能量,蕴含强大潜力" : "能量独特,需结合整体命盘解读");
|