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
package cn.iocoder.yudao.module.mes.service.safety;
import cn.iocoder.yudao.framework.common.pojo.*;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.mes.controller.admin.safety.vo.risk.SafetyRiskSourceSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.safety.SafetyRiskSourceDO;
import cn.iocoder.yudao.module.mes.dal.mysql.safety.SafetyRiskSourceMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.Objects;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.invalidParamException;
 
@Service public class SafetyRiskSourceService {
    @Resource private SafetyRiskSourceMapper mapper;
    public PageResult<SafetyRiskSourceDO> page(PageParam page, String name) {
        return mapper.selectPage(page, new LambdaQueryWrapperX<SafetyRiskSourceDO>().likeIfPresent(SafetyRiskSourceDO::getName,name).orderByDesc(SafetyRiskSourceDO::getId));
    }
    @Transactional(rollbackFor=Exception.class)
    public Long save(SafetyRiskSourceSaveReqVO req) {
        SafetyRiskSourceDO old = req.getId()==null ? null : mapper.selectById(req.getId());
        if (req.getId()!=null && old==null) throw invalidParamException("危险源不存在");
        String status;
        try { status=SafetyRiskThreshold.evaluate(req.getCurrentValue(),req.getLowerLimit(),req.getUpperLimit()); }
        catch (IllegalArgumentException e) { throw invalidParamException(e.getMessage()); }
        var row=BeanUtils.toBean(req,SafetyRiskSourceDO.class);
        row.setRiskStatus(status);
        row.setObservedAt(req.getCurrentValue()==null ? null : old==null || !Objects.equals(old.getCurrentValue(),req.getCurrentValue()) ? LocalDateTime.now() : old.getObservedAt());
        if (old==null) mapper.insert(row); else mapper.updateById(row);
        return row.getId();
    }
    public void delete(Long id) { if (mapper.selectById(id)==null) throw invalidParamException("危险源不存在"); mapper.deleteById(id); }
}