MonitorService.java 8.7 KB

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