| | |
| | | |
| | | 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.mdm.controller.admin.unit.vo.MdmUnitMeasureImportExcelVO; |
| | | import cn.iocoder.yudao.module.mdm.controller.admin.unit.vo.MdmUnitMeasureImportRespVO; |
| | | import cn.iocoder.yudao.module.mdm.controller.admin.unit.vo.MdmUnitMeasurePageReqVO; |
| | | import cn.iocoder.yudao.module.mdm.controller.admin.unit.vo.MdmUnitMeasureSaveReqVO; |
| | | import cn.iocoder.yudao.module.mdm.dal.dataobject.unit.MdmUnitMeasureDO; |
| | |
| | | import cn.iocoder.yudao.module.mdm.dal.mysql.unit.MdmUnitMeasureMapper; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.util.*; |
| | |
| | | /** |
| | | * MDM 计量单位 Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void deleteUnitMeasure(Long id) { |
| | | // 校验存在 |
| | | validateUnitMeasureExists(id); |
| | | MdmUnitMeasureDO unitMeasure = validateUnitMeasureExists(id); |
| | | // 校验是否被物料使用(直接使用 Mapper 避免循环依赖) |
| | | if (itemMapper.selectCountByUnitMeasureId(id) > 0) { |
| | | throw exception(MDM_UNIT_MEASURE_EXISTS_ITEM); |
| | | } |
| | | // 先清理同编码的已软删除脏数据,避免当前记录软删时触发唯一键冲突 |
| | | unitMeasureMapper.physicalDeleteByCode(unitMeasure.getCode()); |
| | | // 删除 |
| | | unitMeasureMapper.deleteById(id); |
| | | } |
| | |
| | | return unitMeasureMapper.selectByCode(code); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MdmUnitMeasureImportRespVO importUnitMeasureList(List<MdmUnitMeasureImportExcelVO> importList, Boolean isUpdateSupport) { |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(MDM_UNIT_MEASURE_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | MdmUnitMeasureImportRespVO respVO = MdmUnitMeasureImportRespVO.builder() |
| | | .createList(new ArrayList<>()).updateList(new ArrayList<>()) |
| | | .failureList(new LinkedHashMap<>()).build(); |
| | | |
| | | // 1. 建立编码 -> ID 映射(先加载现有单位,再叠加本次新建的单位),用于解析"主单位编码"列 |
| | | Map<String, Long> codeIdMap = new HashMap<>(); |
| | | for (MdmUnitMeasureDO unit : unitMeasureMapper.selectList()) { |
| | | codeIdMap.put(unit.getCode(), unit.getId()); |
| | | } |
| | | // 记录本次导入已处理过的编码,避免同一文件内后续行找不到刚导入的主单位 |
| | | Map<String, Long> importedCodeIdMap = new HashMap<>(); |
| | | |
| | | // 2. 遍历导入 |
| | | for (MdmUnitMeasureImportExcelVO importVO : importList) { |
| | | // 2.1 基础校验 |
| | | if (StrUtil.isBlank(importVO.getCode()) || StrUtil.isBlank(importVO.getName())) { |
| | | respVO.getFailureList().put(StrUtil.blankToDefault(importVO.getName(), importVO.getCode()), "单位编码和单位名称不能为空"); |
| | | continue; |
| | | } |
| | | // 2.2 解析主单位 ID(非主单位时必填主单位编码) |
| | | Long primaryId = null; |
| | | if (StrUtil.isNotBlank(importVO.getPrimaryCode())) { |
| | | primaryId = importedCodeIdMap.get(importVO.getPrimaryCode()) != null |
| | | ? importedCodeIdMap.get(importVO.getPrimaryCode()) |
| | | : codeIdMap.get(importVO.getPrimaryCode()); |
| | | if (primaryId == null) { |
| | | respVO.getFailureList().put(importVO.getName(), "主单位编码不存在:" + importVO.getPrimaryCode()); |
| | | continue; |
| | | } |
| | | } |
| | | |
| | | // 2.3 已存在(按编码判断) |
| | | MdmUnitMeasureDO existByCode = unitMeasureMapper.selectByCode(importVO.getCode()); |
| | | if (existByCode != null) { |
| | | if (!Boolean.TRUE.equals(isUpdateSupport)) { |
| | | respVO.getFailureList().put(importVO.getName(), "单位编码已存在"); |
| | | continue; |
| | | } |
| | | // 支持更新:以编码为准更新 |
| | | MdmUnitMeasureDO updateObj = BeanUtils.toBean(importVO, MdmUnitMeasureDO.class); |
| | | updateObj.setId(existByCode.getId()); |
| | | updateObj.setPrimaryId(primaryId); |
| | | unitMeasureMapper.updateById(updateObj); |
| | | respVO.getUpdateList().add(importVO.getName()); |
| | | } else { |
| | | // 2.4 校验名称唯一(新增场景) |
| | | MdmUnitMeasureDO existByName = unitMeasureMapper.selectByName(importVO.getName()); |
| | | if (existByName != null) { |
| | | respVO.getFailureList().put(importVO.getName(), "单位名称已存在"); |
| | | continue; |
| | | } |
| | | // 2.5 新增 |
| | | MdmUnitMeasureDO newObj = BeanUtils.toBean(importVO, MdmUnitMeasureDO.class); |
| | | newObj.setPrimaryId(primaryId); |
| | | unitMeasureMapper.insert(newObj); |
| | | respVO.getCreateList().add(importVO.getName()); |
| | | // 记录本次新建的编码,供同文件后续行引用 |
| | | importedCodeIdMap.put(importVO.getCode(), newObj.getId()); |
| | | } |
| | | } |
| | | return respVO; |
| | | } |
| | | |
| | | } |