gongchunyi
2026-06-28 86379aae223a16fed59d46f915ade96ef987b8d2
src/main/java/com/ruoyi/aftersalesservice/service/impl/AfterSalesServiceServiceImpl.java
@@ -1,17 +1,36 @@
package com.ruoyi.aftersalesservice.service.impl;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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;
import com.ruoyi.aftersalesservice.pojo.AfterSalesService;
import com.ruoyi.aftersalesservice.service.AfterSalesServiceService;
import com.ruoyi.common.utils.OrderUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.mapper.SysDeptMapper;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.project.system.service.ISysNoticeService;
import com.ruoyi.sales.dto.SalesLedgerDto;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import com.ruoyi.sales.service.ISalesLedgerProductService;
import com.ruoyi.sales.service.ISalesLedgerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
/**
 * @author :yys
@@ -27,14 +46,154 @@
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ISalesLedgerProductService salesLedgerProductService;
    @Autowired
    private ISalesLedgerService salesLedgerService;
    @Autowired
    private ISysNoticeService sysNoticeService;
    @Override
    public IPage<AfterSalesService> listPage(Page page, AfterSalesService afterSalesService) {
    public IPage<AfterSalesServiceNewDto> listPage(Page page, AfterSalesServiceNewDto afterSalesService) {
        Long tenantId = SecurityUtils.getLoginUser().getTenantId();
        SysDept sysDept = sysDeptMapper.selectDeptById(tenantId);
        IPage<AfterSalesService> afterSalesServiceIPage = afterSalesServiceMapper.listPage(page, afterSalesService);
        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;
    }
    @Override
    public boolean addAfterSalesServiceDto(AfterSalesServiceNewDto afterSalesServiceNewDto) {
        afterSalesServiceNewDto.setStatus(1);
        if (afterSalesServiceNewDto.getProductModelIdList() != null && !afterSalesServiceNewDto.getProductModelIdList().isEmpty()) {
            String productModelIds = afterSalesServiceNewDto.getProductModelIdList().stream()
                    .map(String::valueOf)
                    .collect(Collectors.joining(","));
            afterSalesServiceNewDto.setProductModelIds(productModelIds);
        }
        if (afterSalesServiceNewDto.getAfterSalesProductList() != null && !afterSalesServiceNewDto.getAfterSalesProductList().isEmpty()) {
            afterSalesServiceNewDto.setAfterSalesProducts(JSON.toJSONString(afterSalesServiceNewDto.getAfterSalesProductList()));
        }
        SysUser sysUser = sysUserMapper.selectUserById(afterSalesServiceNewDto.getCheckUserId());
        if (sysUser == null) throw new RuntimeException("审核人不存在");
        afterSalesServiceNewDto.setCheckNickName(sysUser.getNickName());
        if (StringUtils.isEmpty(afterSalesServiceNewDto.getAfterSalesServiceNo())) {
            String string = OrderUtils.countAfterServiceTodayByCreateTime(afterSalesServiceMapper, "SH_");
            afterSalesServiceNewDto.setAfterSalesServiceNo(string);
        }
        boolean saved = this.save(afterSalesServiceNewDto);
        if (saved && afterSalesServiceNewDto.getSalesLedgerId() != null) {
            try {
                SalesLedger salesLedger = salesLedgerService.getById(afterSalesServiceNewDto.getSalesLedgerId());
                if (salesLedger != null && StringUtils.isNotEmpty(salesLedger.getEntryPerson())) {
                    Long entryUserId = Long.parseLong(salesLedger.getEntryPerson());
                    String webPath = "/customerService/afterSalesHandling?afterSalesServiceNo=" + afterSalesServiceNewDto.getAfterSalesServiceNo();
                    sysNoticeService.simpleNoticeByUser(
                            "新增售后单通知",
                            "您录入的销售订单有一条新的售后记录,售后单号:" + afterSalesServiceNewDto.getAfterSalesServiceNo(),
                            Collections.singletonList(entryUserId),
                            webPath
                    );
                }
            } catch (Exception e) {
                log.error("发送售后新增通知失败: ", e);
            }
        }
        return saved;
    }
    @Override
    public AfterSalesServiceNewDto getAfterSalesServiceNewDtoById(Long id) {
        AfterSalesService afterSalesService = afterSalesServiceMapper.selectById(id);
        SalesLedger byId = salesLedgerService.getById(afterSalesService.getSalesLedgerId());
        List<Long> collect = Arrays.stream(afterSalesService.getProductModelIds().split(",")).map(Long::valueOf).collect(Collectors.toList());
        List<SalesLedgerProduct> list = salesLedgerProductService.list(new QueryWrapper<SalesLedgerProduct>().lambda().in(SalesLedgerProduct::getId, collect));
        if (StringUtils.isNotEmpty(afterSalesService.getAfterSalesProducts())) {
            List<com.ruoyi.aftersalesservice.dto.AfterSalesProductDto> afterSalesProductList = com.alibaba.fastjson2.JSON.parseArray(afterSalesService.getAfterSalesProducts(), com.ruoyi.aftersalesservice.dto.AfterSalesProductDto.class);
            for (SalesLedgerProduct product : list) {
                for (com.ruoyi.aftersalesservice.dto.AfterSalesProductDto productDto : afterSalesProductList) {
                    if (product.getId().equals(productDto.getId())) {
                        product.setAfterSalesQuantity(productDto.getAfterSalesQuantity());
                        break;
                    }
                }
            }
        }
        AfterSalesServiceNewDto afterSalesServiceNewDto = new AfterSalesServiceNewDto();
        BeanUtils.copyProperties(afterSalesService, afterSalesServiceNewDto);
        SalesLedgerDto salesLedgerDto = new SalesLedgerDto();
        BeanUtils.copyProperties(byId, salesLedgerDto);
        salesLedgerDto.setProductData(list);
        afterSalesServiceNewDto.setSalesLedgerDto(salesLedgerDto);
        return afterSalesServiceNewDto;
    }
    @Override
    public List<CountDto> countAfterSalesService() {
        List<CountDto> stringIntegerHashMap = afterSalesServiceMapper.countAfterSalesService();
        Integer total = 0;
        for (CountDto countDto : stringIntegerHashMap) {
            total += countDto.getCount();
        }
        CountDto countDto = new CountDto();
        countDto.setStatus(3);
        countDto.setCount(total);
        stringIntegerHashMap.add(countDto);
        return stringIntegerHashMap;
    }
}