gongchunyi
18 小时以前 b572e82dcafea0fd893d908c7bb0e048483a1dd3
src/main/java/com/ruoyi/production/service/impl/ProductMaterialSkuServiceImpl.java
@@ -11,7 +11,7 @@
import com.ruoyi.production.mapper.ProductMaterialSkuMapper;
import com.ruoyi.production.pojo.ProductMaterial;
import com.ruoyi.production.pojo.ProductMaterialSku;
import com.ruoyi.production.pojo.ProductMaterialSkuImportDto;
import com.ruoyi.production.dto.ProductMaterialSkuImportDto;
import com.ruoyi.production.service.ProductMaterialSkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -20,12 +20,7 @@
import org.springframework.web.multipart.MultipartFile;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -39,9 +34,7 @@
 */
@Slf4j
@Service
public class ProductMaterialSkuServiceImpl
        extends ServiceImpl<ProductMaterialSkuMapper, ProductMaterialSku>
        implements ProductMaterialSkuService {
public class ProductMaterialSkuServiceImpl extends ServiceImpl<ProductMaterialSkuMapper, ProductMaterialSku> implements ProductMaterialSkuService {
    @Autowired
    private ProductMaterialMapper productMaterialMapper;
@@ -50,43 +43,8 @@
     * 查询物料规格列表
     */
    @Override
    public Page<ProductMaterialSkuDto> productMaterialSkuList(Page<ProductMaterialSku> page, ProductMaterialSkuDto dto) {
        LambdaQueryWrapper<ProductMaterialSku> queryWrapper = new LambdaQueryWrapper<>();
        if (dto != null && dto.getMaterialId() != null) {
            queryWrapper.eq(ProductMaterialSku::getMaterialId, dto.getMaterialId())
                    .like(StringUtils.isNotBlank(dto.getSpecification()),
                            ProductMaterialSku::getSpecification, dto.getSpecification())
                    .like(StringUtils.isNotBlank(dto.getMaterialCode()),
                            ProductMaterialSku::getMaterialCode, dto.getMaterialCode())
                    .orderByAsc(ProductMaterialSku::getId);
        }
        Page<ProductMaterialSku> skuPage = this.page(page, queryWrapper);
        List<ProductMaterialSku> skuList = skuPage.getRecords();
        if (skuList == null || skuList.isEmpty()) {
            return new Page<>();
        }
        ProductMaterial material = productMaterialMapper.selectById(dto.getMaterialId());
        String materialName = material != null ? material.getMaterialName() : null;
        String baseUnit = material != null ? material.getBaseUnit() : null;
        List<ProductMaterialSkuDto> result = new ArrayList<>(skuList.size());
        for (ProductMaterialSku sku : skuList) {
            ProductMaterialSkuDto productMaterialSkuDto = new ProductMaterialSkuDto();
            productMaterialSkuDto.setMaterialId(dto.getMaterialId());
            productMaterialSkuDto.setMaterialName(materialName);
            productMaterialSkuDto.setMaterialCode(sku.getMaterialCode());
            productMaterialSkuDto.setBaseUnit(baseUnit);
            productMaterialSkuDto.setSkuId(sku.getId());
            productMaterialSkuDto.setSpecification(sku.getSpecification());
            productMaterialSkuDto.setSupplyType(sku.getSupplyType());
            result.add(productMaterialSkuDto);
        }
        Page<ProductMaterialSkuDto> dtoPage = new Page<>();
        dtoPage.setCurrent(skuPage.getCurrent());
        dtoPage.setSize(skuPage.getSize());
        dtoPage.setTotal(skuPage.getTotal());
        dtoPage.setRecords(result);
        return dtoPage;
    public Page<ProductMaterialSkuDto> productMaterialSkuList(Page<ProductMaterialSkuDto> page, ProductMaterialSkuDto dto, Integer type) {
        return baseMapper.selectSkuWithMaterialPage(page, dto, type);
    }
    /**
@@ -96,12 +54,12 @@
    public void addProductMaterialSku(ProductMaterialSku sku) {
        validateProductMaterialSku(sku, false);
        // 校验物料是否存在
        ProductMaterial material = productMaterialMapper.selectById(sku.getMaterialId());
        ProductMaterial material = productMaterialMapper.selectById(sku.getProductId());
        if (material == null) {
            throw new ServiceException("物料不存在");
        }
        // 校验规格是否重复
        if (existsSameSpecification(sku.getMaterialId(), sku.getSpecification(), null)) {
        if (existsSameSpecification(sku.getProductId(), sku.getModel(), null)) {
            throw new ServiceException("该物料已存在相同规格");
        }
        LocalDateTime now = LocalDateTime.now();
@@ -113,7 +71,7 @@
        if (!this.save(sku)) {
            throw new ServiceException("新增物料规格失败");
        }
        log.info("新增物料规格成功 materialId={}, specification={}", sku.getMaterialId(), sku.getSpecification());
        log.info("新增物料规格成功 materialId={}, specification={}", sku.getProductId(), sku.getModel());
    }
    /**
@@ -123,7 +81,7 @@
    public void updateProductMaterialSku(ProductMaterialSku sku) {
        validateProductMaterialSku(sku, true);
        // 校验规格是否重复
        if (existsSameSpecification(sku.getMaterialId(), sku.getSpecification(), sku.getId())) {
        if (existsSameSpecification(sku.getProductId(), sku.getModel(), sku.getId())) {
            throw new ServiceException("该物料已存在相同规格");
        }
        sku.setUpdateTime(LocalDateTime.now());
@@ -157,10 +115,10 @@
        if (requireId && sku.getId() == null) {
            throw new ServiceException("主键ID不能为空");
        }
        if (sku.getMaterialId() == null) {
        if (sku.getProductId() == null) {
            throw new ServiceException("物料ID不能为空");
        }
        if (StringUtils.isEmpty(sku.getSpecification())) {
        if (StringUtils.isEmpty(sku.getModel())) {
            throw new ServiceException("规格不能为空");
        }
    }
@@ -170,8 +128,8 @@
     */
    private boolean existsSameSpecification(Long materialId, String specification, Long excludeId) {
        LambdaQueryWrapper<ProductMaterialSku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductMaterialSku::getMaterialId, materialId)
                .eq(ProductMaterialSku::getSpecification, specification);
        queryWrapper.eq(ProductMaterialSku::getProductId, materialId)
                .eq(ProductMaterialSku::getModel, specification);
        if (excludeId != null) {
            queryWrapper.ne(ProductMaterialSku::getId, excludeId);
        }
@@ -191,7 +149,7 @@
        ProductMaterial material = productMaterialMapper.selectById(materialId);
        if (material == null) {
            throw new ServiceException("物料不存在");
            throw new ServiceException("主物料信息不存在");
        }
        ExcelUtil<ProductMaterialSkuImportDto> excelUtil = new ExcelUtil<>(ProductMaterialSkuImportDto.class);
@@ -202,70 +160,81 @@
            log.error("导入物料规格Excel解析失败", e);
            throw new ServiceException("Excel解析失败");
        }
        if (importList == null || importList.isEmpty()) {
            throw new ServiceException("Excel没有数据");
            throw new ServiceException("Excel中未检测到有效数据");
        }
        List<Integer> emptyCodeRows = new ArrayList<>();
        int currentRow = 1;
        Map<String, ProductMaterialSkuImportDto> specMap = new LinkedHashMap<>();
        for (ProductMaterialSkuImportDto dto : importList) {
            if (dto == null || StringUtils.isEmpty(dto.getSpecification())) {
            currentRow++;
            if (dto == null) continue;
            //  物料编码不能为空
            if (StringUtils.isEmpty(dto.getMaterialCode())) {
                emptyCodeRows.add(currentRow);
                continue;
            }
            String specification = dto.getSpecification().trim();
            if (specification.isEmpty()) {
            if (StringUtils.isEmpty(dto.getModel())) {
                continue;
            }
            specMap.putIfAbsent(specification, dto);
            String modelKey = dto.getModel().trim();
            specMap.putIfAbsent(modelKey, dto);
        }
        if (!emptyCodeRows.isEmpty()) {
            throw new ServiceException("导入失败,以下行号的【物料编码】为空:" + emptyCodeRows.toString());
        }
        if (specMap.isEmpty()) {
            throw new ServiceException("Excel没有有效的规格数据");
            throw new ServiceException("Excel没有有效的规格型号数据");
        }
        Set<String> specifications = specMap.keySet();
        Set<String> modelSet = specMap.keySet();
        List<ProductMaterialSku> existList = this.list(new LambdaQueryWrapper<ProductMaterialSku>()
                .eq(ProductMaterialSku::getMaterialId, materialId)
                .in(ProductMaterialSku::getSpecification, specifications));
        Map<String, ProductMaterialSku> existMap = existList.stream()
                .collect(Collectors.toMap(ProductMaterialSku::getSpecification, sku -> sku, (a, b) -> a));
                .eq(ProductMaterialSku::getProductId, materialId)
                .in(ProductMaterialSku::getModel, modelSet));
        Map<String, ProductMaterialSku> existMap = existList.stream().collect(Collectors.toMap(ProductMaterialSku::getModel, sku -> sku, (a, b) -> a));
        LocalDateTime now = LocalDateTime.now();
        List<ProductMaterialSku> saveList = new ArrayList<>();
        List<ProductMaterialSku> updateList = new ArrayList<>();
        for (Map.Entry<String, ProductMaterialSkuImportDto> entry : specMap.entrySet()) {
            String specification = entry.getKey();
            String model = entry.getKey();
            ProductMaterialSkuImportDto dto = entry.getValue();
            String supplyType = StringUtils.isNotEmpty(dto.getSupplyType()) ? dto.getSupplyType().trim() : null;
            ProductMaterialSku exist = existMap.get(specification);
            if (exist == null) {
                ProductMaterialSku sku = new ProductMaterialSku();
                sku.setMaterialId(materialId);
                sku.setSpecification(specification);
                sku.setSupplyType(supplyType);
                sku.setCreateTime(now);
                sku.setUpdateTime(now);
                saveList.add(sku);
            String excelMaterialCode = dto.getMaterialCode().trim();
            String excelSupplyType = StringUtils.isNotEmpty(dto.getSupplyType()) ? dto.getSupplyType().trim() : null;
            ProductMaterialSku existSku = existMap.get(model);
            if (existSku == null) {
                ProductMaterialSku newSku = new ProductMaterialSku();
                newSku.setProductId(materialId);
                newSku.setModel(model);
                newSku.setMaterialCode(excelMaterialCode);
                newSku.setSupplyType(excelSupplyType);
                newSku.setCreateTime(now);
                newSku.setUpdateTime(now);
                saveList.add(newSku);
            } else {
                boolean needUpdate = false;
                if (supplyType != null && !supplyType.equals(exist.getSupplyType())) {
                    exist.setSupplyType(supplyType);
                if (!Objects.equals(excelMaterialCode, existSku.getMaterialCode())) {
                    existSku.setMaterialCode(excelMaterialCode);
                    needUpdate = true;
                }
                if (!Objects.equals(excelSupplyType, existSku.getSupplyType())) {
                    existSku.setSupplyType(excelSupplyType);
                    needUpdate = true;
                }
                if (needUpdate) {
                    exist.setUpdateTime(now);
                    updateList.add(exist);
                    existSku.setUpdateTime(now);
                    updateList.add(existSku);
                }
            }
        }
        if (saveList.isEmpty() && updateList.isEmpty()) {
            throw new ServiceException("Excel与现有数据一致,无需导入");
            throw new ServiceException("Excel内容与现有数据完全一致");
        }
        if (!saveList.isEmpty()) {
            this.saveBatch(saveList);
        }
@@ -273,6 +242,6 @@
            this.updateBatchById(updateList);
        }
        log.info("物料规格导入完成 materialId={}, 新增{}条,更新{}条", materialId, saveList.size(), updateList.size());
        log.info("物料规格导入完成! materialId={}, 新增{}条,更新{}条", materialId, saveList.size(), updateList.size());
    }
}