xiaoyi
4 天以前 abf1c0a35d85d38239ff5d2ed746a4e4b797c9d1
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
package cn.iocoder.yudao.module.bi.service.decision;
 
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
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 static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 
/**
 * 预警规则 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));
    }
 
}