123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- package com.example.xiaoshiweixinback.service;
- import com.alibaba.fastjson.JSON;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.example.xiaoshiweixinback.business.common.base.Records;
- import com.example.xiaoshiweixinback.business.exception.BusinessException;
- import com.example.xiaoshiweixinback.business.exception.ExceptionEnum;
- import com.example.xiaoshiweixinback.business.utils.CacheUtil;
- import com.example.xiaoshiweixinback.business.utils.LoginUtils;
- import com.example.xiaoshiweixinback.domain.AssoPersonProduct;
- import com.example.xiaoshiweixinback.domain.AssoVipFunction;
- import com.example.xiaoshiweixinback.domain.Monitor;
- import com.example.xiaoshiweixinback.domain.Product;
- import com.example.xiaoshiweixinback.entity.dto.AssoPersonProductDTO;
- import com.example.xiaoshiweixinback.entity.dto.GetProductDTO;
- import com.example.xiaoshiweixinback.entity.dto.monitoring.AddMonitoringDTO;
- import com.example.xiaoshiweixinback.entity.dto.monitoring.CancelMonitoringDTO;
- import com.example.xiaoshiweixinback.entity.dto.monitoring.SelectMonitoringDTO;
- import com.example.xiaoshiweixinback.entity.product.ProductAddDTO;
- import com.example.xiaoshiweixinback.entity.sysFuctionRights.FunctionConfig;
- import com.example.xiaoshiweixinback.entity.vo.PersonnelVO;
- import com.example.xiaoshiweixinback.entity.vo.ProductVO;
- import com.example.xiaoshiweixinback.mapper.MonitorMapper;
- import com.example.xiaoshiweixinback.mapper.ProductMapper;
- import lombok.RequiredArgsConstructor;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.CachedIntrospectionResults;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Lazy;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- /**
- * @author admin
- * @description 针对表【monitor(产品监控表)】的数据库操作Service实现
- * @createDate 2024-04-28 16:29:49
- */
- @Service
- public class MonitorService extends ServiceImpl<MonitorMapper, Monitor> {
- @Lazy
- @Autowired
- private AssoPersonProductService assoPersonProductService;
- @Lazy
- @Autowired
- private ProductService productService;
- @Autowired
- private CacheUtil cacheUtil;
- @Autowired
- private ProductMapper productMapper;
- @Autowired
- private VipService vipService;
- private static String FUNCTION_UUID = "7";
- /**
- * 添加监控
- *
- * @param addMonitoringDTO
- * @return
- */
- @Transactional(rollbackFor = Exception.class)
- public Integer addMonitoring(AddMonitoringDTO addMonitoringDTO) {
- PersonnelVO personnelVO = cacheUtil.getLoginUser(LoginUtils.getToken());
- Integer productId = addMonitoringDTO.getProductId();
- String monitorPeriod = addMonitoringDTO.getMonitorPeriod();
- Integer concernType = addMonitoringDTO.getConcernType();
- Monitor orgMonitor =this.checkAdmin(productId);
- //关注产品
- if (productId != null) {
- AssoPersonProductDTO assoPersonProductDTO = new AssoPersonProductDTO();
- assoPersonProductDTO.setProductId(productId);
- assoPersonProductDTO.setConcernType(concernType);
- assoPersonProductService.add(assoPersonProductDTO);
- } else if (productId == null) {
- ProductAddDTO productAddDTO = new ProductAddDTO();
- BeanUtils.copyProperties(addMonitoringDTO, productAddDTO);
- productId = productService.addOrUpdateProduct(productAddDTO);
- }
- LambdaQueryWrapper<Monitor> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.eq(Monitor::getCreateId, personnelVO.getUuid())
- .eq(Monitor::getProductId, productId);
- Monitor monitor = this.getOne(queryWrapper, false);
- if (monitor != null) {
- monitor.setIfDelete(false);
- monitor.updateById();
- return productId;
- }
- //添加监控
- monitor = new Monitor();
- monitor.setCreateId(personnelVO.getUuid());
- monitor.setMonitorPeriod(monitorPeriod);
- monitor.setProductId(productId);
- monitor.insert();
- return productId;
- }
- /**
- * 取消监控
- *
- * @param cancelMonitoringDTO
- * @return
- */
- @Transactional(rollbackFor = Exception.class)
- public List<Integer> cancelMonitoring(CancelMonitoringDTO cancelMonitoringDTO) {
- List<Integer> productIds = cancelMonitoringDTO.getProductIds();
- Boolean ifCancelConcern = cancelMonitoringDTO.getIfCancelConcern();
- //取消监控
- this.cancelMonitorByProductIds(productIds);
- if (ifCancelConcern != null && ifCancelConcern.equals(true)) {
- //取消关注
- assoPersonProductService.cancelConcern(productIds);
- }
- //TODO 添加定时任务
- return productIds;
- }
- public List<Integer> cancelMonitorByProductIds(List<Integer> productIds) {
- PersonnelVO personnelVO = cacheUtil.getLoginUser(LoginUtils.getToken());
- LambdaQueryWrapper<Monitor> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.in(Monitor::getProductId, productIds)
- .eq(Monitor::getCreateId, personnelVO.getUuid());
- List<Monitor> monitors = this.list(queryWrapper);
- if (monitors != null && monitors.size() > 0) {
- monitors.forEach(item -> {
- item.setIfDelete(true);
- item.updateById();
- });
- }
- return productIds;
- }
- /**
- * 查询监控信息
- *
- * @param selectMonitoringDTO
- * @return
- */
- public Records selectMonitoring(SelectMonitoringDTO selectMonitoringDTO) {
- Long current = selectMonitoringDTO.getCurrent();
- Long size = selectMonitoringDTO.getSize();
- PersonnelVO personnelVO = cacheUtil.getLoginUser(LoginUtils.getToken());
- GetProductDTO getProductDTO = new GetProductDTO();
- getProductDTO.setSize(size);
- getProductDTO.setCurrent(current);
- getProductDTO.setPersonUuid(personnelVO.getUuid());
- List<ProductVO> productVOS = productMapper.getMonitoringProduct(getProductDTO);
- Long total = productMapper.getMonitoringProductCount(getProductDTO);
- try {
- productService.loadProduct(productVOS, false);
- } catch (Exception e) {
- throw new BusinessException(ExceptionEnum.BUSINESS_ERROR, "装载错误");
- }
- Records records = new Records();
- records.setTotal(total);
- records.setData(productVOS);
- records.setSize(size);
- records.setCurrent(current);
- return records;
- }
- public List<Product> getAllMonitorProducts() {
- List<Product> products = new ArrayList<>();
- LambdaQueryWrapper<Monitor> queryWrapper = new LambdaQueryWrapper<>();
- List<Monitor> monitors = this.list(queryWrapper);
- if (monitors.size() == 0) {
- return products;
- }
- List<Integer> productIds = monitors.stream().map(Monitor::getProductId).distinct().collect(Collectors.toList());
- if (productIds != null && productIds.size() > 0) {
- LambdaQueryWrapper<Product> queryWrapper1 = new LambdaQueryWrapper<>();
- queryWrapper1.in(Product::getId, productIds);
- products = productService.list(queryWrapper1);
- }
- return products;
- }
- public Monitor checkAdmin(Integer productId) {
- AssoPersonProduct assoPersonProduct = null;
- PersonnelVO personnelVO = cacheUtil.getLoginUser(LoginUtils.getToken());
- AssoVipFunction assoVipFunction = vipService.getVipFunctionMessage(FUNCTION_UUID);
- FunctionConfig functionConfig = JSON.parseObject(assoVipFunction.getFunctionParameter(), FunctionConfig.class);
- Integer totalNum = functionConfig.getCanGetNum();
- LambdaQueryWrapper<Monitor> queryWrapper1 = new LambdaQueryWrapper<>();
- queryWrapper1.eq(Monitor::getProductId, productId)
- .eq(Monitor::getCreateId, personnelVO.getUuid());
- Monitor org = this.getOne(queryWrapper1, false);
- if (org != null) {
- return org;
- }
- //查看关注的数量
- LambdaQueryWrapper<Monitor> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.eq(Monitor::getCreateId, personnelVO.getUuid());
- Long total = this.count(queryWrapper);
- if (total >= totalNum) {
- if(assoVipFunction.getVipType().equals(0)){
- throw new BusinessException(ExceptionEnum.PERMISSION_NO_VIP);
- }
- throw new BusinessException(ExceptionEnum.BUSINESS_ERROR, "已超过可监控数量");
- }
- return org;
- }
- }
|