|
@@ -0,0 +1,642 @@
|
|
|
+package com.tofly.scada.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.tofly.scada.common.FlowConstant;
|
|
|
+import com.tofly.scada.entity.Scada;
|
|
|
+import com.tofly.scada.entity.StatisticsScada;
|
|
|
+import com.tofly.scada.entity.dto.ScadaQuery;
|
|
|
+import com.tofly.scada.entity.vo.ScadaVo;
|
|
|
+import com.tofly.scada.mapper.ScadaMapper;
|
|
|
+import com.tofly.scada.service.ScadaService;
|
|
|
+import com.tofly.scada.service.StatisticsScadaService;
|
|
|
+import oracle.sql.DATE;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.scheduling.annotation.EnableScheduling;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.sql.Wrapper;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+
|
|
|
+ * @author HaiQiu
|
|
|
+ * @date 2022/4/25
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+public class ScadaServiceImpl implements ScadaService {
|
|
|
+
|
|
|
+ private final Logger logger = LoggerFactory.getLogger(ScadaServiceImpl.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ScadaMapper scadaMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private StatisticsScadaService statisticsScadaService;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 根据当前时间动态自动创建月份表,创建完成之后返回表名
|
|
|
+ *
|
|
|
+ * 往后往前推迟月份,1即创建下一月的表,-1表示创建上个月表
|
|
|
+ * @return 月份表名
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 同步24小时数据,支持自定义时间添加
|
|
|
+ *
|
|
|
+ */
|
|
|
+
|
|
|
+ @Scheduled(cron = "0 0 * * * ? ")
|
|
|
+
|
|
|
+ public void insert24H() {
|
|
|
+ logger.info("同步scada历史数据到24小时表:正在进行时间调整");
|
|
|
+
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ Date date = new Date();
|
|
|
+ calendar.setTime(date);
|
|
|
+
|
|
|
+ calendar.add(Calendar.HOUR_OF_DAY, -1);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
+ Date startTime = calendar.getTime();
|
|
|
+ String start = DateUtil.format(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+ calendar.set(Calendar.MINUTE, 59);
|
|
|
+ calendar.set(Calendar.SECOND, 59);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 999);
|
|
|
+ Date endTime = calendar.getTime();
|
|
|
+ String end = DateUtil.format(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ logger.info("同步scada历史数据到24小时表:正在进行查询历史数据");
|
|
|
+
|
|
|
+ List<Scada> scadas = scadaMapper.selectHistoryByTime(FlowConstant.SCADA_HISTORY, start, end);
|
|
|
+ if (!CollectionUtils.isEmpty(scadas)) {
|
|
|
+ logger.info("同步scada历史数据到24小时表:正在进行数据分组");
|
|
|
+
|
|
|
+ Map<String, List<Scada>> listMap = scadas.stream().filter(scada -> scada.getCode() != null && scada.getValue() != null)
|
|
|
+ .collect(Collectors.groupingBy(Scada::getCode));
|
|
|
+ logger.info("同步scada历史数据到24小时表:正在进行分组循环计算");
|
|
|
+
|
|
|
+ saveStatisticsData(startTime, endTime, listMap,FlowConstant.INTEGER_ZERO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void insert24H(Date time) {
|
|
|
+ logger.info("同步scada历史数据到24小时表:正在进行时间调整");
|
|
|
+
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ Date date = time == null ? new Date() : time;
|
|
|
+ calendar.setTime(date);
|
|
|
+
|
|
|
+ calendar.add(Calendar.HOUR_OF_DAY, -1);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
+ Date startTime = calendar.getTime();
|
|
|
+ String start = DateUtil.format(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+ calendar.set(Calendar.MINUTE, 59);
|
|
|
+ calendar.set(Calendar.SECOND, 59);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 999);
|
|
|
+ Date endTime = calendar.getTime();
|
|
|
+ String end = DateUtil.format(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ logger.info("同步scada历史数据到24小时表:正在进行查询历史数据");
|
|
|
+
|
|
|
+ List<Scada> scadas = scadaMapper.selectHistoryByTime(FlowConstant.SCADA_HISTORY, start, end);
|
|
|
+ if (!CollectionUtils.isEmpty(scadas)) {
|
|
|
+ logger.info("同步scada历史数据到24小时表:正在进行数据分组");
|
|
|
+
|
|
|
+ Map<String, List<Scada>> listMap = scadas.stream().filter(scada -> scada.getCode() != null && scada.getValue() != null)
|
|
|
+ .collect(Collectors.groupingBy(Scada::getCode));
|
|
|
+ logger.info("同步scada历史数据到24小时表:正在进行分组循环计算");
|
|
|
+
|
|
|
+ saveStatisticsData(startTime, endTime, listMap,FlowConstant.INTEGER_ZERO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 同步每天数据,每天凌晨一点半同步昨天的数据
|
|
|
+ *
|
|
|
+ */
|
|
|
+ @Scheduled(cron = "0 30 1 * * ? ")
|
|
|
+
|
|
|
+ public void syncDaysData() {
|
|
|
+ logger.info("同步scada历史数据到表:正在进行时间调整");
|
|
|
+
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ Date date = new Date();
|
|
|
+ calendar.setTime(date);
|
|
|
+
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, -1);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
+ Date startTime = calendar.getTime();
|
|
|
+ String start = DateUtil.format(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ calendar.set(Calendar.MINUTE, 59);
|
|
|
+ calendar.set(Calendar.SECOND, 59);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 999);
|
|
|
+ Date endTime = calendar.getTime();
|
|
|
+ String end = DateUtil.format(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ logger.info("同步scada历史数据到24小时表:正在进行查询历史数据");
|
|
|
+
|
|
|
+ List<Scada> scadas = scadaMapper.selectHistoryByTime(FlowConstant.SCADA_HISTORY, start, end);
|
|
|
+ if (!CollectionUtils.isEmpty(scadas)) {
|
|
|
+ logger.info("同步scada历史数据到表:正在进行数据分组");
|
|
|
+
|
|
|
+ Map<String, List<Scada>> listMap = scadas.stream().filter(scada -> scada.getCode() != null && scada.getValue() != null)
|
|
|
+ .collect(Collectors.groupingBy(Scada::getCode));
|
|
|
+ logger.info("同步scada历史数据到表:正在进行分组循环计算");
|
|
|
+
|
|
|
+ saveStatisticsData(startTime, endTime, listMap,FlowConstant.INTEGER_ONE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void syncDaysData(Date time) {
|
|
|
+ logger.info("同步scada历史数据到表:正在进行时间调整");
|
|
|
+
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(time == null ? new Date() : time);
|
|
|
+
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, -1);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
+ Date startTime = calendar.getTime();
|
|
|
+ String start = DateUtil.format(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ calendar.set(Calendar.MINUTE, 59);
|
|
|
+ calendar.set(Calendar.SECOND, 59);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 999);
|
|
|
+ Date endTime = calendar.getTime();
|
|
|
+ String end = DateUtil.format(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ logger.info("同步scada历史数据到24小时表:正在进行查询历史数据");
|
|
|
+
|
|
|
+ List<Scada> scadas = scadaMapper.selectHistoryByTime(FlowConstant.SCADA_HISTORY, start, end);
|
|
|
+ if (!CollectionUtils.isEmpty(scadas)) {
|
|
|
+ logger.info("同步scada历史数据到表:正在进行数据分组");
|
|
|
+
|
|
|
+ Map<String, List<Scada>> listMap = scadas.stream().filter(scada -> scada.getCode() != null && scada.getValue() != null)
|
|
|
+ .collect(Collectors.groupingBy(Scada::getCode));
|
|
|
+ logger.info("同步scada历史数据到表:正在进行分组循环计算");
|
|
|
+
|
|
|
+ saveStatisticsData(startTime, endTime, listMap,FlowConstant.INTEGER_ONE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 每月1号凌晨1点同步更新历史数据与上个月表数据,支持自定义时间添加
|
|
|
+ *
|
|
|
+ */
|
|
|
+ @Scheduled(cron = "0 0 1 1 * ? ")
|
|
|
+
|
|
|
+ public void syncDataIntoMonths() {
|
|
|
+ logger.info("同步scada历史数据到月份表:正在进行时间调整");
|
|
|
+
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(new Date());
|
|
|
+
|
|
|
+ calendar.add(Calendar.MONTH, -1);
|
|
|
+ calendar.set(Calendar.DATE, 1);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
+ Date startTime = calendar.getTime();
|
|
|
+ String start = DateUtil.format(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+ calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ calendar.set(Calendar.MINUTE, 59);
|
|
|
+ calendar.set(Calendar.SECOND, 59);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 999);
|
|
|
+ Date endTime = calendar.getTime();
|
|
|
+ String end = DateUtil.format(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ logger.info("同步scada历史数据到月份表:正在进行历史数据查询....");
|
|
|
+ List<Scada> scadas = scadaMapper.selectHistoryByTime(FlowConstant.SCADA_HISTORY, start, end);
|
|
|
+ logger.info("同步scada历史数据到月份表:历史数据查询完成,数据存在" + !CollectionUtils.isEmpty(scadas));
|
|
|
+ if (!CollectionUtils.isEmpty(scadas)) {
|
|
|
+ logger.info("同步scada历史数据到24小时表:正在进行数据分组");
|
|
|
+
|
|
|
+ Map<String, List<Scada>> listMap = scadas.stream().filter(scada -> scada.getCode() != null && scada.getValue() != null)
|
|
|
+ .collect(Collectors.groupingBy(Scada::getCode));
|
|
|
+
|
|
|
+ saveStatisticsData(startTime, endTime, listMap,FlowConstant.INTEGER_TWO);
|
|
|
+ }
|
|
|
+ logger.info("同步scada历史数据到月份表:操作完成");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void syncDataIntoMonths(Date time) {
|
|
|
+ logger.info("同步scada历史数据到月份表:正在进行时间调整");
|
|
|
+
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(time == null ? new Date() : time);
|
|
|
+
|
|
|
+ calendar.add(Calendar.MONTH, -1);
|
|
|
+ calendar.set(Calendar.DATE, 1);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
+ Date startTime = calendar.getTime();
|
|
|
+ String start = DateUtil.format(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+ calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ calendar.set(Calendar.MINUTE, 59);
|
|
|
+ calendar.set(Calendar.SECOND, 59);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 999);
|
|
|
+ Date endTime = calendar.getTime();
|
|
|
+ String end = DateUtil.format(calendar.getTime(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ logger.info("同步scada历史数据到月份表:正在进行历史数据查询....");
|
|
|
+ List<Scada> scadas = scadaMapper.selectHistoryByTime(FlowConstant.SCADA_HISTORY, start, end);
|
|
|
+ logger.info("同步scada历史数据到月份表:历史数据查询完成,数据存在" + !CollectionUtils.isEmpty(scadas));
|
|
|
+ if (!CollectionUtils.isEmpty(scadas)) {
|
|
|
+ logger.info("同步scada历史数据到24小时表:正在进行数据分组");
|
|
|
+
|
|
|
+ Map<String, List<Scada>> listMap = scadas.stream().filter(scada -> scada.getCode() != null && scada.getValue() != null)
|
|
|
+ .collect(Collectors.groupingBy(Scada::getCode));
|
|
|
+
|
|
|
+ saveStatisticsData(startTime, endTime, listMap,FlowConstant.INTEGER_TWO);
|
|
|
+ }
|
|
|
+ logger.info("同步scada历史数据到月份表:操作完成");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 设置统计数据
|
|
|
+ * @param startTime 开始时间
|
|
|
+ * @param endTime 结束时间
|
|
|
+ * @param listMap 数据集合分组
|
|
|
+ */
|
|
|
+ private void saveStatisticsData(Date startTime, Date endTime, Map<String, List<Scada>> listMap,Integer DateType) {
|
|
|
+ for (Map.Entry<String, List<Scada>> entity : listMap.entrySet()) {
|
|
|
+ logger.info("统计添加数据中。。。。");
|
|
|
+ List<Scada> scadaList = entity.getValue();
|
|
|
+ double average = scadaList.stream().map(scada -> Double.parseDouble(scada.getValue()))
|
|
|
+ .mapToDouble(Double::shortValue).average().getAsDouble();
|
|
|
+ double max = scadaList.stream().map(scada -> Double.parseDouble(scada.getValue()))
|
|
|
+ .mapToDouble(Double::shortValue).max().getAsDouble();
|
|
|
+ double min = scadaList.stream().map(scada -> Double.parseDouble(scada.getValue()))
|
|
|
+ .mapToDouble(Double::shortValue).min().getAsDouble();
|
|
|
+ double sum = scadaList.stream().map(scada -> Double.parseDouble(scada.getValue()))
|
|
|
+ .mapToDouble(Double::shortValue).sum();
|
|
|
+
|
|
|
+ StatisticsScada statisticsScada = new StatisticsScada();
|
|
|
+ statisticsScada.setCode(scadaList.get(0).getCode());
|
|
|
+ statisticsScada.setValue(scadaList.get(0).getValue());
|
|
|
+ statisticsScada.setAve(average);
|
|
|
+ statisticsScada.setMax(max);
|
|
|
+ statisticsScada.setMin(min);
|
|
|
+ statisticsScada.setFold(sum);
|
|
|
+ statisticsScada.setStartTime(startTime);
|
|
|
+ statisticsScada.setEndTime(endTime);
|
|
|
+ statisticsScada.setStatisticsType(DateType);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("START_TIME", statisticsScada.getStartTime());
|
|
|
+ map.put("END_TIME", statisticsScada.getEndTime());
|
|
|
+ map.put("VALUE", statisticsScada.getValue());
|
|
|
+ map.put("CODE", statisticsScada.getCode());
|
|
|
+ statisticsScadaService.removeByMap(map);
|
|
|
+
|
|
|
+ statisticsScadaService.save(statisticsScada);
|
|
|
+ logger.info("统计添加数据完成");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Async
|
|
|
+ public void insertMouth(List<Scada> scadas, String tableName) {
|
|
|
+ int size = scadas.size();
|
|
|
+ int i = 0;
|
|
|
+ while (size > 600) {
|
|
|
+ scadaMapper.insertMouth(scadas.subList(i, i + 600), tableName);
|
|
|
+ i = i + 600;
|
|
|
+ size = size - 600;
|
|
|
+ }
|
|
|
+ if (size > 0) {
|
|
|
+ scadaMapper.insertMouth(scadas.subList(i, i + size), tableName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Scada> getListData() {
|
|
|
+ return scadaMapper.getListData();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean fetch24HoursData(String syncTime) {
|
|
|
+ Date date = null;
|
|
|
+ if (StringUtils.isEmpty(syncTime)) {
|
|
|
+ date = new Date();
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ } else {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
|
|
|
+ try {
|
|
|
+ Date parse = format.parse(syncTime);
|
|
|
+ Date now = new Date();
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(now);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ if (parse.getTime() >= now.getTime()) {
|
|
|
+ throw new RuntimeException("时间参数不允许大于等于当前时间:" + format.format(now));
|
|
|
+ }
|
|
|
+ date = parse;
|
|
|
+ } catch (ParseException e) {
|
|
|
+ throw new RuntimeException("时间参数格式传入错误:" + e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ insert24H(date);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void fetchMonthsData(String syncTime) {
|
|
|
+ Date date = null;
|
|
|
+ if (StringUtils.isEmpty(syncTime)) {
|
|
|
+ date = new Date();
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ } else {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ try {
|
|
|
+ Date parse = format.parse(syncTime);
|
|
|
+ Date now = new Date();
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(now);
|
|
|
+ if (parse.getTime() > now.getTime()) {
|
|
|
+ throw new RuntimeException("时间参数不允许大于当前时间:" + format.format(now));
|
|
|
+ }
|
|
|
+ date = parse;
|
|
|
+ } catch (ParseException e) {
|
|
|
+ throw new RuntimeException("时间参数格式传入错误:" + e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ syncDataIntoMonths(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void fetchDaysData(String syncTime) {
|
|
|
+ Date date = null;
|
|
|
+ if (StringUtils.isEmpty(syncTime)) {
|
|
|
+ date = new Date();
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ } else {
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ try {
|
|
|
+ Date parse = format.parse(syncTime);
|
|
|
+ Date now = new Date();
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(now);
|
|
|
+ if (parse.getTime() > now.getTime()) {
|
|
|
+ throw new RuntimeException("时间参数不允许大于当前时间:" + format.format(now));
|
|
|
+ }
|
|
|
+ date = parse;
|
|
|
+ } catch (ParseException e) {
|
|
|
+ throw new RuntimeException("时间参数格式传入错误:" + e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ syncDaysData(date);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<ScadaVo> get24HoursData(Page page, ScadaQuery scadaQuery) {
|
|
|
+ QueryWrapper<ScadaQuery> queryWrapper = new QueryWrapper<>();
|
|
|
+ if (StringUtils.hasText(scadaQuery.getCode())) {
|
|
|
+ queryWrapper.like("hours.CODE", scadaQuery.getCode());
|
|
|
+ }
|
|
|
+ if (StringUtils.hasText(scadaQuery.getValue())) {
|
|
|
+ queryWrapper.like("hours.VALUE", scadaQuery.getValue());
|
|
|
+ }
|
|
|
+ if (scadaQuery.getStart() != null && scadaQuery.getEnd() != null) {
|
|
|
+ queryWrapper.between("hours.SCADA_TIME", scadaQuery.getStart(), scadaQuery.getEnd());
|
|
|
+ }
|
|
|
+ queryWrapper.orderByDesc("hours.SCADA_TIME");
|
|
|
+ return scadaMapper.pageList(page, queryWrapper, "SCADA_HOURS");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page getMonthsData(Page page, ScadaQuery scadaQuery) {
|
|
|
+ QueryWrapper<ScadaQuery> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.orderByDesc("hours.SCADA_TIME");
|
|
|
+ if (StringUtils.hasText(scadaQuery.getCode())) {
|
|
|
+ queryWrapper.like("hours.CODE", scadaQuery.getCode());
|
|
|
+ }
|
|
|
+ if (StringUtils.hasText(scadaQuery.getValue())) {
|
|
|
+ queryWrapper.like("hours.VALUE", scadaQuery.getValue());
|
|
|
+ }
|
|
|
+ if (scadaQuery.getStart() != null && scadaQuery.getEnd() != null) {
|
|
|
+
|
|
|
+ if (scadaQuery.getStart().getYear() == scadaQuery.getEnd().getYear() &&
|
|
|
+ scadaQuery.getStart().getMonth() == scadaQuery.getEnd().getMonth()) {
|
|
|
+ List<Date> startAndEndByDate = getMonthsToStartAndEndByDate(scadaQuery.getStart());
|
|
|
+ queryWrapper.between("hours.SCADA_TIME", scadaQuery.getStart(), scadaQuery.getEnd());
|
|
|
+
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(startAndEndByDate.get(0));
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy_MM");
|
|
|
+ String date = format.format(cal.getTime());
|
|
|
+ String tableName = "SCADA_MONTH_" + date;
|
|
|
+ return scadaMapper.pageList(page, queryWrapper, tableName);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ return scadaMapper.pageList(page, queryWrapper, "SCADA_HISTORY");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 获取当前月份的开始时间和结束时间
|
|
|
+ *
|
|
|
+ * @param time 时间
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<Date> getMonthsToStartAndEndByDate(Date time) {
|
|
|
+
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(time == null ? new Date() : time);
|
|
|
+
|
|
|
+ calendar.set(Calendar.DATE, 1);
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
+ Date start = calendar.getTime();
|
|
|
+
|
|
|
+ calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ calendar.set(Calendar.MINUTE, 59);
|
|
|
+ calendar.set(Calendar.SECOND, 59);
|
|
|
+ calendar.set(Calendar.MILLISECOND, 999);
|
|
|
+ Date end = calendar.getTime();
|
|
|
+ List<Date> list = new ArrayList<>();
|
|
|
+ list.add(start);
|
|
|
+ list.add(end);
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * @param startMonth @example: "2016-01"
|
|
|
+ * @param endMonth @example: "2019-11"
|
|
|
+ * @return 两个时间之间的月份(含开始, 结束)
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ private List<String> getMonths(String startMonth, String endMonth) throws ParseException {
|
|
|
+ LinkedList<String> months = new LinkedList<>();
|
|
|
+
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
|
|
|
+ Calendar minCalender = Calendar.getInstance();
|
|
|
+ Calendar maxCalender = Calendar.getInstance();
|
|
|
+
|
|
|
+
|
|
|
+ minCalender.setTime(sdf.parse(startMonth));
|
|
|
+ minCalender.set(minCalender.get(Calendar.YEAR), minCalender.get(Calendar.MONTH), 1);
|
|
|
+
|
|
|
+ maxCalender.setTime(sdf.parse(endMonth));
|
|
|
+
|
|
|
+ maxCalender.set(maxCalender.get(Calendar.YEAR), maxCalender.get(Calendar.MONTH), 2);
|
|
|
+
|
|
|
+ while (minCalender.before(maxCalender)) {
|
|
|
+ months.add(sdf.format(minCalender.getTime()));
|
|
|
+ minCalender.add(Calendar.MONTH, 1);
|
|
|
+ }
|
|
|
+ return months;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * @return 两个时间之间的月份(含开始, 结束) + 表名
|
|
|
+ * @throws ParseException
|
|
|
+ */
|
|
|
+ private List<String> getMonths(Date start, Date end) {
|
|
|
+ LinkedList<String> tableNames = new LinkedList<>();
|
|
|
+
|
|
|
+ SimpleDateFormat format = new SimpleDateFormat("yyyy_MM");
|
|
|
+
|
|
|
+ Calendar minCalender = Calendar.getInstance();
|
|
|
+ Calendar maxCalender = Calendar.getInstance();
|
|
|
+
|
|
|
+
|
|
|
+ minCalender.setTime(start);
|
|
|
+ minCalender.set(minCalender.get(Calendar.YEAR), minCalender.get(Calendar.MONTH), 1);
|
|
|
+
|
|
|
+ maxCalender.setTime(end);
|
|
|
+
|
|
|
+ maxCalender.set(maxCalender.get(Calendar.YEAR), maxCalender.get(Calendar.MONTH), 2);
|
|
|
+
|
|
|
+ while (minCalender.before(maxCalender)) {
|
|
|
+ String date = format.format(minCalender.getTime());
|
|
|
+ String tableName = "SCADA_MONTH_" + date;
|
|
|
+ tableNames.add(tableName);
|
|
|
+ minCalender.add(Calendar.MONTH, 1);
|
|
|
+ }
|
|
|
+ return tableNames;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String getMonthsByEndDay(Date date) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.add(Calendar.DATE, cal.getActualMaximum(Calendar.DATE));
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
+ cal.set(Calendar.MINUTE, 59);
|
|
|
+ cal.set(Calendar.SECOND, 59);
|
|
|
+ cal.set(Calendar.MILLISECOND, 999);
|
|
|
+ String end = DateUtil.format(cal.getTime(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ return end;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getMonthsByStartDay(Date date) {
|
|
|
+ Calendar cal = Calendar.getInstance();
|
|
|
+ cal.setTime(date);
|
|
|
+ cal.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
+ cal.set(Calendar.MINUTE, 0);
|
|
|
+ cal.set(Calendar.SECOND, 0);
|
|
|
+ cal.set(Calendar.MILLISECOND, 0);
|
|
|
+ String start = DateUtil.format(cal.getTime(), "yyyy-MM-dd HH:mm:ss");
|
|
|
+ return start;
|
|
|
+ }
|
|
|
+}
|