Prechádzať zdrojové kódy

fix(sparePart): 修复备件导入功能中的空值处理和数据匹配问题

- 添加库位名称空值继承逻辑,当前行库位列为空时继承上一行的库位值
- 修复备件类型编号比较时的空指针异常,增加null值检查
- 完善备件更新导入的数据匹配逻辑,支持通过ID或编码no进行匹配
- 优化异常处理机制,添加详细的错误日志记录和统一的业务异常抛出
- 增强数据导入的健壮性,处理ID和编码都为空的边界情况
dazhaxie 3 dní pred
rodič
commit
093d59f65f

BIN
platform-rest/src/main/resources/templates/备件导入模板.xlsx


BIN
platform-rest/src/main/resources/templates/备件类型导入模板.xlsx


+ 52 - 12
platform-service/src/main/java/com/platform/service/sqarepartmanage/impl/SparePartInfoServiceImpl.java

@@ -1185,8 +1185,16 @@ public class SparePartInfoServiceImpl extends BaseServiceImpl<SparePartInfoMappe
                 List<FirmProducer> producerList = firmProducerMapper.selectAll();
                 List<SparePartInfo> oldList = sparePartInfoMapper.selectAll();
                 int lineNum = 1;
+                String lastStoreName = null; // 上一行的库位名称,用于空值继承
                 // 检测是否符合条件
                 for (SparePartInfoVO item : items) {
+                    // 库位列为空时,继承上一行的库位
+                    if (StringUtils.isBlank(item.getStoreName()) && StringUtils.isNotBlank(lastStoreName)) {
+                        item.setStoreName(lastStoreName);
+                    }
+                    if (StringUtils.isNotBlank(item.getStoreName())) {
+                        lastStoreName = item.getStoreName();
+                    }
                     SparePartInfo sparePartInfo = BeanConverterUtil.copyObjectProperties(item, SparePartInfo.class);
                     // 是否存在
                     if (StringUtils.isBlank(item.getNo())) {
@@ -1229,7 +1237,7 @@ public class SparePartInfoServiceImpl extends BaseServiceImpl<SparePartInfoMappe
                     if (StringUtils.isNotBlank(item.getTypeId())) {
                         boolean isExist = false;
                         for (SpareType spareType : spareTypeList) {
-                            if (spareType.getNo().equals(item.getTypeId().trim())) {
+                            if (spareType.getNo() != null && spareType.getNo().equals(item.getTypeId().trim())) {
                                 isExist = true;
                                 sparePartInfo.setTypeId(spareType.getId());
                                 break;
@@ -1279,8 +1287,11 @@ public class SparePartInfoServiceImpl extends BaseServiceImpl<SparePartInfoMappe
                 }
             }
             return "总计新增导入:" + (addItemsReal.size());
+        } catch (BusinessException e) {
+            throw e;
         } catch (Exception e) {
-            throw new BusinessException(e.getMessage());
+            log.error("备件新增导入异常, 异常类型={}, 消息={}", e.getClass().getName(), e.getMessage(), e);
+            throw new BusinessException("导入失败,请检查导入的数据是否正确");
         }
     }
 
@@ -1342,17 +1353,24 @@ public class SparePartInfoServiceImpl extends BaseServiceImpl<SparePartInfoMappe
                 List<Store> storeList = storeMapper.selectAll();
                 List<SpareType> spareTypeList = spareTypeMapper.selectAll(); // 备件类别
                 List<FirmProducer> producerList = firmProducerMapper.selectAll();
+                String lastStoreName = null; // 上一行的库位名称,用于空值继承
                 for (SparePartInfoVO item : items) {
-                    // id不可为空
-                    SparePartInfo updInfo = BeanConverterUtil.copyObjectProperties(item, SparePartInfo.class);
-                    if (StringUtils.isBlank(updInfo.getId())) {
-                        throw new DeniedException("第" + addNum + "行的主键不为空");
+                    // 库位列为空时,继承上一行的库位
+                    if (StringUtils.isBlank(item.getStoreName()) && StringUtils.isNotBlank(lastStoreName)) {
+                        item.setStoreName(lastStoreName);
+                    }
+                    if (StringUtils.isNotBlank(item.getStoreName())) {
+                        lastStoreName = item.getStoreName();
                     }
-                    // 检查ID对应的记录是否存在,不存在则通过备件编码no查找,仍找不到则新增
+                    SparePartInfo updInfo = BeanConverterUtil.copyObjectProperties(item, SparePartInfo.class);
+                    // 通过ID或编码no匹配已有记录,都找不到则新增
                     boolean isNew = false;
-                    SparePartInfo existingById = mapper.selectByPrimaryKey(updInfo.getId());
-                    if (existingById == null) {
-                        if (StringUtils.isNotBlank(updInfo.getNo())) {
+                    if (StringUtils.isNotBlank(updInfo.getId())) {
+                        SparePartInfo existingById = mapper.selectByPrimaryKey(updInfo.getId());
+                        if (existingById != null) {
+                            // ID匹配成功,走更新逻辑
+                        } else if (StringUtils.isNotBlank(updInfo.getNo())) {
+                            // ID对应的记录不存在,尝试通过编码no查找
                             SparePartInfo queryNo = new SparePartInfo();
                             queryNo.setNo(updInfo.getNo());
                             SparePartInfo existingByNo = mapper.selectOne(queryNo);
@@ -1370,6 +1388,25 @@ public class SparePartInfoServiceImpl extends BaseServiceImpl<SparePartInfoMappe
                             updInfo.setCreatedTime(now);
                             updInfo.setCreatedUserId(SecurityUtils.getUserInfo().getUserId());
                         }
+                    } else if (StringUtils.isNotBlank(updInfo.getNo())) {
+                        // ID为空,通过编码no查找
+                        SparePartInfo queryNo = new SparePartInfo();
+                        queryNo.setNo(updInfo.getNo());
+                        SparePartInfo existingByNo = mapper.selectOne(queryNo);
+                        if (existingByNo != null) {
+                            updInfo.setId(existingByNo.getId());
+                        } else {
+                            isNew = true;
+                            updInfo.setId(IdGeneratorUtils.getObjectId());
+                            updInfo.setCreatedTime(now);
+                            updInfo.setCreatedUserId(SecurityUtils.getUserInfo().getUserId());
+                        }
+                    } else {
+                        // ID和编码都为空,无法匹配,作为新增处理
+                        isNew = true;
+                        updInfo.setId(IdGeneratorUtils.getObjectId());
+                        updInfo.setCreatedTime(now);
+                        updInfo.setCreatedUserId(SecurityUtils.getUserInfo().getUserId());
                     }
                     if (!isNew) {
                         // 先删除当前备件的所有bom,再新增excel导入的bom
@@ -1413,7 +1450,7 @@ public class SparePartInfoServiceImpl extends BaseServiceImpl<SparePartInfoMappe
                     if (StringUtils.isNotBlank(item.getTypeId())) {
                         boolean isExist = false;
                         for (SpareType spareType : spareTypeList) {
-                            if (spareType.getNo().equals(item.getTypeId().trim())) {
+                            if (spareType.getNo() != null && spareType.getNo().equals(item.getTypeId().trim())) {
                                 isExist = true;
                                 updInfo.setTypeId(spareType.getId());
                                 break;
@@ -1469,8 +1506,11 @@ public class SparePartInfoServiceImpl extends BaseServiceImpl<SparePartInfoMappe
                 }
             }
             return "总计修改导入:" + (updItems.size() + insertItems.size()) + "条,更新" + updItems.size() + "条,新增" + insertItems.size() + "条";
+        } catch (BusinessException e) {
+            throw e;
         } catch (Exception e) {
-            throw new BusinessException(e.getMessage());
+            log.error("备件修改导入异常, 异常类型={}, 消息={}", e.getClass().getName(), e.getMessage(), e);
+            throw new BusinessException("导入失败,请检查导入的数据是否正确");
         }
         /*
          * int addNum = 0;