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);
|
}
|
|
}
|