DeviceEventMsgServiceImpl.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. .build()
  65. .orderByDesc(!Emptys.check(pageBean.getOrders()), DeviceEventMsg::getId);
  66. IPage<DeviceEventMsg> iPage = page(toIPage(pageBean), lambdaQueryWrapper);
  67. PageBean<DeviceEventMsgDto.Vo> voPageBean = toPageBean(DeviceEventMsgDto.Vo.class, iPage);
  68. //反显设备名称
  69. if (Emptys.check(voPageBean.getRecords())) {
  70. List<Long> deviceIdList = voPageBean.getRecords().stream().map(DeviceEventMsgDto.Vo::getDeviceId).collect(Collectors.toList());
  71. Map<Long, String> deviceMap = deviceInfoService.getDeviceNameList(new DeviceInfoDto.DeviceIdDto().setDeviceId(deviceIdList)).getData();
  72. voPageBean.getRecords().forEach(i -> {
  73. i.setDeviceName(deviceMap.get(i.getDeviceId()));
  74. SysCodeConfigureRedis sysCodeConfigureRedis = stringSysCodeConfigureRedisMap.get(i.getCode());
  75. if (sysCodeConfigureRedis != null) {
  76. i.setSysCodeConfigureRedis(sysCodeConfigureRedis);
  77. }
  78. });
  79. }
  80. return R.ok(voPageBean);
  81. }
  82. @PostMapping("list")
  83. @ApiOperation("集合查询")
  84. public R<List<DeviceEventMsgDto.Vo>> list(@RequestBody DeviceEventMsgDto.SelectList selectList) {
  85. Map<String, SysCodeConfigureRedis> stringSysCodeConfigureRedisMap = SysCodeConfigureUtils.all();
  86. List<String> codes = new ArrayList<>();
  87. if (Emptys.check(selectList.getCode())) {
  88. codes = sonCodes(selectList.getCode(), stringSysCodeConfigureRedisMap);
  89. codes.add(selectList.getCode());
  90. }
  91. LambdaQueryWrapper<DeviceEventMsg> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(selectList, DeviceEventMsg.class)
  92. .in(DeviceEventMsg::getId, selectList.getIds())
  93. .in(DeviceEventMsg::getDeviceId, selectList.getDeviceIds())
  94. .in(DeviceEventMsg::getCode, codes)
  95. .build();
  96. List<DeviceEventMsg> list = list(lambdaQueryWrapper);
  97. List<DeviceEventMsgDto.Vo> vos = copy(DeviceEventMsgDto.Vo.class, list);
  98. vos.forEach(vo -> {
  99. SysCodeConfigureRedis sysCodeConfigureRedis = stringSysCodeConfigureRedisMap.get(vo.getCode());
  100. if (sysCodeConfigureRedis != null) {
  101. vo.setSysCodeConfigureRedis(sysCodeConfigureRedis);
  102. }
  103. });
  104. return R.ok(vos);
  105. }
  106. @PostMapping("count")
  107. @ApiOperation("统计查询")
  108. public R<List<DeviceEventMsgDto.CountVo>> count(@RequestBody @Validated DeviceEventMsgDto.Count count) {
  109. List<String> codes = count.getCodes();
  110. List<DeviceEventMsgDto.CountVo> countVos = new ArrayList<>(codes.size());
  111. codes.forEach(code -> {
  112. LambdaQueryWrapper<DeviceEventMsg> queryWrapper = new LambdaQueryWrapper<DeviceEventMsg>()
  113. .eq(DeviceEventMsg::getCode, code)
  114. .ge(Emptys.check(count.getCreateTimeBegin()), DeviceEventMsg::getCreateTime, count.getCreateTimeBegin())
  115. .le(Emptys.check(count.getCreateTimeEnd()), DeviceEventMsg::getCreateTime, count.getCreateTimeEnd());
  116. long countNum = count(queryWrapper);
  117. DeviceEventMsgDto.CountVo countVo = new DeviceEventMsgDto.CountVo()
  118. .setCode(code)
  119. .setCount(countNum);
  120. countVos.add(countVo);
  121. });
  122. return R.ok(countVos);
  123. }
  124. /**
  125. * 递归获取子编码
  126. *
  127. * @param code
  128. * @return
  129. */
  130. private List<String> sonCodes(String code, Map<String, SysCodeConfigureRedis> stringSysCodeConfigureRedisMap) {
  131. List<String> codes = new ArrayList<>();
  132. if (Emptys.check(code)) {
  133. stringSysCodeConfigureRedisMap.forEach((codeStr, sysCodeConfigureRedis) -> {
  134. if (!Emptys.check(sysCodeConfigureRedis.getPaterCode())) {
  135. return;
  136. }
  137. if (sysCodeConfigureRedis.getPaterCode().equals(code)) {
  138. codes.add(codeStr);
  139. List<String> list = sonCodes(codeStr, stringSysCodeConfigureRedisMap);
  140. if (Emptys.check(list)) {
  141. codes.addAll(list);
  142. }
  143. }
  144. });
  145. }
  146. return codes;
  147. }
  148. }