gongchunyi
3 天以前 fc24827d4eb7b15c28a184123376eb758d93a9ad
src/main/java/com/ruoyi/productionPlan/service/impl/ProductionPlanServiceImpl.java
@@ -504,28 +504,68 @@
        if (list == null || list.isEmpty()) {
            throw new ServiceException("Excel没有数据");
        }
        List<ProductionPlan> entityList = new ArrayList<>(list.size());
        ProductionPlan entity;
        for (ProductionPlanImportDto dto : list) {
            entity = new ProductionPlan();
            BeanUtils.copyProperties(dto, entity);
            entity.setAssignedQuantity(BigDecimal.ZERO);
            entity.setCreateTime(LocalDateTime.now());
            entity.setUpdateTime(LocalDateTime.now());
            entity.setDataSourceType(DataSourceTypeEnum.MANUAL.getCode());
            // 根据物料编码填充关联ID
            if (StringUtils.isNotEmpty(dto.getMaterialCode())) {
                LambdaQueryWrapper<ProductMaterialSku> skuQueryWrapper = new LambdaQueryWrapper<>();
                skuQueryWrapper.eq(ProductMaterialSku::getMaterialCode, dto.getMaterialCode());
                ProductMaterialSku sku = productMaterialSkuService.getOne(skuQueryWrapper);
                if (sku != null) {
                    entity.setProductMaterialSkuId(sku.getId());
        Set<String> applyNos = new HashSet<>();
        Set<String> materialCodes = new HashSet<>();
        for (int i = 0; i < list.size(); i++) {
            ProductionPlanImportDto dto = list.get(i);
            String applyNo = dto.getApplyNo();
            String materialCode = dto.getMaterialCode();
            if (StringUtils.isEmpty(applyNo)) {
                throw new ServiceException("导入失败:第 " + (i + 2) + " 行申请单编号不能为空");
            }
            if (!applyNos.add(applyNo)) {
                throw new ServiceException("导入失败:Excel 中存在重复的申请单编号: " + applyNo);
            }
            if (StringUtils.isEmpty(materialCode)) {
                throw new ServiceException("导入失败:第 " + (i + 2) + " 行物料编码不能为空");
            }
            String strength = dto.getStrength();
            if (StringUtils.isNotEmpty(strength)) {
                if (!"A3.5".equals(strength) && !"A5.0".equals(strength)) {
                    throw new ServiceException("导入失败:第 " + (i + 2) + " 行强度只能是 A3.5 或 A5.0");
                }
            }
            entityList.add(entity);
            materialCodes.add(materialCode);
        }
        //  申请单编号是否已存在
        Long existApplyNoCount = baseMapper.selectCount(Wrappers.<ProductionPlan>lambdaQuery()
                .in(ProductionPlan::getApplyNo, applyNos));
        if (existApplyNoCount > 0) {
            List<String> existApplyNos = baseMapper.selectList(Wrappers.<ProductionPlan>lambdaQuery()
                            .in(ProductionPlan::getApplyNo, applyNos))
                    .stream().map(ProductionPlan::getApplyNo).collect(Collectors.toList());
            throw new ServiceException("导入失败,申请单编号已存在: " + String.join(", ", existApplyNos));
        }
        Map<String, Long> skuMap = productMaterialSkuService.list(Wrappers.<ProductMaterialSku>lambdaQuery()
                        .in(ProductMaterialSku::getMaterialCode, materialCodes))
                .stream().collect(Collectors.toMap(ProductMaterialSku::getMaterialCode, ProductMaterialSku::getId, (k1, k2) -> k1));
        List<String> missingCodes = materialCodes.stream()
                .filter(code -> !skuMap.containsKey(code))
                .collect(Collectors.toList());
        if (!missingCodes.isEmpty()) {
            throw new ServiceException("导入失败,以下物料编码不存在: " + String.join(", ", missingCodes));
        }
        LocalDateTime now = LocalDateTime.now();
        List<ProductionPlan> entityList = list.stream().map(dto -> {
            ProductionPlan entity = new ProductionPlan();
            BeanUtils.copyProperties(dto, entity);
            entity.setProductMaterialSkuId(skuMap.get(dto.getMaterialCode()));
            entity.setAssignedQuantity(BigDecimal.ZERO);
            entity.setDataSourceType(DataSourceTypeEnum.MANUAL.getCode());
            entity.setStatus(0);
            entity.setCreateTime(now);
            entity.setUpdateTime(now);
            return entity;
        }).collect(Collectors.toList());
        this.saveBatch(entityList);
    }