xiaoyi
3 天以前 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
77
78
79
80
81
82
83
84
85
86
87
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.DecisionAlertPageReqVO;
import cn.iocoder.yudao.module.bi.dal.dataobject.decision.BiAlertRecordDO;
import cn.iocoder.yudao.module.bi.dal.mysql.decision.BiAlertRecordMapper;
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.time.LocalDateTime;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 
/**
 * 预警记录 Service 实现
 *
 * @author 超级管理员
 */
@Service
public class BiAlertRecordServiceImpl implements BiAlertRecordService {
 
    @Resource
    private BiAlertRecordMapper alertRecordMapper;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createRecord(BiAlertRecordDO record) {
        record.setStatus(0); // 未处理
        record.setTriggeredTime(LocalDateTime.now());
        alertRecordMapper.insert(record);
        return record.getId();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void confirmRecord(Long id, Long handlerId) {
        BiAlertRecordDO record = alertRecordMapper.selectById(id);
        if (record == null) {
            throw exception(ErrorCodeConstants.ALERT_RECORD_NOT_EXISTS);
        }
        if (record.getStatus() >= 1) {
            throw exception(ErrorCodeConstants.ALERT_RECORD_ALREADY_HANDLED);
        }
        alertRecordMapper.updateById(BiAlertRecordDO.builder()
                .id(id)
                .status(1) // 已确认
                .handlerId(handlerId)
                .build());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleRecord(Long id, Long handlerId, String handleRemark) {
        BiAlertRecordDO record = alertRecordMapper.selectById(id);
        if (record == null) {
            throw exception(ErrorCodeConstants.ALERT_RECORD_NOT_EXISTS);
        }
        if (record.getStatus() >= 2) {
            throw exception(ErrorCodeConstants.ALERT_RECORD_ALREADY_HANDLED);
        }
        alertRecordMapper.updateById(BiAlertRecordDO.builder()
                .id(id)
                .status(2) // 已处理
                .handlerId(handlerId)
                .handleTime(LocalDateTime.now())
                .handleRemark(handleRemark)
                .build());
    }
 
    @Override
    public PageResult<BiAlertRecordDO> getRecordPage(DecisionAlertPageReqVO pageReq) {
        return alertRecordMapper.selectPage(pageReq, new LambdaQueryWrapperX<BiAlertRecordDO>()
                .eqIfPresent(BiAlertRecordDO::getStatus, pageReq.getStatus())
                .eqIfPresent(BiAlertRecordDO::getSeverity, pageReq.getSeverity())
                .eqIfPresent(BiAlertRecordDO::getKpiCode, pageReq.getKpiCode())
                .orderByDesc(BiAlertRecordDO::getTriggeredTime));
    }
 
    @Override
    public Long getUnprocessedCount() {
        return alertRecordMapper.selectCountByStatus(0);
    }
 
}