4 小时以前 620bb4712a31791231c4381581f0f60088f079fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package com.ruoyi.account.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.account.bean.dto.AccountReportDto;
import com.ruoyi.account.bean.dto.DeviceTypeDetail;
import com.ruoyi.account.bean.dto.DeviceTypeDistributionVO;
import com.ruoyi.account.bean.dto.purchase.PurchaseInboundDto;
import com.ruoyi.account.bean.dto.purchase.PurchaseReturnDto;
import com.ruoyi.account.bean.dto.sales.SalesOutboundDto;
import com.ruoyi.account.bean.dto.sales.SalesReturnDto;
import com.ruoyi.account.bean.vo.AccountReportVo;
import com.ruoyi.account.bean.vo.purchase.PurchaseInboundVo;
import com.ruoyi.account.bean.vo.purchase.PurchaseReturnVo;
import com.ruoyi.account.bean.vo.sales.SalesOutboundVo;
import com.ruoyi.account.bean.vo.sales.SalesReturnVo;
import com.ruoyi.account.mapper.purchase.AccountPurchasePaymentMapper;
import com.ruoyi.account.mapper.sales.AccountSalesCollectionMapper;
import com.ruoyi.account.pojo.purchase.AccountPurchasePayment;
import com.ruoyi.account.pojo.sales.AccountSalesCollection;
import com.ruoyi.account.service.AccountingService;
import com.ruoyi.device.mapper.DeviceLedgerMapper;
import com.ruoyi.device.pojo.DeviceLedger;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.procurementrecord.mapper.CustomStorageMapper;
import com.ruoyi.procurementrecord.mapper.ProcurementRecordMapper;
import com.ruoyi.procurementrecord.mapper.ProcurementRecordOutMapper;
import com.ruoyi.procurementrecord.mapper.ReturnManagementMapper;
import com.ruoyi.procurementrecord.pojo.CustomStorage;
import com.ruoyi.procurementrecord.pojo.ProcurementRecordOut;
import com.ruoyi.procurementrecord.pojo.ProcurementRecordStorage;
import com.ruoyi.purchase.mapper.PurchaseReturnOrdersMapper;
import com.ruoyi.stock.mapper.StockInRecordMapper;
import com.ruoyi.stock.mapper.StockOutRecordMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author :yys
 * @date : 2026/1/17 10:41
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class AccountingServiceImpl implements AccountingService {
 
    private final DeviceLedgerMapper deviceLedgerMapper;
    private final CustomStorageMapper customStorageMapper;
    private final ProcurementRecordMapper procurementRecordMapper;
    private final ProcurementRecordOutMapper procurementRecordOutMapper;
    private final AccountSalesCollectionMapper accountSalesCollectionMapper;
    private final AccountPurchasePaymentMapper accountPurchasePaymentMapper;
    private final StockOutRecordMapper stockOutRecordMapper;
    private final ReturnManagementMapper returnManagementMapper;
    private final StockInRecordMapper stockInRecordMapper;
    private final PurchaseReturnOrdersMapper purchaseReturnOrdersMapper;
 
 
    @Override
    public AjaxResult total(Integer year) {
        Map<String,Object> map = new HashMap<>();
        map.put("deprAmount",0); // 折旧金额
        map.put("deviceTotal",0); // 设备总数
        map.put("deviceAmount",0); // 设备资产原值
        map.put("netValue",0); // 净值
        map.put("debt",0); // 负债
        map.put("inventoryValue",0); // 库存资产
        LambdaQueryWrapper<DeviceLedger> deviceLedgerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deviceLedgerLambdaQueryWrapper.like(DeviceLedger::getCreateTime,year);
        List<DeviceLedger> deviceLedgers = deviceLedgerMapper.selectList(deviceLedgerLambdaQueryWrapper);
        if(CollectionUtils.isNotEmpty(deviceLedgers)){
            map.put("deviceTotal",deviceLedgers.size());
            BigDecimal reduce = deviceLedgers.stream()
                    .map(DeviceLedger::getTaxIncludingPriceTotal)
                    .filter(Objects::nonNull)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            map.put("deviceAmount",reduce);
            List<DeviceLedger> collect = deviceLedgers.stream().filter(deviceLedger -> deviceLedger.getIsDepr() != null && deviceLedger.getIsDepr() == 1).collect(Collectors.toList());
            // 根据当前年份和设备录入时间年份比较 * 每年折旧金额 = 设备折旧金额
            BigDecimal total = new BigDecimal(0);
            if(CollectionUtils.isNotEmpty(collect)){
                for (DeviceLedger deviceLedger : collect) {
                    BigDecimal totalDepreciation = calculatePreciseDepreciation(deviceLedger);
                    total = total.add(totalDepreciation);
                }
                map.put("deprAmount",total);
            }
            // 净值 = 设备资产原值 - 设备累计折旧金额
            map.put("netValue",reduce.subtract(total));
        }
        // 负债
        map.put("debt",BigDecimal.ZERO);
 
        // 库存资产
        LambdaQueryWrapper<ProcurementRecordStorage> procurementRecordStorageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        procurementRecordStorageLambdaQueryWrapper.like(ProcurementRecordStorage::getCreateTime,year);
        List<ProcurementRecordStorage> procurementRecordStorages = procurementRecordMapper.selectList(procurementRecordStorageLambdaQueryWrapper);
        BigDecimal procurementRecordTotal = new BigDecimal(0);
        if(CollectionUtils.isNotEmpty(procurementRecordStorages)){
            // 获取出库数据
            LambdaQueryWrapper<ProcurementRecordOut> procurementRecordOutLambdaQueryWrapper = new LambdaQueryWrapper<>();
            procurementRecordOutLambdaQueryWrapper.like(ProcurementRecordOut::getCreateTime,year)
                    .in(ProcurementRecordOut::getProcurementRecordStorageId, procurementRecordStorages
                            .stream()
                            .map(ProcurementRecordStorage::getId)
                            .collect(Collectors.toList()))
                    .in(ProcurementRecordOut::getType,Arrays.asList(1,2));
            List<ProcurementRecordOut> procurementRecordOuts = procurementRecordOutMapper.selectList(procurementRecordOutLambdaQueryWrapper);
            for (ProcurementRecordStorage procurementRecordStorage : procurementRecordStorages) {
                // 采购,生产入库总价值
                procurementRecordTotal.add(procurementRecordStorage.getUnitPrice().multiply(procurementRecordStorage.getInboundNum()));
                // 通过入库id,类型获取出库数据
                List<ProcurementRecordOut> procurementRecordOutsByStorageId = procurementRecordOuts.stream()
                        .filter(procurementRecordOut -> procurementRecordOut.getProcurementRecordStorageId().equals(procurementRecordStorage.getId())
                        && procurementRecordOut.getType().equals(procurementRecordStorage.getType()))
                        .collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(procurementRecordOutsByStorageId)){
                    for (ProcurementRecordOut procurementRecordOut : procurementRecordOutsByStorageId) {
                        // 采购,生产出库总价值
                        procurementRecordTotal.subtract(procurementRecordStorage.getUnitPrice().multiply(procurementRecordOut.getInboundNum()));
                    }
                }
 
            }
 
 
        }
        LambdaQueryWrapper<CustomStorage> customStorageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        customStorageLambdaQueryWrapper.like(CustomStorage::getInboundDate,year);
        List<CustomStorage> customStorages = customStorageMapper.selectList(customStorageLambdaQueryWrapper);
        BigDecimal customStorageTotal = new BigDecimal(0);
        if(CollectionUtils.isNotEmpty(customStorages)){
            // 获取出库数据
            LambdaQueryWrapper<ProcurementRecordOut> procurementRecordOutLambdaQueryWrapper = new LambdaQueryWrapper<>();
            procurementRecordOutLambdaQueryWrapper.like(ProcurementRecordOut::getCreateTime,year)
                    .in(ProcurementRecordOut::getProcurementRecordStorageId, customStorages
                            .stream()
                            .map(CustomStorage::getId)
                            .collect(Collectors.toList()))
                    .eq(ProcurementRecordOut::getType,3);
            List<ProcurementRecordOut> procurementRecordOuts = procurementRecordOutMapper.selectList(procurementRecordOutLambdaQueryWrapper);
            customStorages.forEach(customStorage -> {
                // 自定义入库总价值
                customStorageTotal.add(customStorage.getTaxInclusiveUnitPrice().multiply(customStorage.getInboundNum()));
                // 通过入库id,类型获取出库数据
                List<ProcurementRecordOut> procurementRecordOutsByStorageId = procurementRecordOuts.stream()
                        .filter(procurementRecordOut -> procurementRecordOut.getProcurementRecordStorageId().equals(customStorage.getId()))
                        .collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(procurementRecordOutsByStorageId)){
                    for (ProcurementRecordOut procurementRecordOut : procurementRecordOutsByStorageId) {
                        // 自定义出库总价值
                        customStorageTotal.subtract(procurementRecordOut.getInboundNum().multiply(customStorage.getTaxInclusiveUnitPrice()));
                    }
                }
            });
        }
        map.put("inventoryValue",procurementRecordTotal.add(customStorageTotal));
        return AjaxResult.success( map);
    }
 
    /**
     * 计算设备累计折旧金额
     * @param deviceLedger 设备台账实体
     * @return 累计折旧金额(BigDecimal,保留2位小数)
     */
    public static BigDecimal calculateTotalDepreciation(DeviceLedger deviceLedger) {
        // 1. 空值校验
        if (deviceLedger == null) {
            return BigDecimal.ZERO;
        }
 
        // 2. 判断是否开启折旧,未开启则折旧金额为0
        Integer isDepr = deviceLedger.getIsDepr();
        if (isDepr == null || isDepr != 1) { // 1-是 2-否
            return BigDecimal.ZERO;
        }
 
        // 3. 获取每年折旧金额,为空则返回0
        BigDecimal annualDepreciation = deviceLedger.getAnnualDepreciationAmount();
        if (annualDepreciation == null || annualDepreciation.compareTo(BigDecimal.ZERO) <= 0) {
            return BigDecimal.ZERO;
        }
 
        // 4. 获取设备录入时间,为空则返回0
        LocalDateTime createTime = deviceLedger.getCreateTime();
        if (createTime == null) {
            return BigDecimal.ZERO;
        }
 
        // 5. 计算年份差值
        int currentYear = Year.now().getValue(); // 当前年份
        int createYear = createTime.getYear();    // 设备录入年份
        int yearDiff = currentYear - createYear;
 
        // 6. 处理年份差值为负数的情况(录入时间在未来)
        if (yearDiff < 0) {
            return BigDecimal.ZERO;
        }
 
        // 7. 计算总折旧金额 = 年份差值 * 每年折旧金额
        BigDecimal totalDepreciation = annualDepreciation.multiply(BigDecimal.valueOf(yearDiff));
 
        // 8. 返回保留2位小数的结果(金额规范)
        return totalDepreciation.setScale(2, BigDecimal.ROUND_HALF_UP);
    }
 
    /**
     * 【进阶版】按实际天数计算折旧(更精准)
     * @param deviceLedger 设备台账实体
     * @return 累计折旧金额
     */
    public static BigDecimal calculatePreciseDepreciation(DeviceLedger deviceLedger) {
        if (deviceLedger == null) {
            return BigDecimal.ZERO;
        }
 
        // 判断是否开启折旧
        Integer isDepr = deviceLedger.getIsDepr();
        if (isDepr == null || isDepr != 1) {
            return BigDecimal.ZERO;
        }
 
        // 获取每年折旧金额
        BigDecimal annualDepreciation = deviceLedger.getAnnualDepreciationAmount();
        if (annualDepreciation == null || annualDepreciation.compareTo(BigDecimal.ZERO) <= 0) {
            return BigDecimal.ZERO;
        }
 
        // 获取设备录入时间
        LocalDateTime createTime = deviceLedger.getCreateTime();
        if (createTime == null) {
            return BigDecimal.ZERO;
        }
 
        // 当前时间
        LocalDateTime now = LocalDateTime.now();
 
        // 如果录入时间在未来,返回0
        if (createTime.isAfter(now)) {
            return BigDecimal.ZERO;
        }
 
        // 计算总天数
        long days = ChronoUnit.DAYS.between(createTime, now);
 
        // 按每年365天计算折旧金额
        BigDecimal dailyDepreciation = annualDepreciation.divide(BigDecimal.valueOf(365), 6, BigDecimal.ROUND_HALF_UP);
        BigDecimal totalDepreciation = dailyDepreciation.multiply(BigDecimal.valueOf(days));
 
        return totalDepreciation.setScale(2, BigDecimal.ROUND_HALF_UP);
    }
 
    @Override
    public AjaxResult deviceTypeDistribution(Integer year) {
        // 2. 组装返回VO
       DeviceTypeDistributionVO vo = new DeviceTypeDistributionVO();
       List<DeviceTypeDetail> details = deviceLedgerMapper.getDeviceTypeDistributionByYear( year);
        vo.setDetails(details);
       if(CollectionUtils.isNotEmpty(details)){
           // 3. 提取图表所需的分类和数据
           vo.setCategories(details.stream()
                   .map(DeviceTypeDetail::getType)
                   .collect(Collectors.toList()));
 
           vo.setCountData(details.stream()
                   .map(DeviceTypeDetail::getCount)
                   .collect(Collectors.toList()));
 
           vo.setAmountData(details.stream()
                   .map(DeviceTypeDetail::getAmount)
                   .collect(Collectors.toList()));
           vo.setTotalCount(vo.getCategories().size());
       }
        return AjaxResult.success(vo);
    }
 
    @Override
    public AjaxResult calculateDepreciation(Page page, Integer year) {
        LambdaQueryWrapper<DeviceLedger> deviceLedgerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deviceLedgerLambdaQueryWrapper.like(DeviceLedger::getCreateTime,year)
                .eq(DeviceLedger::getIsDepr,1);
        IPage<DeviceLedger> deviceLedgerIPage = deviceLedgerMapper.selectPage(page, deviceLedgerLambdaQueryWrapper);
        for (DeviceLedger record : deviceLedgerIPage.getRecords()) {
            record.setDeprAmount(calculatePreciseDepreciation(record));
            record.setNetValue(record.getTaxIncludingPriceTotal().subtract(record.getDeprAmount()));
        }
        return AjaxResult.success(deviceLedgerIPage);
    }
 
    @Override
    public AccountReportVo getAccountStatementDetailsByMonth(AccountReportDto accountReportDto) {
        AccountReportVo accountReportVo = new AccountReportVo();
        LocalDate start = accountReportDto.getEntryDateStart();
        LocalDate end = accountReportDto.getEntryDateEnd();
        DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
 
        // ========== 1. 顶部卡片数据 ==========
        // 1.1 总营收 = 收款单总金额
        List<AccountSalesCollection> accountSalesCollections = accountSalesCollectionMapper.selectList(
                Wrappers.<AccountSalesCollection>lambdaQuery()
                        .between(AccountSalesCollection::getCollectionDate, start, end)
        );
        BigDecimal totalIncome = Optional.of(
                accountSalesCollections.stream()
                        .map(AccountSalesCollection::getCollectionAmount)
                        .filter(Objects::nonNull)
                        .reduce(BigDecimal.ZERO, BigDecimal::add)
        ).orElse(BigDecimal.ZERO);
        accountReportVo.setTotalIncome(totalIncome);
        // 1.2 总支出 = 付款单总金额
        List<AccountPurchasePayment> accountPurchasePayments = accountPurchasePaymentMapper.selectList(
                Wrappers.<AccountPurchasePayment>lambdaQuery()
                        .between(AccountPurchasePayment::getPaymentDate, start, end)
        );
        BigDecimal totalExpense = Optional.of(
                accountPurchasePayments.stream()
                        .map(AccountPurchasePayment::getPaymentAmount)
                        .filter(Objects::nonNull)
                        .reduce(BigDecimal.ZERO, BigDecimal::add)
        ).orElse(BigDecimal.ZERO);
        accountReportVo.setTotalExpense(totalExpense);
        // 1.3 应收账款 = 销售出库金额合计 - 销售退货金额合计
        SalesOutboundDto salesOutboundDto = new SalesOutboundDto();
        salesOutboundDto.setStartDate(accountReportDto.getEntryDateStart());
        salesOutboundDto.setEndDate(accountReportDto.getEntryDateEnd());
        List<SalesOutboundVo> salesOutboundVos = stockOutRecordMapper.listPageAccountSales(new Page(1, -1), salesOutboundDto).getRecords();
        BigDecimal salesOutAmount = Optional.of(
                salesOutboundVos.stream()
                        .map(SalesOutboundVo::getOutboundAmount)
                        .filter(Objects::nonNull)
                        .reduce(BigDecimal.ZERO, BigDecimal::add)
        ).orElse(BigDecimal.ZERO);
        SalesReturnDto salesReturnDto = new SalesReturnDto();
        salesReturnDto.setStartDate(accountReportDto.getEntryDateStart());
        salesReturnDto.setEndDate(accountReportDto.getEntryDateEnd());
        List<SalesReturnVo> salesReturnVos = returnManagementMapper.listPageAccountSalesReturn(new Page(1, -1), salesReturnDto).getRecords();
        BigDecimal salesReturnAmount = Optional.of(
                salesReturnVos.stream()
                        .map(SalesReturnVo::getRefundAmount)
                        .filter(Objects::nonNull)
                        .reduce(BigDecimal.ZERO, BigDecimal::add)
        ).orElse(BigDecimal.ZERO);
        accountReportVo.setAccountsReceivable(salesOutAmount.subtract(salesReturnAmount));
        // 1.4 应付账款 = 采购入库金额合计 - 采购退货金额合计
        PurchaseInboundDto purchaseInboundDto = new PurchaseInboundDto();
        purchaseInboundDto.setStartDate(accountReportDto.getEntryDateStart());
        purchaseInboundDto.setEndDate(accountReportDto.getEntryDateEnd());
        List<PurchaseInboundVo> purchaseInboundVos = stockInRecordMapper.listPageAccountPurchase(new Page(1, -1), purchaseInboundDto).getRecords();
        BigDecimal purchaseInAmount = Optional.of(
                purchaseInboundVos.stream()
                        .map(PurchaseInboundVo::getInboundAmount)
                        .filter(Objects::nonNull)
                        .reduce(BigDecimal.ZERO, BigDecimal::add)
        ).orElse(BigDecimal.ZERO);
        PurchaseReturnDto purchaseReturnDto = new PurchaseReturnDto();
        purchaseReturnDto.setStartDate(accountReportDto.getEntryDateStart());
        purchaseReturnDto.setEndDate(accountReportDto.getEntryDateEnd());
        List<PurchaseReturnVo> purchaseReturnVos = purchaseReturnOrdersMapper.listPageAccountPurchaseReturn(new Page(1, -1), purchaseReturnDto).getRecords();
        BigDecimal purchaseReturnAmount = Optional.of(
                purchaseReturnVos.stream()
                        .map(PurchaseReturnVo::getTotalAmount)
                        .filter(Objects::nonNull)
                        .reduce(BigDecimal.ZERO, BigDecimal::add)
        ).orElse(BigDecimal.ZERO);
        accountReportVo.setAccountsPayable(purchaseInAmount.subtract(purchaseReturnAmount));
        // 1.5 净利润 = 总营收 - 总支出
        BigDecimal netProfit = totalIncome.subtract(totalExpense);
        accountReportVo.setNetRevenue(netProfit);
 
        // ========== 2. 折线图:月度营收/支出/净利润趋势 ==========
        Map<String, BigDecimal> monthIncomeMap = new HashMap<>();
        Map<String, BigDecimal> monthExpenseMap = new HashMap<>();
        // 月度营收
        accountSalesCollections.forEach(item -> {
            String month = item.getCollectionDate().format(monthFormatter);
            monthIncomeMap.put(month, monthIncomeMap.getOrDefault(month, BigDecimal.ZERO)
                    .add(Optional.ofNullable(item.getCollectionAmount()).orElse(BigDecimal.ZERO)));
        });
        // 月度支出
        accountPurchasePayments.forEach(item -> {
            String month = item.getPaymentDate().format(monthFormatter);
            monthExpenseMap.put(month, monthExpenseMap.getOrDefault(month, BigDecimal.ZERO)
                    .add(Optional.ofNullable(item.getPaymentAmount()).orElse(BigDecimal.ZERO)));
        });
        // 生成连续月份列表
        List<String> monthList = new ArrayList<>();
        LocalDate current = start.withDayOfMonth(1);
        while (!current.isAfter(end.withDayOfMonth(1))) {
            monthList.add(current.format(monthFormatter));
            current = current.plusMonths(1);
        }
        // 组装趋势数据
        List<AccountReportVo.MonthlyTrendVO> trendList = new ArrayList<>();
        for (String month : monthList) {
            BigDecimal income = monthIncomeMap.getOrDefault(month, BigDecimal.ZERO);
            BigDecimal expense = monthExpenseMap.getOrDefault(month, BigDecimal.ZERO);
            AccountReportVo.MonthlyTrendVO trend = new AccountReportVo.MonthlyTrendVO();
            trend.setMonth(month);
            trend.setIncome(income);
            trend.setExpense(expense);
            trend.setProfit(income.subtract(expense));
            trendList.add(trend);
        }
        accountReportVo.setMonthlyTrendList(trendList);
 
        // ========== 3. 柱状图:月度应收/应付数据 ==========
        Map<String, BigDecimal> monthReceivableMap = new HashMap<>();
        Map<String, BigDecimal> monthPayableMap = new HashMap<>();
        // 月度应收(销售出库-退货)
        salesOutboundVos.forEach(item -> {
            String month = item.getShippingDate().format(monthFormatter);
            monthReceivableMap.put(month, monthReceivableMap.getOrDefault(month, BigDecimal.ZERO)
                    .add(Optional.ofNullable(item.getOutboundAmount()).orElse(BigDecimal.ZERO)));
        });
        salesReturnVos.forEach(item -> {
            String month = item.getMakeTime().format(monthFormatter);
            monthReceivableMap.put(month, monthReceivableMap.getOrDefault(month, BigDecimal.ZERO)
                    .subtract(Optional.ofNullable(item.getRefundAmount()).orElse(BigDecimal.ZERO)));
        });
 
        // 月度应付(采购入库-退货)
        purchaseInboundVos.forEach(item -> {
            String month = item.getInboundDate().format(monthFormatter);
            monthPayableMap.put(month, monthPayableMap.getOrDefault(month, BigDecimal.ZERO)
                    .add(Optional.ofNullable(item.getInboundAmount()).orElse(BigDecimal.ZERO)));
        });
        purchaseReturnVos.forEach(item -> {
            String month = item.getPreparedAt().format(monthFormatter);
            monthPayableMap.put(month, monthPayableMap.getOrDefault(month, BigDecimal.ZERO)
                    .subtract(Optional.ofNullable(item.getTotalAmount()).orElse(BigDecimal.ZERO)));
        });
        // 组装应收应付数据
        List<AccountReportVo.ReceivablePayableVO> rpList = new ArrayList<>();
        for (String month : monthList) {
            AccountReportVo.ReceivablePayableVO rp = new AccountReportVo.ReceivablePayableVO();
            rp.setMonth(month);
            rp.setReceivable(monthReceivableMap.getOrDefault(month, BigDecimal.ZERO));
            rp.setPayable(monthPayableMap.getOrDefault(month, BigDecimal.ZERO));
            rpList.add(rp);
        }
        accountReportVo.setReceivablePayableList(rpList);
        return accountReportVo;
    }
}