MonitorInfoController.java 16 KB

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