MonitorService.java 8.5 KB

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