| | |
| | | package cn.iocoder.yudao.module.mes.service.pd.product; |
| | | |
| | | 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.framework.dict.core.DictFrameworkUtils; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pd.product.vo.MesPdProductImportExcelVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pd.product.vo.MesPdProductImportRespVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pd.product.vo.MesPdProductPageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pd.product.vo.MesPdProductRespVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pd.product.vo.MesPdProductSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pd.product.MesPdProductDO; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.pd.product.MesPdProductMapper; |
| | | import cn.iocoder.yudao.module.mes.enums.DictTypeConstants; |
| | | import cn.iocoder.yudao.module.mes.enums.md.autocode.MesMdAutoCodeRuleCodeEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.pd.MesPdAuditStatusEnum; |
| | | 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 lombok.extern.slf4j.Slf4j; |
| | |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.time.LocalDateTime; |
| | | 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.PD_PRODUCT_AUDIT_STATUS_INVALID; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PD_PRODUCT_IMPORT_LIST_IS_EMPTY; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PD_PRODUCT_NOT_EXISTS; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PD_PRODUCT_SUBMIT_DUPLICATE; |
| | | |
| | |
| | | private void fillDictName(MesPdProductRespVO respVO) { |
| | | respVO.setArchiveTypeName(DictFrameworkUtils.parseDictDataLabel( |
| | | DictTypeConstants.MES_PD_PRODUCT_ARCHIVE_TYPE, respVO.getArchiveType())); |
| | | respVO.setAuditStatusName(DictFrameworkUtils.parseDictDataLabel( |
| | | DictTypeConstants.MES_PD_PRODUCT_AUDIT_STATUS, respVO.getAuditStatus())); |
| | | String auditStatusName = DictFrameworkUtils.parseDictDataLabel( |
| | | DictTypeConstants.MES_PD_PRODUCT_AUDIT_STATUS, respVO.getAuditStatus()); |
| | | respVO.setAuditStatusName(auditStatusName != null |
| | | ? auditStatusName : MesPdAuditStatusEnum.getNameByStatus(respVO.getAuditStatus())); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MesPdProductImportRespVO importProductList(List<MesPdProductImportExcelVO> importList, |
| | | boolean updateSupport) { |
| | | // 1. 校验非空 |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(PD_PRODUCT_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | |
| | | // 2. 逐行处理 |
| | | MesPdProductImportRespVO respVO = MesPdProductImportRespVO.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 (importItem.getArchiveType() == null) { |
| | | respVO.getFailureCodes().put(key, "档案类型不能为空"); |
| | | return; |
| | | } |
| | | if (StrUtil.isBlank(importItem.getName())) { |
| | | respVO.getFailureCodes().put(key, "档案名称不能为空"); |
| | | return; |
| | | } |
| | | // 2.1 判断:创建 or 更新(编码为空时自动生成) |
| | | if (StrUtil.isNotBlank(importItem.getCode())) { |
| | | MesPdProductDO existProduct = productMapper.selectByCode(importItem.getCode()); |
| | | if (existProduct == null) { |
| | | // 2.1.1 指定编码新增 |
| | | MesPdProductDO product = BeanUtils.toBean(importItem, MesPdProductDO.class); |
| | | product.setId(null); |
| | | product.setAuditStatus(MesPdAuditStatusEnum.DRAFT.getStatus()); |
| | | product.setStatus(ObjUtil.defaultIfNull(importItem.getStatus(), 1)); |
| | | productMapper.insert(product); |
| | | respVO.getCreateCodes().add(importItem.getCode()); |
| | | } else if (updateSupport) { |
| | | // 2.1.2 更新 |
| | | MesPdProductDO updateObj = BeanUtils.toBean(importItem, MesPdProductDO.class); |
| | | updateObj.setId(existProduct.getId()); |
| | | updateObj.setCode(existProduct.getCode()); |
| | | productMapper.updateById(updateObj); |
| | | respVO.getUpdateCodes().add(importItem.getCode()); |
| | | } else { |
| | | respVO.getFailureCodes().put(key, "档案编码已存在"); |
| | | } |
| | | } else { |
| | | // 2.1.3 编码为空:自动生成编码新增 |
| | | MesPdProductDO product = BeanUtils.toBean(importItem, MesPdProductDO.class); |
| | | product.setId(null); |
| | | product.setCode(autoCodeRecordService.generateAutoCode( |
| | | MesMdAutoCodeRuleCodeEnum.PD_PRODUCT_CODE.getCode())); |
| | | product.setAuditStatus(MesPdAuditStatusEnum.DRAFT.getStatus()); |
| | | product.setStatus(ObjUtil.defaultIfNull(importItem.getStatus(), 1)); |
| | | productMapper.insert(product); |
| | | respVO.getCreateCodes().add(product.getCode()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | } |