|
@@ -0,0 +1,285 @@
|
|
|
+package com.example.opc_da.quartz.task;
|
|
|
+
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import com.example.opc_common.entity.*;
|
|
|
+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.RawDataDao;
|
|
|
+import com.example.opc_da.dao.ReportTableDao;
|
|
|
+import com.example.opc_da.task.AsyncAsyncTask;
|
|
|
+import com.example.opc_da.task.ReportTask;
|
|
|
+import com.example.opc_da.util.CronUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.quartz.JobDataMap;
|
|
|
+import org.quartz.JobExecutionContext;
|
|
|
+import org.quartz.JobExecutionException;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.scheduling.quartz.QuartzJobBean;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class AutoTableTask extends QuartzJobBean {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ReportTableDao reportTableDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RawDataDao rawDataDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AsyncAsyncTask asyncAsyncTask;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
|
|
|
+ JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
|
|
|
+ ReportTable reportTable = (ReportTable) jobDataMap.get("reportTable");
|
|
|
+ log.info("自动报表,{},执行开始,时间为{}", reportTable, DateUtil.dateChangeStrYmdhmss(new Date()));
|
|
|
+ Integer tableTemplateId = reportTable.getTableTemplateId();
|
|
|
+ TableTemplate tableTemplate = reportTableDao.getTableTemplateNoDataById(tableTemplateId);
|
|
|
+ if (Blank.isEmpty(tableTemplate)) {
|
|
|
+ log.info("报表模板不存在");
|
|
|
+ }
|
|
|
+ //通过报表关联的模板,查询出模板对应的统计图,以及统计图数据项信息
|
|
|
+ List<TableTemplateChart> tableTemplateChartList = reportTableDao.getTtcByTemplateId(tableTemplateId);
|
|
|
+ //生成一张新的子报表,并添加相应的日志,以及用户组授权
|
|
|
+ String id = IdUtil.createSnowflake(1, 1).nextId() + "";
|
|
|
+ asyncAsyncTask.addHaveKeyChReport(reportTable, id, ConstantStr.AUTOMATIC_GENERATE_REPORT, null);
|
|
|
+ //根据报表的cron和开始、结束时间,生成此次执行报表抓取数据的开始时间和结束时间
|
|
|
+ String cron = reportTable.getCron();
|
|
|
+ Date startTime = CronUtil.mappingAutoTime(cron, reportTable.getStartTime());
|
|
|
+ Date endTime = CronUtil.getTimeAfter(cron, startTime);
|
|
|
+ //获取此报表管理的报表模板,所关联的表格数据项
|
|
|
+ List<ReportTableItem> reportTableItemList = new ArrayList<>();
|
|
|
+ //通过时段类型,数据组id,数据源id,对数据项进行分组
|
|
|
+ List<TableTemplateTableItem> tableTemplateItemGroupList = reportTableDao.getTttiByTemplateIdGroup(tableTemplateId);
|
|
|
+ if (Blank.isNotEmpty(tableTemplateItemGroupList)) {
|
|
|
+ for (TableTemplateTableItem ttti : tableTemplateItemGroupList) {
|
|
|
+ Integer itemGroupId = ttti.getItemGroupId();
|
|
|
+ Integer dataSourceId = ttti.getDataSourceId();
|
|
|
+ Integer remainder = itemGroupId % ConstantStr.SUB_TABLE_NUM;
|
|
|
+ Integer bucketType = ttti.getBucketType();
|
|
|
+ //查询每个分组下的报表模板数据项信息
|
|
|
+ List<TableTemplateTableItem> tableTemplateItemList = reportTableDao.getTttiByGroup(tableTemplateId, itemGroupId, dataSourceId, bucketType);
|
|
|
+ List<RawData> rawDataList = new ArrayList<>();
|
|
|
+ List<CursorRawData> cursorRawDataList = new ArrayList<>();
|
|
|
+ //获取所有数据项的数据
|
|
|
+ if (bucketType.equals(ConstantStr.PERIOD_TIME_DAY) || bucketType.equals(ConstantStr.PERIOD_TIME_HOUR)
|
|
|
+ || bucketType.equals(ConstantStr.PERIOD_TIME_MINUTE) || bucketType.equals(ConstantStr.PERIOD_TIME_SECOND)) {
|
|
|
+ String startBelongTime;
|
|
|
+ String endBelongTime;
|
|
|
+
|
|
|
+ //从历史表中,查出历史数据;从临时表中,查出所有临时数据
|
|
|
+ if (bucketType.equals(ConstantStr.PERIOD_TIME_DAY)) {
|
|
|
+ startBelongTime = DateUtil.dateChangeStrYmdhms(startTime).substring(0, ConstantStr.TIME_DAY_STR.length());
|
|
|
+ endBelongTime = DateUtil.dateChangeStrYmdhms(endTime).substring(0, ConstantStr.TIME_DAY_STR.length());
|
|
|
+ rawDataList = rawDataDao.getTableDataGroupByDay(itemGroupId, remainder, dataSourceId, tableTemplateItemList, startBelongTime, endBelongTime);
|
|
|
+ cursorRawDataList = rawDataDao.getTableCursorDataGroupByDay(itemGroupId, dataSourceId, tableTemplateItemList, startBelongTime, endBelongTime);
|
|
|
+ } else {
|
|
|
+ startBelongTime = DateUtil.dateChangeStrYmdhms(startTime).substring(0, ConstantStr.TIME_HOUR_STR.length());
|
|
|
+ endBelongTime = DateUtil.dateChangeStrYmdhms(endTime).substring(0, ConstantStr.TIME_HOUR_STR.length());
|
|
|
+ rawDataList = rawDataDao.getTableDataGroup(itemGroupId, remainder, dataSourceId, tableTemplateItemList, startBelongTime, endBelongTime);
|
|
|
+ cursorRawDataList = rawDataDao.getTableCursorDataGroup(itemGroupId, dataSourceId, tableTemplateItemList, startBelongTime, endBelongTime);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "目前还未适配此种类型的取值模式");
|
|
|
+ }
|
|
|
+
|
|
|
+ for (TableTemplateTableItem t : tableTemplateItemList) {
|
|
|
+ //循环得到每个数据项的历史数据和临时数据,归属时间
|
|
|
+ Integer itemGroupId1 = t.getItemGroupId();
|
|
|
+ Integer dataSourceId1 = t.getDataSourceId();
|
|
|
+ String itemReadName = t.getItemReadName();
|
|
|
+ List<RawData> currentRawDataList = new ArrayList<>();
|
|
|
+ List<CursorRawData> currentCursorRawDataList = new ArrayList<>();
|
|
|
+ for (int i = 0; i < rawDataList.size(); i++) {
|
|
|
+ RawData rawData = rawDataList.get(i);
|
|
|
+ //生成历史数据
|
|
|
+ if (rawData.getItemGroupId().equals(itemGroupId1)
|
|
|
+ && rawData.getDataSourceId().equals(dataSourceId1)
|
|
|
+ && rawData.getItemName().equals(itemReadName)) {
|
|
|
+ currentRawDataList.add(rawData);
|
|
|
+// rawDataList.remove(i);
|
|
|
+// i--;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (int i = 0; i < cursorRawDataList.size(); i++) {
|
|
|
+ CursorRawData cursorRawData = cursorRawDataList.get(i);
|
|
|
+ //生成临时数据
|
|
|
+ if (cursorRawData.getItemGroupId().equals(itemGroupId1)
|
|
|
+ && cursorRawData.getDataSourceId().equals(dataSourceId1)
|
|
|
+ && cursorRawData.getItemName().equals(itemReadName)) {
|
|
|
+ currentCursorRawDataList.add(cursorRawData);
|
|
|
+// cursorRawDataList.remove(i);
|
|
|
+// i--;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //将所有数据组合成一个List<CursorRawData>
|
|
|
+ currentCursorRawDataList.addAll(ReportTask.genRawChangeCursor(currentRawDataList));
|
|
|
+ //对currentCursorRawDataList进行排序,使用dataValueTime排序
|
|
|
+ Collections.sort(currentCursorRawDataList, new Comparator<CursorRawData>() {
|
|
|
+ @Override
|
|
|
+ public int compare(CursorRawData o1, CursorRawData o2) {
|
|
|
+ return o1.getDataValueTime().compareTo(o2.getDataValueTime());
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ //计算出数据项的值集合
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ Integer valueTakingMode = t.getValueTakingMode();
|
|
|
+ Integer bucketValue = t.getBucketValue();
|
|
|
+ if (Blank.isNotEmpty(currentCursorRawDataList)) {
|
|
|
+ String dataType = currentCursorRawDataList.get(0).getDataType();
|
|
|
+ if (dataType.equals("boolean")) {
|
|
|
+ map = ReportTask.genBoolean(currentCursorRawDataList, startTime, endTime, valueTakingMode, bucketType, bucketValue);
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ map = ReportTask.genBigDecimal(currentCursorRawDataList, startTime, endTime, valueTakingMode, bucketType, bucketValue);
|
|
|
+ } catch (Exception e) {
|
|
|
+ map = ReportTask.genString(currentCursorRawDataList, startTime, endTime, bucketType, bucketValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ map = ReportTask.genString(currentCursorRawDataList, startTime, endTime, bucketType, bucketValue);
|
|
|
+ }
|
|
|
+ Integer valueType = t.getValueType();
|
|
|
+ boolean flage = valueType.equals(ConstantStr.CALCULATED_VALUE);
|
|
|
+ String valueList = map.get(flage ? "valueList" : "orgValueList");
|
|
|
+ String valueTimeList = map.get("valueTimeList");
|
|
|
+ String valueIndexList = map.get("valueIndexList");
|
|
|
+ Integer itemId = t.getItemId();
|
|
|
+ Integer xaxis = t.getXaxis();
|
|
|
+ Integer yaxis = t.getYaxis();
|
|
|
+ String standby = t.getStandby();
|
|
|
+ //将数据项的信息放到集合中
|
|
|
+ reportTableItemList.add(new ReportTableItem(
|
|
|
+ id, itemId, valueType, xaxis, yaxis, standby, valueList, valueTimeList, valueIndexList
|
|
|
+ ));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (Blank.isNotEmpty(reportTableItemList)) {
|
|
|
+ reportTableDao.addReportTableItemList(reportTableItemList);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Blank.isNotEmpty(tableTemplateChartList)) {
|
|
|
+ for (TableTemplateChart ttc : tableTemplateChartList) {
|
|
|
+ Integer valueTakingMode = ttc.getValueTakingMode();
|
|
|
+ Integer valueType = ttc.getValueType();
|
|
|
+ Integer bucketType = ttc.getBucketType();
|
|
|
+ Integer bucketValue = ttc.getBucketValue();
|
|
|
+ boolean flage = valueType.equals(ConstantStr.CALCULATED_VALUE);
|
|
|
+ //根据模板中的统计图生成相应的报表统计图,并添加
|
|
|
+ ReportChart reportChart = new ReportChart(
|
|
|
+ id, ttc.getId(), ttc.getChartType(), ttc.getChartName(), valueType, ttc.getStandby()
|
|
|
+ );
|
|
|
+ reportTableDao.addReportChart(reportChart);
|
|
|
+ Integer reportChartId = reportChart.getId();
|
|
|
+ //获取模板统计图中的数据项信息
|
|
|
+ List<TableTemplateChartItem> tableTemplateChartItemList = ttc.getTableTemplateChartItemList();
|
|
|
+ List<ReportChartItem> reportChartItemList = new ArrayList<>();
|
|
|
+ if (Blank.isNotEmpty(tableTemplateChartItemList)) {
|
|
|
+ TableTemplateChartItem tableTemplateChartItem = tableTemplateChartItemList.get(0);
|
|
|
+ Integer itemGroupId = tableTemplateChartItem.getItemGroupId();
|
|
|
+ Integer dataSourceId = tableTemplateChartItem.getDataSourceId();
|
|
|
+ Integer remainder = itemGroupId % ConstantStr.SUB_TABLE_NUM;
|
|
|
+ List<RawData> rawDataList = new ArrayList<>();
|
|
|
+ List<CursorRawData> cursorRawDataList = new ArrayList<>();
|
|
|
+ if (bucketType.equals(ConstantStr.PERIOD_TIME_DAY) || bucketType.equals(ConstantStr.PERIOD_TIME_HOUR)
|
|
|
+ || bucketType.equals(ConstantStr.PERIOD_TIME_MINUTE) || bucketType.equals(ConstantStr.PERIOD_TIME_SECOND)) {
|
|
|
+ String startBelongTime;
|
|
|
+ String endBelongTime;
|
|
|
+
|
|
|
+ //从历史表中,查出历史数据;从临时表中,查出所有临时数据
|
|
|
+ if (bucketType.equals(ConstantStr.PERIOD_TIME_DAY)) {
|
|
|
+ startBelongTime = DateUtil.dateChangeStrYmdhms(startTime).substring(0, ConstantStr.TIME_DAY_STR.length());
|
|
|
+ endBelongTime = DateUtil.dateChangeStrYmdhms(endTime).substring(0, ConstantStr.TIME_DAY_STR.length());
|
|
|
+ rawDataList = rawDataDao.getTableChartDataGroupByDay(itemGroupId, remainder, dataSourceId, tableTemplateChartItemList, startBelongTime, endBelongTime);
|
|
|
+ cursorRawDataList = rawDataDao.getTableChartCursorDataGroupByDay(itemGroupId, dataSourceId, tableTemplateChartItemList, startBelongTime, endBelongTime);
|
|
|
+ } else {
|
|
|
+ startBelongTime = DateUtil.dateChangeStrYmdhms(startTime).substring(0, ConstantStr.TIME_HOUR_STR.length());
|
|
|
+ endBelongTime = DateUtil.dateChangeStrYmdhms(endTime).substring(0, ConstantStr.TIME_HOUR_STR.length());
|
|
|
+ rawDataList = rawDataDao.getTableChartDataGroup(itemGroupId, remainder, dataSourceId, tableTemplateChartItemList, startBelongTime, endBelongTime);
|
|
|
+ cursorRawDataList = rawDataDao.getTableChartCursorDataGroup(itemGroupId, dataSourceId, tableTemplateChartItemList, startBelongTime, endBelongTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (TableTemplateChartItem t : tableTemplateChartItemList) {
|
|
|
+ Integer itemGroupId1 = t.getItemGroupId();
|
|
|
+ Integer dataSourceId1 = t.getDataSourceId();
|
|
|
+ Integer itemId = t.getItemId();
|
|
|
+ String itemReadName = t.getItemReadName();
|
|
|
+ List<RawData> currentRawDataList = new ArrayList<>();
|
|
|
+ List<CursorRawData> currentCursorRawDataList = new ArrayList<>();
|
|
|
+ for (int i = 0; i < rawDataList.size(); i++) {
|
|
|
+ RawData rawData = rawDataList.get(i);
|
|
|
+ //生成历史数据
|
|
|
+ if (rawData.getItemGroupId().equals(itemGroupId1)
|
|
|
+ && rawData.getDataSourceId().equals(dataSourceId1)
|
|
|
+ && rawData.getItemName().equals(itemReadName)) {
|
|
|
+ currentRawDataList.add(rawData);
|
|
|
+// rawDataList.remove(i);
|
|
|
+// i--;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (int i = 0; i < cursorRawDataList.size(); i++) {
|
|
|
+ CursorRawData cursorRawData = cursorRawDataList.get(i);
|
|
|
+ //生成临时数据
|
|
|
+ if (cursorRawData.getItemGroupId().equals(itemGroupId1)
|
|
|
+ && cursorRawData.getDataSourceId().equals(dataSourceId1)
|
|
|
+ && cursorRawData.getItemName().equals(itemReadName)) {
|
|
|
+ currentCursorRawDataList.add(cursorRawData);
|
|
|
+// cursorRawDataList.remove(i);
|
|
|
+// i--;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //将所有数据组合成一个List<CursorRawData>
|
|
|
+ currentCursorRawDataList.addAll(ReportTask.genRawChangeCursor(currentRawDataList));
|
|
|
+ //对currentCursorRawDataList进行排序,使用dataValueTime排序
|
|
|
+ Collections.sort(currentCursorRawDataList, new Comparator<CursorRawData>() {
|
|
|
+ @Override
|
|
|
+ public int compare(CursorRawData o1, CursorRawData o2) {
|
|
|
+ return o1.getDataValueTime().compareTo(o2.getDataValueTime());
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ //计算出数据项的值集合
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ if (Blank.isNotEmpty(currentCursorRawDataList)) {
|
|
|
+ String dataType = currentCursorRawDataList.get(0).getDataType();
|
|
|
+ if (dataType.equals("boolean")) {
|
|
|
+ map = ReportTask.genBoolean(currentCursorRawDataList, startTime, endTime, valueTakingMode, bucketType, bucketValue);
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ map = ReportTask.genBigDecimal(currentCursorRawDataList, startTime, endTime, valueTakingMode, bucketType, bucketValue);
|
|
|
+ } catch (Exception e) {
|
|
|
+ map = ReportTask.genString(currentCursorRawDataList, startTime, endTime, bucketType, bucketValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ map = ReportTask.genString(currentCursorRawDataList, startTime, endTime, bucketType, bucketValue);
|
|
|
+ }
|
|
|
+ String valueList = map.get(flage ? "valueList" : "orgValueList");
|
|
|
+ String valueTimeList = map.get("valueTimeList");
|
|
|
+ String valueIndexList = map.get("valueIndexList");
|
|
|
+ reportChartItemList.add(new ReportChartItem(
|
|
|
+ reportChartId, itemId, valueList, valueTimeList, valueIndexList
|
|
|
+ ));
|
|
|
+ }
|
|
|
+ //添加报表统计图相关的数据项
|
|
|
+ if (Blank.isNotEmpty(reportChartItemList)) {
|
|
|
+ reportTableDao.addReportChartItem(reportChartItemList);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "目前还未适配此种类型的取值模式");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("自动报表,{},执行结束,时间为{}", reportTable, DateUtil.dateChangeStrYmdhmss(new Date()));
|
|
|
+ }
|
|
|
+}
|