gaoluyang
3 小时以前 7a923e32e55e9c34830690341469cc170f65092e
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package cn.iocoder.yudao.module.mes.service.pro.process;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
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.process.vo.MesProProcessPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.pro.process.vo.MesProProcessSaveReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.pro.process.vo.MesProProcessImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.pro.process.vo.MesProProcessImportRespVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.process.MesProProcessDO;
import cn.iocoder.yudao.module.mes.dal.mysql.pro.process.MesProProcessMapper;
import cn.iocoder.yudao.module.mes.enums.pro.MesProProcessBillingTypeEnum;
import cn.iocoder.yudao.module.mes.service.pro.route.MesProRouteProcessService;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.Map;
 
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 MesProProcessServiceImpl implements MesProProcessService {
 
    @Resource
    private MesProProcessMapper processMapper;
 
    @Resource
    private MesProProcessContentService processContentService;
    @Resource
    @Lazy
    private MesProRouteProcessService routeProcessService;
 
    @Override
    public Long createProcess(MesProProcessSaveReqVO createReqVO) {
        // 1. 校验编码、名称唯一
        validateProcessSaveData(createReqVO);
 
        // 2. 插入工序
        MesProProcessDO process = BeanUtils.toBean(createReqVO, MesProProcessDO.class);
        processMapper.insert(process);
        return process.getId();
    }
 
    @Override
    public void updateProcess(MesProProcessSaveReqVO updateReqVO) {
        // 1.1 校验存在
        validateProcessExists(updateReqVO.getId());
        // 1.2 校验编码、名称唯一
        validateProcessSaveData(updateReqVO);
 
        // 2. 更新工序
        MesProProcessDO updateObj = BeanUtils.toBean(updateReqVO, MesProProcessDO.class);
        processMapper.updateById(updateObj);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProcess(Long id) {
        // 1.1 校验存在
        validateProcessExists(id);
        // 1.2 校验是否被工艺路线引用
        if (CollUtil.isNotEmpty(routeProcessService.getRouteProcessListByProcessId(id))) {
            throw exception(PRO_PROCESS_USED_BY_ROUTE);
        }
 
        // 2. 删除工序
        processMapper.deleteById(id);
        // 3. 级联删除工序内容
        processContentService.deleteProcessContentByProcessId(id);
    }
 
    @Override
    public void validateProcessExists(Long id) {
        if (processMapper.selectById(id) == null) {
            throw exception(PRO_PROCESS_NOT_EXISTS);
        }
    }
 
    @Override
    public void validateProcessExistsAndEnable(Long id) {
        MesProProcessDO process = processMapper.selectById(id);
        if (process == null) {
            throw exception(PRO_PROCESS_NOT_EXISTS);
        }
        if (ObjUtil.notEqual(CommonStatusEnum.ENABLE.getStatus(), process.getStatus())) {
            throw exception(PRO_PROCESS_IS_DISABLE);
        }
    }
 
    private void validateProcessCodeUnique(Long id, String code) {
        MesProProcessDO process = processMapper.selectByCode(code);
        if (process == null) {
            return;
        }
        if (id == null) {
            throw exception(PRO_PROCESS_CODE_EXISTS);
        }
        if (!process.getId().equals(id)) {
            throw exception(PRO_PROCESS_CODE_EXISTS);
        }
    }
 
    private void validateProcessNameUnique(Long id, String name) {
        MesProProcessDO process = processMapper.selectByName(name);
        if (process == null) {
            return;
        }
        if (id == null) {
            throw exception(PRO_PROCESS_NAME_EXISTS);
        }
        if (!process.getId().equals(id)) {
            throw exception(PRO_PROCESS_NAME_EXISTS);
        }
    }
 
    private void validateProcessSaveData(MesProProcessSaveReqVO saveReqVO) {
        // 1. 校验编码唯一
        validateProcessCodeUnique(saveReqVO.getId(), saveReqVO.getCode());
        // 2. 校验名称唯一
        validateProcessNameUnique(saveReqVO.getId(), saveReqVO.getName());
        // 3. 校验计费类型合法(1-计时 2-计件),否则报工无法确定工资算法
        if (MesProProcessBillingTypeEnum.valueOfType(saveReqVO.getBillingType()) == null) {
            throw exception(PRO_PROCESS_BILLING_TYPE_INVALID, saveReqVO.getName(), saveReqVO.getBillingType());
        }
        // 4. 校验工资定额非负(允许 0,表示暂未定价,此时报工工资为 0,核算时可按人调整)
        if (saveReqVO.getWageRate() != null && saveReqVO.getWageRate().signum() < 0) {
            throw exception(PRO_PROCESS_WAGE_RATE_INVALID, saveReqVO.getName(), saveReqVO.getWageRate());
        }
    }
 
    @Override
    public MesProProcessDO getProcess(Long id) {
        return processMapper.selectById(id);
    }
 
    @Override
    public List<MesProProcessDO> getProcessList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return processMapper.selectByIds(ids);
    }
 
    @Override
    public PageResult<MesProProcessDO> getProcessPage(MesProProcessPageReqVO pageReqVO) {
        return processMapper.selectPage(pageReqVO);
    }
 
    @Override
    public MesProProcessImportRespVO importProcessList(List<MesProProcessImportExcelVO> rows, boolean updateSupport) {
        if (CollUtil.isEmpty(rows)) {
            throw new cn.iocoder.yudao.framework.common.exception.ServiceException(400, "导入文件没有工序数据");
        }
        MesProProcessImportRespVO result = MesProProcessImportRespVO.builder()
                .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>())
                .failureCodes(new LinkedHashMap<>()).build();
        Set<String> seenCodes = new HashSet<>();
        for (int index = 0; index < rows.size(); index++) {
            MesProProcessImportExcelVO row = rows.get(index);
            String code = row.getCode() == null ? "" : row.getCode().trim();
            String key = code.isEmpty() ? "第 " + (index + 2) + " 行" : code;
            try {
                if (code.isEmpty() || row.getName() == null || row.getName().isBlank()) {
                    throw new IllegalArgumentException("工序编码和名称不能为空");
                }
                if (!seenCodes.add(code)) throw new IllegalArgumentException("文件内工序编码重复");
                if (row.getBillingType() == null || row.getWageRate() == null) {
                    throw new IllegalArgumentException("计费类型和工资定额不能为空");
                }
                if (row.getStatus() != null && row.getStatus() != 0 && row.getStatus() != 1) {
                    throw new IllegalArgumentException("状态只能填写 0 或 1");
                }
                MesProProcessSaveReqVO save = BeanUtils.toBean(row, MesProProcessSaveReqVO.class);
                save.setStdTimeMode(parseStdTimeMode(row.getStdTimeModeName()));
                save.setCode(code);
                save.setName(row.getName().trim());
                save.setStatus(row.getStatus() == null ? 0 : row.getStatus());
                MesProProcessDO existing = processMapper.selectByCode(code);
                if (existing == null) {
                    createProcess(save);
                    result.getCreateCodes().add(code);
                } else if (updateSupport) {
                    save.setId(existing.getId());
                    updateProcess(save);
                    result.getUpdateCodes().add(code);
                } else {
                    throw new IllegalArgumentException("工序编码已存在,勾选覆盖后可更新");
                }
            } catch (RuntimeException ex) {
                result.getFailureCodes().put(key, ex.getMessage());
            }
        }
        return result;
    }
 
    private Integer parseStdTimeMode(String value) {
        if (value == null || value.isBlank()) return null;
        String normalized = value.trim().replace(" ", "").replace("*", "×").replace("*", "×");
        return switch (normalized) {
            case "0", "未启用" -> 0;
            case "1", "转速×节距" -> 1;
            case "2", "线速度直取" -> 2;
            case "3", "满速百分比×节距" -> 3;
            default -> throw new IllegalArgumentException("标准工时计算方式请填写:未启用、转速×节距、线速度直取、满速百分比×节距");
        };
    }
 
    @Override
    public List<MesProProcessDO> getProcessListByStatus(Integer status) {
        return processMapper.selectListByStatus(status);
    }
 
}