| | |
| | | package cn.iocoder.yudao.module.mes.service.tm.tool; |
| | | |
| | | 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.tm.tool.vo.type.MesTmToolTypeImportExcelVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.tm.tool.vo.type.MesTmToolTypeImportRespVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.tm.tool.vo.type.MesTmToolTypePageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.tm.tool.vo.type.MesTmToolTypeSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.tm.tool.MesTmToolTypeDO; |
| | |
| | | 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 cn.hutool.core.util.ObjUtil; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | 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.*; |
| | |
| | | return toolTypeMapper.selectByIds(ids); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MesTmToolTypeImportRespVO importToolTypeList(List<MesTmToolTypeImportExcelVO> importList, boolean updateSupport) { |
| | | // 校验导入数据不能为空 |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(TM_TOOL_TYPE_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | MesTmToolTypeImportRespVO respVO = MesTmToolTypeImportRespVO.builder() |
| | | .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>()) |
| | | .failureCodes(new LinkedHashMap<>()).build(); |
| | | // 索引,用于错误提示 |
| | | AtomicInteger index = new AtomicInteger(1); |
| | | importList.forEach(importItem -> { |
| | | int currentIndex = index.getAndIncrement(); |
| | | String code = importItem.getCode(); |
| | | String key = StrUtil.blankToDefault(code, "第 " + currentIndex + " 行"); |
| | | // 校验编码必填 |
| | | if (StrUtil.isBlank(code)) { |
| | | respVO.getFailureCodes().put(key, "类型编码不能为空"); |
| | | return; |
| | | } |
| | | // 校验名称必填 |
| | | if (StrUtil.isBlank(importItem.getName())) { |
| | | respVO.getFailureCodes().put(key, "类型名称不能为空"); |
| | | return; |
| | | } |
| | | MesTmToolTypeDO existingByCode = toolTypeMapper.selectByCode(code); |
| | | MesTmToolTypeDO existingByName = toolTypeMapper.selectByName(importItem.getName()); |
| | | if (existingByCode != null && updateSupport) { |
| | | // 名称唯一校验(排除自己) |
| | | if (existingByName != null && !existingByCode.getId().equals(existingByName.getId())) { |
| | | respVO.getFailureCodes().put(key, "类型名称已存在:" + importItem.getName()); |
| | | return; |
| | | } |
| | | // 更新 |
| | | MesTmToolTypeDO updateObj = new MesTmToolTypeDO(); |
| | | updateObj.setId(existingByCode.getId()); |
| | | updateObj.setName(importItem.getName()); |
| | | updateObj.setCodeFlag(ObjUtil.defaultIfNull(importItem.getCodeFlag(), Boolean.FALSE)); |
| | | updateObj.setMaintenType(importItem.getMaintenType()); |
| | | updateObj.setMaintenPeriod(importItem.getMaintenPeriod()); |
| | | updateObj.setRemark(importItem.getRemark()); |
| | | toolTypeMapper.updateById(updateObj); |
| | | respVO.getUpdateCodes().add(code); |
| | | } else if (existingByCode != null) { |
| | | respVO.getFailureCodes().put(key, "类型编码已存在:" + code); |
| | | } else { |
| | | // 名称唯一校验 |
| | | if (existingByName != null) { |
| | | respVO.getFailureCodes().put(key, "类型名称已存在:" + importItem.getName()); |
| | | return; |
| | | } |
| | | // 创建 |
| | | MesTmToolTypeDO createObj = new MesTmToolTypeDO(); |
| | | createObj.setId(null); |
| | | createObj.setCode(code); |
| | | createObj.setName(importItem.getName()); |
| | | createObj.setCodeFlag(ObjUtil.defaultIfNull(importItem.getCodeFlag(), Boolean.FALSE)); |
| | | createObj.setMaintenType(importItem.getMaintenType()); |
| | | createObj.setMaintenPeriod(importItem.getMaintenPeriod()); |
| | | createObj.setRemark(importItem.getRemark()); |
| | | toolTypeMapper.insert(createObj); |
| | | respVO.getCreateCodes().add(code); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | } |