liding
2025-05-09 dac03b5ff8efb1b733d37dce7553ed0aff32c780
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -1,11 +1,13 @@
package com.ruoyi.sales.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.mapper.CustomerMapper;
import com.ruoyi.basic.pojo.Customer;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sales.dto.SalesLedgerDto;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
@@ -19,10 +21,13 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
@@ -48,33 +53,34 @@
    private final RedisTemplate<String, String> redisTemplate;
    @Override
    public List<SalesLedger> selectSalesLedgerList(SalesLedger salesLedger) {
        return salesLedgerMapper.selectList(new LambdaQueryWrapper<>());
    public List<SalesLedger> selectSalesLedgerList(SalesLedgerDto salesLedgerDto) {
        LambdaQueryWrapper<SalesLedger> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(salesLedgerDto.getCustomerName())) {
            queryWrapper.eq(SalesLedger::getCustomerName, salesLedgerDto.getCustomerName());
        }
        return salesLedgerMapper.selectList(queryWrapper);
    }
    public List<SalesLedgerDto> getSalesLedgerWithProducts() {
        List<SalesLedger> ledgers = salesLedgerMapper.selectList(new LambdaQueryWrapper<>());
        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<>());
        Map<Long, List<SalesLedgerProduct>> productMap = products.stream()
                .collect(Collectors.groupingBy(SalesLedgerProduct::getSalesLedgerId));
        return ledgers.stream().map(ledger -> {
            SalesLedgerDto dto = new SalesLedgerDto();
            org.springframework.beans.BeanUtils.copyProperties(ledger, dto);
            List<SalesLedgerProduct> ledgerProducts = productMap.getOrDefault(ledger.getId(), Collections.emptyList());
            if (!ledgerProducts.isEmpty()) {
                dto.setHasChildren(true);
                dto.setProductData(ledgerProducts);
            }
            return dto;
        }).collect(Collectors.toList());
    public SalesLedgerDto getSalesLedgerWithProducts(SalesLedgerDto salesLedgerDto) {
        // 1. 查询主表
        SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerDto.getId());
        if (salesLedger == null) {
            throw new BaseException("台账不存在");
    }
    @Override
    public SalesLedger selectSalesLedgerById(Long id) {
        return salesLedgerMapper.selectById(id);
        // 2. 查询子表
        LambdaQueryWrapper<SalesLedgerProduct> productWrapper = new LambdaQueryWrapper<>();
        productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, salesLedger.getId());
        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper);
        // 3. 转换 DTO
        SalesLedgerDto resultDto = new SalesLedgerDto();
        BeanUtils.copyProperties(salesLedger, resultDto);
        if (!products.isEmpty()) {
            resultDto.setHasChildren(true);
            resultDto.setProductData(products);
        }
        return resultDto;
    }
    @Override
@@ -120,10 +126,19 @@
        }
        // 4. 处理子表数据
        if (salesLedgerDto.getProductData() != null && !salesLedgerDto.getProductData().isEmpty()) {
            handleSalesLedgerProducts(salesLedger.getId(), salesLedgerDto.getProductData());
        }
        List<SalesLedgerProduct> productList = salesLedgerDto.getProductData();
        if (productList != null && !productList.isEmpty()) {
            handleSalesLedgerProducts(salesLedger.getId(), productList);
            // ✅ 调用通用方法更新主表金额
            updateMainContractAmount(
                    salesLedger.getId(),
                    productList,
                    SalesLedgerProduct::getTaxInclusiveTotalPrice,
                    salesLedgerMapper,
                    SalesLedger.class
            );
        }
        return 1; // 操作成功返回1
    }
@@ -138,11 +153,15 @@
        // 执行更新操作
        if (!updateList.isEmpty()) {
            salesLedgerProductMapper.updateBatchSomeColumn(updateList);
            for (SalesLedgerProduct product : updateList) {
                salesLedgerProductMapper.updateById(product);
            }
        }
        // 执行插入操作
        if (!insertList.isEmpty()) {
            salesLedgerProductMapper.insertBatchSomeColumn(insertList);
            for (SalesLedgerProduct salesLedgerProduct : insertList) {
                salesLedgerProductMapper.insert(salesLedgerProduct);
            }
        }
    }
@@ -213,4 +232,39 @@
        }
        return next;
    }
    public <T, S> void updateMainContractAmount(
            Long mainId,
            List<T> subList,
            Function<T, BigDecimal> amountGetter,
            BaseMapper<S> mainMapper,
            Class<S> mainEntityClass) {
        if (mainId == null || subList == null || subList.isEmpty()) {
            return;
        }
        // 计算子表金额总和
        BigDecimal totalAmount = subList.stream()
                .map(amountGetter)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        // 构造主表更新对象(支持任意主表类型)
        try {
            S entity = mainEntityClass.getDeclaredConstructor().newInstance();
            Field idField = mainEntityClass.getDeclaredField("id");
            idField.setAccessible(true);
            idField.set(entity, mainId);
            // 设置 contractAmount 字段,注意这里假设字段名为 "contractAmount"
            Field amountField = mainEntityClass.getDeclaredField("contractAmount");
            amountField.setAccessible(true);
            amountField.set(entity, totalAmount);
            mainMapper.updateById(entity);
        } catch (Exception e) {
            throw new RuntimeException("动态更新主表金额失败", e);
        }
    }
}