| | |
| | | package cn.iocoder.yudao.module.mes.service.tm.tool; |
| | | |
| | | 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.tm.tool.vo.MesTmToolImportExcelVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.tm.tool.vo.MesTmToolImportRespVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.tm.tool.vo.MesTmToolPageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.tm.tool.vo.MesTmToolSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.tm.tool.MesTmToolDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.tm.tool.MesTmToolTypeDO; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.tm.tool.MesTmToolMapper; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.tm.tool.MesTmToolTypeMapper; |
| | | import cn.iocoder.yudao.module.mes.enums.tm.MesTmMaintenTypeEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.tm.MesTmToolStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.BarcodeBizTypeEnum; |
| | | import cn.iocoder.yudao.module.mes.service.wm.barcode.MesWmBarcodeService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.batch.MesWmBatchService; |
| | | 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.Collections; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | 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 MesTmToolMapper toolMapper; |
| | | |
| | | @Resource |
| | | private MesTmToolTypeMapper toolTypeMapper; |
| | | |
| | | @Resource |
| | | @Lazy // 延迟加载,避免循环依赖 |
| | |
| | | return toolMapper.selectByIds(ids); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MesTmToolImportRespVO importToolList(List<MesTmToolImportExcelVO> importList, |
| | | boolean updateSupport) { |
| | | // 1. 校验非空 |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(TM_TOOL_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | |
| | | // 2. 逐行处理 |
| | | MesTmToolImportRespVO respVO = MesTmToolImportRespVO.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.getCode(), "第 " + currentIndex + " 行"); |
| | | if (StrUtil.isBlank(importItem.getCode())) { |
| | | respVO.getFailureCodes().put(key, "工具编码不能为空"); |
| | | return; |
| | | } |
| | | if (StrUtil.isBlank(importItem.getName())) { |
| | | respVO.getFailureCodes().put(key, "工具名称不能为空"); |
| | | return; |
| | | } |
| | | if (importItem.getQuantity() == null) { |
| | | respVO.getFailureCodes().put(key, "数量不能为空"); |
| | | return; |
| | | } |
| | | // 解析工具类型(按名称) |
| | | Long toolTypeId = null; |
| | | if (StrUtil.isNotBlank(importItem.getToolTypeName())) { |
| | | MesTmToolTypeDO toolType = toolTypeMapper.selectByName(importItem.getToolTypeName()); |
| | | if (toolType == null) { |
| | | respVO.getFailureCodes().put(key, "工具类型不存在:" + importItem.getToolTypeName()); |
| | | return; |
| | | } |
| | | toolTypeId = toolType.getId(); |
| | | } |
| | | // 2.1 判断:创建 or 更新 |
| | | MesTmToolDO existTool = toolMapper.selectByCode(importItem.getCode()); |
| | | if (existTool == null) { |
| | | // 2.1.1 新增 |
| | | MesTmToolDO tool = BeanUtils.toBean(importItem, MesTmToolDO.class); |
| | | tool.setId(null); |
| | | tool.setToolTypeId(toolTypeId); |
| | | tool.setStatus(ObjUtil.defaultIfNull(importItem.getStatus(), MesTmToolStatusEnum.STORE.getStatus())); |
| | | tool.setAvailableQuantity(ObjUtil.defaultIfNull(importItem.getAvailableQuantity(), importItem.getQuantity())); |
| | | toolMapper.insert(tool); |
| | | respVO.getCreateCodes().add(importItem.getCode()); |
| | | } else if (updateSupport) { |
| | | // 2.1.2 更新(保养类型互斥清理,与手动更新保持一致) |
| | | MesTmToolDO updateObj = BeanUtils.toBean(importItem, MesTmToolDO.class); |
| | | updateObj.setId(existTool.getId()); |
| | | updateObj.setCode(existTool.getCode()); |
| | | updateObj.setToolTypeId(toolTypeId); |
| | | if (Objects.equals(updateObj.getMaintenType(), MesTmMaintenTypeEnum.REGULAR.getType())) { |
| | | updateObj.setNextMaintenPeriod(null); |
| | | } else if (Objects.equals(updateObj.getMaintenType(), MesTmMaintenTypeEnum.USAGE.getType())) { |
| | | updateObj.setNextMaintenDate(null); |
| | | } |
| | | toolMapper.updateById(updateObj); |
| | | respVO.getUpdateCodes().add(importItem.getCode()); |
| | | } else { |
| | | respVO.getFailureCodes().put(key, "工具编码已存在"); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | } |