TwinCalcHourEnergyServiceImpl.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package com.jjt.calc.service.impl;
  2. import cn.hutool.json.JSONArray;
  3. import cn.hutool.json.JSONObject;
  4. import com.jjt.calc.domain.TwinCalcHourEnergy;
  5. import com.jjt.calc.mapper.TwinCalcHourEnergyMapper;
  6. import com.jjt.calc.service.ITwinCalcHourEnergyService;
  7. import com.jjt.common.utils.DateUtils;
  8. import com.jjt.utils.IotService;
  9. import com.jjt.utils.Tools;
  10. import com.jjt.ws.domain.TwinWorkshop;
  11. import com.jjt.ws.domain.TwinWorkshopEnergy;
  12. import com.jjt.ws.service.ITwinWorkshopService;
  13. import javafx.util.Pair;
  14. import org.apache.ibatis.session.ExecutorType;
  15. import org.apache.ibatis.session.SqlSession;
  16. import org.apache.ibatis.session.SqlSessionFactory;
  17. import org.springframework.stereotype.Service;
  18. import javax.annotation.Resource;
  19. import java.math.BigDecimal;
  20. import java.time.LocalDate;
  21. import java.time.LocalDateTime;
  22. import java.time.ZoneOffset;
  23. import java.util.*;
  24. /**
  25. * 能源1小时统计数据Service业务层处理
  26. *
  27. * @author wukai
  28. * @date 2025-01-18
  29. */
  30. @Service
  31. public class TwinCalcHourEnergyServiceImpl implements ITwinCalcHourEnergyService {
  32. @Resource
  33. private TwinCalcHourEnergyMapper twinCalcHourEnergyMapper;
  34. @Resource
  35. private IotService iotService;
  36. @Resource
  37. private SqlSessionFactory factory;
  38. @Resource
  39. private ITwinWorkshopService workshopService;
  40. /**
  41. * 查询能源1小时统计数据
  42. *
  43. * @param autoId 能源1小时统计数据主键
  44. * @return 能源1小时统计数据
  45. */
  46. @Override
  47. public TwinCalcHourEnergy selectTwinCalcHourEnergyByAutoId(Long autoId) {
  48. return twinCalcHourEnergyMapper.selectTwinCalcHourEnergyByAutoId(autoId);
  49. }
  50. /**
  51. * 查询能源1小时统计数据列表
  52. *
  53. * @param twinCalcHourEnergy 能源1小时统计数据
  54. * @return 能源1小时统计数据
  55. */
  56. @Override
  57. public List<TwinCalcHourEnergy> selectTwinCalcHourEnergyList(TwinCalcHourEnergy twinCalcHourEnergy) {
  58. return twinCalcHourEnergyMapper.selectTwinCalcHourEnergyList(twinCalcHourEnergy);
  59. }
  60. /**
  61. * 新增能源1小时统计数据
  62. *
  63. * @param twinCalcHourEnergy 能源1小时统计数据
  64. * @return 结果
  65. */
  66. @Override
  67. public int insertTwinCalcHourEnergy(TwinCalcHourEnergy twinCalcHourEnergy) {
  68. return twinCalcHourEnergyMapper.insertTwinCalcHourEnergy(twinCalcHourEnergy);
  69. }
  70. /**
  71. * 修改能源1小时统计数据
  72. *
  73. * @param twinCalcHourEnergy 能源1小时统计数据
  74. * @return 结果
  75. */
  76. @Override
  77. public int updateTwinCalcHourEnergy(TwinCalcHourEnergy twinCalcHourEnergy) {
  78. return twinCalcHourEnergyMapper.updateTwinCalcHourEnergy(twinCalcHourEnergy);
  79. }
  80. /**
  81. * 批量删除能源1小时统计数据
  82. *
  83. * @param autoIds 需要删除的能源1小时统计数据主键
  84. * @return 结果
  85. */
  86. @Override
  87. public int deleteTwinCalcHourEnergyByAutoIds(Long[] autoIds) {
  88. return twinCalcHourEnergyMapper.deleteTwinCalcHourEnergyByAutoIds(autoIds);
  89. }
  90. /**
  91. * 删除能源1小时统计数据信息
  92. *
  93. * @param autoId 能源1小时统计数据主键
  94. * @return 结果
  95. */
  96. @Override
  97. public int deleteTwinCalcHourEnergyByAutoId(Long autoId) {
  98. return twinCalcHourEnergyMapper.deleteTwinCalcHourEnergyByAutoId(autoId);
  99. }
  100. /**
  101. * 统计指定日期指定时段能耗数据
  102. *
  103. * @param start 开始时间戳
  104. * @param end 结束时间戳
  105. */
  106. @Override
  107. public void calcEnergy(LocalDateTime start, LocalDateTime end) {
  108. Date date = Date.from(start.toLocalDate().atStartOfDay(ZoneOffset.of("+8")).toInstant());
  109. int period = start.getHour();
  110. Long startTime = start.toInstant(ZoneOffset.of("+8")).toEpochMilli();
  111. Long endTime = end.toInstant(ZoneOffset.of("+8")).toEpochMilli();
  112. String baseSql = "select %s from %s where time>%s and time <=%s";
  113. TwinWorkshop ws = workshopService.selectTwinWorkshopByWsId(3L);
  114. List<TwinWorkshopEnergy> energyList = ws.getTwinWorkshopEnergyList();
  115. List<TwinCalcHourEnergy> list = new ArrayList<>();
  116. for (TwinWorkshopEnergy energy : energyList) {
  117. String sql = String.format(baseSql, energy.getEnergyCode(), energy.getEnergyPath(), startTime, endTime);
  118. JSONObject jsonObject = iotService.query(sql);
  119. JSONObject data = jsonObject.getJSONObject("data");
  120. JSONArray values = data.getJSONArray("values");
  121. JSONArray first = values.getJSONArray(0);
  122. JSONArray last = values.getJSONArray(values.size() - 1);
  123. int res = last.getInt(0) - first.getInt(0);
  124. TwinCalcHourEnergy calcEnergy = new TwinCalcHourEnergy();
  125. calcEnergy.setEnergyId(energy.getEnergyId());
  126. BigDecimal dataV = BigDecimal.valueOf(res).multiply(energy.getCoefficient());
  127. calcEnergy.setDataValue(dataV);
  128. calcEnergy.setDataDate(date);
  129. calcEnergy.setHour(period);
  130. list.add(calcEnergy);
  131. }
  132. if (list.size() > 0) {
  133. try (SqlSession sqlSession = factory.openSession(ExecutorType.BATCH, false)) {
  134. TwinCalcHourEnergyMapper mapper = sqlSession.getMapper(TwinCalcHourEnergyMapper.class);
  135. list.forEach(mapper::insertTwinCalcHourEnergy);
  136. sqlSession.commit();
  137. }
  138. }
  139. }
  140. /**
  141. * 统计上一个时段的能源
  142. */
  143. @Override
  144. public void calcLastEnergy() {
  145. LocalDateTime ldt = Tools.currWholeTime();
  146. //上一个小时
  147. ldt = ldt.minusHours(1);
  148. //这里不需要向前取一秒了
  149. LocalDateTime start = ldt;
  150. LocalDateTime end = ldt.plusHours(1);
  151. calcEnergy(start, end);
  152. }
  153. /**
  154. * 按时间查询
  155. *
  156. * @param date 时间
  157. * @return 结果
  158. */
  159. @Override
  160. public List<TwinCalcHourEnergy> selectTwinEmpCalcListByDate(Date date) {
  161. LocalDate localDate = DateUtils.toLocalDate(date);
  162. //计算统计时间
  163. Pair<Date, Date> pair = Tools.calcDay(localDate);
  164. Date sTime = pair.getKey();
  165. Date eTime = pair.getValue();
  166. TwinCalcHourEnergy hour = new TwinCalcHourEnergy();
  167. Map<String, Object> params = new HashMap<>(16);
  168. params.put("sTime", sTime);
  169. params.put("eTime", eTime);
  170. hour.setParams(params);
  171. return selectTwinCalcHourEnergyList(hour);
  172. }
  173. }