| | |
| | | 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 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 实现 |
| | |
| | | .orderByDesc(BiAlertRuleDO::getId)); |
| | | } |
| | | |
| | | } |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public DecisionAlertRuleImportRespVO importAlertRuleList(List<DecisionAlertRuleImportExcelVO> 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; |
| | | } |
| | | |
| | | } |