MonitorService.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package com.example.xiaoshiweixinback.service;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  4. import com.example.xiaoshiweixinback.business.common.base.Records;
  5. import com.example.xiaoshiweixinback.business.exception.BusinessException;
  6. import com.example.xiaoshiweixinback.business.utils.CacheUtil;
  7. import com.example.xiaoshiweixinback.business.utils.LoginUtils;
  8. import com.example.xiaoshiweixinback.domain.Monitor;
  9. import com.example.xiaoshiweixinback.domain.Product;
  10. import com.example.xiaoshiweixinback.entity.dto.AssoPersonProductDTO;
  11. import com.example.xiaoshiweixinback.entity.dto.GetProductDTO;
  12. import com.example.xiaoshiweixinback.entity.dto.monitoring.AddMonitoringDTO;
  13. import com.example.xiaoshiweixinback.entity.dto.monitoring.CancelMonitoringDTO;
  14. import com.example.xiaoshiweixinback.entity.dto.monitoring.SelectMonitoringDTO;
  15. import com.example.xiaoshiweixinback.entity.product.ProductAddDTO;
  16. import com.example.xiaoshiweixinback.entity.vo.PersonnelVO;
  17. import com.example.xiaoshiweixinback.entity.vo.ProductVO;
  18. import com.example.xiaoshiweixinback.mapper.MonitorMapper;
  19. import com.example.xiaoshiweixinback.mapper.ProductMapper;
  20. import lombok.RequiredArgsConstructor;
  21. import org.springframework.beans.BeanUtils;
  22. import org.springframework.beans.CachedIntrospectionResults;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.context.annotation.Lazy;
  25. import org.springframework.stereotype.Service;
  26. import org.springframework.transaction.annotation.Transactional;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import java.util.stream.Collectors;
  30. /**
  31. * @author admin
  32. * @description 针对表【monitor(产品监控表)】的数据库操作Service实现
  33. * @createDate 2024-04-28 16:29:49
  34. */
  35. @Service
  36. public class MonitorService extends ServiceImpl<MonitorMapper, Monitor> {
  37. @Lazy
  38. @Autowired
  39. private AssoPersonProductService assoPersonProductService;
  40. @Lazy
  41. @Autowired
  42. private ProductService productService;
  43. @Autowired
  44. private CacheUtil cacheUtil;
  45. @Autowired
  46. private ProductMapper productMapper;
  47. /**
  48. * 添加监控
  49. *
  50. * @param addMonitoringDTO
  51. * @return
  52. */
  53. @Transactional(rollbackFor = Exception.class)
  54. public Integer addMonitoring(AddMonitoringDTO addMonitoringDTO) {
  55. PersonnelVO personnelVO = cacheUtil.getLoginUser(LoginUtils.getToken());
  56. Integer productId = addMonitoringDTO.getProductId();
  57. String monitorPeriod = addMonitoringDTO.getMonitorPeriod();
  58. Integer concernType = addMonitoringDTO.getConcernType();
  59. //关注产品
  60. if (productId != null) {
  61. AssoPersonProductDTO assoPersonProductDTO = new AssoPersonProductDTO();
  62. assoPersonProductDTO.setProductId(productId);
  63. assoPersonProductDTO.setConcernType(concernType);
  64. assoPersonProductService.add(assoPersonProductDTO);
  65. } else if (productId == null) {
  66. ProductAddDTO productAddDTO = new ProductAddDTO();
  67. BeanUtils.copyProperties(addMonitoringDTO, productAddDTO);
  68. productId = productService.addOrUpdateProduct(productAddDTO);
  69. }
  70. LambdaQueryWrapper<Monitor> queryWrapper = new LambdaQueryWrapper<>();
  71. queryWrapper.eq(Monitor::getCreateId, personnelVO.getUuid())
  72. .eq(Monitor::getProductId, productId);
  73. Monitor monitor = this.getOne(queryWrapper, false);
  74. if (monitor != null) {
  75. monitor.setIfDelete(false);
  76. monitor.updateById();
  77. return productId;
  78. }
  79. //添加监控
  80. monitor = new Monitor();
  81. monitor.setCreateId(personnelVO.getUuid());
  82. monitor.setMonitorPeriod(monitorPeriod);
  83. monitor.setProductId(productId);
  84. monitor.insert();
  85. //TODO 添加定时任务
  86. return productId;
  87. }
  88. /**
  89. * 取消监控
  90. *
  91. * @param cancelMonitoringDTO
  92. * @return
  93. */
  94. @Transactional(rollbackFor = Exception.class)
  95. public List<Integer> cancelMonitoring(CancelMonitoringDTO cancelMonitoringDTO) {
  96. List<Integer> productIds = cancelMonitoringDTO.getProductIds();
  97. Boolean ifCancelConcern = cancelMonitoringDTO.getIfCancelConcern();
  98. //取消监控
  99. this.cancelMonitorByProductIds(productIds);
  100. if (ifCancelConcern != null && ifCancelConcern.equals(true)) {
  101. //取消关注
  102. assoPersonProductService.cancelConcern(productIds);
  103. }
  104. //TODO 添加定时任务
  105. return productIds;
  106. }
  107. public List<Integer> cancelMonitorByProductIds(List<Integer> productIds) {
  108. PersonnelVO personnelVO = cacheUtil.getLoginUser(LoginUtils.getToken());
  109. LambdaQueryWrapper<Monitor> queryWrapper = new LambdaQueryWrapper<>();
  110. queryWrapper.in(Monitor::getProductId, productIds)
  111. .eq(Monitor::getCreateId, personnelVO.getUuid());
  112. List<Monitor> monitors = this.list(queryWrapper);
  113. if (monitors != null && monitors.size() > 0) {
  114. monitors.forEach(item -> {
  115. item.setIfDelete(true);
  116. item.updateById();
  117. });
  118. }
  119. return productIds;
  120. }
  121. /**
  122. * 查询监控信息
  123. *
  124. * @param selectMonitoringDTO
  125. * @return
  126. */
  127. public Records selectMonitoring(SelectMonitoringDTO selectMonitoringDTO) {
  128. Long current = selectMonitoringDTO.getCurrent();
  129. Long size = selectMonitoringDTO.getSize();
  130. PersonnelVO personnelVO = cacheUtil.getLoginUser(LoginUtils.getToken());
  131. GetProductDTO getProductDTO = new GetProductDTO();
  132. getProductDTO.setSize(size);
  133. getProductDTO.setCurrent(current);
  134. getProductDTO.setPersonUuid(personnelVO.getUuid());
  135. List<ProductVO> productVOS = productMapper.getMonitoringProduct(getProductDTO);
  136. Long total = productMapper.getMonitoringProductCount(getProductDTO);
  137. try {
  138. productService.loadProduct(productVOS, false);
  139. } catch (Exception e) {
  140. throw new BusinessException("607", "装载错误");
  141. }
  142. Records records = new Records();
  143. records.setTotal(total);
  144. records.setData(productVOS);
  145. records.setSize(size);
  146. records.setCurrent(current);
  147. return records;
  148. }
  149. public List<Product> getAllMonitorProducts() {
  150. List<Product> products = new ArrayList<>();
  151. LambdaQueryWrapper<Monitor> queryWrapper = new LambdaQueryWrapper<>();
  152. List<Monitor> monitors = this.list(queryWrapper);
  153. if (monitors.size() == 0) {
  154. return products;
  155. }
  156. List<Integer> productIds = monitors.stream().map(Monitor::getProductId).distinct().collect(Collectors.toList());
  157. if (productIds != null && productIds.size() > 0) {
  158. LambdaQueryWrapper<Product> queryWrapper1 = new LambdaQueryWrapper<>();
  159. queryWrapper1.in(Product::getId, productIds);
  160. products = productService.list(queryWrapper1);
  161. }
  162. return products;
  163. }
  164. }