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 { @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 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 cancelMonitoring(CancelMonitoringDTO cancelMonitoringDTO) { List productIds = cancelMonitoringDTO.getProductIds(); Boolean ifCancelConcern = cancelMonitoringDTO.getIfCancelConcern(); //取消监控 this.cancelMonitorByProductIds(productIds); if (ifCancelConcern != null && ifCancelConcern.equals(true)) { //取消关注 assoPersonProductService.cancelConcern(productIds); } //TODO 添加定时任务 return productIds; } public List cancelMonitorByProductIds(List productIds) { PersonnelVO personnelVO = cacheUtil.getLoginUser(LoginUtils.getToken()); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.in(Monitor::getProductId, productIds) .eq(Monitor::getCreateId, personnelVO.getUuid()); List 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 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 getAllMonitorProducts() { List products = new ArrayList<>(); LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); List monitors = this.list(queryWrapper); if (monitors.size() == 0) { return products; } List productIds = monitors.stream().map(Monitor::getProductId).distinct().collect(Collectors.toList()); if (productIds != null && productIds.size() > 0) { LambdaQueryWrapper 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 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 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; } }