From 69a3c374ca3afb770e3b9ffcbdda07ce362cbf58 Mon Sep 17 00:00:00 2001
From: 1 <1@123>
Date: 星期五, 09 一月 2026 19:59:29 +0800
Subject: [PATCH] #

---
 rsf-open-api/src/main/java/com/vincent/rsf/openApi/utils/ParamsMapUtils.java |  273 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 273 insertions(+), 0 deletions(-)

diff --git a/rsf-open-api/src/main/java/com/vincent/rsf/openApi/utils/ParamsMapUtils.java b/rsf-open-api/src/main/java/com/vincent/rsf/openApi/utils/ParamsMapUtils.java
new file mode 100644
index 0000000..302e515
--- /dev/null
+++ b/rsf-open-api/src/main/java/com/vincent/rsf/openApi/utils/ParamsMapUtils.java
@@ -0,0 +1,273 @@
+package com.vincent.rsf.openApi.utils;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.vincent.rsf.openApi.entity.app.ApiFunction;
+import com.vincent.rsf.openApi.entity.app.ApiMap;
+import com.vincent.rsf.openApi.entity.app.App;
+import com.vincent.rsf.openApi.service.ApiMapService;
+import com.vincent.rsf.openApi.service.ApiFunctionService;
+import com.vincent.rsf.openApi.service.AppService;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.tika.utils.StringUtils;
+import org.springframework.boot.context.event.ApplicationReadyEvent;
+import org.springframework.context.event.EventListener;
+import org.springframework.stereotype.Component;
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 鎺ュ彛瀛楁鍔ㄦ�佹槧灏勫伐鍏风被
+ * 鏀寔浠庢暟鎹簱鍔犺浇鏄犲皠閰嶇疆锛屽苟鎻愪緵瀛楁杞崲鍔熻兘
+ *
+ * @author vincent
+ * @since 2026-01-04
+ */
+@Slf4j
+@Component
+@RequiredArgsConstructor
+public class ParamsMapUtils {
+
+    private final AppService appService;
+    private final ApiFunctionService functionService;
+    private final ApiMapService mapService;
+
+    // 缂撳瓨鍒板唴瀛�
+    public static List<App> APPS = new ArrayList<>();
+    public static List<ApiFunction> FUNCTIONS = new ArrayList<>();
+    public static List<ApiMap> ATTRIBUTE_MAPS = new ArrayList<>();
+
+    /**
+     * 搴旂敤瀹屽叏鍚姩鍚庤嚜鍔ㄥ姞杞介厤缃�
+     * 浣跨敤 ApplicationReadyEvent 纭繚 Spring 瀹瑰櫒瀹屽叏鍒濆鍖�
+     */
+    @EventListener(ApplicationReadyEvent.class)
+    public void init() {
+        log.info("=============== 寮�濮嬪姞杞芥帴鍙f槧灏勯厤缃� ===============");
+        try {
+            appService.refreshCache();
+            functionService.refreshCache();
+            mapService.refreshCache();
+
+            log.info("鎺ュ彛鏄犲皠閰嶇疆鍔犺浇瀹屾垚 - 搴旂敤鏁�:{}, 鍔熻兘鏁�:{}, 鏄犲皠瑙勫垯鏁�:{}",
+                    APPS.size(), FUNCTIONS.size(), ATTRIBUTE_MAPS.size());
+        } catch (Exception e) {
+            log.error("鎺ュ彛鏄犲皠閰嶇疆鍔犺浇澶辫触", e);
+        }
+    }
+
+    /**
+     * 鎵嬪姩鍒锋柊鎵�鏈夌紦瀛�
+     */
+    public void refreshAll() {
+        log.info("鎵嬪姩鍒锋柊鎵�鏈夋槧灏勭紦瀛�...");
+        appService.refreshCache();
+        functionService.refreshCache();
+        mapService.refreshCache();
+    }
+
+    /**
+     * 鎵ц瀛楁鏄犲皠杞崲
+     *
+     * @param appId 搴旂敤ID
+     * @param funcId 鍔熻兘ID
+     * @param params 鍘熷鍙傛暟
+     * @return 杞崲鍚庣殑鍙傛暟
+     */
+    public static JSONObject apiMaps(String appId, String funcId, JSONObject params) {
+        if (params == null || params.isEmpty()) {
+            return params;
+        }
+
+        // 1銆佽幏鍙栨槧灏勮〃
+        List<ApiMap> maps = ATTRIBUTE_MAPS.stream()
+                .filter(map -> map.getAppId().equals(appId) && map.getFuncId().equals(funcId))
+                .toList();
+
+        if (maps.isEmpty()) {
+            log.debug("鏈壘鍒版槧灏勯厤缃� - appId:{}, funcId:{}", appId, funcId);
+            return params;
+        }
+
+        // 2銆佹瀯寤烘槧灏勮鍒橫ap
+        Map<String, String> mappingRules = new HashMap<>();
+        for (ApiMap map : maps) {
+            String sourceAttribute = map.getSourceAttribute();
+            String targetAttribute = map.getTargetAttribute();
+            if (!StringUtils.isBlank(sourceAttribute) && !StringUtils.isBlank(targetAttribute)) {
+                mappingRules.put(sourceAttribute, targetAttribute);
+            }
+        }
+
+        // 3銆侀�掑綊鏇挎崲鎵�鏈夊眰绾х殑灞炴�у悕绉�
+        JSONObject result = replaceKeysRecursive(params, mappingRules);
+
+        return result;
+    }
+
+    /**
+     * 閫掑綊鏇挎崲JSON鎵�鏈夊眰绾х殑灞炴�у悕绉�
+     * 鏀寔宓屽瀵硅薄鍜屾暟缁勭殑娣卞害閬嶅巻
+     *
+     * @param json 鍘熷JSON瀵硅薄
+     * @param mappingRules 鏄犲皠瑙勫垯 (婧愬瓧娈靛悕 -> 鐩爣瀛楁鍚�)
+     * @return 鏇挎崲鍚庣殑JSON瀵硅薄
+     */
+    public static JSONObject replaceKeysRecursive(JSONObject json, Map<String, String> mappingRules) {
+        if (json == null || json.isEmpty()) {
+            return json;
+        }
+
+        JSONObject result = new JSONObject();
+
+        for (String key : json.keySet()) {
+            Object value = json.get(key);
+
+            // 纭畾鏂扮殑閿悕锛堝鏋滄湁鏄犲皠瑙勫垯鍒欎娇鐢ㄦ槧灏勫悗鐨勫悕绉帮級
+            String newKey = mappingRules.getOrDefault(key, key);
+
+            if (value instanceof JSONObject) {
+                // 閫掑綊澶勭悊宓屽瀵硅薄
+                JSONObject nestedResult = replaceKeysRecursive((JSONObject) value, mappingRules);
+                result.put(newKey, nestedResult);
+                log.debug("鏇挎崲瀵硅薄瀛楁: {} -> {}", key, newKey);
+
+            } else if (value instanceof JSONArray) {
+                // 閫掑綊澶勭悊鏁扮粍
+                JSONArray arrayResult = replaceKeysInArray((JSONArray) value, mappingRules);
+                result.put(newKey, arrayResult);
+                log.debug("鏇挎崲鏁扮粍瀛楁: {} -> {}", key, newKey);
+
+            } else {
+                // 鏅�氬�肩洿鎺ヨ祴鍊�
+                Object convertedValue = convertValue(value, newKey);
+                result.put(newKey, convertedValue);
+                if (!key.equals(newKey)) {
+                    log.debug("鏇挎崲瀛楁: {} -> {} (鍊�: {})", key, newKey, convertedValue);
+                }
+            }
+        }
+
+        return result;
+    }
+
+    /**
+     * 閫掑綊澶勭悊JSON鏁扮粍涓殑鎵�鏈夊厓绱�
+     *
+     * @param array 鍘熷JSON鏁扮粍
+     * @param mappingRules 鏄犲皠瑙勫垯
+     * @return 澶勭悊鍚庣殑JSON鏁扮粍
+     */
+    private static JSONArray replaceKeysInArray(JSONArray array, Map<String, String> mappingRules) {
+        if (array == null || array.isEmpty()) {
+            return array;
+        }
+
+        JSONArray result = new JSONArray();
+
+        for (int i = 0; i < array.size(); i++) {
+            Object element = array.get(i);
+
+            if (element instanceof JSONObject) {
+                // 鏁扮粍鍏冪礌鏄璞★紝閫掑綊澶勭悊
+                JSONObject replacedObject = replaceKeysRecursive((JSONObject) element, mappingRules);
+                result.add(replacedObject);
+
+            } else if (element instanceof JSONArray) {
+                // 鏁扮粍鍏冪礌鏄暟缁勶紝閫掑綊澶勭悊
+                JSONArray replacedArray = replaceKeysInArray((JSONArray) element, mappingRules);
+                result.add(replacedArray);
+
+            } else {
+                // 鍩烘湰绫诲瀷锛岀洿鎺ユ坊鍔�
+                result.add(element);
+            }
+        }
+
+        return result;
+    }
+
+    /**
+     * 閫氱敤鐨凧SON灞炴�у悕鏇挎崲鏂规硶锛堝澶栨彁渚涳級
+     * 鍙互鐩存帴浼犲叆鏄犲皠瑙勫垯杩涜鏇挎崲
+     *
+     * @param json 鍘熷JSON瀵硅薄
+     * @param mappingRules 鏄犲皠瑙勫垯 Map<婧愬瓧娈靛悕, 鐩爣瀛楁鍚�>
+     * @return 鏇挎崲鍚庣殑JSON瀵硅薄
+     */
+    public static JSONObject replaceJsonKeys(JSONObject json, Map<String, String> mappingRules) {
+        return replaceKeysRecursive(json, mappingRules);
+    }
+
+    /**
+     * 閫氱敤鐨凧SON鏁扮粍灞炴�у悕鏇挎崲鏂规硶锛堝澶栨彁渚涳級
+     *
+     * @param array 鍘熷JSON鏁扮粍
+     * @param mappingRules 鏄犲皠瑙勫垯 Map<婧愬瓧娈靛悕, 鐩爣瀛楁鍚�>
+     * @return 鏇挎崲鍚庣殑JSON鏁扮粍
+     */
+    public static JSONArray replaceJsonArrayKeys(JSONArray array, Map<String, String> mappingRules) {
+        return replaceKeysInArray(array, mappingRules);
+    }
+
+
+    /**
+     * 鍊肩被鍨嬭浆鎹紙鍙墿灞曪級
+     *
+     * @param value 鍘熷鍊�
+     * @param targetField 鐩爣瀛楁鍚�
+     * @return 杞崲鍚庣殑鍊�
+     */
+    private static Object convertValue(Object value, String targetField) {
+        if (value == null) {
+            return null;
+        }
+
+        // 杩欓噷鍙互鏍规嵁闇�瑕佹坊鍔犳洿澶氱被鍨嬭浆鎹㈤�昏緫
+        // 渚嬪锛氭棩鏈熸牸寮忚浆鎹€�佹暟瀛楃簿搴﹁浆鎹㈢瓑
+
+        // 绀轰緥锛氬鏋滃瓧娈靛悕鍖呭惈amount銆乸rice绛夛紝杞崲涓築igDecimal
+        String lowerField = targetField.toLowerCase();
+        if ((lowerField.contains("amount") || lowerField.contains("price")
+                || lowerField.contains("qty")) && value instanceof String) {
+            try {
+                return new BigDecimal(value.toString());
+            } catch (Exception e) {
+                log.warn("鏁板瓧杞崲澶辫触: {} -> {}", value, targetField);
+                return value;
+            }
+        }
+
+        return value;
+    }
+
+    /**
+     * 鑾峰彇搴旂敤淇℃伅
+     *
+     * @param appId 搴旂敤ID
+     * @return 搴旂敤淇℃伅
+     */
+    public static App getApp(String appId) {
+        return APPS.stream()
+                .filter(app -> app.getId().equals(appId))
+                .findFirst()
+                .orElse(null);
+    }
+
+    /**
+     * 鑾峰彇鍔熻兘淇℃伅
+     *
+     * @param funcId 鍔熻兘ID
+     * @return 鍔熻兘淇℃伅
+     */
+    public static ApiFunction getFunction(String funcId) {
+        return FUNCTIONS.stream()
+                .filter(func -> func.getId().equals(funcId))
+                .findFirst()
+                .orElse(null);
+    }
+}

--
Gitblit v1.9.1