|
@@ -0,0 +1,212 @@
|
|
|
+package com.example.opc_da.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.example.opc_common.entity.*;
|
|
|
+import com.example.opc_common.enums.ResultEnum;
|
|
|
+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_common.util.Result;
|
|
|
+import com.example.opc_da.dao.ChartDao;
|
|
|
+import com.example.opc_da.dao.ItemGroupDao;
|
|
|
+import com.example.opc_da.dao.RawDataDao;
|
|
|
+import com.example.opc_da.service.ChartService;
|
|
|
+import com.example.opc_da.task.ReportTask;
|
|
|
+import com.example.opc_da.util.UserUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Transactional
|
|
|
+public class ChartServiceImpl implements ChartService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ChartDao chartDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserUtil userUtil;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ItemGroupDao itemGroupDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RawDataDao rawDataDao;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public synchronized Result addChart(Chart chart) {
|
|
|
+ String userId = userUtil.getCurrentUserId();
|
|
|
+ chart.setUserId(userId);
|
|
|
+ if (chartDao.addChart(chart) <= 0) {
|
|
|
+ return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "添加统计图失败");
|
|
|
+ }
|
|
|
+ chartDao.addChartItem(chart.getId(), chart.getChartItemList());
|
|
|
+ return Result.ok("添加统计图成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public synchronized Result updateChart(Chart chart) {
|
|
|
+ if (chartDao.updateChart(chart) <= 0) {
|
|
|
+ return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "修改统计图失败");
|
|
|
+ }
|
|
|
+ return Result.ok("修改统计图成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result getChartById(Integer id) {
|
|
|
+ Chart chart = chartDao.getChartById(id);
|
|
|
+ List<ChartItem> chartItemList = chartDao.getChartItemList(id);
|
|
|
+ if (Blank.isNotEmpty(chartItemList)) {
|
|
|
+ Integer valueType = chart.getValueType();
|
|
|
+ Boolean flage = valueType.equals(ConstantStr.CALCULATED_VALUE);
|
|
|
+ Integer valueTakingMode = chart.getValueTakingMode();
|
|
|
+ Integer bucketType = chart.getBucketType();
|
|
|
+ Integer bucketValue = chart.getBucketValue();
|
|
|
+ Date startTime = chart.getStartTime();
|
|
|
+ Date endTime = chart.getEndTime();
|
|
|
+ String pattern = null;
|
|
|
+ if (bucketType.equals(ConstantStr.PERIOD_TIME_DAY)) {
|
|
|
+ pattern = ConstantStr.TIME_DAY_STR;
|
|
|
+ } else if (bucketType.equals(ConstantStr.PERIOD_TIME_HOUR)) {
|
|
|
+ pattern = ConstantStr.TIME_HOUR_STR;
|
|
|
+ } else if (bucketType.equals(ConstantStr.PERIOD_TIME_MINUTE)) {
|
|
|
+ pattern = ConstantStr.TIME_MINUTE_STR;
|
|
|
+ } else if (bucketType.equals(ConstantStr.PERIOD_TIME_SECOND)) {
|
|
|
+ pattern = ConstantStr.TIME_SECOND_STR;
|
|
|
+ }
|
|
|
+ int timeLength = DateUtil.timeDifference(startTime, endTime, pattern, bucketValue);
|
|
|
+ for (int i = 0; i < chartItemList.size(); i++) {
|
|
|
+ ChartItem chartItem = chartItemList.get(i);
|
|
|
+ Integer itemId = chartItem.getItemId();
|
|
|
+ Item item = itemGroupDao.getItemById(itemId);
|
|
|
+ Integer itemGroupId = item.getItemGroupId();
|
|
|
+ Integer remainder = itemGroupId % ConstantStr.SUB_TABLE_NUM;
|
|
|
+ ItemGroup itemGroup = itemGroupDao.getItemGroupById(itemGroupId);
|
|
|
+ Integer dataSourceId = itemGroup.getDataSourceId();
|
|
|
+ String itemName = item.getItemName();
|
|
|
+ if (Blank.isEmpty(item)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Integer itemType = item.getItemType();
|
|
|
+ long startTimeTime = startTime.getTime();
|
|
|
+ long endTimeTime;
|
|
|
+ if (itemType.equals(ConstantStr.ATTACH_ATTRIBUTES)) {
|
|
|
+ AttachRawData attachRawData = rawDataDao.getAttachRawData(itemGroupId, dataSourceId, itemName);
|
|
|
+ String dataType = attachRawData.getDataType();
|
|
|
+ if (!dataType.toLowerCase().equals("boolean")) {
|
|
|
+ try {
|
|
|
+ List<BigDecimal> valueList = new ArrayList<>();
|
|
|
+ List<String> valueTimeList = new ArrayList<>();
|
|
|
+ List<Long> valueIndexList = new ArrayList<>();
|
|
|
+ BigDecimal bigDecimal = JSON.parseObject(flage ? attachRawData.getDataValue() : attachRawData.getOrgDataValue(), BigDecimal.class);
|
|
|
+ for (int j = 0; j < timeLength; j++) {
|
|
|
+ if (i != timeLength - 1) {
|
|
|
+ endTimeTime = DateUtil.addTimeStamp(startTimeTime, pattern, bucketValue);
|
|
|
+ } else {
|
|
|
+ endTimeTime = endTime.getTime();
|
|
|
+ }
|
|
|
+ valueList.add(bigDecimal);
|
|
|
+ valueTimeList.add(DateUtil.dateChangeStrYmdhmss(new Date(startTimeTime)));
|
|
|
+ valueIndexList.add((long) j);
|
|
|
+ startTimeTime = endTimeTime;
|
|
|
+ }
|
|
|
+ chartItem.setValueList(valueList.toString());
|
|
|
+ chartItem.setValueTimeList(valueTimeList.toString());
|
|
|
+ chartItem.setValueIndexList(valueIndexList.toString());
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ String startBelongTime;
|
|
|
+ String endBelongTime;
|
|
|
+ List<RawData> rawDataList = new ArrayList<>();
|
|
|
+ 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.getTableDataByDay(itemGroupId, remainder, dataSourceId, itemName, 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.getTableData(itemGroupId, remainder, dataSourceId, itemName, startBelongTime, endBelongTime);
|
|
|
+ }
|
|
|
+ List<String> sqlCurrentYmdhList = rawDataDao.getCursorYmdh(itemGroupId, dataSourceId, itemName, startBelongTime, endBelongTime);
|
|
|
+ if (Blank.isNotEmpty(sqlCurrentYmdhList)) {
|
|
|
+ for (String sqlCurrentYmdh : sqlCurrentYmdhList) {
|
|
|
+ List<CursorRawData> cursorRawDataList = rawDataDao.getTableCursorData(itemGroupId, dataSourceId, itemName, sqlCurrentYmdh);
|
|
|
+ rawDataList = ReportTask.genRawAddCursor(rawDataList, cursorRawDataList, itemGroupId, dataSourceId, sqlCurrentYmdh);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (Blank.isNotEmpty(rawDataList)) {
|
|
|
+ String dataType = rawDataList.get(0).getDataType();
|
|
|
+ if (dataType.equals("boolean")) {
|
|
|
+// map = ReportTask.genBoolean(rawDataList, startTime, endTime, valueTakingMode, bucketType, bucketValue);
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+// map = ReportTask.genBigDecimal(rawDataList, startTime, endTime, valueTakingMode, bucketType, bucketValue);
|
|
|
+ } catch (Exception e) {
|
|
|
+// map = ReportTask.genString(rawDataList, startTime, endTime, bucketType, bucketValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ chart.setChartItemList(chartItemList);
|
|
|
+ return Result.ok(chart);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result getAllChart(Integer page, Integer limit) {
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ String userId = userUtil.getCurrentUserId();
|
|
|
+ Long count = chartDao.getChartCount(userId);
|
|
|
+ Long startNum = Long.valueOf((page - 1) * limit);
|
|
|
+ List<Chart> chartList = chartDao.getAllChart(userId, startNum, Long.valueOf(limit));
|
|
|
+ jsonObject.put("count", count);
|
|
|
+ jsonObject.put("chartList", chartList);
|
|
|
+ return Result.ok(jsonObject);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result chartAssignUserGroup(Chart chart) {
|
|
|
+ Integer id = chart.getId();
|
|
|
+ chartDao.delChartUserGroup(id);
|
|
|
+ if (Blank.isNotEmpty(chart.getUserGroupList())) {
|
|
|
+ if (chartDao.addChartUserGroup(id, chart.getUserGroupList()) <= 0) {
|
|
|
+ return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "分配用户组失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Result.ok("分配用户组成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result getAllOkChart(Integer page, Integer limit, String chartType, String chartName) {
|
|
|
+ String userId = userUtil.getCurrentUserId();
|
|
|
+ Long startNum = Long.valueOf((page - 1) * limit);
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("count", chartDao.getAllOkChartCount(userId, chartType, chartName));
|
|
|
+ jsonObject.put("reportTableList", chartDao.getAllOkChart(startNum, Long.valueOf(limit), userId, chartType, chartName));
|
|
|
+ return Result.ok(jsonObject);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result delChartById(Integer id) {
|
|
|
+ Chart chart = chartDao.getChartById(id);
|
|
|
+ if (!chart.getUserId().equals(userUtil.getCurrentUserId())) {
|
|
|
+ return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "这是别人分享的统计图,不能删除");
|
|
|
+ }
|
|
|
+ if (chartDao.delChartById(id) <= 0) {
|
|
|
+ return Result.no(ResultEnum.SERVER_ERROR.getRespCode(), "删除统计图失败");
|
|
|
+ }
|
|
|
+ chartDao.delChartItem(id);
|
|
|
+ chartDao.delChartUserGroup(id);
|
|
|
+ return Result.ok("删除统计图成功");
|
|
|
+ }
|
|
|
+}
|