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 getRecordPage(DecisionAlertPageReqVO pageReq) { return alertRecordMapper.selectPage(pageReq, new LambdaQueryWrapperX() .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); } }