| | |
| | | package cn.iocoder.yudao.module.mes.service.pd.processparam; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | 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.pd.processparam.vo.MesPdProcessParamImportExcelVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pd.processparam.vo.MesPdProcessParamImportRespVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pd.processparam.vo.MesPdProcessParamPageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pd.processparam.vo.MesPdProcessParamSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.md.unitmeasure.MesMdUnitMeasureDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pd.processparam.MesPdProcessParamDO; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.md.unitmeasure.MesMdUnitMeasureMapper; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.pd.processparam.MesPdProcessParamMapper; |
| | | import cn.iocoder.yudao.module.mes.enums.md.autocode.MesMdAutoCodeRuleCodeEnum; |
| | | import cn.iocoder.yudao.module.mes.service.md.autocode.MesMdAutoCodeRecordService; |
| | | 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.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*; |
| | |
| | | |
| | | @Resource |
| | | private MesPdProcessParamMapper processParamMapper; |
| | | |
| | | @Resource |
| | | private MesMdUnitMeasureMapper unitMeasureMapper; |
| | | |
| | | @Resource |
| | | @Lazy |
| | |
| | | return processParamMapper.selectBatchIds(ids); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MesPdProcessParamImportRespVO importProcessParamList(List<MesPdProcessParamImportExcelVO> importList, |
| | | boolean updateSupport) { |
| | | // 1. 校验非空 |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(PD_PROCESS_PARAM_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | |
| | | // 2. 逐行处理 |
| | | MesPdProcessParamImportRespVO respVO = MesPdProcessParamImportRespVO.builder() |
| | | .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>()) |
| | | .failureCodes(new LinkedHashMap<>()).build(); |
| | | AtomicInteger index = new AtomicInteger(1); |
| | | importList.forEach(importItem -> { |
| | | int currentIndex = index.getAndIncrement(); |
| | | String key = StrUtil.blankToDefault(importItem.getParamCode(), "第 " + currentIndex + " 行"); |
| | | if (StrUtil.isBlank(importItem.getParamName())) { |
| | | respVO.getFailureCodes().put(key, "参数名称不能为空"); |
| | | return; |
| | | } |
| | | // 解析计量单位(按名称) |
| | | Long unitMeasureId = null; |
| | | if (StrUtil.isNotBlank(importItem.getUnitMeasureName())) { |
| | | MesMdUnitMeasureDO unitMeasure = unitMeasureMapper.selectByName(importItem.getUnitMeasureName()); |
| | | if (unitMeasure == null) { |
| | | respVO.getFailureCodes().put(key, "计量单位不存在:" + importItem.getUnitMeasureName()); |
| | | return; |
| | | } |
| | | unitMeasureId = unitMeasure.getId(); |
| | | } |
| | | // 2.1 判断:创建 or 更新(编码为空时自动生成) |
| | | if (StrUtil.isNotBlank(importItem.getParamCode())) { |
| | | MesPdProcessParamDO existParam = processParamMapper.selectByParamCode(importItem.getParamCode()); |
| | | if (existParam == null) { |
| | | // 2.1.1 指定编码新增 |
| | | MesPdProcessParamDO param = BeanUtils.toBean(importItem, MesPdProcessParamDO.class); |
| | | param.setId(null); |
| | | param.setUnitMeasureId(unitMeasureId); |
| | | param.setRequired(ObjUtil.defaultIfNull(importItem.getRequired(), Boolean.FALSE)); |
| | | processParamMapper.insert(param); |
| | | respVO.getCreateCodes().add(importItem.getParamCode()); |
| | | } else if (updateSupport) { |
| | | // 2.1.2 更新 |
| | | MesPdProcessParamDO updateObj = BeanUtils.toBean(importItem, MesPdProcessParamDO.class); |
| | | updateObj.setId(existParam.getId()); |
| | | updateObj.setParamCode(existParam.getParamCode()); |
| | | updateObj.setUnitMeasureId(unitMeasureId); |
| | | processParamMapper.updateById(updateObj); |
| | | respVO.getUpdateCodes().add(importItem.getParamCode()); |
| | | } else { |
| | | respVO.getFailureCodes().put(key, "参数编码已存在"); |
| | | } |
| | | } else { |
| | | // 2.1.3 编码为空:自动生成编码新增 |
| | | MesPdProcessParamDO param = BeanUtils.toBean(importItem, MesPdProcessParamDO.class); |
| | | param.setId(null); |
| | | param.setParamCode(autoCodeRecordService.generateAutoCode( |
| | | MesMdAutoCodeRuleCodeEnum.PD_PROCESS_PARAM_CODE.getCode())); |
| | | param.setUnitMeasureId(unitMeasureId); |
| | | param.setRequired(ObjUtil.defaultIfNull(importItem.getRequired(), Boolean.FALSE)); |
| | | processParamMapper.insert(param); |
| | | respVO.getCreateCodes().add(param.getParamCode()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | } |