DeviceEventMsgServiceImpl.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.SysCodeConfigureRedis;
  9. import com.xy.mapper.DeviceEventMsgMapper;
  10. import com.xy.service.factory.device.DeviceFactory;
  11. import com.xy.utils.*;
  12. import io.swagger.annotations.Api;
  13. import io.swagger.annotations.ApiOperation;
  14. import lombok.AllArgsConstructor;
  15. import org.springframework.context.annotation.Lazy;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.validation.annotation.Validated;
  18. import org.springframework.web.bind.annotation.PostMapping;
  19. import org.springframework.web.bind.annotation.RequestBody;
  20. import javax.validation.constraints.NotEmpty;
  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())).getData();
  45. if (deviceInfo == null) {
  46. return R.fail("设备不存在");
  47. }
  48. return FactoryUtils.getServiceRoute(DeviceFactory.class, deviceInfo.getDeviceType()).eventMsg(save);
  49. }
  50. @Override
  51. @ApiOperation("分页查询")
  52. public R<PageBean<DeviceEventMsgDto.Vo>> page(@RequestBody DeviceEventMsgDto.Page page) {
  53. Map<String, SysCodeConfigureRedis> stringSysCodeConfigureRedisMap = SysCodeConfigureUtils.all();
  54. List<String> codes = new ArrayList<>();
  55. if (Emptys.check(page.getCode())) {
  56. codes = sonCodes(page.getCode(), stringSysCodeConfigureRedisMap);
  57. codes.add(page.getCode());
  58. }
  59. PageBean pageBean = page.getPage();
  60. LambdaQueryWrapper<DeviceEventMsg> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(page, DeviceEventMsg.class)
  61. .ge(DeviceEventMsg::getCreateTime, page.getBeginCreateTime())
  62. .le(DeviceEventMsg::getCreateTime, page.getEndCreateTime())
  63. .in(DeviceEventMsg::getCode, codes)
  64. .like(DeviceEventMsg::getMsg)
  65. .build()
  66. .orderByDesc(!Emptys.check(pageBean.getOrders()), DeviceEventMsg::getId);
  67. IPage<DeviceEventMsg> iPage = page(toIPage(pageBean), lambdaQueryWrapper);
  68. PageBean<DeviceEventMsgDto.Vo> voPageBean = toPageBean(DeviceEventMsgDto.Vo.class, iPage);
  69. //反显设备名称
  70. if (Emptys.check(voPageBean.getRecords())) {
  71. List<Long> deviceIdList = voPageBean.getRecords().stream().map(DeviceEventMsgDto.Vo::getDeviceId).collect(Collectors.toList());
  72. Map<Long, String> deviceMap = deviceInfoService.getDeviceNameList(new DeviceInfoDto.DeviceIdDto().setDeviceId(deviceIdList)).getData();
  73. voPageBean.getRecords().forEach(i -> {
  74. i.setDeviceName(deviceMap.get(i.getDeviceId()));
  75. SysCodeConfigureRedis sysCodeConfigureRedis = stringSysCodeConfigureRedisMap.get(i.getCode());
  76. if (sysCodeConfigureRedis != null) {
  77. i.setSysCodeConfigureRedis(sysCodeConfigureRedis);
  78. }
  79. });
  80. }
  81. return R.ok(voPageBean);
  82. }
  83. @PostMapping("list")
  84. @ApiOperation("集合查询")
  85. public R<List<DeviceEventMsgDto.Vo>> list(@RequestBody DeviceEventMsgDto.SelectList selectList) {
  86. Map<String, SysCodeConfigureRedis> stringSysCodeConfigureRedisMap = SysCodeConfigureUtils.all();
  87. List<String> codes = new ArrayList<>();
  88. if (Emptys.check(selectList.getCode())) {
  89. codes = sonCodes(selectList.getCode(), stringSysCodeConfigureRedisMap);
  90. codes.add(selectList.getCode());
  91. }
  92. LambdaQueryWrapper<DeviceEventMsg> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(selectList, DeviceEventMsg.class)
  93. .in(DeviceEventMsg::getId, selectList.getIds())
  94. .in(DeviceEventMsg::getDeviceId, selectList.getDeviceIds())
  95. .in(DeviceEventMsg::getCode, codes)
  96. .like(DeviceEventMsg::getMsg)
  97. .build();
  98. List<DeviceEventMsg> list = list(lambdaQueryWrapper);
  99. List<DeviceEventMsgDto.Vo> vos = copy(DeviceEventMsgDto.Vo.class, list);
  100. vos.forEach(vo -> {
  101. SysCodeConfigureRedis sysCodeConfigureRedis = stringSysCodeConfigureRedisMap.get(vo.getCode());
  102. if (sysCodeConfigureRedis != null) {
  103. vo.setSysCodeConfigureRedis(sysCodeConfigureRedis);
  104. }
  105. });
  106. return R.ok(vos);
  107. }
  108. @PostMapping("count")
  109. @ApiOperation("统计查询")
  110. public R<List<DeviceEventMsgDto.CountVo>> count(@RequestBody @Validated DeviceEventMsgDto.Count count) {
  111. List<String> codes = count.getCodes();
  112. List<DeviceEventMsgDto.CountVo> countVos = new ArrayList<>(codes.size());
  113. codes.forEach(code -> {
  114. LambdaQueryWrapper<DeviceEventMsg> queryWrapper = new LambdaQueryWrapper<DeviceEventMsg>()
  115. .eq(DeviceEventMsg::getCode, code)
  116. .ge(Emptys.check(count.getCreateTimeBegin()), DeviceEventMsg::getCreateTime, count.getCreateTimeBegin())
  117. .le(Emptys.check(count.getCreateTimeEnd()), DeviceEventMsg::getCreateTime, count.getCreateTimeEnd());
  118. long countNum = count(queryWrapper);
  119. DeviceEventMsgDto.CountVo countVo = new DeviceEventMsgDto.CountVo()
  120. .setCode(code)
  121. .setCount(countNum);
  122. countVos.add(countVo);
  123. });
  124. return R.ok(countVos);
  125. }
  126. /**
  127. * 递归获取子编码
  128. *
  129. * @param code
  130. * @return
  131. */
  132. private List<String> sonCodes(String code, Map<String, SysCodeConfigureRedis> stringSysCodeConfigureRedisMap) {
  133. List<String> codes = new ArrayList<>();
  134. if (Emptys.check(code)) {
  135. stringSysCodeConfigureRedisMap.forEach((codeStr, sysCodeConfigureRedis) -> {
  136. if (!Emptys.check(sysCodeConfigureRedis.getPaterCode())) {
  137. return;
  138. }
  139. if (sysCodeConfigureRedis.getPaterCode().equals(code)) {
  140. codes.add(codeStr);
  141. List<String> list = sonCodes(codeStr, stringSysCodeConfigureRedisMap);
  142. if (Emptys.check(list)) {
  143. codes.addAll(list);
  144. }
  145. }
  146. });
  147. }
  148. return codes;
  149. }
  150. }