liding
9 天以前 94ec4e9e9214cb5dc28aef4a7f38be462cfda728
src/main/java/com/ruoyi/basic/service/impl/ProductModelServiceImpl.java
@@ -3,6 +3,7 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.dto.ProductDto;
@@ -12,6 +13,7 @@
import com.ruoyi.basic.pojo.Product;
import com.ruoyi.basic.pojo.ProductModel;
import com.ruoyi.basic.service.IProductModelService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
@@ -23,9 +25,7 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -103,13 +103,24 @@
            ExcelUtil<ProductModel> productModelExcelUtil = new ExcelUtil<>(ProductModel.class);
            List<ProductModel> productModelList = productModelExcelUtil.importExcel(file.getInputStream());
            if (productModelList == null || productModelList.isEmpty()) {
            if (CollectionUtils.isEmpty(productModelList)) {
                return AjaxResult.error("导入数据不能为空");
            }
            //  获取当前产品下所有的规格型号名
            List<ProductModel> existingModels = list(new LambdaQueryWrapper<ProductModel>().eq(ProductModel::getProductId, productId));
            Set<String> existingModelNames = existingModels.stream().map(ProductModel::getModel).collect(Collectors.toSet());
            List<ProductModel> waitToSaveList = new ArrayList<>();
            int skipCount = 0;
            for (int i = 0; i < productModelList.size(); i++) {
                ProductModel item = productModelList.get(i);
                int rowNum = i + 2;
                if (StringUtils.isEmpty(item.getMaterialCode())) {
                    return AjaxResult.error("第 " + rowNum + " 行导入失败: [物料编号] 不能为空");
                }
                if (StringUtils.isEmpty(item.getModel())) {
                    return AjaxResult.error("第 " + rowNum + " 行导入失败: [规格型号] 不能为空");
@@ -117,14 +128,87 @@
                if (StringUtils.isEmpty(item.getUnit())) {
                    return AjaxResult.error("第 " + rowNum + " 行导入失败: [单位] 不能为空");
                }
                //  去重,如果已包含该型号,则跳过
                if (existingModelNames.contains(item.getModel())) {
                    skipCount++;
                    continue;
                }
                item.setProductId(product.getId());
                waitToSaveList.add(item);
                existingModelNames.add(item.getModel());
            }
            saveOrUpdateBatch(productModelList);
            return AjaxResult.success("成功导入 " + productModelList.size() + " 条数据");
            if (!waitToSaveList.isEmpty()) {
                saveBatch(waitToSaveList);
            }
            if (skipCount == 0) {
                return AjaxResult.success(String.format("成功导入 %d 条数据", waitToSaveList.size()));
            } else {
                return AjaxResult.success(String.format("成功导入 %d 条,跳过已存在数据 %d 条", waitToSaveList.size(), skipCount));
            }
        } catch (Exception e) {
            log.error("导入产品规格异常", e);
            return AjaxResult.error("导入失败");
            throw new ServiceException("导入失败");
        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int downCopy(ProductModelDto productModelDto) {
        if (productModelDto == null || productModelDto.getProductId() == null || productModelDto.getTargetProductId() == null) {
            throw new ServiceException("源产品ID和目标产品ID不能为空");
        }
        Long sourceProductId = productModelDto.getProductId();
        Long targetProductId = productModelDto.getTargetProductId();
        List<Long> modelListId = productModelDto.getModelListId();
        if (sourceProductId.equals(targetProductId)) {
            throw new ServiceException("源产品和目标产品不能相同");
        }
        Product sourceProduct = productMapper.selectById(sourceProductId);
        if (sourceProduct == null) {
            throw new ServiceException("源产品不存在");
        }
        Product targetProduct = productMapper.selectById(targetProductId);
        if (targetProduct == null) {
            throw new ServiceException("目标产品不存在");
        }
        LambdaQueryWrapper<ProductModel> queryWrapper = new LambdaQueryWrapper<ProductModel>()
                .eq(ProductModel::getProductId, sourceProductId)
                .orderByAsc(ProductModel::getId);
        Set<Long> selectedModelIds = null;
        if (CollectionUtils.isNotEmpty(modelListId)) {
            selectedModelIds = new LinkedHashSet<>(modelListId);
            queryWrapper.in(ProductModel::getId, selectedModelIds);
        }
        List<ProductModel> sourceModels = productModelMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(sourceModels)) {
            throw new ServiceException("源产品下没有可复制的型号");
        }
        if (selectedModelIds != null && sourceModels.size() != selectedModelIds.size()) {
            throw new ServiceException("部分型号不存在或不属于源产品");
        }
        List<ProductModel> copyList = new ArrayList<>();
        for (ProductModel sourceModel : sourceModels) {
            ProductModel copy = new ProductModel();
            BeanUtils.copyProperties(sourceModel, copy);
            copy.setId(null);
            copy.setProductId(targetProductId);
            copy.setTenantId(null);
            copyList.add(copy);
        }
        saveBatch(copyList);
        return copyList.size();
    }
}