|
@@ -1,264 +0,0 @@
|
|
|
-package com.jjt.utils;
|
|
|
-
|
|
|
-import cn.hutool.json.JSONArray;
|
|
|
-import cn.hutool.json.JSONObject;
|
|
|
-import com.jjt.biz.domain.TwinDevice;
|
|
|
-import com.jjt.biz.service.ITwinDeviceService;
|
|
|
-import com.jjt.calc.domain.*;
|
|
|
-import com.jjt.calc.mapper.TwinCalcHourEnergyMapper;
|
|
|
-import com.jjt.ws.domain.TwinWorkshop;
|
|
|
-import com.jjt.ws.domain.TwinWorkshopEnergy;
|
|
|
-import com.jjt.ws.service.ITwinWorkshopService;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.ibatis.session.ExecutorType;
|
|
|
-import org.apache.ibatis.session.SqlSession;
|
|
|
-import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
-import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import javax.annotation.Resource;
|
|
|
-import java.math.BigDecimal;
|
|
|
-import java.time.LocalDate;
|
|
|
-import java.time.LocalDateTime;
|
|
|
-import java.time.LocalTime;
|
|
|
-import java.time.ZoneOffset;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.concurrent.ExecutionException;
|
|
|
-import java.util.concurrent.Future;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
-import java.util.concurrent.TimeoutException;
|
|
|
-
|
|
|
-/**
|
|
|
- * 统计任务
|
|
|
- *
|
|
|
- * @author wukai
|
|
|
- * @date 2024/5/4 20:35
|
|
|
- */
|
|
|
-@Service
|
|
|
-@Slf4j
|
|
|
-public class TaskService {
|
|
|
- @Resource
|
|
|
- private ITwinDeviceService deviceService;
|
|
|
- @Resource
|
|
|
- private IotService iotService;
|
|
|
- @Resource
|
|
|
- private JdbcTemplate jdbcTemplate;
|
|
|
-// @Resource
|
|
|
-// private AsyncServiceImpl asyncService;
|
|
|
-// @Resource
|
|
|
-// private ITwinCalcDayService dayService;
|
|
|
-// @Resource
|
|
|
-// private ITwinCalcStopService stopService;
|
|
|
-// @Resource
|
|
|
-// private ITwinCalcAlarmsService alarmsService;
|
|
|
-// @Resource
|
|
|
-// private ITwinCalcHourService hourService;
|
|
|
- @Resource
|
|
|
- private SqlSessionFactory factory;
|
|
|
- @Resource
|
|
|
- private ITwinWorkshopService workshopService;
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 统计指定日期指定时段能耗数据
|
|
|
- *
|
|
|
- * @param start 开始时间戳
|
|
|
- * @param end 结束时间戳
|
|
|
- */
|
|
|
- public void calcEnergy(LocalDateTime start, LocalDateTime end) {
|
|
|
- Date date = Date.from(start.toLocalDate().atStartOfDay(ZoneOffset.of("+8")).toInstant());
|
|
|
- int period = start.getHour();
|
|
|
- Long startTime = start.toInstant(ZoneOffset.of("+8")).toEpochMilli();
|
|
|
- Long endTime = end.toInstant(ZoneOffset.of("+8")).toEpochMilli();
|
|
|
- String baseSql = "select %s from %s where time>%s and time <=%s";
|
|
|
- TwinWorkshop ws = workshopService.selectTwinWorkshopByWsId(3L);
|
|
|
- List<TwinWorkshopEnergy> energyList = ws.getTwinWorkshopEnergyList();
|
|
|
- List<TwinCalcHourEnergy> list = new ArrayList<>();
|
|
|
- for (TwinWorkshopEnergy energy : energyList) {
|
|
|
- String sql = String.format(baseSql, energy.getEnergyCode(), energy.getEnergyPath(), startTime, endTime);
|
|
|
- JSONObject jsonObject = iotService.query(sql);
|
|
|
- JSONObject data = jsonObject.getJSONObject("data");
|
|
|
- JSONArray values = data.getJSONArray("values");
|
|
|
- JSONArray first = values.getJSONArray(0);
|
|
|
- JSONArray last = values.getJSONArray(values.size() - 1);
|
|
|
- int res = last.getInt(0) - first.getInt(0);
|
|
|
- TwinCalcHourEnergy calcEnergy = new TwinCalcHourEnergy();
|
|
|
- calcEnergy.setEnergyId(energy.getEnergyId());
|
|
|
- BigDecimal dataV = BigDecimal.valueOf(res).multiply(energy.getCoefficient());
|
|
|
- calcEnergy.setDataValue(dataV);
|
|
|
- calcEnergy.setDataDate(date);
|
|
|
- calcEnergy.setHour(period);
|
|
|
- list.add(calcEnergy);
|
|
|
- }
|
|
|
- if (list.size() > 0) {
|
|
|
- try (SqlSession sqlSession = factory.openSession(ExecutorType.BATCH, false)) {
|
|
|
- TwinCalcHourEnergyMapper mapper = sqlSession.getMapper(TwinCalcHourEnergyMapper.class);
|
|
|
- list.forEach(mapper::insertTwinCalcHourEnergy);
|
|
|
- sqlSession.commit();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 统计上一个时段的能源
|
|
|
- */
|
|
|
- public void calcLastEnergy() {
|
|
|
- LocalDateTime ldt = Tools.currWholeTime();
|
|
|
- //上一个小时
|
|
|
- ldt = ldt.minusHours(1);
|
|
|
- //这里不需要向前取一秒了
|
|
|
- LocalDateTime start = ldt;
|
|
|
- LocalDateTime end = ldt.plusHours(1);
|
|
|
- calcEnergy(start, end);
|
|
|
- }
|
|
|
-
|
|
|
-// /**
|
|
|
-// * 统计昨日数据
|
|
|
-// */
|
|
|
-// public void calcYesterday() {
|
|
|
-// LocalDate localDate = LocalDate.now().minusDays(1);
|
|
|
-// calc(localDate);
|
|
|
-// }
|
|
|
-//
|
|
|
-// /**
|
|
|
-// * 统计当日数据,直到当前时间的上一个偶数时间
|
|
|
-// */
|
|
|
-// public void calcToday() {
|
|
|
-// Tools.timePeriod().forEach(pair -> {
|
|
|
-// calc4device(pair.getKey(), pair.getValue());
|
|
|
-// });
|
|
|
-// }
|
|
|
-//
|
|
|
-// /**
|
|
|
-// * 统计指定日期数据
|
|
|
-// *
|
|
|
-// * @param date 指定日期 yyyy-mm-dd
|
|
|
-// */
|
|
|
-// public void calc(String date) {
|
|
|
-// LocalDate localDate = LocalDate.parse(date);
|
|
|
-// calc(localDate);
|
|
|
-// }
|
|
|
-//
|
|
|
-// /**
|
|
|
-// * 统计指定日期指定时段数据
|
|
|
-// *
|
|
|
-// * @param date 指定日期 yyyy-mm-dd
|
|
|
-// * @param period 时段
|
|
|
-// */
|
|
|
-// public void calc(String date, int period) {
|
|
|
-// LocalDate localDate = LocalDate.parse(date);
|
|
|
-// LocalDateTime ldt = LocalDateTime.of(localDate, LocalTime.MIN);
|
|
|
-// LocalDateTime start = ldt.plusHours(period);
|
|
|
-// LocalDateTime end = start.plusHours(1);
|
|
|
-// start = start.minusSeconds(1);
|
|
|
-// calc4device(start, end);
|
|
|
-// }
|
|
|
-//
|
|
|
-// /**
|
|
|
-// * 统计指定日期数据
|
|
|
-// *
|
|
|
-// * @param date 指定日期
|
|
|
-// */
|
|
|
-// @Override
|
|
|
-// public void calc(LocalDate date) {
|
|
|
-// Tools.timePeriod(date).forEach(pair -> {
|
|
|
-// calc4device(pair.getKey(), pair.getValue());
|
|
|
-// });
|
|
|
-// }
|
|
|
-//
|
|
|
-//
|
|
|
-// /**
|
|
|
-// * 执行数据获取操作
|
|
|
-// *
|
|
|
-// * @param list 设备列表
|
|
|
-// * @param date 日期
|
|
|
-// * @param startTime 开始时间
|
|
|
-// * @param endTime 结束时间
|
|
|
-// * @param period 小时数
|
|
|
-// * @param calcHourList 1小时统计数据
|
|
|
-// * @param stopList 停机数据
|
|
|
-// * @param panList 盘头数据
|
|
|
-// * @param alarmsList 告警数据
|
|
|
-// * @return 返回未获取成功的数据列表
|
|
|
-// */
|
|
|
-//
|
|
|
-// private List<TwinDevice> exec(List<TwinDevice> list, Date date, Long startTime, Long endTime, int period, List<Object[]> calcHourList, List<TwinRecordStop> stopList, List<Object[]> panList, List<TwinRecordAlarms> alarmsList, List<TwinCalcHourSpec> specList) {
|
|
|
-// List<Future<Map<String, List<?>>>> futureList = new ArrayList<>();
|
|
|
-// for (int i = 0; i < list.size(); i++) {
|
|
|
-// TwinDevice twinDevice = list.get(i);
|
|
|
-// futureList.add(asyncService.process(twinDevice, date, startTime, endTime, period));
|
|
|
-// }
|
|
|
-//
|
|
|
-// List<TwinDevice> errList = new ArrayList<>();
|
|
|
-// try {
|
|
|
-// for (Future<Map<String, List<?>>> future : futureList) {
|
|
|
-// // 任务完成后获取结果
|
|
|
-// try {
|
|
|
-// Map<String, List<?>> map = future.get(10L, TimeUnit.SECONDS);
|
|
|
-// List<TwinCalcHour> calcHours = (List<TwinCalcHour>) map.get("calc");
|
|
|
-// List<TwinRecordStop> stops = (List<TwinRecordStop>) map.get("stopRecord");
|
|
|
-//
|
|
|
-// List<TwinRecordAlarms> alarms = (List<TwinRecordAlarms>) map.get("alarmRecord");
|
|
|
-// List<TwinPanHeadInfo> panHeadInfoList = (List<TwinPanHeadInfo>) map.get("panHead");
|
|
|
-// List<TwinCalcHourSpec> specs = (List<TwinCalcHourSpec>) map.get("specList");
|
|
|
-// specList.addAll(specs);
|
|
|
-// calcHours.forEach(calcHour -> calcHourList.add(calcHour.toArray()));
|
|
|
-// stopList.addAll(stops);
|
|
|
-// alarmsList.addAll(alarms);
|
|
|
-// panHeadInfoList.forEach(pan -> panList.add(pan.toArray()));
|
|
|
-// } catch (TimeoutException e) {
|
|
|
-// errList.add(list.get(futureList.indexOf(future)));
|
|
|
-// }
|
|
|
-// }
|
|
|
-// } catch (InterruptedException e) {
|
|
|
-// throw new RuntimeException(e);
|
|
|
-// } catch (ExecutionException e) {
|
|
|
-// throw new RuntimeException(e);
|
|
|
-// }
|
|
|
-//
|
|
|
-// return errList;
|
|
|
-// }
|
|
|
-//
|
|
|
-// /**
|
|
|
-// * 数据入库操作
|
|
|
-// *
|
|
|
-// * @param endTime 结束时间
|
|
|
-// * @param calcHours 1小时统计数据
|
|
|
-// * @param stopList 停机记录
|
|
|
-// * @param alarmsList 告警记录
|
|
|
-// * @param panList 盘头记录
|
|
|
-// */
|
|
|
-// private void dbProcess(long endTime, List<Object[]> calcHours, List<TwinRecordStop> stopList, List<TwinRecordAlarms> alarmsList, List<Object[]> panList) {
|
|
|
-// //1小时统计数据
|
|
|
-// String sql = "INSERT INTO TWIN_CALC_HOUR(DATA_DATE,HOUR,DEVICE_ID,KWH,WEIGHT,LENGTH,OPEN_TIME,CLOSE_TIME) VALUES (?,?,?,?,?,?,?,?)";
|
|
|
-// jdbcTemplate.batchUpdate(sql, calcHours);
|
|
|
-// //盘头数据
|
|
|
-// sql = "INSERT INTO TWIN_PAN_HEAD_INFO (DEVICE_ID,PH_NUM,PH_MAX,RECORD_TIME) VALUES (?,?,?,?)";
|
|
|
-// jdbcTemplate.batchUpdate(sql, panList);
|
|
|
-// //执行停机记录处理
|
|
|
-// stopList.forEach(stop -> {
|
|
|
-// if (stop.getStartTime() != null) {
|
|
|
-// jdbcTemplate.update("INSERT INTO TWIN_RECORD_STOP (DEVICE_ID,DATA_DATE,HOUR,START_TIME,STOP_TYPE,CALC_STATUS) VALUES (?,?,?,?,?,?)", stop.getDeviceId(), stop.getDataDate(), stop.getHour(), stop.getStartTime(), stop.getStopType(), "0");
|
|
|
-// }
|
|
|
-// if (stop.getEndTime() != null) {
|
|
|
-// jdbcTemplate.update("UPDATE TWIN_RECORD_STOP SET END_TIME=? WHERE DEVICE_ID=? AND STOP_TYPE=? AND END_TIME IS NULL", stop.getEndTime(), stop.getDeviceId(), stop.getStopType());
|
|
|
-// }
|
|
|
-// });
|
|
|
-// //补一次,所有没有结束时间的为当前结束时间
|
|
|
-// jdbcTemplate.update("UPDATE TWIN_RECORD_STOP SET END_TIME=?,CALC_STATUS=1 WHERE END_TIME IS NULL", new Date(endTime));
|
|
|
-// //执行告警记录处理
|
|
|
-// alarmsList.forEach(alarms -> {
|
|
|
-// if (alarms.getStartTime() != null) {
|
|
|
-// jdbcTemplate.update("INSERT INTO TWIN_RECORD_ALARMS (DEVICE_ID,DATA_DATE,HOUR,START_TIME,ALARM_TYPE,CALC_STATUS) VALUES (?,?,?,?,?,?)", alarms.getDeviceId(), alarms.getDataDate(), alarms.getHour(), alarms.getStartTime(), alarms.getAlarmType(), "0");
|
|
|
-// }
|
|
|
-// if (alarms.getEndTime() != null) {
|
|
|
-// jdbcTemplate.update("UPDATE TWIN_RECORD_ALARMS SET END_TIME=? WHERE DEVICE_ID=? AND ALARM_TYPE=? AND END_TIME IS NULL", alarms.getEndTime(), alarms.getDeviceId(), alarms.getAlarmType());
|
|
|
-// }
|
|
|
-// });
|
|
|
-// //补一次,所有没有结束时间的为当前结束时间
|
|
|
-// jdbcTemplate.update("UPDATE TWIN_RECORD_ALARMS SET END_TIME=?,CALC_STATUS=1 WHERE END_TIME IS NULL", new Date(endTime));
|
|
|
-// }
|
|
|
-}
|