SecSupplyService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package com.huaxu.service;
  2. import com.huaxu.dto.*;
  3. import com.huaxu.entity.*;
  4. import com.huaxu.util.ByteArrayUtils;
  5. import com.huaxu.util.RedisUtil;
  6. import org.bouncycastle.crypto.engines.AESLightEngine;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. import java.text.DateFormat;
  10. import java.text.DecimalFormat;
  11. import java.text.SimpleDateFormat;
  12. import java.util.*;
  13. @Service
  14. public class SecSupplyService {
  15. @Autowired
  16. private SceneService sceneService;
  17. @Autowired
  18. private MonthReportService monthReportService;
  19. @Autowired
  20. private DeviceParmService deviceParmService;
  21. @Autowired
  22. private RedisUtil redisUtil;
  23. @Autowired
  24. private DayReportService dayReportService;
  25. @Autowired
  26. private YearReportService yearReportService;
  27. /**
  28. * 查询30天供水量
  29. * @param sceneEntity
  30. * @return
  31. */
  32. public List<AmountDayThirtyDto> getAmountDayThirty(SceneEntity sceneEntity) {
  33. List<SceneEntity> sceneEntities = sceneService.selectByTypeName(sceneEntity);
  34. if (sceneEntities.size() == 0)
  35. return null;
  36. MonthReportEntity monthReportEntity = new MonthReportEntity();
  37. monthReportEntity.setParentSceneLists(sceneEntities);
  38. List<MonthReportEntity> monthReportEntities = monthReportService.findAmountBySceneIds(monthReportEntity);
  39. Map<String, Double> maps = new LinkedHashMap<>();
  40. for (MonthReportEntity item : monthReportEntities) {
  41. maps.put(item.getYear() + "-" + String.format("%02d", item.getMonth()) + "-" + String.format("%02d", item.getDay()), item.getSumValue());
  42. }
  43. Calendar begin = Calendar.getInstance();// 得到一个Calendar的实例
  44. begin.setTime(new Date()); // 设置时间为当前时间
  45. List<AmountDayThirtyDto> listC = new ArrayList<>();
  46. for (int i = 1; i <= 30; i++) {
  47. begin.add(Calendar.DATE, -1);// 日期加1
  48. Date d = new Date(begin.getTimeInMillis());
  49. DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  50. AmountDayThirtyDto amountDayThirtyDto = new AmountDayThirtyDto();
  51. if (maps.containsKey(df.format(d).toString())) {
  52. amountDayThirtyDto.setAmountTime(d);
  53. amountDayThirtyDto.setAmount((double)Math.round(maps.get(df.format(d))*1000)/1000);
  54. } else {
  55. amountDayThirtyDto.setAmountTime(d);
  56. amountDayThirtyDto.setAmount(0d);
  57. }
  58. listC.add(amountDayThirtyDto);
  59. }
  60. return listC;
  61. }
  62. /**
  63. * 查询供水及瞬时流量
  64. * @param sceneEntity
  65. * @return
  66. */
  67. public AmountTotalDto findAmountTotalByTypeName(SceneEntity sceneEntity) {
  68. AmountTotalDto amountTotalDto = new AmountTotalDto();
  69. List<SceneEntity> sceneEntities = sceneService.selectByTypeName(sceneEntity);
  70. if (sceneEntities.size() == 0)
  71. return null;
  72. Calendar begin = Calendar.getInstance();// 得到一个Calendar的实例
  73. begin.setTime(new Date()); // 设置时间为当前时间
  74. begin.add(Calendar.DATE, -1);// 日期加1
  75. //查询所有的关联设备及测点信息
  76. DeviceParmEntity deviceParmEntity = new DeviceParmEntity();
  77. deviceParmEntity.setSceneEntities(sceneEntities);
  78. deviceParmEntity.setParmType(14);//查询瞬时流量
  79. List<DeviceParmEntity> deviceParmEntities = deviceParmService.selectDeviceBySceneIdAndType(deviceParmEntity);
  80. //瞬时流量
  81. double instantFlow = 0d;
  82. for (DeviceParmEntity item : deviceParmEntities) {
  83. //取缓存里的数据
  84. byte[] bytes = redisUtil.get(("sms_water_" + item.getDeviceCode()).getBytes());
  85. if (bytes != null && bytes.length > 0) {
  86. MonitorDataEntity monitorDataEntity = (MonitorDataEntity) ByteArrayUtils.bytesToObject(bytes).get();
  87. //筛选该设备相同属性的值
  88. Map<Long, MonitorDataValueEntity> map = new HashMap<>();
  89. //将缓存中的实时数据放到map中方便进行遍历
  90. for (MonitorDataValueEntity dateValue : monitorDataEntity.getDataValues()) {
  91. map.put(dateValue.getAttributeId(), dateValue);
  92. }
  93. if (map.containsKey(item.getAttributeId().longValue()))
  94. instantFlow += map.get(item.getAttributeId().longValue()).getDataValue();
  95. }
  96. }
  97. amountTotalDto.setInstantFlow((double) Math.round(instantFlow * 1000) / 1000);
  98. //本日供水量
  99. deviceParmEntity.setParmType(3);//查询供水量
  100. List<DeviceParmEntity> deviceParms = deviceParmService.selectDeviceBySceneIdAndType(deviceParmEntity);
  101. double dayAmount = 0d;
  102. for (DeviceParmEntity item : deviceParms) {
  103. //取缓存里的数据
  104. byte[] bytes = redisUtil.get(("sms_water_" + item.getDeviceCode()).getBytes());
  105. if (bytes != null && bytes.length > 0) {
  106. MonitorDataEntity monitorDataEntity = (MonitorDataEntity) ByteArrayUtils.bytesToObject(bytes).get();
  107. //筛选该设备相同属性的值
  108. Map<Long, MonitorDataValueEntity> map = new HashMap<>();
  109. //将缓存中的实时数据放到map中方便进行遍历
  110. for (MonitorDataValueEntity dateValue : monitorDataEntity.getDataValues()) {
  111. map.put(dateValue.getAttributeId(), dateValue);
  112. }
  113. if (map.containsKey(item.getAttributeId().longValue())) {
  114. DayReportEntity dayReportEntity = new DayReportEntity();
  115. dayReportEntity.setYear(begin.get(Calendar.YEAR));
  116. dayReportEntity.setMonth(begin.get(Calendar.MONTH) + 1);
  117. dayReportEntity.setDay(begin.get(Calendar.DAY_OF_MONTH));
  118. dayReportEntity.setDeviceId(item.getDeviceId().longValue());
  119. dayReportEntity.setAttributeId(item.getAttributeId().longValue());
  120. List<DayReportEntity> dayReportEntities = dayReportService.findDeviceLastDayValue(dayReportEntity);
  121. if (dayReportEntities.size() > 0) {
  122. dayAmount += map.get(item.getAttributeId().longValue()).getDataValue();
  123. System.out.println("item.getAttributeId()"+item.getAttributeId()+"--"+map.get(item.getAttributeId().longValue()));
  124. dayAmount = dayAmount - dayReportEntities.get(0).getLatestValue();
  125. }
  126. }
  127. }
  128. }
  129. amountTotalDto.setDayAmount((double) Math.round(dayAmount * 1000) / 1000);
  130. //本月供水量
  131. double monthAmount = 0d;
  132. begin.add(Calendar.DATE, 1);// 恢复到当前日期
  133. MonthReportEntity monthReportEntity = new MonthReportEntity();
  134. monthReportEntity.setYear(begin.get(Calendar.YEAR));
  135. monthReportEntity.setMonth(begin.get(Calendar.MONTH) + 1);
  136. monthReportEntity.setParentSceneLists(sceneEntities);
  137. List<MonthReportEntity> monthReportNew = monthReportService.findAmountTotalBySceneIds(monthReportEntity);
  138. begin.add(Calendar.MONTH, -1);// 恢复到当前日期
  139. MonthReportEntity monthReportLast = new MonthReportEntity();
  140. monthReportLast.setYear(begin.get(Calendar.YEAR));
  141. monthReportLast.setMonth(begin.get(Calendar.MONTH) + 1);
  142. monthReportLast.setParentSceneLists(sceneEntities);
  143. List<MonthReportEntity> monthReportsLast = monthReportService.findAmountTotalBySceneIds(monthReportLast);
  144. if (monthReportNew.size() > 0 && monthReportsLast.size() > 0 && monthReportNew.get(0) != null && monthReportsLast.get(0) != null) {
  145. monthAmount = monthReportNew.get(0).getLatestValue() - monthReportsLast.get(0).getLatestValue() + dayAmount;
  146. } else if (monthReportNew.size() > 0 && monthReportNew.get(0) != null) {
  147. monthAmount = monthReportNew.get(0).getLatestValue() + dayAmount;
  148. } else {
  149. monthAmount = dayAmount;
  150. }
  151. amountTotalDto.setMonthAmount((double) Math.round(monthAmount * 1000) / 1000);
  152. //本年供水量
  153. double yearAmount = 0d;
  154. begin.add(Calendar.MONTH, 1);// 恢复到当前日期
  155. YearReportEntity yearReportEntity = new YearReportEntity();
  156. yearReportEntity.setYear(begin.get(Calendar.YEAR));
  157. yearReportEntity.setParentSceneLists(sceneEntities);
  158. List<YearReportEntity> yearReportsNew = yearReportService.findAmountTotalBySceneIds(yearReportEntity);
  159. yearReportEntity.setYear(begin.get(Calendar.YEAR) - 1);
  160. List<YearReportEntity> yearReportsLast = yearReportService.findAmountTotalBySceneIds(yearReportEntity);
  161. if (yearReportsNew.size() > 0 && yearReportsLast.size() > 0 && yearReportsNew.get(0) != null && yearReportsLast.get(0) != null) {
  162. yearAmount = yearReportsNew.get(0).getLatestValue() - yearReportsLast.get(0).getLatestValue() + monthAmount;
  163. } else if (yearReportsNew.size() > 0 && yearReportsNew.get(0) != null) {
  164. yearAmount = yearReportsNew.get(0).getLatestValue() + monthAmount;
  165. } else {
  166. yearAmount = monthAmount;
  167. }
  168. amountTotalDto.setYearAmount((double) Math.round(yearAmount * 1000) / 1000);
  169. return amountTotalDto;
  170. }
  171. public WaterPieDto selectWaterQualityByTypeName(SceneEntity sceneEntity) {
  172. List<SceneEntity> sceneEntities = sceneService.selectByTypeName(sceneEntity);
  173. if (sceneEntities.size() == 0)
  174. return null;
  175. WaterPieDto waterPieDto = new WaterPieDto();
  176. DeviceParmEntity deviceParmEntity = new DeviceParmEntity();
  177. deviceParmEntity.setSceneEntities(sceneEntities);
  178. List<ParmTypeCountDto> listTotal = deviceParmService.findAlarmTotalCount(deviceParmEntity);
  179. List<ParmTypeCountDto> listAlarmTotal =deviceParmService.findAlarmCountTotalCount(deviceParmEntity);
  180. WaterPieStateDto waterPieStateDto4 = new WaterPieStateDto();
  181. waterPieStateDto4.setNormalCount(listTotal.get(0).getTotalCount()- listAlarmTotal.get(0).getNbnormalCount());
  182. waterPieStateDto4.setNbnormalCount(listTotal.get(0).getNbnormalCount());
  183. waterPieStateDto4.setPercentage((double) (Math.round((Double.valueOf(listTotal.get(0).getTotalCount()- listAlarmTotal.get(0).getNbnormalCount())/Double.valueOf(listTotal.get(0).getTotalCount()))*100)));
  184. waterPieDto.setWaterQuality(waterPieStateDto4);
  185. List<ParmTypeCountDto> list = deviceParmService.findAlarmCount(deviceParmEntity);
  186. //余氯11 浊度9 PH8 cod 15 15,8,9,11,18,19
  187. for(ParmTypeCountDto item : list)
  188. {
  189. switch(item.getParmType())
  190. {
  191. case 8:
  192. WaterPieStateDto waterPieStateDto1 = new WaterPieStateDto();
  193. waterPieStateDto1.setNormalCount(listTotal.get(0).getTotalCount()- item.getNbnormalCount());
  194. waterPieStateDto1.setNbnormalCount(item.getNbnormalCount());
  195. DecimalFormat df1 = new DecimalFormat("#.00");
  196. waterPieStateDto1.setPercentage((double) (Math.round((Double.valueOf(listTotal.get(0).getTotalCount()- item.getNbnormalCount())/Double.valueOf(listTotal.get(0).getTotalCount()))*100)));
  197. waterPieDto.setPh(waterPieStateDto1);
  198. break;
  199. case 9:
  200. WaterPieStateDto waterPieStateDto2 = new WaterPieStateDto();
  201. waterPieStateDto2.setNormalCount(listTotal.get(0).getTotalCount()- item.getNbnormalCount());
  202. waterPieStateDto2.setNbnormalCount(item.getNbnormalCount());
  203. DecimalFormat df2 = new DecimalFormat("#.00");
  204. waterPieStateDto2.setPercentage((double) (Math.round((Double.valueOf(listTotal.get(0).getTotalCount()- item.getNbnormalCount())/Double.valueOf(listTotal.get(0).getTotalCount()))*100)));
  205. waterPieDto.setTurbidity(waterPieStateDto2);
  206. break;
  207. case 11:
  208. WaterPieStateDto waterPieStateDto3 = new WaterPieStateDto();
  209. waterPieStateDto3.setNormalCount(listTotal.get(0).getTotalCount()- item.getNbnormalCount());
  210. waterPieStateDto3.setNbnormalCount(item.getNbnormalCount());
  211. waterPieStateDto3.setPercentage((double) (Math.round((Double.valueOf(listTotal.get(0).getTotalCount()- item.getNbnormalCount())/Double.valueOf(listTotal.get(0).getTotalCount()))*100)));
  212. waterPieDto.setResidualChlorine(waterPieStateDto3);
  213. break;
  214. }
  215. }
  216. return waterPieDto;
  217. }
  218. }