|
|
@@ -11,6 +11,7 @@ import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Service
|
|
|
@@ -24,10 +25,12 @@ public class DifyService {
|
|
|
|
|
|
private final RestTemplate restTemplate;
|
|
|
private final Gson gson;
|
|
|
+ private final ConfigService configService;
|
|
|
|
|
|
- public DifyService(RestTemplate restTemplate, Gson gson) {
|
|
|
+ public DifyService(RestTemplate restTemplate, Gson gson, ConfigService configService) {
|
|
|
this.restTemplate = restTemplate;
|
|
|
this.gson = gson;
|
|
|
+ this.configService = configService;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -66,6 +69,125 @@ public class DifyService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Invoke Dify Workflow (for structured interpretation).
|
|
|
+ * Uses POST /v1/workflows/run endpoint.
|
|
|
+ * Returns the workflow outputs as JSON string.
|
|
|
+ */
|
|
|
+ public String invokeWorkflow(Map<String, Object> inputs, String userId) {
|
|
|
+ if (isMockMode()) {
|
|
|
+ return generateMockInterpretation(inputs);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String workflowAppId = configService.getValue("dify.workflow.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) {
|
|
|
+ return generateMockInterpretation(inputs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Generate a structured mock interpretation JSON from chart context.
|
|
|
+ * Sections: mainCharacter, left (21-40), top (41-60), right (61+).
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ String generateMockInterpretation(Map<String, Object> inputs) {
|
|
|
+ try {
|
|
|
+ String chartContext = (String) inputs.get("chart_context");
|
|
|
+ JsonObject chart = gson.fromJson(chartContext, JsonObject.class);
|
|
|
+ JsonObject pos = chart.getAsJsonObject("positions");
|
|
|
+ if (pos == null) pos = chart;
|
|
|
+
|
|
|
+ int O = getInt(pos, "O", 7);
|
|
|
+ int P = getInt(pos, "P", 5);
|
|
|
+ int Q = getInt(pos, "Q", 0);
|
|
|
+ int R = getInt(pos, "R", 0);
|
|
|
+ int V = getInt(pos, "V", 8);
|
|
|
+ int W = getInt(pos, "W", 0);
|
|
|
+ int X = getInt(pos, "X", 0);
|
|
|
+ int S = getInt(pos, "S", 9);
|
|
|
+ int T = getInt(pos, "T", 0);
|
|
|
+ int U = getInt(pos, "U", 0);
|
|
|
+
|
|
|
+ JsonObject result = new JsonObject();
|
|
|
+
|
|
|
+ // 主性格解读
|
|
|
+ JsonObject mainCharacter = new JsonObject();
|
|
|
+ mainCharacter.addProperty("number", O);
|
|
|
+ mainCharacter.addProperty("title", "主性格解读");
|
|
|
+ mainCharacter.addProperty("content", generateMainInterpretation(O));
|
|
|
+ result.add("mainCharacter", mainCharacter);
|
|
|
+
|
|
|
+ // 左区(21-40岁)
|
|
|
+ JsonObject left = new JsonObject();
|
|
|
+ left.addProperty("number", P);
|
|
|
+ left.addProperty("title", "左区(21-40岁)");
|
|
|
+ left.addProperty("content", generateZoneInterpretation("左区(青年发展期)", P, Q, R));
|
|
|
+ result.add("left", left);
|
|
|
+
|
|
|
+ // 顶部(41-60岁)
|
|
|
+ JsonObject top = new JsonObject();
|
|
|
+ top.addProperty("number", V);
|
|
|
+ top.addProperty("title", "顶部(41-60岁)");
|
|
|
+ top.addProperty("content", generateZoneInterpretation("顶部(中年发展期)", V, W, X));
|
|
|
+ result.add("top", top);
|
|
|
+
|
|
|
+ // 右区(61+岁)
|
|
|
+ JsonObject right = new JsonObject();
|
|
|
+ right.addProperty("number", S);
|
|
|
+ right.addProperty("title", "右区(61+岁)");
|
|
|
+ right.addProperty("content", generateZoneInterpretation("右区(成熟发展期)", S, T, U));
|
|
|
+ result.add("right", right);
|
|
|
+
|
|
|
+ return result.toString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ return "{\"mainCharacter\":{\"number\":7,\"title\":\"主性格解读\",\"content\":\"请查看完整命盘以获取详细解读。\"}}";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String generateMainInterpretation(int number) {
|
|
|
+ String desc = NUMBER_DESC.getOrDefault(number,
|
|
|
+ number > 9 ? "卓越数能量,蕴含强大潜力" : "能量独特,需结合整体命盘解读");
|
|
|
+ return String.format(
|
|
|
+ "您的核心数字为 %d,代表「%s」。\n\n" +
|
|
|
+ "此数字奠定了您整体的性格基调与人生轨迹方向。" +
|
|
|
+ "在决策、人际和事业发展中,%d 的能量将持续发挥作用。" +
|
|
|
+ "建议深入了解此数字的优势与调整方向,以更好地发挥天赋潜能。",
|
|
|
+ number, desc, number);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String generateZoneInterpretation(String zoneName, int a, int b, int c) {
|
|
|
+ String desc = NUMBER_DESC.getOrDefault(a,
|
|
|
+ a > 9 ? "卓越数能量" : "独特能量组合");
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append(zoneName).append("的能量数字为 ").append(a);
|
|
|
+ if (b != 0) sb.append(" → ").append(b);
|
|
|
+ if (c != 0) sb.append(" → ").append(c);
|
|
|
+ sb.append(",核心数字 ").append(a).append(" 代表「").append(desc).append("」。\n\n");
|
|
|
+ sb.append("此阶段您在以下方面将有明显体现:\n");
|
|
|
+ sb.append("• 个人成长与发展方向\n");
|
|
|
+ sb.append("• 人际关系与社交模式\n");
|
|
|
+ sb.append("• 事业机遇与挑战\n\n");
|
|
|
+ sb.append("建议关注数字 ").append(a).append(" 的正向特质,同时觉察其过度或不足带来的影响。");
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
// ── Mock response generation ──────────────────────────────────────────────
|
|
|
|
|
|
/** Number descriptions for mock interpretations. */
|