Browse Source

Merge branch 'master' of http://116.63.33.55/git/industry-data-platform

ws 1 year ago
parent
commit
3724783def

+ 15 - 4
industry-system/industry-da/src/main/java/com/example/opc_da/service/impl/ReportDataPolicyServiceImpl.java

@@ -24,12 +24,16 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
 import java.util.*;
 
 @Service
 @Transactional
 public class ReportDataPolicyServiceImpl implements ReportDataPolicyService {
 
+    private final String timePattern = "yyyy-MM-dd HH:mm:ss";
+
     @Autowired
     private ReportDataPolicyDao reportDataPolicyDao;
 
@@ -364,14 +368,21 @@ public class ReportDataPolicyServiceImpl implements ReportDataPolicyService {
                                                    String startTime, String endTime, Integer limit) {
         List<ReportDataPolicyItem> policyItemList = reportDataPolicyDao.getRdpItemListByIdList(idList);
         policyItemList = (List<ReportDataPolicyItem>) inFluxDBServiceUtil
-                .exchangeHistoryData(policyItemList, valueType.toString(), DateUtil.strYmdhmsChangeDate(startTime),
-                        DateUtil.strYmdhmsChangeDate(endTime));
+                .exchangeHistoryData(policyItemList, valueType.toString(),
+                        LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern(timePattern)),
+                        LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern(timePattern)));
         if (Blank.isEmpty(limit)) {
             return Result.ok(policyItemList);
         } else {
             policyItemList.forEach(policyItem -> {
-                policyItem.setDataValueList(policyItem.getDataValueList().subList(0, limit));
-                policyItem.setDataTimeList(policyItem.getDataTimeList().subList(0, limit));
+                policyItem.setDataValueList(
+                        policyItem.getDataValueList().size() > limit ?
+                                policyItem.getDataValueList().subList(0, limit) : policyItem.getDataValueList()
+                );
+                policyItem.setDataTimeList(
+                        policyItem.getDataTimeList().size() > limit ?
+                                policyItem.getDataTimeList().subList(0, limit) : policyItem.getDataTimeList()
+                );
             });
             return Result.ok(policyItemList);
         }

+ 1 - 1
industry-system/industry-da/src/main/java/com/example/opc_da/service/impl/ReportTableServiceImpl.java

@@ -281,7 +281,7 @@ public class ReportTableServiceImpl implements ReportTableService {
         if (Blank.isEmpty(reportTable)) {
             return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "报表不存在,请刷新再试");
         }
