gaoluyang
昨天 8c65f898ed6e5b0c7a94c792c4562f6ec831dccd
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.pro.worktime;
 
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.pro.worktime.vo.MesProWorktimeAuxPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.pro.worktime.vo.MesProWorktimeAuxSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.process.MesProProcessDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.worktime.MesProWorktimeAuxDO;
import cn.iocoder.yudao.module.mes.dal.mysql.pro.feedback.MesProFeedbackAuxMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.pro.worktime.MesProWorktimeAuxMapper;
import cn.iocoder.yudao.module.mes.service.pro.process.MesProProcessService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
 
import java.math.BigDecimal;
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 MesProWorktimeAuxServiceImpl implements MesProWorktimeAuxService {
 
    @Resource
    private MesProWorktimeAuxMapper worktimeAuxMapper;
    @Resource
    private MesProFeedbackAuxMapper feedbackAuxMapper;
    @Resource
    private MesProProcessService processService;
 
    @Override
    public Long createWorktimeAux(MesProWorktimeAuxSaveReqVO createReqVO) {
        validateWorktimeAux(createReqVO, null);
        MesProWorktimeAuxDO aux = BeanUtils.toBean(createReqVO, MesProWorktimeAuxDO.class)
                .setId(null)
                .setStatus(ObjUtil.defaultIfNull(createReqVO.getStatus(), CommonStatusEnum.ENABLE.getStatus()))
                .setSort(ObjUtil.defaultIfNull(createReqVO.getSort(), 0));
        worktimeAuxMapper.insert(aux);
        return aux.getId();
    }
 
    @Override
    public void updateWorktimeAux(MesProWorktimeAuxSaveReqVO updateReqVO) {
        validateWorktimeAux(updateReqVO, updateReqVO.getId());
        MesProWorktimeAuxDO updateObj = BeanUtils.toBean(updateReqVO, MesProWorktimeAuxDO.class);
        worktimeAuxMapper.updateById(updateObj);
    }
 
    @Override
    public void deleteWorktimeAux(Long id) {
        MesProWorktimeAuxDO aux = validateWorktimeAuxExists(id);
        Long usedCount = feedbackAuxMapper.selectCountByAuxItemId(id);
        if (usedCount != null && usedCount > 0) {
            throw exception(PRO_WORKTIME_AUX_IN_USE, aux.getItemName());
        }
        worktimeAuxMapper.deleteById(id);
    }
 
    @Override
    public MesProWorktimeAuxDO getWorktimeAux(Long id) {
        return worktimeAuxMapper.selectById(id);
    }
 
    @Override
    public PageResult<MesProWorktimeAuxDO> getWorktimeAuxPage(MesProWorktimeAuxPageReqVO pageReqVO) {
        return worktimeAuxMapper.selectPage(pageReqVO);
    }
 
    @Override
    public List<MesProWorktimeAuxDO> getApplicableList(Long processId, Long workstationId) {
        if (processId == null) {
            return List.of();
        }
        return worktimeAuxMapper.selectListByProcessIdAndWorkstationId(processId, workstationId).stream()
                .filter(aux -> CommonStatusEnum.ENABLE.getStatus().equals(aux.getStatus()))
                .toList();
    }
 
    @Override
    public List<MesProWorktimeAuxDO> getWorktimeAuxListByProcessId(Long processId) {
        return worktimeAuxMapper.selectListByProcessId(processId);
    }
 
    private MesProWorktimeAuxDO validateWorktimeAuxExists(Long id) {
        MesProWorktimeAuxDO aux = worktimeAuxMapper.selectById(id);
        if (aux == null) {
            throw exception(PRO_WORKTIME_AUX_NOT_EXISTS);
        }
        return aux;
    }
 
    private void validateWorktimeAux(MesProWorktimeAuxSaveReqVO reqVO, Long id) {
        // 1. 工序必须存在
        MesProProcessDO process = processService.getProcess(reqVO.getProcessId());
        if (process == null) {
            throw exception(PRO_WORKTIME_AUX_PROCESS_NOT_EXISTS, reqVO.getProcessId());
        }
        // 2. 计量方式 + 对应的时间参数
        Integer measureType = reqVO.getMeasureType();
        boolean measureValid = MesProWorktimeAuxDO.MEASURE_TIMES == measureType
                || MesProWorktimeAuxDO.MEASURE_PIECES == measureType
                || MesProWorktimeAuxDO.MEASURE_REELS == measureType
                || MesProWorktimeAuxDO.MEASURE_METERS == measureType;
        if (!measureValid) {
            throw exception(PRO_WORKTIME_AUX_MEASURE_TYPE_INVALID, reqVO.getItemName(), measureType);
        }
        if (MesProWorktimeAuxDO.MEASURE_METERS == measureType) {
            BigDecimal speed = reqVO.getSpeedMPerMin();
            if (speed == null || speed.signum() <= 0) {
                throw exception(PRO_WORKTIME_AUX_SPEED_REQUIRED, reqVO.getItemName());
            }
        } else {
            BigDecimal duration = reqVO.getDurationMinutes();
            if (duration == null || duration.signum() <= 0) {
                throw exception(PRO_WORKTIME_AUX_DURATION_REQUIRED, reqVO.getItemName(), measureTypeName(measureType));
            }
        }
        // 3. 同一工序 + 同一机台范围下项目编码不能重复
        MesProWorktimeAuxDO exists = worktimeAuxMapper.selectOneByProcessIdAndWorkstationIdAndCode(
                reqVO.getProcessId(), reqVO.getWorkstationId(), reqVO.getItemCode());
        if (exists != null && !exists.getId().equals(id)) {
            String workstationScope = reqVO.getWorkstationId() == null ? "不限机台" : "机台 ID=" + reqVO.getWorkstationId();
            throw exception(PRO_WORKTIME_AUX_CODE_DUPLICATE, process.getName(), reqVO.getItemCode(), workstationScope);
        }
    }
 
    private static String measureTypeName(Integer measureType) {
        if (measureType == null) {
            return "未知";
        }
        return switch (measureType) {
            case MesProWorktimeAuxDO.MEASURE_TIMES -> "按次";
            case MesProWorktimeAuxDO.MEASURE_PIECES -> "按个";
            case MesProWorktimeAuxDO.MEASURE_REELS -> "按盘";
            case MesProWorktimeAuxDO.MEASURE_METERS -> "按米折算";
            default -> "未知";
        };
    }
 
}