DeviceEventMsgServiceImpl.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package com.xy.service;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.xy.dto.DeviceEventMsgDto;
  6. import com.xy.dto.DeviceInfoDto;
  7. import com.xy.entity.DeviceEventMsg;
  8. import com.xy.entity.DeviceInfo;
  9. import com.xy.entity.SysCodeConfigureRedis;
  10. import com.xy.mapper.DeviceEventMsgMapper;
  11. import com.xy.utils.*;
  12. import com.xy.utils.enums.SysCodeConfigureEnum;
  13. import io.swagger.annotations.Api;
  14. import io.swagger.annotations.ApiOperation;
  15. import lombok.AllArgsConstructor;
  16. import org.springframework.context.annotation.Lazy;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.web.bind.annotation.PostMapping;
  19. import org.springframework.web.bind.annotation.RequestBody;
  20. import java.time.LocalDateTime;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.stream.Collectors;
  25. import static com.xy.utils.Beans.copy;
  26. import static com.xy.utils.PlusBeans.toIPage;
  27. import static com.xy.utils.PlusBeans.toPageBean;
  28. /**
  29. * <p>
  30. * 机器-消息表 服务实现类
  31. * </p>
  32. *
  33. * @author lijin
  34. * @since 2023-01-09
  35. */
  36. @Service
  37. @Api(tags = "机器-消息表")
  38. @AllArgsConstructor(onConstructor_ = @Lazy)
  39. public class DeviceEventMsgServiceImpl extends ServiceImpl<DeviceEventMsgMapper, DeviceEventMsg> implements DeviceEventMsgService {
  40. private DeviceInfoServiceImpl deviceInfoService;
  41. @Override
  42. @ApiOperation("添加")
  43. public R save(DeviceEventMsgDto.Save save) {
  44. DeviceInfoDto.Vo deviceInfo = deviceInfoService.obj(new DeviceInfoDto.Obj().setDeviceId(save.getDeviceId()).setIsSysinfo(true)).getData();
  45. if (deviceInfo == null) {
  46. return R.fail("设备不存在");
  47. }
  48. //查询消息编码信息
  49. String paterCode = save.getCode().substring(0, save.getCode().length() - 1);
  50. SysCodeConfigureRedis sysCodeConfigureRedis = SysCodeConfigureUtils.get(paterCode, save.getCode());
  51. //添加消息记录
  52. save.setMercId(deviceInfo.getMercId());
  53. DeviceEventMsg deviceEventMsg = copy(DeviceEventMsg.class, save)
  54. .setCreateTime(LocalDateTime.now());
  55. save(deviceEventMsg);
  56. //异常消息,修改设备当前故障等级
  57. if (paterCode.equals(SysCodeConfigureEnum.D01.getCode())) {
  58. if (Emptys.check(sysCodeConfigureRedis.getExpand())) {
  59. DeviceInfo updateDeviceInfo = new DeviceInfo()
  60. .setDeviceId(deviceInfo.getDeviceId())
  61. .setFaultLevel(Integer.valueOf(sysCodeConfigureRedis.getExpand()));
  62. deviceInfoService.updateById(updateDeviceInfo);
  63. }
  64. }
  65. return R.ok();
  66. }
  67. @Override
  68. @ApiOperation("分页查询")
  69. public R<PageBean<DeviceEventMsgDto.Vo>> page(@RequestBody DeviceEventMsgDto.Page page) {
  70. List<String> codes = getCodes(page.getCode());
  71. PageBean pageBean = page.getPage();
  72. LambdaQueryWrapper<DeviceEventMsg> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(page, DeviceEventMsg.class)
  73. .ge(DeviceEventMsg::getCreateTime, page.getBeginCreateTime())
  74. .le(DeviceEventMsg::getCreateTime, page.getEndCreateTime())
  75. .in(DeviceEventMsg::getCode, codes)
  76. .build()
  77. .orderByDesc(!Emptys.check(pageBean.getOrders()), DeviceEventMsg::getId);
  78. IPage<DeviceEventMsg> iPage = page(toIPage(pageBean), lambdaQueryWrapper);
  79. PageBean<DeviceEventMsgDto.Vo> voPageBean = toPageBean(DeviceEventMsgDto.Vo.class, iPage);
  80. //反显设备名称
  81. if (Emptys.check(voPageBean.getRecords())) {
  82. List<Long> deviceIdList = voPageBean.getRecords().stream().map(DeviceEventMsgDto.Vo::getDeviceId).collect(Collectors.toList());
  83. Map<Long, String> deviceMap = deviceInfoService.getDeviceNameList(new DeviceInfoDto.DeviceIdDto().setDeviceId(deviceIdList)).getData();
  84. Map<String, SysCodeConfigureRedis> stringSysCodeConfigureRedisMap = SysCodeConfigureUtils.get(SysCodeConfigureEnum.D01.getCode());
  85. voPageBean.getRecords().forEach(i -> {
  86. i.setDeviceName(deviceMap.get(i.getDeviceId()));
  87. SysCodeConfigureRedis sysCodeConfigureRedis = stringSysCodeConfigureRedisMap.get(i.getCode());
  88. if (sysCodeConfigureRedis != null) {
  89. i.setSysCodeConfigureRedis(sysCodeConfigureRedis);
  90. }
  91. });
  92. }
  93. return R.ok(voPageBean);
  94. }
  95. @PostMapping("list")
  96. @ApiOperation("集合查询")
  97. public R<List<DeviceEventMsgDto.Vo>> list(@RequestBody DeviceEventMsgDto.SelectList selectList) {
  98. List<String> codes = getCodes(selectList.getCode());
  99. LambdaQueryWrapper<DeviceEventMsg> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(selectList, DeviceEventMsg.class)
  100. .in(DeviceEventMsg::getId, selectList.getIds())
  101. .in(DeviceEventMsg::getDeviceId, selectList.getDeviceIds())
  102. .in(DeviceEventMsg::getCode, codes)
  103. .build();
  104. List<DeviceEventMsg> list = list(lambdaQueryWrapper);
  105. Map<String, SysCodeConfigureRedis> stringSysCodeConfigureRedisMap = SysCodeConfigureUtils.get(SysCodeConfigureEnum.D01.getCode());
  106. List<DeviceEventMsgDto.Vo> vos = copy(DeviceEventMsgDto.Vo.class, list);
  107. vos.forEach(vo -> {
  108. SysCodeConfigureRedis sysCodeConfigureRedis = stringSysCodeConfigureRedisMap.get(vo.getCode());
  109. if (sysCodeConfigureRedis != null) {
  110. vo.setSysCodeConfigureRedis(sysCodeConfigureRedis);
  111. }
  112. });
  113. return R.ok(vos);
  114. }
  115. private List<String> getCodes(String code) {
  116. List<String> codes = new ArrayList<>();
  117. if (Emptys.check(code)) {
  118. Map<String, SysCodeConfigureRedis> stringSysCodeConfigureRedisMap = SysCodeConfigureUtils.get(code);
  119. if (Emptys.check(stringSysCodeConfigureRedisMap)) {
  120. stringSysCodeConfigureRedisMap.forEach((code2, sysCodeConfigureRedis) -> codes.add(code2));
  121. } else {
  122. codes.add(code);
  123. }
  124. }
  125. return codes;
  126. }
  127. }