gaoluyang
2026-10-16 a4a360b6ea88c6c1c8bc3f7098ee7e25a6a101c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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<BiAlertRuleDO> getRulePage(DecisionAlertRulePageReqVO pageReq) {
        return alertRuleMapper.selectPage(pageReq, new LambdaQueryWrapperX<BiAlertRuleDO>()
                .eqIfPresent(BiAlertRuleDO::getKpiId, pageReq.getKpiId())
                .eqIfPresent(BiAlertRuleDO::getEnabled, pageReq.getEnabled())
                .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;
    }
 
}