gaoluyang
6 天以前 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
150
151
152
153
154
155
156
157
158
package cn.iocoder.yudao.module.mes.service.qc.ipqc;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.mes.controller.admin.qc.ipqc.vo.MesQcIpqcWageFieldRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.qc.ipqc.vo.MesQcIpqcWageFieldSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pro.wage.MesProProcessWageFieldDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.ipqc.MesQcIpqcDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.ipqc.MesQcIpqcWageFieldDO;
import cn.iocoder.yudao.module.mes.dal.mysql.qc.ipqc.MesQcIpqcWageFieldMapper;
import cn.iocoder.yudao.module.mes.service.pro.wage.MesProProcessWageFieldService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.math.BigDecimal;
import java.util.*;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.QC_IPQC_NOT_EXISTS;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.QC_IPQC_WAGE_FIELD_NOT_BELONG;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.QC_IPQC_WAGE_FIELD_VALUE_INVALID;
 
/**
 * MES 过程检验单-工资字段 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class MesQcIpqcWageFieldServiceImpl implements MesQcIpqcWageFieldService {
 
    @Resource
    private MesQcIpqcWageFieldMapper wageFieldMapper;
    @Resource
    private MesProProcessWageFieldService processWageFieldService;
    @Resource
    private MesQcIpqcService ipqcService;
 
    @Override
    public List<MesQcIpqcWageFieldRespVO> getFieldList(Long processId, Long ipqcId) {
        Long finalProcessId = processId;
        if (finalProcessId == null && ipqcId != null) {
            MesQcIpqcDO ipqc = ipqcService.getIpqc(ipqcId);
            finalProcessId = ipqc != null ? ipqc.getProcessId() : null;
        }
        if (finalProcessId == null) {
            return Collections.emptyList();
        }
        // 1. 以工序当前启用的工资字段为准
        List<MesProProcessWageFieldDO> fieldList = processWageFieldService.getFieldList(finalProcessId).stream()
                .filter(field -> Objects.equals(field.getStatus(), CommonStatusEnum.ENABLE.getStatus()))
                .sorted(Comparator.comparing(MesProProcessWageFieldDO::getSort,
                        Comparator.nullsLast(Comparator.naturalOrder())))
                .toList();
        if (CollUtil.isEmpty(fieldList)) {
            return Collections.emptyList();
        }
        // 2. 合并该检验单已填写的值
        Map<Long, MesQcIpqcWageFieldDO> valueMap = new HashMap<>();
        if (ipqcId != null) {
            for (MesQcIpqcWageFieldDO saved : wageFieldMapper.selectListByIpqcId(ipqcId)) {
                valueMap.put(saved.getFieldId(), saved);
            }
        }
        List<MesQcIpqcWageFieldRespVO> result = new ArrayList<>(fieldList.size());
        for (MesProProcessWageFieldDO field : fieldList) {
            MesQcIpqcWageFieldDO saved = valueMap.get(field.getId());
            result.add(new MesQcIpqcWageFieldRespVO()
                    .setFieldId(field.getId())
                    .setFieldCode(field.getFieldCode())
                    .setFieldName(field.getFieldName())
                    .setFieldType(field.getFieldType())
                    .setUnit(field.getUnit())
                    .setCalcRole(field.getCalcRole())
                    .setUnitPrice(field.getUnitPrice())
                    .setSort(field.getSort())
                    .setDefaultValue(field.getFieldValue())
                    .setValue(saved != null ? saved.getValue() : null)
                    .setRemark(saved != null ? saved.getRemark() : null));
        }
        return result;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveValues(MesQcIpqcWageFieldSaveReqVO reqVO) {
        MesQcIpqcDO ipqc = ipqcService.getIpqc(reqVO.getIpqcId());
        if (ipqc == null) {
            throw exception(QC_IPQC_NOT_EXISTS, reqVO.getIpqcId());
        }
        Map<Long, MesProProcessWageFieldDO> fieldMap = new HashMap<>();
        for (MesProProcessWageFieldDO field : processWageFieldService.getFieldList(ipqc.getProcessId())) {
            fieldMap.put(field.getId(), field);
        }
        // 1. 校验并组装明细(快照字段定义,历史检验单不受字段改名影响)
        List<MesQcIpqcWageFieldDO> details = new ArrayList<>();
        if (reqVO.getList() != null) {
            for (MesQcIpqcWageFieldSaveReqVO.Item item : reqVO.getList()) {
                MesProProcessWageFieldDO field = fieldMap.get(item.getFieldId());
                if (field == null) {
                    throw exception(QC_IPQC_WAGE_FIELD_NOT_BELONG, item.getFieldId(),
                            ipqc != null ? ipqc.getCode() : reqVO.getIpqcId());
                }
                String value = StrUtil.trim(item.getValue());
                if (StrUtil.isNotBlank(value) && needsNumber(field.getCalcRole())) {
                    try {
                        new BigDecimal(value);
                    } catch (NumberFormatException ex) {
                        throw exception(QC_IPQC_WAGE_FIELD_VALUE_INVALID, field.getFieldName(), value);
                    }
                }
                details.add(new MesQcIpqcWageFieldDO()
                        .setIpqcId(ipqc.getId())
                        .setFieldId(field.getId())
                        .setFieldCode(field.getFieldCode())
                        .setFieldName(field.getFieldName())
                        .setFieldType(field.getFieldType())
                        .setUnit(field.getUnit())
                        .setCalcRole(field.getCalcRole())
                        .setUnitPrice(field.getUnitPrice())
                        .setSort(field.getSort())
                        .setValue(value)
                        .setRemark(StrUtil.trim(item.getRemark())));
            }
        }
        // 2. 全量覆盖该检验单的工资字段值(未提交=未填写)
        wageFieldMapper.deleteByIpqcId(ipqc.getId());
        details.forEach(wageFieldMapper::insert);
    }
 
    @Override
    public Map<String, String> getValueMapByIpqcIds(Collection<Long> ipqcIds) {
        if (CollUtil.isEmpty(ipqcIds)) {
            return Collections.emptyMap();
        }
        Map<String, String> result = new HashMap<>();
        for (MesQcIpqcWageFieldDO item : wageFieldMapper.selectListByIpqcIds(ipqcIds)) {
            if (StrUtil.isBlank(item.getFieldCode()) || StrUtil.isBlank(item.getValue())) {
                continue;
            }
            result.putIfAbsent(item.getFieldCode(), item.getValue());
        }
        return result;
    }
 
    /**
     * 系数/加减金额/计价数量三种计算角色必须是数字
     */
    private static boolean needsNumber(Integer calcRole) {
        return Objects.equals(calcRole, MesProProcessWageFieldDO.CALC_ROLE_COEFFICIENT)
                || Objects.equals(calcRole, MesProProcessWageFieldDO.CALC_ROLE_AMOUNT)
                || Objects.equals(calcRole, MesProProcessWageFieldDO.CALC_ROLE_QUANTITY_PRICE);
    }
 
}