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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package cn.iocoder.yudao.module.mes.service.pro.emergencyplan;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.pro.emergencyplan.vo.MesProEmergencyPlanPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.pro.emergencyplan.vo.MesProEmergencyPlanSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.emergencyplan.MesProEmergencyPlanDO;
import cn.iocoder.yudao.module.mes.dal.mysql.pro.emergencyplan.MesProEmergencyPlanMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.util.Collection;
import java.util.Objects;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PRO_WORK_ORDER_NOT_EXISTS;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PRO_WORK_ORDER_NOT_PREPARE;
 
/**
 * MES 应急处置预案 Service 实现类
 *
 * @author xiaoyi
 */
@Service
@Validated
public class MesProEmergencyPlanServiceImpl implements MesProEmergencyPlanService {
 
    private static final Integer STATUS_DRAFT = 0;
    private static final Integer STATUS_PUBLISHED = 10;
 
    @Resource
    private MesProEmergencyPlanMapper planMapper;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createPlan(MesProEmergencyPlanSaveReqVO createReqVO) {
        String code = generateCode();
        if (planMapper.selectByCode(code) != null) {
            throw exception(PRO_WORK_ORDER_NOT_EXISTS);
        }
        MesProEmergencyPlanDO plan = BeanUtils.toBean(createReqVO, MesProEmergencyPlanDO.class, req -> req
                .setCode(code)
                .setStatus(STATUS_DRAFT));
        planMapper.insert(plan);
        return plan.getId();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePlan(MesProEmergencyPlanSaveReqVO updateReqVO) {
        MesProEmergencyPlanDO oldPlan = validatePlanCanModify(updateReqVO.getId());
        MesProEmergencyPlanDO updateObj = BeanUtils.toBean(updateReqVO, MesProEmergencyPlanDO.class, req -> req
                .setCode(oldPlan.getCode())
                .setStatus(oldPlan.getStatus()));
        planMapper.updateById(updateObj);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePlan(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        for (Long id : ids) {
            validatePlanCanModify(id);
            planMapper.deleteById(id);
        }
    }
 
    @Override
    public MesProEmergencyPlanDO getPlan(Long id) {
        return planMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesProEmergencyPlanDO> getPlanPage(MesProEmergencyPlanPageReqVO pageReqVO) {
        return planMapper.selectPage(pageReqVO);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void publishPlan(Long id) {
        MesProEmergencyPlanDO plan = validatePlanExists(id);
        if (!Objects.equals(plan.getStatus(), STATUS_DRAFT)) {
            throw exception(PRO_WORK_ORDER_NOT_PREPARE);
        }
        planMapper.updateById(new MesProEmergencyPlanDO().setId(id).setStatus(STATUS_PUBLISHED));
    }
 
    private String generateCode() {
        return "YJ" + DateUtil.format(java.time.LocalDateTime.now(), "yyyyMMddHHmmss")
                + String.format("%03d", (int) (Math.random() * 1000));
    }
 
    private MesProEmergencyPlanDO validatePlanExists(Long id) {
        MesProEmergencyPlanDO plan = planMapper.selectById(id);
        if (plan == null) {
            throw exception(PRO_WORK_ORDER_NOT_EXISTS);
        }
        return plan;
    }
 
    private MesProEmergencyPlanDO validatePlanCanModify(Long id) {
        MesProEmergencyPlanDO plan = validatePlanExists(id);
        if (!Objects.equals(plan.getStatus(), STATUS_DRAFT)) {
            throw exception(PRO_WORK_ORDER_NOT_PREPARE);
        }
        return plan;
    }
 
}