9 天以前 8c14b50773a1e582b652c03f5a63bb2233d069b8
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package cn.iocoder.yudao.module.mes.service.cal.plan;
 
import cn.hutool.core.lang.Pair;
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.cal.plan.vo.shift.MesCalPlanShiftPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.cal.plan.vo.shift.MesCalPlanShiftSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.cal.plan.MesCalPlanDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.cal.plan.MesCalPlanShiftDO;
import cn.iocoder.yudao.module.mes.dal.mysql.cal.plan.MesCalPlanShiftMapper;
import cn.iocoder.yudao.module.mes.enums.cal.MesCalShiftTypeEnum;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import cn.hutool.core.collection.CollUtil;
 
import java.util.Collection;
import java.util.Collections;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*;
 
/**
 * MES 计划班次 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MesCalPlanShiftServiceImpl implements MesCalPlanShiftService {
 
    @Resource
    private MesCalPlanShiftMapper planShiftMapper;
 
    @Resource
    @Lazy
    private MesCalPlanService planService;
 
    @Override
    public Long createPlanShift(MesCalPlanShiftSaveReqVO createReqVO) {
        // 校验计划未确认
        planService.validatePlanPrepare(createReqVO.getPlanId());
        // 校验班次数量限制
        validatePlanShiftCount(createReqVO.getPlanId());
 
        // 插入
        MesCalPlanShiftDO planShift = BeanUtils.toBean(createReqVO, MesCalPlanShiftDO.class);
        planShiftMapper.insert(planShift);
        return planShift.getId();
    }
 
    @Override
    public void updatePlanShift(MesCalPlanShiftSaveReqVO updateReqVO) {
        // 校验存在
        MesCalPlanShiftDO existShift = validatePlanShiftExists(updateReqVO.getId());
        // 校验计划未确认
        planService.validatePlanPrepare(existShift.getPlanId());
        // 更新
        MesCalPlanShiftDO updateObj = BeanUtils.toBean(updateReqVO, MesCalPlanShiftDO.class);
        planShiftMapper.updateById(updateObj);
    }
 
    @Override
    public void deletePlanShift(Long id) {
        // 校验存在
        MesCalPlanShiftDO existShift = validatePlanShiftExists(id);
        // 校验计划未确认
        planService.validatePlanPrepare(existShift.getPlanId());
        // 删除
        planShiftMapper.deleteById(id);
    }
 
    private MesCalPlanShiftDO validatePlanShiftExists(Long id) {
        MesCalPlanShiftDO shift = planShiftMapper.selectById(id);
        if (shift == null) {
            throw exception(CAL_PLAN_SHIFT_NOT_EXISTS);
        }
        return shift;
    }
 
    /**
     * 校验班次数量是否超限
     * <p>
     * 通过 planId 查询已有班次数量,根据排班计划的轮班方式校验
     */
    private void validatePlanShiftCount(Long planId) {
        MesCalPlanDO plan = planService.getPlan(planId);
        if (plan == null) {
            return;
        }
        Long count = planShiftMapper.selectCountByPlanId(planId);
        MesCalShiftTypeEnum shiftTypeEnum = MesCalShiftTypeEnum.valueOf(plan.getShiftType());
        if (count >= shiftTypeEnum.getShiftCount()) {
            throw exception(CAL_PLAN_SHIFT_COUNT_EXCEED);
        }
    }
 
    @Override
    public MesCalPlanShiftDO getPlanShift(Long id) {
        return planShiftMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesCalPlanShiftDO> getPlanShiftPage(MesCalPlanShiftPageReqVO pageReqVO) {
        return planShiftMapper.selectPage(pageReqVO);
    }
 
    @Override
    public List<MesCalPlanShiftDO> getPlanShiftListByPlanId(Long planId) {
        return planShiftMapper.selectListByPlanId(planId);
    }
 
    @Override
    public Long getPlanShiftCountByPlanId(Long planId) {
        return planShiftMapper.selectCountByPlanId(planId);
    }
 
    @Override
    public void addDefaultPlanShift(Long planId, Integer shiftType) {
        // 根据轮班方式,通过枚举配置生成默认班次
        MesCalShiftTypeEnum shiftTypeEnum = MesCalShiftTypeEnum.valueOf(shiftType);
        List<Pair<String, String[]>> shifts = shiftTypeEnum.getShifts();
        for (int i = 0; i < shifts.size(); i++) {
            Pair<String, String[]> shift = shifts.get(i);
            MesCalPlanShiftDO planShiftDO = MesCalPlanShiftDO.builder()
                    .planId(planId).sort(i + 1).name(shift.getKey())
                    .startTime(shift.getValue()[0]).endTime(shift.getValue()[1])
                    .build();
            planShiftMapper.insert(planShiftDO);
        }
    }
 
    @Override
    public List<MesCalPlanShiftDO> getPlanShiftList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return planShiftMapper.selectByIds(ids);
    }
 
    @Override
    public void deletePlanShiftByPlanId(Long planId) {
        planShiftMapper.deleteByPlanId(planId);
    }
 
}