liding
2025-05-09 dac03b5ff8efb1b733d37dce7553ed0aff32c780
1.销售台账合同金额(所有产品含税总价)
已修改5个文件
已删除1个文件
已添加1个文件
267 ■■■■ 文件已修改
src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java 25 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/sales/dto/SalesLedgerProductDTO.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/sales/dto/SalesLedgerProductDto.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/sales/pojo/SalesLedgerProduct.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/sales/service/ISalesLedgerService.java 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java 116 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java 110 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java
@@ -11,7 +11,6 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -38,10 +37,10 @@
     * æŸ¥è¯¢é”€å”®å°è´¦åˆ—表
     */
    @GetMapping("/list")
    public TableDataInfo list(SalesLedger salesLedger)
    public TableDataInfo list(SalesLedgerDto salesLedgerDto)
    {
        startPage();
        List<SalesLedger> list = salesLedgerService.selectSalesLedgerList(salesLedger);
        List<SalesLedger> list = salesLedgerService.selectSalesLedgerList(salesLedgerDto);
        return getDataTable(list);
    }
@@ -49,11 +48,8 @@
     * æŸ¥è¯¢é”€å”®å°è´¦å’Œäº§å“çˆ¶å­åˆ—表
     */
    @GetMapping("/getSalesLedgerWithProducts")
    public TableDataInfo getSalesLedgerWithProducts()
    {
        startPage();
        List<SalesLedgerDto> list = salesLedgerService.getSalesLedgerWithProducts();
        return getDataTable(list);
    public SalesLedgerDto getSalesLedgerWithProducts(SalesLedgerDto salesLedgerDto){
        return  salesLedgerService.getSalesLedgerWithProducts(salesLedgerDto);
    }
    /**
@@ -61,20 +57,11 @@
     */
    @Log(title = "销售台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SalesLedger salesLedger)
    public void export(HttpServletResponse response, SalesLedgerDto salesLedgerDto)
    {
        List<SalesLedger> list = salesLedgerService.selectSalesLedgerList(salesLedger);
        List<SalesLedger> list = salesLedgerService.selectSalesLedgerList(salesLedgerDto);
        ExcelUtil<SalesLedger> util = new ExcelUtil<SalesLedger>(SalesLedger.class);
        util.exportExcel(response, list, "销售台账数据");
    }
    /**
     * èŽ·å–é”€å”®å°è´¦è¯¦ç»†ä¿¡æ¯
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(salesLedgerService.selectSalesLedgerById(id));
    }
    /**
src/main/java/com/ruoyi/sales/dto/SalesLedgerProductDTO.java
ÎļþÒÑɾ³ý
src/main/java/com/ruoyi/sales/dto/SalesLedgerProductDto.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,4 @@
package com.ruoyi.sales.dto;
public class SalesLedgerProductDto {
}
src/main/java/com/ruoyi/sales/pojo/SalesLedgerProduct.java
@@ -59,7 +59,7 @@
     * ç¨Žçއ
     */
    @Excel(name = "税率")
    private BigDecimal taxRate;
    private String taxRate;
    /**
     * å«ç¨Žå•ä»·
src/main/java/com/ruoyi/sales/service/ISalesLedgerService.java
@@ -14,13 +14,11 @@
 */
public interface ISalesLedgerService extends IService<SalesLedger> {
    List<SalesLedger> selectSalesLedgerList(SalesLedger salesLedger);
    SalesLedger selectSalesLedgerById(Long id);
    List<SalesLedger> selectSalesLedgerList(SalesLedgerDto salesLedgerDto);
    int deleteSalesLedgerByIds(Long[] ids);
    int addOrUpdateSalesLedger(SalesLedgerDto salesLedgerDto);
    List<SalesLedgerDto> getSalesLedgerWithProducts();
    SalesLedgerDto getSalesLedgerWithProducts(SalesLedgerDto salesLedgerDto);
}
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
@@ -1,15 +1,25 @@
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.extension.service.impl.ServiceImpl;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import com.ruoyi.sales.service.ISalesLedgerProductService;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
 * äº§å“ä¿¡æ¯Service业务层处理
@@ -18,9 +28,12 @@
 * @date 2025-05-08
 */
@Service
@AllArgsConstructor
public class SalesLedgerProductServiceImpl extends ServiceImpl<SalesLedgerProductMapper, SalesLedgerProduct> implements ISalesLedgerProductService {
    @Autowired
    private SalesLedgerProductMapper salesLedgerProductMapper;
    private SalesLedgerMapper salesLedgerMapper;
    @Override
    public SalesLedgerProduct selectSalesLedgerProductById(Long id) {
@@ -35,16 +48,109 @@
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSalesLedgerProductByIds(Long[] ids) {
        return salesLedgerProductMapper.deleteBatchIds(Arrays.asList(ids));
        if (ids == null || ids.length == 0) {
            return 0;
        }
        // 1. å…ˆæŸ¥è¯¢è¦åˆ é™¤çš„子表记录,获取对应的 salesLedgerId
        List<SalesLedgerProduct> deletedProducts = salesLedgerProductMapper.selectBatchIds(Arrays.asList(ids));
        if (deletedProducts.isEmpty()) {
            return 0; // æ²¡æœ‰å¯åˆ é™¤çš„æ•°æ®
        }
        // å¯èƒ½å±žäºŽå¤šä¸ªä¸»è¡¨ï¼ˆä½†é€šå¸¸ä¸€ä¸ªæŽ¥å£åªå¤„理一个主表)
        Set<Long> mainIds = deletedProducts.stream()
                .map(SalesLedgerProduct::getSalesLedgerId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        // 2. æ‰§è¡Œåˆ é™¤æ“ä½œ
        int result = salesLedgerProductMapper.deleteBatchIds(Arrays.asList(ids));
        // 3. å¯¹æ¯ä¸ªä¸»è¡¨ID进行金额更新
        for (Long salesLedgerId : mainIds) {
            LambdaQueryWrapper<SalesLedgerProduct> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerId);
            List<SalesLedgerProduct> remainingProducts = salesLedgerProductMapper.selectList(wrapper);
            // è°ƒç”¨é€šç”¨æ–¹æ³•更新主表金额
            updateMainContractAmount(
                    salesLedgerId,
                    remainingProducts,
                    SalesLedgerProduct::getTaxInclusiveTotalPrice,
                    salesLedgerMapper,
                    SalesLedger.class
            );
        }
        return result;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addOrUpdateSalesLedgerProduct(SalesLedgerProduct salesLedgerProduct) {
        int result;
        Long salesLedgerId = salesLedgerProduct.getSalesLedgerId();
        if (salesLedgerProduct.getId() == null){
            return salesLedgerProductMapper.insert(salesLedgerProduct);
            result = salesLedgerProductMapper.insert(salesLedgerProduct);
        }else {
            return  salesLedgerProductMapper.updateById(salesLedgerProduct);
            result = salesLedgerProductMapper.updateById(salesLedgerProduct);
        }
        // å¦‚果插入或更新成功,并且有 salesLedgerId,才继续更新主表金额
        if (result > 0 && salesLedgerId != null) {
            // æŸ¥è¯¢è¯¥ä¸»è¡¨ä¸‹çš„æ‰€æœ‰å­è¡¨æ•°æ®
            LambdaQueryWrapper<SalesLedgerProduct> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerId);
            List<SalesLedgerProduct> productList = salesLedgerProductMapper.selectList(wrapper);
            // è°ƒç”¨é€šç”¨æ–¹æ³•更新主表金额
            updateMainContractAmount(
                    salesLedgerId,
                    productList,
                    SalesLedgerProduct::getTaxInclusiveTotalPrice,
                    salesLedgerMapper,
                    SalesLedger.class
            );
        }
        return result;
    }
    /**
     * é€šç”¨æ–¹æ³•:根据主表ID和子表列表,更新主表的合同金额
     */
    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);
            Field amountField = mainEntityClass.getDeclaredField("contractAmount");
            amountField.setAccessible(true);
            amountField.set(entity, totalAmount);
            mainMapper.updateById(entity);
        } catch (Exception e) {
            throw new RuntimeException("动态更新主表金额失败", e);
        }
    }
}
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);
        }
    }
}