| | |
| | | package cn.iocoder.yudao.module.mes.service.pd.processparamtemplate; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | 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.processparamtemplate.vo.MesPdProcessParamTemplateImportExcelVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pd.processparamtemplate.vo.MesPdProcessParamTemplateImportRespVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pd.processparamtemplate.vo.MesPdProcessParamTemplatePageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pd.processparamtemplate.vo.MesPdProcessParamTemplateSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pd.processparam.MesPdProcessParamDO; |
| | |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.util.ArrayList; |
| | | 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.*; |
| | |
| | | /** |
| | | * MES 工艺参数模板 Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createProcessParamTemplate(MesPdProcessParamTemplateSaveReqVO createReqVO) { |
| | | // 1. 生成模板编码 |
| | | String templateCode = autoCodeRecordService.generateAutoCode( |
| | | MesMdAutoCodeRuleCodeEnum.PD_PROCESS_PARAM_TEMPLATE_CODE.getCode()); |
| | | // 1. 使用手填编码,或自动生成编码 |
| | | String templateCode = createReqVO.getTemplateCode(); |
| | | if (StrUtil.isNotBlank(templateCode)) { |
| | | if (templateMapper.selectByTemplateCode(templateCode) != null) { |
| | | throw exception(PD_PROCESS_PARAM_TEMPLATE_CODE_DUPLICATE); |
| | | } |
| | | } else { |
| | | templateCode = autoCodeRecordService.generateAutoCode( |
| | | MesMdAutoCodeRuleCodeEnum.PD_PROCESS_PARAM_TEMPLATE_CODE.getCode()); |
| | | } |
| | | // 2. 插入模板主表 |
| | | MesPdProcessParamTemplateDO template = BeanUtils.toBean(createReqVO, MesPdProcessParamTemplateDO.class); |
| | | template.setTemplateCode(templateCode); |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateProcessParamTemplate(MesPdProcessParamTemplateSaveReqVO updateReqVO) { |
| | | // 1. 校验存在 |
| | | validateProcessParamTemplateExists(updateReqVO.getId()); |
| | | // 2. 更新模板主表 |
| | | MesPdProcessParamTemplateDO existing = validateProcessParamTemplateExists(updateReqVO.getId()); |
| | | // 2. 更新模板主表:编码留空或未变化则不修改;填写了新编码则校验唯一 |
| | | MesPdProcessParamTemplateDO updateObj = BeanUtils.toBean(updateReqVO, MesPdProcessParamTemplateDO.class); |
| | | String templateCode = updateReqVO.getTemplateCode(); |
| | | if (StrUtil.isNotBlank(templateCode) && !StrUtil.equals(templateCode, existing.getTemplateCode())) { |
| | | if (templateMapper.selectByTemplateCode(templateCode) != null) { |
| | | throw exception(PD_PROCESS_PARAM_TEMPLATE_CODE_DUPLICATE); |
| | | } |
| | | updateObj.setTemplateCode(templateCode); |
| | | } else { |
| | | // 置空,避免 MyBatis-Plus 更新时覆盖原编码 |
| | | updateObj.setTemplateCode(null); |
| | | } |
| | | templateMapper.updateById(updateObj); |
| | | // 3. 删除旧明细并重新插入(快照模式:先删后插) |
| | | detailMapper.deleteByTemplateId(updateReqVO.getId()); |
| | |
| | | return detailMapper.selectListByTemplateId(templateId); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MesPdProcessParamTemplateImportRespVO importProcessParamTemplateList(List<MesPdProcessParamTemplateImportExcelVO> importList, |
| | | boolean updateSupport) { |
| | | // 1. 校验非空 |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(PD_PROCESS_PARAM_TEMPLATE_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | |
| | | // 2. 逐行处理 |
| | | MesPdProcessParamTemplateImportRespVO respVO = MesPdProcessParamTemplateImportRespVO.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.getTemplateCode(), "第 " + currentIndex + " 行"); |
| | | if (StrUtil.isBlank(importItem.getTemplateName())) { |
| | | respVO.getFailureCodes().put(key, "模板名称不能为空"); |
| | | return; |
| | | } |
| | | // 2.1 判断:创建 or 更新(编码为空时自动生成) |
| | | if (StrUtil.isNotBlank(importItem.getTemplateCode())) { |
| | | MesPdProcessParamTemplateDO existTemplate = templateMapper.selectByTemplateCode(importItem.getTemplateCode()); |
| | | if (existTemplate == null) { |
| | | // 2.1.1 指定编码新增(仅主表,明细需在页面维护) |
| | | MesPdProcessParamTemplateDO template = BeanUtils.toBean(importItem, MesPdProcessParamTemplateDO.class); |
| | | template.setId(null); |
| | | templateMapper.insert(template); |
| | | respVO.getCreateCodes().add(importItem.getTemplateCode()); |
| | | } else if (updateSupport) { |
| | | // 2.1.2 更新(仅更新主表,不动明细) |
| | | MesPdProcessParamTemplateDO updateObj = BeanUtils.toBean(importItem, MesPdProcessParamTemplateDO.class); |
| | | updateObj.setId(existTemplate.getId()); |
| | | updateObj.setTemplateCode(existTemplate.getTemplateCode()); |
| | | templateMapper.updateById(updateObj); |
| | | respVO.getUpdateCodes().add(importItem.getTemplateCode()); |
| | | } else { |
| | | respVO.getFailureCodes().put(key, "模板编码已存在"); |
| | | } |
| | | } else { |
| | | // 2.1.3 编码为空:自动生成编码新增 |
| | | MesPdProcessParamTemplateDO template = BeanUtils.toBean(importItem, MesPdProcessParamTemplateDO.class); |
| | | template.setId(null); |
| | | template.setTemplateCode(autoCodeRecordService.generateAutoCode( |
| | | MesMdAutoCodeRuleCodeEnum.PD_PROCESS_PARAM_TEMPLATE_CODE.getCode())); |
| | | templateMapper.insert(template); |
| | | respVO.getCreateCodes().add(template.getTemplateCode()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | /** |
| | | * 创建模板明细(快照逻辑:从工艺参数主表复制值到明细) |
| | | * |