package cn.iocoder.yudao.module.bi.service.decision; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.exception.ServiceException; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.module.bi.controller.admin.decision.vo.DecisionAlertRuleImportExcelVO; import cn.iocoder.yudao.module.bi.controller.admin.decision.vo.DecisionAlertRuleImportReqVO; import cn.iocoder.yudao.module.bi.controller.admin.decision.vo.DecisionAlertRuleImportRespVO; import cn.iocoder.yudao.module.bi.controller.admin.decision.vo.DecisionAlertRulePageReqVO; import cn.iocoder.yudao.module.bi.dal.dataobject.decision.BiAlertRuleDO; import cn.iocoder.yudao.module.bi.dal.dataobject.decision.BiKpiDefinitionDO; import cn.iocoder.yudao.module.bi.dal.mysql.decision.BiAlertRuleMapper; import cn.iocoder.yudao.module.bi.dal.mysql.decision.BiKpiDefinitionMapper; import cn.iocoder.yudao.module.bi.enums.ErrorCodeConstants; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; /** * 预警规则 Service 实现 * * @author 超级管理员 */ @Service public class BiAlertRuleServiceImpl implements BiAlertRuleService { @Resource private BiAlertRuleMapper alertRuleMapper; @Resource private BiKpiDefinitionMapper kpiMapper; @Override @Transactional(rollbackFor = Exception.class) public Long createRule(BiAlertRuleDO rule) { // 校验KPI存在 if (kpiMapper.selectById(rule.getKpiId()) == null) { throw exception(ErrorCodeConstants.ALERT_RULE_KPI_NOT_EXISTS); } alertRuleMapper.insert(rule); return rule.getId(); } @Override @Transactional(rollbackFor = Exception.class) public void updateRule(BiAlertRuleDO rule) { if (alertRuleMapper.selectById(rule.getId()) == null) { throw exception(ErrorCodeConstants.ALERT_RULE_NOT_EXISTS); } if (rule.getKpiId() != null && kpiMapper.selectById(rule.getKpiId()) == null) { throw exception(ErrorCodeConstants.ALERT_RULE_KPI_NOT_EXISTS); } alertRuleMapper.updateById(rule); } @Override @Transactional(rollbackFor = Exception.class) public void deleteRule(Long id) { if (alertRuleMapper.selectById(id) == null) { throw exception(ErrorCodeConstants.ALERT_RULE_NOT_EXISTS); } alertRuleMapper.deleteById(id); } @Override public BiAlertRuleDO getRule(Long id) { return alertRuleMapper.selectById(id); } @Override public PageResult getRulePage(DecisionAlertRulePageReqVO pageReq) { return alertRuleMapper.selectPage(pageReq, new LambdaQueryWrapperX() .eqIfPresent(BiAlertRuleDO::getKpiId, pageReq.getKpiId()) .eqIfPresent(BiAlertRuleDO::getEnabled, pageReq.getEnabled()) .orderByDesc(BiAlertRuleDO::getId)); } @Override @Transactional(rollbackFor = Exception.class) public DecisionAlertRuleImportRespVO importAlertRuleList(List importList, DecisionAlertRuleImportReqVO importReqVO) { // 1. 校验非空 importList = filterList(importList, item -> StrUtil.isNotBlank(item.getName())); if (CollUtil.isEmpty(importList)) { throw exception(ErrorCodeConstants.ALERT_RULE_IMPORT_LIST_IS_EMPTY); } // 2. 逐条处理 DecisionAlertRuleImportRespVO respVO = DecisionAlertRuleImportRespVO.builder() .createAlertRuleNames(new ArrayList<>()) .updateAlertRuleNames(new ArrayList<>()) .failureAlertRuleNames(new LinkedHashMap<>()) .build(); importList.forEach(item -> { try { // 2.1 解析KPI BiKpiDefinitionDO kpi = StrUtil.isBlank(item.getKpiCode()) ? null : kpiMapper.selectOne(BiKpiDefinitionDO::getCode, item.getKpiCode()); if (kpi == null) { throw exception(ErrorCodeConstants.ALERT_RULE_IMPORT_KPI_NOT_EXISTS, item.getKpiCode()); } // 2.2 判断是否存在 BiAlertRuleDO exist = alertRuleMapper.selectByName(item.getName()); if (exist == null) { // 2.2.1 新增 BiAlertRuleDO rule = BeanUtils.toBean(item, BiAlertRuleDO.class) .setKpiId(kpi.getId()); alertRuleMapper.insert(rule); respVO.getCreateAlertRuleNames().add(item.getName()); return; } // 2.2.2 已存在,判断是否允许更新 if (!importReqVO.getUpdateSupport()) { respVO.getFailureAlertRuleNames().put(item.getName(), StrUtil.format(ErrorCodeConstants.ALERT_RULE_NAME_EXISTS.getMsg(), item.getName())); return; } BiAlertRuleDO updateObj = BeanUtils.toBean(item, BiAlertRuleDO.class) .setId(exist.getId()) .setKpiId(kpi.getId()); alertRuleMapper.updateById(updateObj); respVO.getUpdateAlertRuleNames().add(item.getName()); } catch (ServiceException ex) { respVO.getFailureAlertRuleNames().put(item.getName(), ex.getMessage()); } }); return respVO; } }