Browse Source

添加报表分析测试类

lhy 1 year ago
parent
commit
4276d6693b

+ 160 - 0
industry-system/industry-da/src/main/test/com/example/opc_da/IndustryDaApplicationTest.java

@@ -0,0 +1,160 @@
+package com.example.opc_da;
+
+import cn.hutool.json.JSONUtil;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.example.opc_common.entity.ReportTable;
+import com.example.opc_common.util.Blank;
+import com.example.opc_da.config.SpringContextUtils;
+import com.example.opc_da.dao.ReportTableDao;
+import lombok.extern.slf4j.Slf4j;
+import org.jetbrains.annotations.NotNull;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import javax.annotation.Resource;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@Slf4j
+public class IndustryDaApplicationTest {
+
+    @Resource
+    private ReportTableDao reportTableDao;
+
+
+    @Before
+    public void setUp() throws Exception {
+    }
+
+    /**
+     * 从报表中分析报表、策略、点位关系
+     * @return
+     */
+    public List<Map<String, Object>> getReportTableIdAndPolicyIdAndItemIdToMapList(){
+        String reportTableId = "1775000193725501440";
+        ReportTable reportTable = reportTableDao.getReportTableById(reportTableId);
+
+        List<Map<String, Object>> resultList = new ArrayList<>();
+
+        //解析自动报表结构
+        String reportTableDataStr = reportTable.getReportTableData();
+        JSONObject reportTableData = JSONObject.parseObject(reportTableDataStr);
+
+        //得到sheet数据
+        JSONObject sheet = reportTableData.getJSONObject("sheet");
+        reportTableIdAndPolicyIdAndItemIdToMapListBySheetData(resultList, reportTableId, sheet);
+
+        //得到chart数据
+        JSONObject chart = reportTableData.getJSONObject("chart");
+        reportTableIdAndPolicyIdAndItemIdToMapListByChartData(resultList, reportTableId, chart);
+
+        // 去重排序
+        resultList = resultList.stream().map((map) ->
+                map.getOrDefault("reportTableId","")
+                        + "-" + map.getOrDefault("policyId","")
+                        + "-" + map.getOrDefault("itemId","")
+        ).distinct().sorted().map((str)->{
+            String[] split = str.split("-");
+            Map<String, Object> map = new HashMap<>();
+            map.put("reportTableId", split[0]);
+            map.put("policyId", split[1]);
+            map.put("itemId", split[2]);
+            return map;
+        }).collect(Collectors.toList());
+
+        log.info("-------------------resultList");
+        log.info(JSONUtil.toJsonStr(resultList));
+
+        return resultList;
+    }
+
+    /**
+     * 遍历sheet所有单元格,获取报表ID,策略ID,点位ID关联关系的list<Map<String, Object>>
+     *
+     * @param resultList
+     * @param reportTableId
+     * @param sheet
+     * @return
+     */
+    private void reportTableIdAndPolicyIdAndItemIdToMapListBySheetData(
+            List<Map<String, Object>> resultList, String reportTableId, JSONObject sheet) {
+        Map<String, Object> resultMap = null;
+        try {
+            JSONArray sheetData = sheet.getJSONArray("data");
+            for (int i = 0; i < sheetData.size(); i++) {
+                JSONArray row = sheetData.getJSONArray(i);
+                for (int j = 0; j < row.size(); j++) {
+                    JSONObject rowCol = row.getJSONObject(j);
+                    if (Blank.isNotEmpty(rowCol)) {
+                        String v = rowCol.getString("v");
+                        if(JSONUtil.isJsonObj(v)){
+                            JSONObject rowColJson = JSONObject.parseObject(v);
+                            //String tableId = rowColJson.getString("tableId");
+                            String type = rowColJson.getString("type");
+                            if (type.equals("data")) {
+                                JSONObject rowColData = rowColJson.getJSONObject("data");
+                                resultMap = new HashMap<>();
+                                resultMap.put("reportTableId", reportTableId);
+                                resultMap.put("policyId", rowColData.getString("policyId"));
+                                resultMap.put("itemId", rowColData.getString("itemId"));
+                                resultList.add(resultMap);
+                            }
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            log.error("获取报表ID,策略ID,点位ID关联关系错误", e);
+        }
+    }
+
+    /**
+     * 遍历统计图配置信息,获取报表ID,策略ID,点位ID关联关系的list<Map<String, Object>>
+     *
+     * @param resultList
+     * @param reportTableId
+     * @param chart
+     * @return
+     */
+    private void reportTableIdAndPolicyIdAndItemIdToMapListByChartData(
+            List<Map<String, Object>> resultList, String reportTableId, JSONObject chart) {
+        try {
+            for (String key : chart.keySet()) {
+                JSONObject chartData = chart.getJSONObject(key);
+                JSONObject dataForm = chartData.getJSONObject("dataForm");
+                JSONArray items = chartData.getJSONArray("items");
+                String policyId = dataForm.getString("chart-item-group");
+                //获取统计图配置的所有点位
+                List<Map<String, Object>>  chartDataList = items.stream().map(v -> {
+                    String itemId = ((JSONObject) v).getString("value");
+                    Map<String, Object> resultMap = new HashMap<>();
+                    resultMap.put("reportTableId", reportTableId);
+                    resultMap.put("policyId", policyId);
+                    resultMap.put("itemId", itemId);
+                    return resultMap;
+                }).collect(Collectors.toList());
+//              log.info("-------------------chartDataList");
+//              log.info(JSONUtil.toJsonStr(chartDataList));
+                resultList.addAll(chartDataList);
+            }
+        } catch (Exception e) {
+            log.error("获取报表ID,策略ID,点位ID关联关系错误", e);
+        }
+    }
+
+    @After
+    public void tearDown() throws Exception {
+    }
+}