MonitorInfoController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package com.huaxu.controller;
  2. import com.huaxu.common.StringUtils;
  3. import com.huaxu.dto.*;
  4. import com.huaxu.entity.*;
  5. import com.huaxu.model.AjaxMessage;
  6. import com.huaxu.model.LoginUser;
  7. import com.huaxu.model.ResultStatus;
  8. import com.huaxu.service.*;
  9. import com.huaxu.util.ByteArrayUtils;
  10. import com.huaxu.util.RedisUtil;
  11. import com.huaxu.util.UserUtil;
  12. import io.swagger.annotations.Api;
  13. import io.swagger.annotations.ApiOperation;
  14. import io.swagger.annotations.ApiParam;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Controller;
  17. import org.springframework.validation.annotation.Validated;
  18. import org.springframework.ui.ModelMap;
  19. import java.time.LocalDateTime;
  20. import java.util.*;
  21. import java.util.stream.Collectors;
  22. import org.springframework.web.bind.annotation.*;
  23. import javax.jws.Oneway;
  24. /**
  25. * 标签信息页面控制器
  26. * @author WYY
  27. * @date 2020-12-02 09:29
  28. */
  29. @RestController
  30. @RequestMapping("/monitorinfo")
  31. @Api(tags = "标签信息")
  32. public class MonitorInfoController {
  33. @Autowired
  34. private MonitorInfoService monitorInfoService;
  35. @Autowired
  36. private DeviceParmService deviceParmService;
  37. @Autowired
  38. private RedisUtil redisUtil;
  39. @Autowired
  40. private SceneService sceneService;
  41. @Autowired
  42. private DeviceService deviceService;
  43. @Autowired
  44. private AlarmDetailsService alarmDetailsService;
  45. @ApiOperation(value = "按场景ID查询供水量出水量耗药量耗电量")
  46. @RequestMapping(value = "/findTotalUsageBySceneId",method = RequestMethod.GET)
  47. @ResponseBody
  48. public AjaxMessage<SceneUsageDto> findTotalUsageBySceneId(@ApiParam(value = "场景ID", required = true) @RequestParam Long id) {
  49. SceneUsageDto sceneUsageDto = new SceneUsageDto();
  50. //查询场景下的所有设备信息
  51. List<DeviceDto> devices = new ArrayList<>();
  52. DeviceDto deviceDto = new DeviceDto();
  53. deviceDto.setSceneIds(sceneService.findByParentIdsLike(id));
  54. devices.addAll(deviceService.selectList(deviceDto));
  55. //取前一个小时的时间
  56. LocalDateTime dateTime = LocalDateTime.now().plusDays(-1);
  57. for (DeviceDto item : devices) {
  58. //设备的几个参数值
  59. SceneDeviceAttributeDto sceneDeviceAttributeDto = new SceneDeviceAttributeDto();
  60. sceneDeviceAttributeDto.setDeviceId(item.getId());
  61. sceneDeviceAttributeDto.setYear(dateTime.getYear());
  62. sceneDeviceAttributeDto.setMonth(dateTime.getMonthValue());
  63. sceneDeviceAttributeDto.setDay(dateTime.getDayOfMonth());
  64. List<SceneDeviceAttributeDto> sceneDeviceAttributeDtos = monitorInfoService.findAttributeList(sceneDeviceAttributeDto);
  65. //取缓存里的数据
  66. byte[] bytes = redisUtil.get(("sms_water_" + item.getDeviceCode()).getBytes());
  67. if (bytes != null && bytes.length > 0) {
  68. MonitorDataEntity monitorDataEntity = (MonitorDataEntity) ByteArrayUtils.bytesToObject(bytes).get();
  69. //筛选该设备相同属性的值
  70. Map<Long, MonitorDataValueEntity> map = new HashMap<>();
  71. //将缓存中的实时数据放到map中方便进行遍历
  72. for (MonitorDataValueEntity dateValue : monitorDataEntity.getDataValues()) {
  73. map.put(dateValue.getAttributeId(), dateValue);
  74. System.out.println("dateValue.getAttributeId()" + dateValue.getAttributeId() + " " + dateValue);
  75. }
  76. for (SceneDeviceAttributeDto itemAttribute : sceneDeviceAttributeDtos) {
  77. System.out.println("itemAttribute.getAttributeId()" + itemAttribute.getAttributeId());
  78. Double attributeDiffValue = 0d;
  79. if(!map.containsKey(itemAttribute.getAttributeId())) {
  80. System.out.println("map.containsKey(itemAttribute.getAttributeId()" + map.containsKey(itemAttribute.getAttributeId()));
  81. continue;
  82. }
  83. if (map.get(itemAttribute.getAttributeId()).getDataValue() != null && itemAttribute.getLatestValue() != null) {
  84. attributeDiffValue = map.get(itemAttribute.getAttributeId()).getDataValue() - itemAttribute.getLatestValue();
  85. } else if (map.get(itemAttribute.getAttributeId()).getDataValue() != null && itemAttribute.getLatestValue() == null) {
  86. attributeDiffValue = map.get(itemAttribute.getAttributeId()).getDataValue();
  87. }
  88. switch (itemAttribute.getAttributeType()) {
  89. case "3":
  90. sceneUsageDto.setYieldWaterUsage(sceneUsageDto.getYieldWaterUsage() != null ? (double) Math.round((sceneUsageDto.getYieldWaterUsage() + attributeDiffValue)*1000)/1000 : (double) Math.round((attributeDiffValue)*1000)/1000 );
  91. break;
  92. case "4":
  93. sceneUsageDto.setIntakeWaterUsage(sceneUsageDto.getIntakeWaterUsage() != null ? (double) Math.round((sceneUsageDto.getIntakeWaterUsage() + attributeDiffValue )*1000)/1000 : (double) Math.round((attributeDiffValue)*1000)/1000 );
  94. break;
  95. case "5":
  96. sceneUsageDto.setPowerUsage(sceneUsageDto.getPowerUsage() != null ? (double) Math.round((sceneUsageDto.getPowerUsage() + attributeDiffValue)*1000)/1000 : (double) Math.round((attributeDiffValue)*1000)/1000 );
  97. break;
  98. case "6":
  99. sceneUsageDto.setDrugUsage(sceneUsageDto.getDrugUsage() != null ? (double) Math.round((sceneUsageDto.getDrugUsage() + attributeDiffValue)*1000)/1000 : (double) Math.round((attributeDiffValue)*1000)/1000 );
  100. break;
  101. }
  102. }
  103. if(sceneUsageDto.getYieldWaterUsage()==null)
  104. sceneUsageDto.setYieldWaterUsage(sceneUsageDto.getYieldWaterUsage());
  105. if(sceneUsageDto.getIntakeWaterUsage()==null)
  106. sceneUsageDto.setIntakeWaterUsage(sceneUsageDto.getIntakeWaterUsage());
  107. if(sceneUsageDto.getPowerUsage()==null)
  108. sceneUsageDto.setPowerUsage(sceneUsageDto.getPowerUsage());
  109. if(sceneUsageDto.getDrugUsage()==null)
  110. sceneUsageDto.setDrugUsage(sceneUsageDto.getDrugUsage());
  111. }
  112. }
  113. return new AjaxMessage<>(ResultStatus.OK,sceneUsageDto);
  114. }
  115. @ApiOperation(value = "按场景ID查询所有标签信息")
  116. @RequestMapping(value = "/findBySceneId",method = RequestMethod.GET)
  117. @ResponseBody
  118. public AjaxMessage<List<MonitorInfoEntity>> findBySceneId(
  119. @ApiParam(value = "场景ID", required = true) @RequestParam Long id,
  120. @ApiParam(value = "图片类型(0鸟瞰图 1工艺图)", required = false) @RequestParam(required = false) Integer imageType,
  121. @ApiParam(value = "设备ID", required = false) @RequestParam(required = false) Long deviceId
  122. ) {
  123. MonitorInfoEntity monitorInfoEntity = new MonitorInfoEntity();
  124. monitorInfoEntity.setSceneId(id);
  125. if (imageType != null) {
  126. monitorInfoEntity.setImageType(imageType);
  127. }
  128. if (deviceId != null) {
  129. monitorInfoEntity.setDeviceId(deviceId);
  130. }
  131. List<MonitorInfoEntity> page = monitorInfoService.findList(monitorInfoEntity);
  132. for (MonitorInfoEntity item : page) {
  133. if (item.getMonitorType().equals(0L)) {
  134. //查询报警信息
  135. List<AlarmDetailsDto> alarmDetailsDtos = alarmDetailsService.selectByDeviceId(item.getDeviceId().intValue());
  136. Map<Integer, AlarmDetailsDto> mapAlarm = new HashMap<>();
  137. //将缓存中的实时数据放到map中方便进行遍历
  138. for (AlarmDetailsDto alarmDetailsDto : alarmDetailsDtos) {
  139. mapAlarm.put(alarmDetailsDto.getAttributeId(), alarmDetailsDto);
  140. }
  141. byte[] bytes = redisUtil.get(("sms_water_" + item.getDeviceCode()).getBytes());
  142. if (bytes != null && bytes.length > 0) {
  143. MonitorDataEntity monitorDataEntity = (MonitorDataEntity) ByteArrayUtils.bytesToObject(bytes).get();
  144. Map<Long, MonitorDataValueEntity> map = new HashMap<>();
  145. //将缓存中的实时数据放到map中方便进行遍历
  146. for (MonitorDataValueEntity dateValue : monitorDataEntity.getDataValues()) {
  147. map.put(dateValue.getAttributeId(), dateValue);
  148. }
  149. //判断如果是标签则进行map中取值
  150. if (item.getMonitorType().equals(0L))//标签
  151. {
  152. item.setMonitorValue(String.format("%.3f", map.get(item.getAttributeId()).getDataValue()) + (StringUtils.isNotEmpty(map.get(item.getAttributeId()).getUnit())?map.get(item.getAttributeId()).getUnit():""));
  153. //判断是否报警
  154. if (mapAlarm.containsKey(item.getAttributeId()))
  155. item.setIsAlarm(1);
  156. else
  157. item.setIsAlarm(0);
  158. }
  159. }
  160. }
  161. }
  162. return new AjaxMessage<>(ResultStatus.OK, page);
  163. }
  164. @RequestMapping(value="getDeviceParmByDeviceId" , method = RequestMethod.GET)
  165. @ApiOperation(value = "查询单个设备参数信息(标签设置)")
  166. public AjaxMessage<List<DeviceParmEntity>> getByDeviceId(@ApiParam(value = "设备id",required = true) @RequestParam Integer sceneId,@ApiParam(value = "设备id",required = true) @RequestParam Integer id){
  167. return new AjaxMessage<>(ResultStatus.OK, deviceParmService.selectByDeviceIdForGis(sceneId,id));
  168. }
  169. /**
  170. * 新增
  171. */
  172. @ApiOperation(value = "单个新增")
  173. @RequestMapping(value = "/add", method = RequestMethod.POST)
  174. @ResponseBody
  175. public AjaxMessage<Integer> addMonitorInfo(@ApiParam(value = "标签信息", required = true) @RequestBody MonitorInfoEntity monitorInfo) {
  176. if (monitorInfo == null) {
  177. return new AjaxMessage<>(ResultStatus.DEVICE_PARENT_ALREADY_EXISTS);
  178. }
  179. LoginUser currentUser = UserUtil.getCurrentUser();
  180. monitorInfo.setTenantId(currentUser.getTenantId());
  181. monitorInfo.setStatus(1);
  182. monitorInfo.setDateCreate(new Date());
  183. monitorInfo.setDateUpdate(new Date());
  184. monitorInfo.setCreateBy(currentUser.getName());
  185. monitorInfo.setUpdateBy(currentUser.getName());
  186. //校验参数
  187. int result = monitorInfoService.addMonitorInfo(monitorInfo) ? 1 : 0;
  188. return new AjaxMessage<>(ResultStatus.OK, result);
  189. }
  190. /**
  191. * 批量新增
  192. */
  193. @RequestMapping(value = "/batchAdd", method = RequestMethod.POST)
  194. @ResponseBody
  195. @ApiOperation(value = "批量新增或修改(按设备)")
  196. public AjaxMessage<Integer> addMonitorInfo(@ApiParam(value = "标签信息", required = true) @RequestBody ArrayList<MonitorInfoEntity> monitorInfos) {
  197. LoginUser currentUser = UserUtil.getCurrentUser();
  198. if (monitorInfos.size() == 0) {
  199. return new AjaxMessage<>(ResultStatus.DEVICE_PARENT_ALREADY_EXISTS);
  200. }
  201. for (MonitorInfoEntity item : monitorInfos) {
  202. item.setTenantId(currentUser.getTenantId());
  203. item.setStatus(1);
  204. item.setDateCreate(new Date());
  205. item.setDateUpdate(new Date());
  206. item.setCreateBy(currentUser.getName());
  207. item.setUpdateBy(currentUser.getName());
  208. }
  209. int result = monitorInfoService.saveOrUpdateBatch(monitorInfos) ? 1 : 0;
  210. return new AjaxMessage<>(ResultStatus.OK, result);
  211. }
  212. /**
  213. * 修改
  214. */
  215. @ApiOperation(value = "按ID查询标签")
  216. @RequestMapping(value = "/selectById", method = RequestMethod.GET)
  217. public AjaxMessage<MonitorInfoEntity> selectById(@ApiParam(value = "ID", required = true) @RequestParam Long id) {
  218. MonitorInfoEntity monitorInfo = monitorInfoService.findMonitorInfoById(id);
  219. return new AjaxMessage<>(ResultStatus.OK, monitorInfo);
  220. }
  221. /**
  222. * 修改保存标签信息
  223. */
  224. @ApiOperation(value = "单个编辑")
  225. @RequestMapping(value = "/edit", method = RequestMethod.POST)
  226. @ResponseBody
  227. public AjaxMessage<Integer> editMonitorInfo(@ApiParam(value = "标签信息", required = true) @RequestBody MonitorInfoEntity monitorInfo) {
  228. int result = monitorInfoService.updateMonitorInfoById(monitorInfo) ? 1 : 0;
  229. return new AjaxMessage<>(ResultStatus.OK, result);
  230. }
  231. /**
  232. * 删除
  233. */
  234. @ApiOperation(value = "批量删除")
  235. @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
  236. @ResponseBody
  237. public AjaxMessage<Integer> del(@ApiParam(value = "场景类型ID", required = true) @RequestBody Long[] ids) {
  238. int result = monitorInfoService.delMonitorInfoByIds(ids) ? 1 : 0;
  239. return new AjaxMessage<>(ResultStatus.OK, result);
  240. }
  241. }