maven
3 天以前 366b67926809852c1090f14c5a24fe00109a81b5
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -204,53 +204,77 @@
    }
    @Override
    public List<MonthlyAmountDto> getAmountHalfYear() {
    public List<MonthlyAmountDto> getAmountHalfYear(Integer type) {
        LocalDate now = LocalDate.now();
        YearMonth currentMonth = YearMonth.from(now);
        LocalDateTime currentDateTime = LocalDateTime.now();
        List<MonthlyAmountDto> monthlyAmounts = new ArrayList<>();
        // 根据 type 确定查询的时间间隔(天数)和总查询天数
        int daysPerPeriod;
        int totalDays;
        switch (type) {
            case 1:
                daysPerPeriod = 5;   // 每5天查一次
                totalDays = 30;       // 6次 × 5天 = 30天
                break;
            case 2:
                daysPerPeriod = 15;    // 每15天查一次
                totalDays = 90;       // 6次 × 15天 = 90天
                break;
            case 3:
                daysPerPeriod = 30;   // 每30天查一次
                totalDays = 180;      // 6次 × 30天 = 180天
                break;
            default:
                throw new IllegalArgumentException("Invalid type value: " + type);
        }
        List<MonthlyAmountDto> result = new ArrayList<>();
        // 循环6次,每次查询一个时间段的数据
        for (int i = 0; i < 6; i++) {
            YearMonth targetMonth = currentMonth.minusMonths(i);
            LocalDate firstDayOfMonth = targetMonth.atDay(1);
            LocalDate firstDayOfNextMonth = targetMonth.plusMonths(1).atDay(1);
            // 计算当前时间段的起始和结束时间
            LocalDateTime endTime = currentDateTime.minusDays(i * daysPerPeriod);
            LocalDateTime startTime = endTime.minusDays(daysPerPeriod);
            LocalDateTime startOfMonth = firstDayOfMonth.atStartOfDay();
            LocalDateTime startOfNextMonth = firstDayOfNextMonth.atStartOfDay();
            // 查询回款金额
            LambdaQueryWrapper<ReceiptPayment> receiptPaymentQuery = new LambdaQueryWrapper<>();
            receiptPaymentQuery
                    .ge(ReceiptPayment::getCreateTime, startTime)
                    .lt(ReceiptPayment::getCreateTime, endTime);
            List<ReceiptPayment> receiptPayments = receiptPaymentMapper.selectList(receiptPaymentQuery);
            LambdaQueryWrapper<ReceiptPayment> receiptPaymentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            receiptPaymentLambdaQueryWrapper.ge(ReceiptPayment::getCreateTime, startOfMonth)
                    .lt(ReceiptPayment::getCreateTime, startOfNextMonth);
            // 查询开票金额
            LambdaQueryWrapper<InvoiceLedger> invoiceLedgerQuery = new LambdaQueryWrapper<>();
            invoiceLedgerQuery
                    .ge(InvoiceLedger::getCreateTime, startTime)
                    .lt(InvoiceLedger::getCreateTime, endTime);
            List<InvoiceLedger> invoiceLedgers = invoiceLedgerMapper.selectList(invoiceLedgerQuery);
            LambdaQueryWrapper<InvoiceLedger> invoiceLedgerLambdaQueryWrapper = new LambdaQueryWrapper<>();
            invoiceLedgerLambdaQueryWrapper.ge(InvoiceLedger::getCreateTime, startOfMonth)
                    .lt(InvoiceLedger::getCreateTime, startOfNextMonth);
            // 获取回款金额
            List<ReceiptPayment> receiptPaymentList = receiptPaymentMapper.selectList(receiptPaymentLambdaQueryWrapper);
            //开票金额
            List<InvoiceLedger> invoiceLedgerList = invoiceLedgerMapper.selectList(invoiceLedgerLambdaQueryWrapper);
            // 使用 Stream 求和
            BigDecimal invoiceAmount = invoiceLedgerList.stream()
                    .map(InvoiceLedger::getInvoiceTotal)
                    .filter(Objects::nonNull)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            BigDecimal receiptAmount = receiptPaymentList.stream()
            // 计算回款总额
            BigDecimal receiptAmount = receiptPayments.stream()
                    .map(ReceiptPayment::getReceiptPaymentAmount)
                    .filter(Objects::nonNull)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            MonthlyAmountDto monthlyAmount = new MonthlyAmountDto();
            monthlyAmount.setMonth(targetMonth.format(DateTimeFormatter.ofPattern("yyyy-MM")));
            monthlyAmount.setInvoiceAmount(invoiceAmount);
            monthlyAmount.setReceiptAmount(receiptAmount);
            // 计算开票总额
            BigDecimal invoiceAmount = invoiceLedgers.stream()
                    .map(InvoiceLedger::getInvoiceTotal)
                    .filter(Objects::nonNull)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            monthlyAmounts.add(monthlyAmount);
            // 构造返回的 DTO
            MonthlyAmountDto dto = new MonthlyAmountDto();
            dto.setMonth(startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + " ~ " +
                    endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
            dto.setReceiptAmount(receiptAmount);
            dto.setInvoiceAmount(invoiceAmount);
            result.add(dto);
        }
        Collections.reverse(monthlyAmounts);
        return monthlyAmounts;
        // 反转列表,使时间顺序从早到晚
        Collections.reverse(result);
        return result;
    }
    @Override