-        String s = reportTableTask.resolveAutoTableData(reportTable.getReportTableData());
+        String s = reportTableTask.resolveAutoTableData(reportTable.getReportTableData(), null);
 
         Integer reportTableType = reportTable.getReportTableType();
         if (Blank.isEmpty(reportTableType)) {

+ 289 - 118
industry-system/industry-da/src/main/java/com/example/opc_da/task/ReportTableTask.java

@@ -8,6 +8,8 @@ import com.cqcy.ei.influxdb.service.InFluxDBService;
 import com.example.opc_common.entity.ReportTable;
 import com.example.opc_common.entity.ReportTableItem;
 import com.example.opc_common.entity.TableTemplate;
+import com.example.opc_common.enums.ResultEnum;
+import com.example.opc_common.exception.CustomException;
 import com.example.opc_common.util.Blank;
 import com.example.opc_common.util.ConstantStr;
 import com.example.opc_common.util.DateUtil;
@@ -22,6 +24,10 @@ import org.springframework.web.client.RestTemplate;
 
 import javax.annotation.Resource;
 import javax.validation.constraints.NotBlank;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.format.DateTimeFormatter;
+import java.time.temporal.TemporalAdjusters;
 import java.util.*;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
@@ -30,7 +36,7 @@ import java.util.stream.Stream;
 @Component
 public class ReportTableTask {
 
-    private final String timeStamp = "yyyy-MM-dd HH:mm:ss.SSS";
+    private final String timePattern = "yyyy-MM-dd HH:mm:ss.SSS";
 
     @Resource
     private ReportTableDao reportTableDao;
@@ -77,7 +83,7 @@ public class ReportTableTask {
     public void newGenAutoTableData1(ReportTable reportTable) {
         log.info("自动报表,{},执行开始,时间为{}", reportTable, DateUtil.dateChangeStrYmdhmss(new Date()));
 
-        reportTable.setReportTableData(resolveAutoTableData(reportTable.getReportTableData()));
+        reportTable.setReportTableData(resolveAutoTableData(reportTable.getReportTableData(), null));
         //如果报表设置为启动打印,则报表生成完毕,马上执行打印
         Integer isRunPrint = reportTable.getIsRunPrint();
         if (isRunPrint.equals(ConstantStr.START_UP)) {
@@ -91,13 +97,23 @@ public class ReportTableTask {
      * @param reportTableDataStr
      * @return
      */
-    public String resolveAutoTableData(@NotBlank String reportTableDataStr) {
+    /**
+     * 将自动报表的reportTableData解析出来,并赋值相应的数据
+     *
+     * @param reportTableDataStr 报表字符串
+     * @param referTime          参考时间
+     * @return
+     */
+    public String resolveAutoTableData(@NotBlank String reportTableDataStr, LocalDateTime referTime) {
+        if (Blank.isEmpty(referTime)) {
+            referTime = LocalDateTime.now();
+        }
         //解析自动报表结构
         JSONObject reportTableData = JSONObject.parseObject(reportTableDataStr);
         //得到sheet数据
         JSONObject sheet = reportTableData.getJSONObject("sheet");
         JSONArray sheetData = sheet.getJSONArray("data");
-        Map<String, JSONObject> sheetMap = new HashMap<>();
+        Map<String, Map<String, JSONObject>> sheetTableMap = new HashMap<>();
         for (int i = 0; i < sheetData.size(); i++) {
             JSONArray row = sheetData.getJSONArray(i);
             for (int j = 0; j < row.size(); j++) {
@@ -106,10 +122,18 @@ public class ReportTableTask {
                     try {
                         System.out.println("第" + i + "行" + j + "列v:" + JSONObject.parseObject(rowCol.getString("v")));
                         JSONObject rowColJson = JSONObject.parseObject(rowCol.getString("v"));
-                        String uid = rowColJson.getString("uid");
-                        //得到所有sheet数据生成条件
-                        if (Blank.isEmpty(sheetMap.get(uid)) && Blank.isNotEmpty(uid)) {
-                            sheetMap.put(uid, rowColJson);
+                        String tableId = rowColJson.getString("tableId");
+                        Map<String, JSONObject> sheetUidMap = sheetTableMap.get(tableId);
+                        if (Blank.isEmpty(sheetUidMap)) sheetUidMap = new HashMap<>();
+                        String type = rowColJson.getString("type");
+                        if (type.equals("data")) {
+                            JSONObject rowColData = rowColJson.getJSONObject("data");
+                            String uid = rowColData.getString("uid");
+                            //得到所有sheet数据生成条件
+                            if (Blank.isEmpty(sheetUidMap.get(uid)) && Blank.isNotEmpty(uid)) {
+                                sheetUidMap.put(uid, rowColJson);
+                                sheetTableMap.put(tableId, sheetUidMap);
+                            }
                         }
                     } catch (Exception e) {
                         System.out.println("第" + i + "行" + j + "列v:" + rowCol.getString("v"));
@@ -119,69 +143,138 @@ public class ReportTableTask {
             }
         }
 
-        //得到所有的数据项配置信息
-        List<JSONObject> allJsonObjectList = sheetMap.values().stream().collect(Collectors.toList());
-        //对数据项配置信息,通过报表数据策略id,开始时间,结束时间,取值类型进行分组
-        Map<JSONObject, List<JSONObject>> jsonMap = allJsonObjectList.stream().collect(Collectors.groupingBy(j -> {
-            JSONObject jsonObject = new JSONObject();
-//            jsonObject.put("policyId", j.get("policyId"));
-            jsonObject.put("itemGroupId", j.get("itemGroupId"));
-//            jsonObject.put("startTime", j.get("startTime"));
-//            jsonObject.put("endTime", j.get("endTime"));
-            jsonObject.put("valueType", j.get("valueType"));
-            return jsonObject;
-        }));
-        Map<String, JSONObject> uidMap = allJsonObjectList.stream().collect(Collectors.toMap(j -> j.getString("uid"), j -> {
-            JSONObject jsonObject = new JSONObject();
-//            jsonObject.put("policyId", j.get("policyId"));
-            jsonObject.put("itemGroupId", j.get("itemGroupId"));
-//            jsonObject.put("startTime", j.get("startTime"));
-//            jsonObject.put("endTime", j.get("endTime"));
-            jsonObject.put("valueType", j.get("valueType"));
-            return jsonObject;
-        }));
-        //通过分组的信息,查询数据,并再分组
-        Map<JSONObject, Map<String, List<Item>>> sheetItemMap = new HashMap<>();
-        for (JSONObject jsonObject : jsonMap.keySet()) {
-            List<JSONObject> jsonObjectList = jsonMap.get(jsonObject);
-            List<String> policyItemIdList = jsonObjectList.stream().map(j -> j.getString("itemId")).collect(Collectors.toList());
-//            String policyId = jsonObject.getString("policyId");
-            String policyId = jsonObject.getString("itemGroupId");
-            String valueType = jsonObject.getString("valueType");
-//            String startTime = jsonObject.getString("startTime");
-//            String endTime = jsonObject.getString("ednTime");
-            Date startTime = new Date(new Date().getTime() - 1000 * 60 * 60);
-            Date endTime = new Date();
-            List<com.cqcy.ei.influxdb.entity.Item> itemList = inFluxDBService.queryHistory(bucket, policyId, startTime, endTime, policyItemIdList, valueType);
-            sheetItemMap.put(jsonObject, itemList.stream().collect(Collectors.groupingBy(i -> i.getName())));
-        }
-        //
-        Map<String, List<Item>> sheetDataMap = new HashMap<>();
-        for (String uid : sheetMap.keySet()) {
-            JSONObject jsonObject = sheetMap.get(uid);
-            String itemId = jsonObject.getString("itemId");
-            JSONObject jsonObject1 = uidMap.get(uid);
-            Map<String, List<Item>> stringListMap = sheetItemMap.get(jsonObject1);
-            if (Blank.isNotEmpty(stringListMap)) {
+        Map<String, Map<String, List<Item>>> sheetTableDataMap = new HashMap<>();
+        for (Map.Entry<String, Map<String, JSONObject>> sheetTable : sheetTableMap.entrySet()) {
+            Map<String, JSONObject> sheetUidMap = sheetTable.getValue();
+            //得到单个区域所有的数据项配置信息
+            List<JSONObject> tableJsonObjectList = sheetUidMap.values().stream().collect(Collectors.toList());
+            //对数据项配置信息,通过报表数据策略id,数据切换类型,开始时间,结束时间,取值类型进行分组
+            Map<JSONObject, List<JSONObject>> jsonMap = tableJsonObjectList.stream().collect(Collectors.groupingBy(j -> {
+                JSONObject data = j.getJSONObject("data");
+                JSONObject jsonObject = new JSONObject();
+                JSONObject dataJsonObject = new JSONObject();
+                dataJsonObject.put("policyId", data.get("policyId"));
+
+                JSONObject setting = j.getJSONObject("setting");
+                JSONObject setJsonObject = new JSONObject();
+                setJsonObject.put("valueType", setting.getString("valueType"));
+
+                setJsonObject.put("bucketType", setting.getString("bucketType"));
+
+                setJsonObject.put("timeType1", setting.getString("timeType1"));
+                setJsonObject.put("timeValue1", setting.getString("timeValue1"));
+                setJsonObject.put("dateTimeType1", setting.getString("dateTimeType1"));
+                setJsonObject.put("startTime", setting.getString("startTime"));
+
+                setJsonObject.put("timeType2", setting.getString("timeType2"));
+                setJsonObject.put("timeValue2", setting.getString("timeValue2"));
+                setJsonObject.put("dateTimeType2", setting.getString("dateTimeType2"));
+                setJsonObject.put("endTime", setting.getString("endTime"));
+
+                jsonObject.put("data", dataJsonObject);
+                jsonObject.put("setting", setJsonObject);
+                return jsonObject;
+            }));
+
+            //将单个区域数据项配置信息,转换为Map<uid,上面的jsonMap键>格式
+            Map<String, JSONObject> uidMap = tableJsonObjectList.stream().collect(Collectors.toMap(j -> j.getJSONObject("data").getString("uid"), j -> {
+                JSONObject data = j.getJSONObject("data");
+                JSONObject jsonObject = new JSONObject();
+                JSONObject dataJsonObject = new JSONObject();
+                dataJsonObject.put("policyId", data.get("policyId"));
+
+                JSONObject setting = j.getJSONObject("setting");
+                JSONObject setJsonObject = new JSONObject();
+                setJsonObject.put("valueType", setting.getString("valueType"));
+
+                setJsonObject.put("bucketType", setting.getString("bucketType"));
+
+                setJsonObject.put("timeType1", setting.getString("timeType1"));
+                setJsonObject.put("timeValue1", setting.getString("timeValue1"));
+                setJsonObject.put("dateTimeType1", setting.getString("dateTimeType1"));
+                setJsonObject.put("startTime", setting.getString("startTime"));
+
+                setJsonObject.put("timeType2", setting.getString("timeType2"));
+                setJsonObject.put("timeValue2", setting.getString("timeValue2"));
+                setJsonObject.put("dateTimeType2", setting.getString("dateTimeType2"));
+                setJsonObject.put("endTime", setting.getString("endTime"));
+
+                jsonObject.put("data", dataJsonObject);
+                jsonObject.put("setting", setJsonObject);
+                return jsonObject;
+            }));
+
+            //通过分组的信息,查询数据,并再分组
+            Map<JSONObject, Map<String, List<Item>>> sheetItemMap = new HashMap<>();
+            for (Map.Entry<JSONObject, List<JSONObject>> json : jsonMap.entrySet()) {
+                List<JSONObject> jsonObjectList = json.getValue();
+                List<String> policyItemIdList = jsonObjectList.stream().map(j -> j.getJSONObject("data").getString("itemId")).collect(Collectors.toList());
+                JSONObject jsonObject = json.getKey();
+                JSONObject data = jsonObject.getJSONObject("data");
+                String policyId = data.getString("policyId");
+
+                JSONObject setting = jsonObject.getJSONObject("setting");
+                String valueType = setting.getString("valueType");
+
+                String bucketType = setting.getString("bucketType");
+
+                String timeType1 = setting.getString("timeType1");
+                Integer timeValue1 = setting.getInteger("timeValue1");
+                String dateTimeType1 = setting.getString("dateTimeType1");
+                String startTime = setting.getString("startTime");
+
+                String timeType2 = setting.getString("timeType2");
+                Integer timeValue2 = setting.getInteger("timeValue2");
+                String dateTimeType2 = setting.getString("dateTimeType2");
+                String endTime = setting.getString("endTime");
+
+                //根据上面时间类型,生成sTime,eTime
+                LocalDateTime sTime = generateCycleTime(referTime, bucketType, timeType1, timeValue1, dateTimeType1, startTime);
+                LocalDateTime eTime = generateCycleTime(referTime, bucketType, timeType2, timeValue2, dateTimeType2, endTime);
+                List<Item> itemList = inFluxDBService.queryHistory(bucket, policyId, sTime, eTime, policyItemIdList, valueType);
+                sheetItemMap.put(jsonObject, itemList.stream().collect(Collectors.groupingBy(i -> i.getName())));
+            }
+            //将单个区域的数据项,生成Map<uid,List<Item>>结构数据
+            Map<String, List<Item>> sheetUidDataMap = new HashMap<>();
+            for (Map.Entry<String, JSONObject> sheetUid : sheetUidMap.entrySet()) {
+                JSONObject jsonObject = sheetUid.getValue();
+                String uid = sheetUid.getKey();
+                String itemId = jsonObject.getJSONObject("data").getString("itemId");
+                Map<String, List<Item>> stringListMap = sheetItemMap.get(uidMap.get(uid));
+//                if (Blank.isNotEmpty(stringListMap)) {
                 List<Item> itemList = stringListMap.get(itemId);
                 if (CollUtil.isNotEmpty(itemList)) {
-//                    String startTime = jsonObject.getString("startTime");
-//                    String endTime = jsonObject.getString("endTime");
-                    Date startTime = new Date(new Date().getTime() - 1000 * 60 * 60);
-                    Date endTime = new Date();
+                    JSONObject setting = jsonObject.getJSONObject("setting");
+                    String bucketType = setting.getString("bucketType");
+
+                    String timeType1 = setting.getString("timeType1");
+                    Integer timeValue1 = setting.getInteger("timeValue1");
+                    String dateTimeType1 = setting.getString("dateTimeType1");
+                    String startTime = setting.getString("startTime");
+
+                    String timeType2 = setting.getString("timeType2");
+                    Integer timeValue2 = setting.getInteger("timeValue2");
+                    String dateTimeType2 = setting.getString("dateTimeType2");
+                    String endTime = setting.getString("endTime");
+                    //根据上面时间类型,生成sTime,eTime
+                    LocalDateTime sTime = generateCycleTime(referTime, bucketType, timeType1, timeValue1, dateTimeType1, startTime);
+                    LocalDateTime eTime = generateCycleTime(referTime, bucketType, timeType2, timeValue2, dateTimeType2, endTime);
+                    //0数据集,1单值
+                    String dataValueType = setting.getString("dataValueType");
                     //取值模式0平均值,1最大值,2最小值,3瞬时值
-                    Integer valueTakingMode = jsonObject.getInteger("valueTakingMode");
-                    //时段类型0天,1时,2分,3秒
-                    Integer bucketType = jsonObject.getInteger("bucketType");
+                    String valueTakingMode = setting.getString("valueTakingMode");
                     //时段值
-                    Integer bucketValue = jsonObject.getInteger("bucketValue");
+                    Integer bucketValue = setting.getInteger("bucketValue");
                     //通过计算得到新的集合
-                    sheetDataMap.put(uid, splitData(itemList, startTime, endTime,
+                    sheetUidDataMap.put(uid, splitData(dataValueType, itemList, sTime, eTime,
                             valueTakingMode, bucketType, bucketValue));
                 }
+//                }
             }
+            sheetTableDataMap.put(sheetTable.getKey(), sheetUidDataMap);
         }
 
+
         //sheet数据最后往Json中存为data[0].data
         for (int i = 0; i < sheetData.size(); i++) {
             JSONArray row = sheetData.getJSONArray(i);
@@ -191,13 +284,22 @@ public class ReportTableTask {
                     //能解析的数据才赋值
                     try {
                         JSONObject rowColJson = JSONObject.parseObject(rowCol.getString("v"));
-                        String uid = rowColJson.getString("uid");
-                        if (Blank.isNotEmpty(uid)) {
-                            List<Item> itemList = sheetDataMap.get(uid);
+                        String type = rowColJson.getString("type");
+                        String tableId = rowColJson.getString("tableId");
+                        if (type.equals("data")) {
+                            JSONObject rowColData = rowColJson.getJSONObject("data");
+                            String uid = rowColData.getString("uid");
+                            if (Blank.isNotEmpty(uid)) {
+                                List<Item> itemList = sheetTableDataMap.get(tableId).get(uid);
+                                rowCol.put("v", null);
+                                rowCol.put("m", itemList.get(0).getValue());
+                                row.set(i, rowCol);
+                                itemList.remove(0);
+                            }
+                        } else if (type.equals("datatime")) {
+                            List<Item> itemList = sheetTableDataMap.get(tableId).entrySet().stream().findFirst().get().getValue();
                             rowCol.put("v", null);
-                            rowCol.put("m", itemList.get(0).getValue());
-                            row.set(i, rowCol);
-                            itemList.remove(0);
+                            rowCol.put("m", itemList.get(0).getTime());
                         }
                     } catch (Exception e) {
 
@@ -234,27 +336,35 @@ public class ReportTableTask {
             String policyId = dataForm.getString("itemGroupId");
 //            String startTime = dataForm.getString("startTime");
 //            String endTime = dataForm.getString("endTime");
-            Date startTime = new Date(new Date().getTime() - 1000 * 60 * 60);
-            Date endTime = new Date();
+//            LocalDateTime sTime = generateCycleTime(LocalDateTime.now(), bucketType, timeType1, timeValue1, dateTimeType1, startTime);
+//            LocalDateTime eTime = generateCycleTime(LocalDateTime.now(), bucketType, timeType2, timeValue2, dateTimeType2, endTime);
+            LocalDateTime sTime = LocalDateTime.now();
+            LocalDateTime eTime = LocalDateTime.now();
 //            JSONObject items = chartJson.getJSONObject("items");
             JSONArray items = chartJson.getJSONArray("items");
             List<String> policyItemIdList = items.stream().map(v -> ((JSONObject) v).getString("value")).collect(Collectors.toList());
-            List<com.cqcy.ei.influxdb.entity.Item> itemList = inFluxDBService.queryHistory(bucket, policyId, startTime, endTime, policyItemIdList, valueType);
-            Map<String, List<com.cqcy.ei.influxdb.entity.Item>> itemDataMap = itemList.stream().collect(Collectors.groupingBy(i -> i.getName()));
+            List<Item> itemList = inFluxDBService.queryHistory(bucket, policyId, sTime, eTime, policyItemIdList, valueType);
+            Map<String, List<Item>> itemDataMap = itemList.stream().collect(Collectors.groupingBy(i -> i.getName()));
             if (CollUtil.isNotEmpty(itemDataMap)) {
                 //取值模式0平均值,1最大值,2最小值,3瞬时值
-                Integer valueTakingMode = dataForm.getInteger("valueTakingMode");
+                String valueTakingMode = dataForm.getString("valueTakingMode");
                 //时段类型0天,1时,2分,3秒
-                Integer bucketType = dataForm.getInteger("bucketType");
+                String bucketType = dataForm.getString("bucketType");
                 //时段值
                 Integer bucketValue = dataForm.getInteger("bucketValue");
+                List<String> xList = new ArrayList<>();
+                List<List<List<String>>> serieList = new ArrayList<>();
                 for (String key : itemDataMap.keySet()) {
-                    List<Item> itemList2 = splitData(itemDataMap.get(key), startTime, endTime, valueTakingMode, bucketType, bucketValue);
+                    List<Item> validDataList = splitData(null, itemDataMap.get(key), sTime, eTime, valueTakingMode, bucketType, bucketValue);
+                    xList.addAll(validDataList.stream().map(Item::getTime).collect(Collectors.toList()));
                     //生成series的数据
-                    itemList2.stream().map(i ->
+                    serieList.add(validDataList.stream().map(i ->
                             Stream.of(i.getTime(), i.getValue()).collect(Collectors.toList())
-                    ).collect(Collectors.toList());
+                    ).collect(Collectors.toList()));
                 }
+                //得到x轴时间数据
+                xList.stream().distinct().sorted().collect(Collectors.toList());
+                //得到series的数据
             }
             //将生成的数据回传到chart中去
             chartJson.put("", "");
@@ -324,53 +434,114 @@ public class ReportTableTask {
     }
 
     /**
+     * 生成周期报表时间方法
+     *
+     * @param referTime     参考时间
+     * @param bucketType    时段类型:0月,1天,2时,3粉,4秒
+     * @param timeType      0当,1次
+     * @param timeValue     值
+     * @param dateTimeType
+     * @param dateTimeValue
+     * @return
+     */
+    public LocalDateTime generateCycleTime(LocalDateTime referTime, String bucketType, String timeType, Integer timeValue,
+                                           String dateTimeType, String dateTimeValue) {
+        if (bucketType.equals(ConstantStr.PERIOD_TIME_MONTH)) {
+            //如果时段类型选择为月
+            if (timeType.equals(ConstantStr.TYPE_LAST)) {
+                //参考时间的上一年 指定月 第一天
+                return referTime.minusYears(1).withMonth(timeValue).with(TemporalAdjusters.firstDayOfMonth());
+            } else if (timeType.equals(ConstantStr.TYPE_CURRENT)) {
+                //参考时间的本年 指定月 第一天
+                return referTime.withMonth(timeValue).with(TemporalAdjusters.firstDayOfMonth());
+            }
+        } else if (bucketType.equals(ConstantStr.PERIOD_TIME_DAY)) {
+            //如果时段类型选择为日
+            if (timeType.equals(ConstantStr.TYPE_LAST)) {
+                //参考时间的本年 上一月 指定天
+                return referTime.minusMonths(1).withDayOfMonth(timeValue);
+            } else if (timeType.equals(ConstantStr.TYPE_CURRENT)) {
+                //参考时间的本年 本月 指定天
+                return referTime.withDayOfMonth(timeValue);
+            }
+        } else if (bucketType.equals(ConstantStr.PERIOD_TIME_HOUR) ||
+                bucketType.equals(ConstantStr.PERIOD_TIME_MINUTE) ||
+                bucketType.equals(ConstantStr.PERIOD_TIME_SECOND)) {
+            //如果时段类型选择为时,分,秒
+            if (dateTimeType.equals(ConstantStr.TYPE_LAST)) {
+                //参考时间的本年 本月 下一天 指定时分秒
+                return referTime.minusDays(1).with(LocalTime.parse(dateTimeValue, DateTimeFormatter.ofPattern("HH:mm:ss")));
+            } else if (dateTimeType.equals(ConstantStr.TYPE_CURRENT)) {
+                //参考时间的本年 本月 本天 指定时分秒
+                return referTime.with(LocalTime.parse(dateTimeValue, DateTimeFormatter.ofPattern("HH:mm:ss")));
+            }
+        }
+        return LocalDateTime.now();
+    }
+
+    /**
      * 通过开始时间和结束时间分割数据,并计算
      *
+     * @param dataValueType   0数据集,1单值
      * @param itemList        全部数据项集合
-     * @param startTime       开始时间
-     * @param endTime         结束时间
+     * @param startDateTime   开始时间
+     * @param endDateTime     结束时间
      * @param valueTakingMode 取值模式:0平均值,1最大值,2最小值,3瞬时值
      * @param bucketType      时段类型:0天,1时,2分,3秒
      * @param bucketValue     时段值
      * @return
      */
-    public List<Item> splitData(List<Item> itemList, Date startTime, Date endTime,
-                                Integer valueTakingMode, Integer bucketType, Integer bucketValue) {
-        long startTimeTime = startTime.getTime();
-        long endTimeTime;
-        //将开始-结束时间的时间段,通过时段类型和时段值,分割为多段
-        int timeLength = DateUtil.timeDifference1(startTime, endTime, bucketType, bucketValue);
-//        int timeLength = DateUtil.timeDifference2(startTime, endTime, bucketType, bucketValue);
+    public List<Item> splitData(String dataValueType, List<Item> itemList, LocalDateTime startDateTime, LocalDateTime endDateTime,
+                                String valueTakingMode, String bucketType, Integer bucketValue) {
         List<Item> newItemList = new ArrayList<>();
-        for (int i = 0; i < timeLength; i++) {
-            List<String> valueList = new ArrayList<>();
-            if (i != timeLength - 1) {
-                endTimeTime = DateUtil.addTimeStamp1(startTimeTime, bucketType, bucketValue);
-            } else {
-                endTimeTime = endTime.getTime();
-            }
-            for (int j = 0; j < itemList.size(); j++) {
-                Item item = itemList.get(j);
-                long time = DateUtil.strChangeDate(item.getTime(), timeStamp).getTime();
-                if (startTimeTime > time) {
-                    itemList.remove(j);
-                    j--;
-                    continue;
-                }
-                if (startTimeTime <= time && time < endTimeTime) {
-                    valueList.add(item.getValue());
-                    itemList.remove(j);
-                    j--;
+        //获取数据集
+        if (dataValueType.equals(ConstantStr.DATA_SET)) {
+            LocalDateTime sDateTime = startDateTime;
+            LocalDateTime eDateTime;
+            //如果sDateTime小于endDateTime
+            while (sDateTime.isBefore(endDateTime)) {
+                if (bucketType.equals(ConstantStr.PERIOD_TIME_MONTH)) {
+                    eDateTime = sDateTime.plusMonths(bucketValue);
+                } else if (bucketType.equals(ConstantStr.PERIOD_TIME_DAY)) {
+                    eDateTime = sDateTime.plusDays(bucketValue);
+                } else if (bucketType.equals(ConstantStr.PERIOD_TIME_HOUR)) {
+                    eDateTime = sDateTime.plusHours(bucketValue);
+                } else if (bucketType.equals(ConstantStr.PERIOD_TIME_MINUTE)) {
+                    eDateTime = sDateTime.plusMinutes(bucketValue);
+                } else if (bucketType.equals(ConstantStr.PERIOD_TIME_SECOND)) {
+                    eDateTime = sDateTime.plusSeconds(bucketValue);
+                } else {
+                    throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "时段类型错误");
                 }
-                if (time >= endTimeTime) {
-                    break;
+                List<String> valueList = new ArrayList<>();
+                for (int i = 0; i < itemList.size(); i++) {
+                    Item item = itemList.get(i);
+                    LocalDateTime itemDateTime = LocalDateTime.parse(item.getTime(), DateTimeFormatter.ofPattern(timePattern));
+                    if (sDateTime.isAfter(itemDateTime)) {
+                        itemList.remove(i);
+                        i--;
+                        continue;
+                    }
+                    if (itemDateTime.isBefore(eDateTime)) {
+                        valueList.add(item.getValue());
+                        itemList.remove(i);
+                        i--;
+                    } else {
+                        break;
+                    }
                 }
+                //求出每段应该返回的值
+                newItemList.add(new Item()
+                        .value(countData(valueList, valueTakingMode))
+                        .time(sDateTime.format(DateTimeFormatter.ofPattern(timePattern))));
+                sDateTime = eDateTime;
             }
-            //求出每段应该返回的值
+            return newItemList;
+        } else if (dataValueType.equals(ConstantStr.SINGLE_VALUE)) {
             newItemList.add(new Item()
-                    .value(countData(valueList, valueTakingMode))
-                    .time(DateUtil.dateChangeStrYmdhmss(new Date(startTimeTime))));
-            startTimeTime = endTimeTime;
+                    .value(countData(itemList.stream().map(i ->
+                            i.getValue()).collect(Collectors.toList()), valueTakingMode))
+                    .time(startDateTime.format(DateTimeFormatter.ofPattern(timePattern))));
         }
         return newItemList;
     }
@@ -379,10 +550,10 @@ public class ReportTableTask {
      * 将传入的集合数据组,通过不同的取值模式,算出来
      *
      * @param valueList       数据集合
-     * @param valueTakingMode 取值模式:0平均值,1最大值,2最小值,3瞬时值
+     * @param valueTakingMode 取值模式:0平均值,1最大值,2最小值,3瞬时值,4求和,5计数
      * @return
      */
-    public String countData(List<String> valueList, Integer valueTakingMode) {
+    public String countData(List<String> valueList, String valueTakingMode) {
         if (CollUtil.isNotEmpty(valueList)) {
             if (valueTakingMode.equals(ConstantStr.AVERAGE_VALUE)) {
                 return valueList.stream().mapToDouble(Double::valueOf).average().getAsDouble() + "";
@@ -396,12 +567,12 @@ public class ReportTableTask {
             if (valueTakingMode.equals(ConstantStr.INSTANTANEOUS_VALUE)) {
                 return valueList.get(0);
             }
-//            if (valueTakingMode.equals(ConstantStr.AVERAGE_VALUE)) {
-//                return valueList.stream().mapToDouble(Double::valueOf).sum() + "";
-//            }
-//            if (valueTakingMode.equals(ConstantStr.AVERAGE_VALUE)) {
-//                return valueList.stream().count() + "";
-//            }
+            if (valueTakingMode.equals(ConstantStr.SUM_VALUE)) {
+                return valueList.stream().mapToDouble(Double::valueOf).sum() + "";
+            }
+            if (valueTakingMode.equals(ConstantStr.COUNT_VALUE)) {
+                return valueList.stream().count() + "";
+            }
         }
         return null;
     }

+ 566 - 0
industry-system/industry-da/src/main/java/com/example/opc_da/task/ReportTableTask1.java

@@ -0,0 +1,566 @@
+package com.example.opc_da.task;
+
+import cn.hutool.core.collection.CollUtil;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.cqcy.ei.influxdb.entity.Item;
+import com.cqcy.ei.influxdb.service.InFluxDBService;
+import com.example.opc_common.entity.ReportTable;
+import com.example.opc_common.entity.ReportTableItem;
+import com.example.opc_common.entity.TableTemplate;
+import com.example.opc_common.enums.ResultEnum;
+import com.example.opc_common.exception.CustomException;
+import com.example.opc_common.util.Blank;
+import com.example.opc_common.util.ConstantStr;
+import com.example.opc_common.util.DateUtil;
+import com.example.opc_da.dao.*;
+import com.example.opc_da.util.RedisUtil;
+import com.example.opc_da.util.UserUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.web.client.RestTemplate;
+
+import javax.annotation.Resource;
+import javax.validation.constraints.NotBlank;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.format.DateTimeFormatter;
+import java.time.temporal.TemporalAdjusters;
+import java.util.*;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+@Slf4j
+@Component
+public class ReportTableTask1 {
+
+    private final String timePattern = "yyyy-MM-dd HH:mm:ss.SSS";
+
+    @Resource
+    private ReportTableDao reportTableDao;
+
+    @Resource
+    private ItemGroupDao itemGroupDao;
+
+    @Autowired
+    private DataSourceDao dataSourceDao;
+
+    @Autowired
+    private DataModelDao dataModelDao;
+
+    @Autowired
+    private UserUtil userUtil;
+
+    @Autowired
+    private RestTemplate restTemplate;
+
+    @Resource
+    private MessageNoticeDao messageNoticeDao;
+
+    @Autowired
+    private AsyncAsyncTask asyncAsyncTask;
+
+    @Autowired
+    private AsyncTask asyncTask;
+
+    @Autowired
+    private InFluxDBService inFluxDBService;
+
+    @Value("${influxdb.bucket}")
+    private String bucket;
+
+    @Autowired
+    private RedisUtil redisUtil;
+
+    public ReportTable getAutoReport1(ReportTable reportTable) {
+
+
+        return reportTable;
+    }
+
+    public void newGenAutoTableData1(ReportTable reportTable) {
+        log.info("自动报表,{},执行开始,时间为{}", reportTable, DateUtil.dateChangeStrYmdhmss(new Date()));
+
+        reportTable.setReportTableData(resolveAutoTableData(reportTable.getReportTableData(), null));
+        //如果报表设置为启动打印,则报表生成完毕,马上执行打印
+        Integer isRunPrint = reportTable.getIsRunPrint();
+        if (isRunPrint.equals(ConstantStr.START_UP)) {
+            asyncTask.runPrint("", reportTable.getPrintIp(), reportTable.getPrintConfigId());
+        }
+    }
+
+    /**
+     * 将自动报表的reportTableData解析出来,并赋值相应的数据
+     *
+     * @param reportTableDataStr
+     * @return
+     */
+    /**
+     * 将自动报表的reportTableData解析出来,并赋值相应的数据
+     *
+     * @param reportTableDataStr 报表字符串
+     * @param referTime          参考时间
+     * @return
+     */
+    public String resolveAutoTableData(@NotBlank String reportTableDataStr, LocalDateTime referTime) {
+        if (Blank.isEmpty(referTime)) {
+            referTime = LocalDateTime.now();
+        }
+        //解析自动报表结构
+        JSONObject reportTableData = JSONObject.parseObject(reportTableDataStr);
+        //得到sheet数据
+        JSONObject sheet = reportTableData.getJSONObject("sheet");
+        JSONArray sheetData = sheet.getJSONArray("data");
+        Map<String, JSONObject> sheetMap = new HashMap<>();
+        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)) {
+                    try {
+                        System.out.println("第" + i + "行" + j + "列v:" + JSONObject.parseObject(rowCol.getString("v")));
+                        JSONObject rowColJson = JSONObject.parseObject(rowCol.getString("v"));
+                        String type = rowColJson.getString("type");
+                        if (type.equals("data")) {
+                            JSONObject rowColData = rowColJson.getJSONObject("data");
+                            String uid = rowColData.getString("uid");
+                            //得到所有sheet数据生成条件
+                            if (Blank.isEmpty(sheetMap.get(uid)) && Blank.isNotEmpty(uid)) {
+                                sheetMap.put(uid, rowColJson);
+                            }
+                        }
+                        String uid = rowColJson.getString("uid");
+                        //得到所有sheet数据生成条件
+                        if (Blank.isEmpty(sheetMap.get(uid)) && Blank.isNotEmpty(uid)) {
+                            sheetMap.put(uid, rowColJson);
+                        }
+                    } catch (Exception e) {
+                        System.out.println("第" + i + "行" + j + "列v:" + rowCol.getString("v"));
+                    }
+                    System.out.println("第" + i + "行" + j + "列m:" + rowCol.getString("m"));
+                }
+            }
+        }
+
+        //得到所有的数据项配置信息
+        List<JSONObject> allJsonObjectList = sheetMap.values().stream().collect(Collectors.toList());
+        //对数据项配置信息,通过报表数据策略id,数据切换类型,开始时间,结束时间,取值类型进行分组
+        Map<JSONObject, List<JSONObject>> jsonMap = allJsonObjectList.stream().collect(Collectors.groupingBy(j -> {
+            JSONObject data = j.getJSONObject("data");
+            JSONObject jsonObject = new JSONObject();
+            JSONObject dataJsonObject = new JSONObject();
+            dataJsonObject.put("policyId", data.get("policyId"));
+
+            JSONObject setting = j.getJSONObject("setting");
+            JSONObject setJsonObject = new JSONObject();
+            setJsonObject.put("valueType", setting.getString("valueType"));
+
+            setJsonObject.put("bucketType", setting.getString("bucketType"));
+
+            setJsonObject.put("timeType1", setting.getString("timeType1"));
+            setJsonObject.put("timeValue1", setting.getString("timeValue1"));
+            setJsonObject.put("dateTimeType1", setting.getString("dateTimeType1"));
+            setJsonObject.put("startTime", setting.getString("startTime"));
+
+            setJsonObject.put("timeType2", setting.getString("timeType2"));
+            setJsonObject.put("timeValue2", setting.getString("timeValue2"));
+            setJsonObject.put("dateTimeType2", setting.getString("dateTimeType2"));
+            setJsonObject.put("endTime", setting.getString("endTime"));
+
+            jsonObject.put("data", dataJsonObject);
+            jsonObject.put("setting", setJsonObject);
+            return jsonObject;
+        }));
+        //将数据项配置信息,转换为Map<uid,上面的jsonMap键>格式
+        Map<String, JSONObject> uidMap = allJsonObjectList.stream().collect(Collectors.toMap(j -> j.getJSONObject("data").getString("uid"), j -> {
+            JSONObject data = j.getJSONObject("data");
+            JSONObject jsonObject = new JSONObject();
+            JSONObject dataJsonObject = new JSONObject();
+            dataJsonObject.put("policyId", data.get("policyId"));
+
+            JSONObject setting = j.getJSONObject("setting");
+            JSONObject setJsonObject = new JSONObject();
+            setJsonObject.put("valueType", setting.getString("valueType"));
+
+            setJsonObject.put("bucketType", setting.getString("bucketType"));
+
+            setJsonObject.put("timeType1", setting.getString("timeType1"));
+            setJsonObject.put("timeValue1", setting.getString("timeValue1"));
+            setJsonObject.put("dateTimeType1", setting.getString("dateTimeType1"));
+            setJsonObject.put("startTime", setting.getString("startTime"));
+
+            setJsonObject.put("timeType2", setting.getString("timeType2"));
+            setJsonObject.put("timeValue2", setting.getString("timeValue2"));
+            setJsonObject.put("dateTimeType2", setting.getString("dateTimeType2"));
+            setJsonObject.put("endTime", setting.getString("endTime"));
+
+            jsonObject.put("data", dataJsonObject);
+            jsonObject.put("setting", setJsonObject);
+            return jsonObject;
+        }));
+        //通过分组的信息,查询数据,并再分组
+        Map<JSONObject, Map<String, List<Item>>> sheetItemMap = new HashMap<>();
+        for (JSONObject jsonObject : jsonMap.keySet()) {
+            List<JSONObject> jsonObjectList = jsonMap.get(jsonObject);
+            List<String> policyItemIdList = jsonObjectList.stream().map(j -> j.getJSONObject("data").getString("itemId")).collect(Collectors.toList());
+            JSONObject data = jsonObject.getJSONObject("data");
+            String policyId = data.getString("policyId");
+
+            JSONObject setting = jsonObject.getJSONObject("setting");
+            String valueType = setting.getString("valueType");
+
+            String bucketType = setting.getString("bucketType");
+
+            String timeType1 = setting.getString("timeType1");
+            Integer timeValue1 = setting.getInteger("timeValue1");
+            String dateTimeType1 = setting.getString("dateTimeType1");
+            String startTime = setting.getString("startTime");
+
+            String timeType2 = setting.getString("timeType2");
+            Integer timeValue2 = setting.getInteger("timeValue2");
+            String dateTimeType2 = setting.getString("dateTimeType2");
+            String endTime = setting.getString("endTime");
+
+            //根据上面时间类型,生成sTime,eTime
+            LocalDateTime sTime = generateCycleTime(referTime, bucketType, timeType1, timeValue1, dateTimeType1, startTime);
+            LocalDateTime eTime = generateCycleTime(referTime, bucketType, timeType2, timeValue2, dateTimeType2, endTime);
+            List<Item> itemList = inFluxDBService.queryHistory(bucket, policyId, sTime, eTime, policyItemIdList, valueType);
+            sheetItemMap.put(jsonObject, itemList.stream().collect(Collectors.groupingBy(i -> i.getName())));
+        }
+        //
+        Map<String, List<Item>> sheetDataMap = new HashMap<>();
+        for (String uid : sheetMap.keySet()) {
+            JSONObject jsonObject = sheetMap.get(uid);
+            String itemId = jsonObject.getJSONObject("data").getString("itemId");
+            Map<String, List<Item>> stringListMap = sheetItemMap.get(uidMap.get(uid));
+            if (Blank.isNotEmpty(stringListMap)) {
+                List<Item> itemList = stringListMap.get(itemId);
+                if (CollUtil.isNotEmpty(itemList)) {
+                    JSONObject setting = jsonObject.getJSONObject("setting");
+                    String bucketType = setting.getString("bucketType");
+
+                    String timeType1 = setting.getString("timeType1");
+                    Integer timeValue1 = setting.getInteger("timeValue1");
+                    String dateTimeType1 = setting.getString("dateTimeType1");
+                    String startTime = setting.getString("startTime");
+
+                    String timeType2 = setting.getString("timeType2");
+                    Integer timeValue2 = setting.getInteger("timeValue2");
+                    String dateTimeType2 = setting.getString("dateTimeType2");
+                    String endTime = setting.getString("endTime");
+                    //根据上面时间类型,生成sTime,eTime
+                    LocalDateTime sTime = generateCycleTime(referTime, bucketType, timeType1, timeValue1, dateTimeType1, startTime);
+                    LocalDateTime eTime = generateCycleTime(referTime, bucketType, timeType2, timeValue2, dateTimeType2, endTime);
+                    //0数据集,1单值
+                    String dataValueType = setting.getString("dataValueType");
+                    //取值模式0平均值,1最大值,2最小值,3瞬时值
+                    String valueTakingMode = setting.getString("valueTakingMode");
+                    //时段值
+                    Integer bucketValue = setting.getInteger("bucketValue");
+                    //通过计算得到新的集合
+                    sheetDataMap.put(uid, splitData(dataValueType, itemList, sTime, eTime,
+                            valueTakingMode, bucketType, bucketValue));
+                }
+            }
+        }
+
+        //sheet数据最后往Json中存为data[0].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)) {
+                    //能解析的数据才赋值
+                    try {
+                        JSONObject rowColJson = JSONObject.parseObject(rowCol.getString("v"));
+                        String type = rowColJson.getString("type");
+                        if (type.equals("data")) {
+                            JSONObject rowColData = rowColJson.getJSONObject("data");
+                            String uid = rowColData.getString("uid");
+                            if (Blank.isNotEmpty(uid)) {
+                                List<Item> itemList = sheetDataMap.get(uid);
+                                rowCol.put("v", null);
+                                rowCol.put("m", itemList.get(0).getValue());
+                                row.set(i, rowCol);
+                                itemList.remove(0);
+                            }
+                        }
+                    } catch (Exception e) {
+
+                    }
+                }
+            }
+        }
+        sheet.put("data", sheetData);
+        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)) {
+                    try {
+                        System.out.println("第" + i + "行" + j + "列v:" + JSONObject.parseObject(rowCol.getString("v")));
+                    } catch (Exception e) {
+                        System.out.println("第" + i + "行" + j + "列v:" + rowCol.getString("v"));
+                    }
+                    System.out.println("第" + i + "行" + j + "列m:" + rowCol.getString("m"));
+                }
+            }
+        }
+        reportTableData.put("sheet", sheet);
+//        ----------------------------------------
+        //得到chart数据
+        JSONObject chart = reportTableData.getJSONObject("chart");
+        Map<String, JSONObject> chartMap = new HashMap<>();
+        for (Map.Entry<String, Object> entry : chart.entrySet()) {
+            JSONObject chartJson = chart.getJSONObject(entry.getKey());
+            JSONObject chartData = chartJson.getJSONObject("chartData");
+            JSONObject dataForm = chartJson.getJSONObject("dataForm");
+            String valueType = dataForm.getString("valueType");
+//            String policyId = dataForm.getString("policyId");
+            String policyId = dataForm.getString("itemGroupId");
+//            String startTime = dataForm.getString("startTime");
+//            String endTime = dataForm.getString("endTime");
+//            LocalDateTime sTime = generateCycleTime(LocalDateTime.now(), bucketType, timeType1, timeValue1, dateTimeType1, startTime);
+//            LocalDateTime eTime = generateCycleTime(LocalDateTime.now(), bucketType, timeType2, timeValue2, dateTimeType2, endTime);
+            LocalDateTime sTime = LocalDateTime.now();
+            LocalDateTime eTime = LocalDateTime.now();
+//            JSONObject items = chartJson.getJSONObject("items");
+            JSONArray items = chartJson.getJSONArray("items");
+            List<String> policyItemIdList = items.stream().map(v -> ((JSONObject) v).getString("value")).collect(Collectors.toList());
+            List<Item> itemList = inFluxDBService.queryHistory(bucket, policyId, sTime, eTime, policyItemIdList, valueType);
+            Map<String, List<Item>> itemDataMap = itemList.stream().collect(Collectors.groupingBy(i -> i.getName()));
+            if (CollUtil.isNotEmpty(itemDataMap)) {
+                //取值模式0平均值,1最大值,2最小值,3瞬时值
+                String valueTakingMode = dataForm.getString("valueTakingMode");
+                //时段类型0天,1时,2分,3秒
+                String bucketType = dataForm.getString("bucketType");
+                //时段值
+                Integer bucketValue = dataForm.getInteger("bucketValue");
+                List<String> xList = new ArrayList<>();
+                List<List<List<String>>> serieList = new ArrayList<>();
+                for (String key : itemDataMap.keySet()) {
+                    List<Item> validDataList = splitData(null, itemDataMap.get(key), sTime, eTime, valueTakingMode, bucketType, bucketValue);
+                    xList.addAll(validDataList.stream().map(Item::getTime).collect(Collectors.toList()));
+                    //生成series的数据
+                    serieList.add(validDataList.stream().map(i ->
+                            Stream.of(i.getTime(), i.getValue()).collect(Collectors.toList())
+                    ).collect(Collectors.toList()));
+                }
+                //得到x轴时间数据
+                xList.stream().distinct().sorted().collect(Collectors.toList());
+                //得到series的数据
+            }
+            //将生成的数据回传到chart中去
+            chartJson.put("", "");
+            chart.put(entry.getKey(), chartJson);
+        }
+        reportTableData.put("chart", chart);
+
+        //将生成的数据重新转换为json
+        return reportTableData.toJSONString();
+    }
+
+    /**
+     * 通过此数据组关联的所有模板信息,生成对应设备报表在redis中的键,值
+     *
+     * @param tableTemplateList
+     */
+    public void genDeviceRedisKey(List<TableTemplate> tableTemplateList) {
+        if (Blank.isNotEmpty(tableTemplateList)) {
+            for (TableTemplate tableTemplate : tableTemplateList) {
+                Integer tableTemplateId = tableTemplate.getId();
+                Integer eventNum = tableTemplate.getEventNum();
+                //根据模板id查询出最新一条的指定类型报表
+                ReportTable reportTable = reportTableDao.getReportTableByTemplate(tableTemplate);
+                String chTableId = reportTableDao.getEventChTableId(reportTable.getId());
+                if (Blank.isNotEmpty(chTableId)) {
+                    ReportTable chReportTable = reportTableDao.getReportTableById(chTableId);
+                    if (reportTable.getVersion() == chReportTable.getVersion()) {
+                        List<ReportTableItem> chReportTableItemList = new ArrayList<>();
+//                        List<ReportTableItem> chReportTableItemList = reportTableDao.getTableItemListById(chTableId);
+                        if (Blank.isNotEmpty(chReportTableItemList)) {
+                            for (ReportTableItem reportTableItem : chReportTableItemList) {
+                                Integer timeItemType = reportTableItem.getTimeItemType();
+                                String itemReadName = reportTableItem.getItemReadName();
+                                if (Blank.isNotEmpty(timeItemType)) {
+                                    if (timeItemType.equals(ConstantStr.END_TIME_ITEM)) {
+                                        String valueIndexList = reportTableItem.getValueIndexList();
+                                        int length = valueIndexList.split(",").length;
+                                        if (eventNum == length) {
+                                            redisUtil.set(ConstantStr.START_NUM + tableTemplateId, 0);
+                                        } else {
+                                            redisUtil.set(ConstantStr.END_NUM + tableTemplateId, length);
+                                            redisUtil.set(ConstantStr.TABLE_ID + tableTemplateId, chTableId);
+                                        }
+                                    }
+                                    if (timeItemType.equals(ConstantStr.START_TIME_ITEM)) {
+                                        String valueIndexList = reportTableItem.getValueIndexList();
+                                        int length = valueIndexList.split(",").length;
+                                        if (eventNum == length) {
+                                            Object start0 = redisUtil.get(ConstantStr.START_NUM + tableTemplateId);
+                                            if (Blank.isEmpty(start0)) {
+                                                redisUtil.set(ConstantStr.START_NUM + tableTemplateId, length);
+                                            }
+                                        } else {
+                                            redisUtil.set(ConstantStr.START_NUM + tableTemplateId, length);
+                                        }
+                                    }
+                                    redisUtil.set(tableTemplateId + ConstantStr.REPORT_TABLE_ITEM + itemReadName + ConstantStr.REPORT_TABLE_ITEM + timeItemType, reportTableItem);
+                                } else {
+                                    redisUtil.set(tableTemplateId + ConstantStr.REPORT_TABLE_ITEM + itemReadName, reportTableItem);
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * 生成周期报表时间方法
+     *
+     * @param referTime     参考时间
+     * @param bucketType    时段类型:0月,1天,2时,3粉,4秒
+     * @param timeType      0当,1次
+     * @param timeValue     值
+     * @param dateTimeType
+     * @param dateTimeValue
+     * @return
+     */
+    public LocalDateTime generateCycleTime(LocalDateTime referTime, String bucketType, String timeType, Integer timeValue,
+                                           String dateTimeType, String dateTimeValue) {
+        if (bucketType.equals(ConstantStr.PERIOD_TIME_MONTH)) {
+            //如果时段类型选择为月
+            if (timeType.equals(ConstantStr.TYPE_LAST)) {
+                //参考时间的上一年 指定月 第一天
+                return referTime.minusYears(1).withMonth(timeValue).with(TemporalAdjusters.firstDayOfMonth());
+            } else if (timeType.equals(ConstantStr.TYPE_CURRENT)) {
+                //参考时间的本年 指定月 第一天
+                return referTime.withMonth(timeValue).with(TemporalAdjusters.firstDayOfMonth());
+            }
+        } else if (bucketType.equals(ConstantStr.PERIOD_TIME_DAY)) {
+            //如果时段类型选择为日
+            if (timeType.equals(ConstantStr.TYPE_LAST)) {
+                //参考时间的本年 上一月 指定天
+                return referTime.minusMonths(1).withDayOfMonth(timeValue);
+            } else if (timeType.equals(ConstantStr.TYPE_CURRENT)) {
+                //参考时间的本年 本月 指定天
+                return referTime.withDayOfMonth(timeValue);
+            }
+        } else if (bucketType.equals(ConstantStr.PERIOD_TIME_HOUR) ||
+                bucketType.equals(ConstantStr.PERIOD_TIME_MINUTE) ||
+                bucketType.equals(ConstantStr.PERIOD_TIME_SECOND)) {
+            //如果时段类型选择为时,分,秒
+            if (dateTimeType.equals(ConstantStr.TYPE_LAST)) {
+                //参考时间的本年 本月 下一天 指定时分秒
+                return referTime.minusDays(1).with(LocalTime.parse(dateTimeValue, DateTimeFormatter.ofPattern("HH:mm:ss")));
+            } else if (dateTimeType.equals(ConstantStr.TYPE_CURRENT)) {
+                //参考时间的本年 本月 本天 指定时分秒
+                return referTime.with(LocalTime.parse(dateTimeValue, DateTimeFormatter.ofPattern("HH:mm:ss")));
+            }
+        }
+        return LocalDateTime.now();
+    }
+
+    /**
+     * 通过开始时间和结束时间分割数据,并计算
+     *
+     * @param dataValueType   0数据集,1单值
+     * @param itemList        全部数据项集合
+     * @param startDateTime   开始时间
+     * @param endDateTime     结束时间
+     * @param valueTakingMode 取值模式:0平均值,1最大值,2最小值,3瞬时值
+     * @param bucketType      时段类型:0天,1时,2分,3秒
+     * @param bucketValue     时段值
+     * @return
+     */
+    public List<Item> splitData(String dataValueType, List<Item> itemList, LocalDateTime startDateTime, LocalDateTime endDateTime,
+                                String valueTakingMode, String bucketType, Integer bucketValue) {
+        List<Item> newItemList = new ArrayList<>();
+        //获取数据集
+        if (dataValueType.equals(ConstantStr.DATA_SET)) {
+            LocalDateTime sDateTime = startDateTime;
+            LocalDateTime eDateTime;
+            //如果sDateTime小于endDateTime
+            while (sDateTime.isBefore(endDateTime)) {
+                if (bucketType.equals(ConstantStr.PERIOD_TIME_MONTH)) {
+                    eDateTime = sDateTime.plusMonths(bucketValue);
+                } else if (bucketType.equals(ConstantStr.PERIOD_TIME_DAY)) {
+                    eDateTime = sDateTime.plusDays(bucketValue);
+                } else if (bucketType.equals(ConstantStr.PERIOD_TIME_HOUR)) {
+                    eDateTime = sDateTime.plusHours(bucketValue);
+                } else if (bucketType.equals(ConstantStr.PERIOD_TIME_MINUTE)) {
+                    eDateTime = sDateTime.plusMinutes(bucketValue);
+                } else if (bucketType.equals(ConstantStr.PERIOD_TIME_SECOND)) {
+                    eDateTime = sDateTime.plusSeconds(bucketValue);
+                } else {
+                    throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "时段类型错误");
+                }
+                List<String> valueList = new ArrayList<>();
+                for (int i = 0; i < itemList.size(); i++) {
+                    Item item = itemList.get(i);
+                    LocalDateTime itemDateTime = LocalDateTime.parse(item.getTime(), DateTimeFormatter.ofPattern(timePattern));
+                    if (sDateTime.isAfter(itemDateTime)) {
+                        itemList.remove(i);
+                        i--;
+                        continue;
+                    }
+                    if (itemDateTime.isBefore(eDateTime)) {
+                        valueList.add(item.getValue());
+                        itemList.remove(i);
+                        i--;
+                    } else {
+                        break;
+                    }
+                }
+                //求出每段应该返回的值
+                newItemList.add(new Item()
+                        .value(countData(valueList, valueTakingMode))
+                        .time(sDateTime.format(DateTimeFormatter.ofPattern(timePattern))));
+                sDateTime = eDateTime;
+            }
+            return newItemList;
+        } else if (dataValueType.equals(ConstantStr.SINGLE_VALUE)) {
+            newItemList.add(new Item()
+                    .value(countData(itemList.stream().map(i ->
+                            i.getValue()).collect(Collectors.toList()), valueTakingMode))
+                    .time(startDateTime.format(DateTimeFormatter.ofPattern(timePattern))));
+        }
+        return newItemList;
+    }
+
+    /**
+     * 将传入的集合数据组,通过不同的取值模式,算出来
+     *
+     * @param valueList       数据集合
+     * @param valueTakingMode 取值模式:0平均值,1最大值,2最小值,3瞬时值,4求和,5计数
+     * @return
+     */
+    public String countData(List<String> valueList, String valueTakingMode) {
+        if (CollUtil.isNotEmpty(valueList)) {
+            if (valueTakingMode.equals(ConstantStr.AVERAGE_VALUE)) {
+                return valueList.stream().mapToDouble(Double::valueOf).average().getAsDouble() + "";
+            }
+            if (valueTakingMode.equals(ConstantStr.MAX_VALUE)) {
+                return valueList.stream().mapToDouble(Double::valueOf).max().getAsDouble() + "";
+            }
+            if (valueTakingMode.equals(ConstantStr.MIN_VALUE)) {
+                return valueList.stream().mapToDouble(Double::valueOf).min().getAsDouble() + "";
+            }
+            if (valueTakingMode.equals(ConstantStr.INSTANTANEOUS_VALUE)) {
+                return valueList.get(0);
+            }
+            if (valueTakingMode.equals(ConstantStr.SUM_VALUE)) {
+                return valueList.stream().mapToDouble(Double::valueOf).sum() + "";
+            }
+            if (valueTakingMode.equals(ConstantStr.COUNT_VALUE)) {
+                return valueList.stream().count() + "";
+            }
+        }
+        return null;
+    }
+
+}