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 getRulePage(DecisionAlertRulePageReqVO pageReq) { return alertRuleMapper.selectPage(pageReq, new LambdaQueryWrapperX() .eqIfPresent(BiAlertRuleDO::getKpiId, pageReq.getKpiId()) .eqIfPresent(BiAlertRuleDO::getEnabled, pageReq.getEnabled()) .orderByDesc(BiAlertRuleDO::getId)); } }