gongchunyi
2026-06-25 83ab4c8eabb1d2f448079c03bbb2efa09d9d468a
src/main/java/com/ruoyi/aftersalesservice/service/impl/AfterSalesServiceServiceImpl.java
@@ -5,6 +5,7 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.aftersalesservice.dto.AfterSalesProductDto;
import com.ruoyi.aftersalesservice.dto.AfterSalesServiceNewDto;
import com.ruoyi.aftersalesservice.dto.CountDto;
import com.ruoyi.aftersalesservice.mapper.AfterSalesServiceMapper;
@@ -28,9 +29,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -61,8 +60,54 @@
        Long tenantId = SecurityUtils.getLoginUser().getTenantId();
        SysDept sysDept = sysDeptMapper.selectDeptById(tenantId);
        IPage<AfterSalesServiceNewDto> afterSalesServiceIPage = afterSalesServiceMapper.listPage(page, afterSalesService);
        List<Long> allSalesLedgerIds = afterSalesServiceIPage.getRecords().stream()
                .map(AfterSalesServiceNewDto::getSalesLedgerId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        Map<Long, List<SalesLedgerProduct>> salesLedgerProductsMap;
        if (!allSalesLedgerIds.isEmpty()) {
            List<SalesLedgerProduct> allProducts = salesLedgerProductService.list(
                    new QueryWrapper<SalesLedgerProduct>().lambda()
                            .in(SalesLedgerProduct::getSalesLedgerId, allSalesLedgerIds)
            );
            salesLedgerProductsMap = allProducts.stream()
                    .collect(Collectors.groupingBy(SalesLedgerProduct::getSalesLedgerId));
        } else {
            salesLedgerProductsMap = new HashMap<>();
        }
        afterSalesServiceIPage.getRecords().forEach(item -> {
            item.setDeptName(sysDept.getDeptName());
            try {
                if (org.apache.commons.lang3.StringUtils.isNotEmpty(item.getAfterSalesProducts())) {
                    List<AfterSalesProductDto> afterSalesProductList = JSON.parseArray(item.getAfterSalesProducts(), AfterSalesProductDto.class);
                    List<Long> selectedIds = afterSalesProductList.stream().map(AfterSalesProductDto::getId).collect(Collectors.toList());
                    if (!selectedIds.isEmpty()) {
                        List<SalesLedgerProduct> ledgerProducts = salesLedgerProductsMap.getOrDefault(item.getSalesLedgerId(), new java.util.ArrayList<>());
                        List<SalesLedgerProduct> matchedProducts = ledgerProducts.stream()
                                .filter(p -> selectedIds.contains(p.getId()))
                                .collect(Collectors.toList());
                        matchedProducts.forEach(p -> {
                            afterSalesProductList.stream()
                                    .filter(ap -> ap.getId().equals(p.getId()))
                                    .findFirst()
                                    .ifPresent(ap -> p.setAfterSalesQuantity(ap.getAfterSalesQuantity()));
                        });
                        SalesLedgerDto salesLedgerDto = new SalesLedgerDto();
                        salesLedgerDto.setProductData(matchedProducts);
                        item.setSalesLedgerDto(salesLedgerDto);
                    }
                }
            } catch (Exception e) {
                log.error("解析售后产品失败", e);
            }
        });
        return afterSalesServiceIPage;
    }