maven
2025-12-12 17132f3dd27f5bd97a081bbd9e08a37e9e8f25af
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
package com.ruoyi.home.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.account.pojo.AccountExpense;
import com.ruoyi.account.pojo.AccountIncome;
import com.ruoyi.approve.mapper.ApproveProcessMapper;
import com.ruoyi.approve.pojo.ApproveProcess;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.dto.MapDto;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.home.dto.*;
import com.ruoyi.home.service.HomeService;
import com.ruoyi.lavorissue.mapper.LavorIssueMapper;
import com.ruoyi.lavorissue.pojo.LaborIssue;
import com.ruoyi.procurementrecord.mapper.ProcurementRecordMapper;
import com.ruoyi.procurementrecord.mapper.ProcurementRecordOutMapper;
import com.ruoyi.procurementrecord.pojo.ProcurementRecordOut;
import com.ruoyi.procurementrecord.pojo.ProcurementRecordStorage;
import com.ruoyi.production.mapper.SalesLedgerWorkMapper;
import com.ruoyi.production.pojo.SalesLedgerWork;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.mapper.SysDeptMapper;
import com.ruoyi.purchase.mapper.PaymentRegistrationMapper;
import com.ruoyi.purchase.mapper.PurchaseLedgerMapper;
import com.ruoyi.purchase.pojo.PaymentRegistration;
import com.ruoyi.purchase.pojo.PurchaseLedger;
import com.ruoyi.quality.mapper.QualityInspectMapper;
import com.ruoyi.quality.pojo.QualityInspect;
import com.ruoyi.sales.mapper.ReceiptPaymentMapper;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.ReceiptPayment;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
 
/**
 * @author :yys
 * @date : 2025/7/25 9:23
 */
@Service
@Slf4j
public class HomeServiceImpl implements HomeService {
 
    @Autowired
    private SalesLedgerMapper salesLedgerMapper;
 
    @Autowired
    private PurchaseLedgerMapper purchaseLedgerMapper;
 
    @Autowired
    private SalesLedgerProductMapper salesLedgerProductMapper;
 
    @Autowired
    private ProcurementRecordOutMapper procurementRecordOutMapper;
 
    @Autowired
    private ProcurementRecordMapper procurementRecordStorageMapper;
 
    @Autowired
    private QualityInspectMapper qualityStatisticsMapper;
 
    @Autowired
    private ApproveProcessMapper approveProcessMapper;
 
    @Autowired
    private ReceiptPaymentMapper receiptPaymentMapper;
 
    @Autowired
    private PaymentRegistrationMapper paymentRegistrationMapper;
 
    @Autowired
    private LavorIssueMapper lavorIssueMapper;
 
    @Autowired
    private SysDeptMapper sysDeptMapper;
 
    @Autowired
    private SalesLedgerWorkMapper salesLedgerWorkMapper;;
 
    @Override
    public HomeBusinessDto business() {
        // 构建结果
        HomeBusinessDto homeBusinessDto = new HomeBusinessDto();
        LocalDate now = LocalDate.now();
        YearMonth currentMonth = YearMonth.from(now);
        // 创建LambdaQueryWrapper
        LambdaQueryWrapper<SalesLedger> salesLedgerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        salesLedgerLambdaQueryWrapper.ge(SalesLedger::getEntryDate, currentMonth.atDay(1).atStartOfDay())  // 大于等于本月第一天
                .lt(SalesLedger::getEntryDate, currentMonth.plusMonths(1).atDay(1).atStartOfDay()); // 小于下月第一天
        List<SalesLedger> salesLedgers = salesLedgerMapper.selectList(salesLedgerLambdaQueryWrapper);
        if(CollectionUtils.isEmpty(salesLedgers)){
            return homeBusinessDto;
        }
        // 合计合同金额
        BigDecimal contractAmount = salesLedgers.stream().map(SalesLedger::getContractAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        LambdaQueryWrapper<SalesLedgerProduct> salesLedgerProductMapperLambdaQueryWrapper = new LambdaQueryWrapper<SalesLedgerProduct>();
        salesLedgerProductMapperLambdaQueryWrapper.eq(SalesLedgerProduct::getType, 1)
                .in(SalesLedgerProduct::getSalesLedgerId, salesLedgers.stream().map(SalesLedger::getId).collect(Collectors.toList()));
        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(salesLedgerProductMapperLambdaQueryWrapper);
        // 未开票金额
        BigDecimal noInvoiceAmountTotal = salesLedgerProducts.stream().map(SalesLedgerProduct::getNoInvoiceAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        homeBusinessDto.setMonthSaleMoney(contractAmount.setScale(2, RoundingMode.HALF_UP).toString());
        homeBusinessDto.setMonthSaleHaveMoney(noInvoiceAmountTotal.setScale(2, RoundingMode.HALF_UP).toString());
        // 创建LambdaQueryWrapper
        LambdaQueryWrapper<PurchaseLedger> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(PurchaseLedger::getEntryDate, currentMonth.atDay(1).atStartOfDay())  // 大于等于本月第一天
                .lt(PurchaseLedger::getEntryDate, currentMonth.plusMonths(1).atDay(1).atStartOfDay()); // 小于下月第一天
 
        // 执行查询并计算总和
        List<PurchaseLedger> purchaseLedgers = purchaseLedgerMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(purchaseLedgers)){
            return homeBusinessDto;
        }
 
        LambdaQueryWrapper<SalesLedgerProduct> salesLedgerProductMapperLambdaQueryWrapperCopy = new LambdaQueryWrapper<SalesLedgerProduct>();
        salesLedgerProductMapperLambdaQueryWrapper.eq(SalesLedgerProduct::getType, 2)
                .in(SalesLedgerProduct::getSalesLedgerId, purchaseLedgers.stream().map(PurchaseLedger::getId).collect(Collectors.toList()));
        List<SalesLedgerProduct> salesLedgerProductsCopy = salesLedgerProductMapper.selectList(salesLedgerProductMapperLambdaQueryWrapperCopy);
        // 合计合同金额
        BigDecimal receiveAmount = purchaseLedgers.stream()
                .map(PurchaseLedger::getContractAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        // 未开票金额
        BigDecimal unReceiptPaymentAmount = salesLedgerProductsCopy.stream()
                .map(SalesLedgerProduct::getNoInvoiceAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
 
        // 统计库存
        List<ProcurementRecordStorage> procurementRecordStorages = procurementRecordStorageMapper.selectList(null);
        BigDecimal stockAmount = procurementRecordStorages.stream()
                .map(ProcurementRecordStorage::getInboundNum)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        List<ProcurementRecordOut> procurementRecordOuts = procurementRecordOutMapper.selectList(null);
        BigDecimal outboundAmount = procurementRecordOuts.stream()
                .map(ProcurementRecordOut::getInboundNum)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal stock = stockAmount.subtract(outboundAmount);
        // 获取当天入库数量
        LambdaQueryWrapper<ProcurementRecordStorage> procurementRecordStorageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        procurementRecordStorageLambdaQueryWrapper.ge(ProcurementRecordStorage::getCreateTime, now)  // 大于等于当天
                .lt(ProcurementRecordStorage::getCreateTime, now.plusDays(1)); // 小于明天
        List<ProcurementRecordStorage> procurementRecordStorages1 = procurementRecordStorageMapper.selectList(procurementRecordStorageLambdaQueryWrapper);
        BigDecimal stockAmount1 = procurementRecordStorages1.stream()
                .map(ProcurementRecordStorage::getInboundNum)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
 
        homeBusinessDto.setMonthPurchaseMoney(receiveAmount.setScale(2, RoundingMode.HALF_UP).toString());
        homeBusinessDto.setMonthPurchaseHaveMoney(unReceiptPaymentAmount.setScale(2, RoundingMode.HALF_UP).toString());
        homeBusinessDto.setInventoryNum(stock.setScale(2, RoundingMode.HALF_UP).toString());
        homeBusinessDto.setTodayInventoryNum(stockAmount1.setScale(2, RoundingMode.HALF_UP).toString());
        return homeBusinessDto;
    }
 
    @Override
    public AnalysisCustomerContractAmountsDto analysisCustomerContractAmounts() {
        List<SalesLedger> salesLedgers = salesLedgerMapper.selectList(null);
        // 合计合同金额
        BigDecimal contractAmount = salesLedgers.stream().map(SalesLedger::getContractAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        // 计算周同比
        // 获取当前时间
        LocalDate today = LocalDate.now();
 
        // 获取本周周一
        LocalDate startOfWeek = today.with(DayOfWeek.MONDAY);
 
        // 获取本周周日
        LocalDate endOfWeek = today.with(DayOfWeek.SUNDAY);
        List<SalesLedger> salesLedgers1 = salesLedgerMapper.selectList(new LambdaQueryWrapper<SalesLedger>()
                .ge(SalesLedger::getEntryDate, startOfWeek)  // 大于等于本周周一
                .lt(SalesLedger::getEntryDate, endOfWeek.plusDays(1))); // 修改:使用 lt 并加上一天来包含周日
        BigDecimal weekContractAmount = salesLedgers1.stream().map(SalesLedger::getContractAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        // 获取去年本周时间
        LocalDate lastYearStartOfWeek = today.minusYears(1).with(DayOfWeek.MONDAY);
        LocalDate lastYearEndOfWeek = today.minusYears(1).with(DayOfWeek.SUNDAY);
        List<SalesLedger> salesLedgers2 = salesLedgerMapper.selectList(new LambdaQueryWrapper<SalesLedger>()
                .ge(SalesLedger::getEntryDate, lastYearStartOfWeek)  // 大于等于去年本周周一
                .lt(SalesLedger::getEntryDate, lastYearEndOfWeek.plusDays(1))); // 修改:使用 lt 并加上一天来包含周日
        BigDecimal lastYearWeekContractAmount = salesLedgers2.stream().map(SalesLedger::getContractAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal subtract = weekContractAmount.subtract(lastYearWeekContractAmount);
        String weekYny = "";
        // 周同比
        if(subtract.compareTo(BigDecimal.ZERO) == 0 || lastYearWeekContractAmount.compareTo(BigDecimal.ZERO) == 0){
            weekYny = "0.00";
        }else{
            weekYny = String.format("%.2f", subtract.divide(lastYearWeekContractAmount, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")));
 
        }
 
        // 计算日环比
        LocalDate yesterday = today.minusDays(1);
        LocalDate plusDays = today.plusDays(1);
        List<SalesLedger> salesLedgers3 = salesLedgerMapper.selectList(new LambdaQueryWrapper<SalesLedger>()
                .ge(SalesLedger::getEntryDate, today)  // 大于等于今天
                .lt(SalesLedger::getEntryDate, plusDays)); // 修改:使用 lt 并加上一天来包含当天
        BigDecimal todayContractAmount = salesLedgers3.stream().map(SalesLedger::getContractAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        List<SalesLedger> salesLedgers4 = salesLedgerMapper.selectList(new LambdaQueryWrapper<SalesLedger>()
                .ge(SalesLedger::getEntryDate, yesterday)  // 大于等于昨天
                .lt(SalesLedger::getEntryDate, today)); // 修改:使用 lt 而不是 gt
        BigDecimal lastYearYesterdayContractAmount = salesLedgers4.stream().map(SalesLedger::getContractAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal subtract1 = todayContractAmount.subtract(lastYearYesterdayContractAmount); // 修改:使用 todayContractAmount 而不是 yesterdayContractAmount
        // 日环比
        String chain = "";
        if(subtract1.compareTo(BigDecimal.ZERO) == 0 || lastYearYesterdayContractAmount.compareTo(BigDecimal.ZERO) == 0){
            chain = "0.00";
        }else{
            chain = String.format("%.2f", subtract1.divide(lastYearYesterdayContractAmount, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")));
        }
 
 
        AnalysisCustomerContractAmountsDto analysisCustomerContractAmountsDto = new AnalysisCustomerContractAmountsDto();
        // 修改:将合同金额保留两位小数
        analysisCustomerContractAmountsDto.setSum(contractAmount.setScale(2, RoundingMode.HALF_UP).toString());
        analysisCustomerContractAmountsDto.setYny(weekYny);
        analysisCustomerContractAmountsDto.setChain(chain);
 
        Map<String, BigDecimal> collect = salesLedgers.stream().collect(Collectors.groupingBy(SalesLedger::getCustomerName, Collectors.reducing(BigDecimal.ZERO,
                SalesLedger::getContractAmount, BigDecimal::add)));
        List<MapDto> mapDtos = new ArrayList<>();
        collect.forEach((k,v)->{
            MapDto mapDto = new MapDto();
            mapDto.setName(k);
            // 修改:将金额值保留两位小数
            mapDto.setValue(v.setScale(2, RoundingMode.HALF_UP).toString());
            if(contractAmount.compareTo(new BigDecimal(0)) == 0){
                mapDto.setRate("0");
            }else{
                mapDto.setRate(String.format("%.2f", v.divide(contractAmount, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100"))));
            }
            mapDtos.add(mapDto);
        });
        analysisCustomerContractAmountsDto.setItem(mapDtos);
        return analysisCustomerContractAmountsDto;
    }
 
 
    @Override
    public QualityStatisticsDto qualityStatistics() {
        // 获取近四个月数据(往前推三个月,共4个完整月份)
        LocalDate today = LocalDate.now();
        // 定义日期格式化器(用于显示“年月”格式)
        DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
 
        QualityStatisticsDto qualityStatisticsDto = new QualityStatisticsDto();
        List<QualityStatisticsItem> qualityStatisticsItems = new ArrayList<>();
 
        BigDecimal supplierNum = new BigDecimal(0);
        BigDecimal factoryNum = new BigDecimal(0);
        BigDecimal processNum = new BigDecimal(0);
        // 循环4次,分别统计近4个月的数据(当前月、前1个月、前2个月、前3个月)
        for (int i = 3; i >= 0; i--) {
            // 计算当前循环对应的月份(i=0:当前月,i=1:前1个月,以此类推)
            LocalDate currentMonth = today.minusMonths(i);
            // 当月的开始日期(每月1号)
            LocalDate monthStart = currentMonth.withDayOfMonth(1);
            // 当月的结束日期(每月最后一天)
            LocalDate monthEnd = currentMonth.withDayOfMonth(currentMonth.lengthOfMonth());
 
            // 构建当月的查询条件(如果想一次性查全4个月数据再内存筛选,可优化为先查全再循环筛选)
            LambdaQueryWrapper<QualityInspect> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.ge(QualityInspect::getCheckTime, monthStart)
                    .le(QualityInspect::getCheckTime, monthEnd); // 筛选当月数据
            List<QualityInspect> monthInspects = qualityStatisticsMapper.selectList(queryWrapper);
            BigDecimal reduce = monthInspects.stream()
                    .filter(inspect -> inspect.getInspectType().equals(0))
                    .map(QualityInspect::getQuantity)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            supplierNum = supplierNum.add(reduce);
            BigDecimal reduce1 = monthInspects.stream()
                    .filter(inspect -> inspect.getInspectType().equals(1))
                    .map(QualityInspect::getQuantity)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            processNum= processNum.add(reduce1);
            BigDecimal reduce2 = monthInspects.stream()
                    .filter(inspect -> inspect.getInspectType().equals(2))
                    .map(QualityInspect::getQuantity)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            factoryNum = factoryNum.add(reduce2);
 
            // 构建当月统计项
            QualityStatisticsItem item = new QualityStatisticsItem();
            item.setDate(monthStart.format(monthFormatter)); // 日期显示为“年月”(如 2025-10)
 
            // 1. 供应商检验(类型0)- 合格数量
            BigDecimal supplierQualified = monthInspects.stream()
                    .filter(inspect -> inspect.getInspectType().equals(0)
                            && "合格".equals(inspect.getCheckResult()))
                    .map(QualityInspect::getQuantity)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            item.setSupplierNum(supplierQualified);
 
            // 2. 工序检验(类型1)- 合格数量
            BigDecimal processQualified = monthInspects.stream()
                    .filter(inspect -> inspect.getInspectType().equals(1)
                            && "合格".equals(inspect.getCheckResult()))
                    .map(QualityInspect::getQuantity)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            item.setProcessNum(processQualified);
 
            // 3. 工厂检验(类型2)- 合格数量
            BigDecimal factoryQualified = monthInspects.stream()
                    .filter(inspect -> inspect.getInspectType().equals(2)
                            && "合格".equals(inspect.getCheckResult()))
                    .map(QualityInspect::getQuantity)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            item.setFactoryNum(factoryQualified);
 
            qualityStatisticsItems.add(item);
        }
        // 统计近4个月总数据(所有月份汇总)
        qualityStatisticsDto.setProcessNum(processNum);
        qualityStatisticsDto.setSupplierNum(supplierNum);
        qualityStatisticsDto.setFactoryNum(factoryNum);
        qualityStatisticsDto.setItem(qualityStatisticsItems);
 
        return qualityStatisticsDto;
    }
 
    @Override
    public List<ApproveProcess> todos() throws ParseException {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        LambdaQueryWrapper<ApproveProcess> approveProcessLambdaQueryWrapper = new LambdaQueryWrapper<>();
        approveProcessLambdaQueryWrapper.eq(ApproveProcess::getApproveDelete, 0)
                .eq(ApproveProcess::getApproveUserCurrentId, loginUser.getUserId())
                .ne(ApproveProcess::getApproveStatus, 2)
                .eq(ApproveProcess::getTenantId, loginUser.getTenantId());
        List<ApproveProcess> approveProcesses = approveProcessMapper.selectList(approveProcessLambdaQueryWrapper);
        if(CollectionUtils.isEmpty(approveProcesses)){
            approveProcesses = new ArrayList<>();
        }
        // 查询未领用劳保记录
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 
        LaborIssue laborIssue1 = new LaborIssue();
        laborIssue1.setAdoptedDate(new Date());
        laborIssue1.setIssueDate(sdf.parse(sdf.format(new Date())));
        List<LaborIssue> laborIssues = lavorIssueMapper.list(laborIssue1);
        if(!CollectionUtils.isEmpty(laborIssues)){
            for (LaborIssue laborIssue : laborIssues) {
                ApproveProcess approveProcess = new ApproveProcess();
                approveProcess.setApproveId(laborIssue.getOrderNo());
                approveProcess.setApproveDeptName(sysDeptMapper.selectDeptById(loginUser.getTenantId()).getDeptName());
                approveProcess.setApproveTime(laborIssue.getIssueDate());
                approveProcess.setApproveReason(laborIssue.getDictTypeName() + "-" + laborIssue.getDictName() + "超时未领取");
                approveProcesses.add(approveProcess);
            }
        }
        return approveProcesses;
    }
 
    /**
     *
     * @param type 1-周 2-月 3-季度
     * @return
     */
    @Override
    public StatisticsReceivablePayableDto statisticsReceivablePayable(Integer type) {
        LocalDate today = LocalDate.now();
        LocalDate startDate = null;
        LocalDate endDate = null;
        switch (type){
            case 1:
                // 获取本周周一
                startDate = today.with(DayOfWeek.MONDAY);
                // 获取本周周日
                endDate = today.with(DayOfWeek.SUNDAY);
                break;
            case 2:
                startDate = today.with(TemporalAdjusters.firstDayOfMonth());
                endDate = today.with(TemporalAdjusters.lastDayOfMonth());
                break;
            case 3:
                Month currentMonth = today.getMonth();
                Month firstMonthOfQuarter = currentMonth.firstMonthOfQuarter();
                Month lastMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
 
                startDate = today.withMonth(firstMonthOfQuarter.getValue())
                        .with(TemporalAdjusters.firstDayOfMonth());
                endDate = today.withMonth(lastMonthOfQuarter.getValue())
                        .with(TemporalAdjusters.lastDayOfMonth());
                break;
        }
        // 应收
        List<SalesLedger> salesLedgers = salesLedgerMapper.selectList(new LambdaQueryWrapper<SalesLedger>()
//                .ge(SalesLedger::getEntryDate, startDate)
//                .lt(SalesLedger::getEntryDate, endDate)
        );
        BigDecimal receivableMoney = salesLedgers.stream().map(SalesLedger::getContractAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        // 应付
        List<PurchaseLedger> procurementRecords = purchaseLedgerMapper.selectList(new LambdaQueryWrapper<PurchaseLedger>()
//                .ge(PurchaseLedger::getEntryDate, startDate)
//                .lt(PurchaseLedger::getEntryDate, endDate)
        );
        BigDecimal payableMoney = procurementRecords.stream().map(PurchaseLedger::getContractAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        // 预收
        List<ReceiptPayment> receiptPayments = receiptPaymentMapper.selectList(new LambdaQueryWrapper<ReceiptPayment>()
//                .ge(ReceiptPayment::getReceiptPaymentDate, startDate)
//                .lt(ReceiptPayment::getReceiptPaymentDate, endDate)
        );
        BigDecimal advanceMoney = receiptPayments.stream().map(ReceiptPayment::getReceiptPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        // 预付
        List<PaymentRegistration> paymentRegistrations = paymentRegistrationMapper.selectList(new LambdaQueryWrapper<PaymentRegistration>()
//                .ge(PaymentRegistration::getPaymentDate, startDate)
//                .lt(PaymentRegistration::getPaymentDate, endDate)
        );
        BigDecimal prepayMoney = paymentRegistrations.stream().map(PaymentRegistration::getCurrentPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        StatisticsReceivablePayableDto statisticsReceivablePayableDto = new StatisticsReceivablePayableDto();
        statisticsReceivablePayableDto.setPayableMoney(payableMoney.subtract(prepayMoney));
        statisticsReceivablePayableDto.setReceivableMoney(receivableMoney.subtract(advanceMoney));
        statisticsReceivablePayableDto.setAdvanceMoney(advanceMoney);
        statisticsReceivablePayableDto.setPrepayMoney(prepayMoney);
 
        return statisticsReceivablePayableDto;
    }
 
    @Override
    public QualityProductQualifiedRateDto qualityProductQualifiedRate() {
        QualityProductQualifiedRateDto qualityProductQualifiedRateDto = new QualityProductQualifiedRateDto();
        List<QualityInspect> qualityInspects = qualityStatisticsMapper.selectList(null);
        if(!CollectionUtils.isEmpty(qualityInspects)){
            // 原材料合格率
            long rawwMaterialCount = qualityInspects.stream().filter(qualityInspect -> qualityInspect.getInspectType() == 0).count();
            long count = qualityInspects.stream().filter(qualityInspect -> "合格".equals(qualityInspect.getCheckResult()) && qualityInspect.getInspectType() == 0).count();
            if(count > 0){
                qualityProductQualifiedRateDto.setRawMaterialQualifiedRate(new BigDecimal(rawwMaterialCount)
                        .divide(new BigDecimal( count), 4, RoundingMode.HALF_UP)
                        .multiply(BigDecimal.valueOf(100.0)));
            }
            // 过程合格率
            long processCount = qualityInspects.stream().filter(qualityInspect -> qualityInspect.getInspectType() == 1).count();
            long countOne = qualityInspects.stream().filter(qualityInspect -> "合格".equals(qualityInspect.getCheckResult()) && qualityInspect.getInspectType() == 1).count();
            if(countOne > 0){
                qualityProductQualifiedRateDto.setProcessQualifiedRate(new BigDecimal(processCount)
                        .divide(new BigDecimal( countOne), 4, RoundingMode.HALF_UP)
                        .multiply(BigDecimal.valueOf(100.0)));
            }
            // 出厂合格率
            long factoryCount = qualityInspects.stream().filter(qualityInspect -> qualityInspect.getInspectType() == 2).count();
            long countTwo = qualityInspects.stream().filter(qualityInspect -> "合格".equals(qualityInspect.getCheckResult()) && qualityInspect.getInspectType() == 2).count();
            if(countTwo > 0){
                qualityProductQualifiedRateDto.setFactoryQualifiedRate(new BigDecimal(factoryCount)
                        .divide(new BigDecimal( countTwo), 4, RoundingMode.HALF_UP)
                        .multiply(BigDecimal.valueOf(100.0)));
            }
        }
        // 库存周转率统计当月出库(使用)的物料总数量÷平均的库存物料总数量×100%。)
        // 获取当前月
        LocalDate today = LocalDate.now();
        LocalDate startDate = today.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate endDate = today.with(TemporalAdjusters.lastDayOfMonth());
        // 获取当前月出库(使用)的物料总数量
        List<ProcurementRecordOut> procurementRecordOuts = procurementRecordOutMapper.selectList(new LambdaQueryWrapper<ProcurementRecordOut>()
                .ge(ProcurementRecordOut::getCreateTime, startDate)
                .lt(ProcurementRecordOut::getCreateTime, endDate));
        // 获取当前月入库的物料总数量
        List<ProcurementRecordStorage> procurementRecordStorages = procurementRecordStorageMapper.selectList(new LambdaQueryWrapper<ProcurementRecordStorage>()
                .ge(ProcurementRecordStorage::getCreateTime, startDate)
                .lt(ProcurementRecordStorage::getCreateTime, endDate));
        BigDecimal reduce = procurementRecordOuts.stream()
                .map(ProcurementRecordOut::getInboundNum)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal reduceOne = procurementRecordStorages.stream()
                .map(ProcurementRecordStorage::getInboundNum)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        if(reduce.compareTo(BigDecimal.ZERO) > 0){
            qualityProductQualifiedRateDto.setInventoryTurnoverRate(reduceOne.divide(reduce, 4, RoundingMode.HALF_UP)
                    .multiply(BigDecimal.valueOf(100.0)));
        }
 
        return qualityProductQualifiedRateDto;
    }
 
    @Override
    public QualityStatisticsDto inventoryStatistics() {
        // 获取近四个月数据(往前推三个月,共4个完整月份)
        LocalDate today = LocalDate.now();
        // 定义日期格式化器(用于显示“年月”格式)
        DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
 
        QualityStatisticsDto qualityStatisticsDto = new QualityStatisticsDto();
        List<QualityStatisticsItem> qualityStatisticsItems = new ArrayList<>();
 
        BigDecimal supplierNum = new BigDecimal(0); //入库数量
        BigDecimal factoryNum = new BigDecimal(0); //出库数量
        // 循环4次,分别统计近4个月的数据(当前月、前1个月、前2个月、前3个月)
        for (int i = 3; i >= 0; i--) {
            // 计算当前循环对应的月份(i=0:当前月,i=1:前1个月,以此类推)
            LocalDate currentMonth = today.minusMonths(i);
            // 当月的开始日期(每月1号)
            LocalDate monthStart = currentMonth.withDayOfMonth(1);
            // 当月的结束日期(每月最后一天)
            LocalDate monthEnd = currentMonth.withDayOfMonth(currentMonth.lengthOfMonth());
 
            // 入库数量
            // 构建当月的查询条件(如果想一次性查全4个月数据再内存筛选,可优化为先查全再循环筛选)
            LambdaQueryWrapper<ProcurementRecordStorage> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.ge(ProcurementRecordStorage::getCreateTime, monthStart)
                    .le(ProcurementRecordStorage::getCreateTime, monthEnd); // 筛选当月数据
            List<ProcurementRecordStorage> monthInspects = procurementRecordStorageMapper.selectList(queryWrapper);
            // 出库数量
            LambdaQueryWrapper<ProcurementRecordOut> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.ge(ProcurementRecordOut::getCreateTime, monthStart)
                    .le(ProcurementRecordOut::getCreateTime, monthEnd);
            List<ProcurementRecordOut> monthInspects1 = procurementRecordOutMapper.selectList(queryWrapper1);
            BigDecimal reduce = monthInspects.stream()
                    .map(ProcurementRecordStorage::getInboundNum)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            supplierNum = supplierNum.add(reduce);
            BigDecimal reduce1 = monthInspects1.stream()
                    .map(ProcurementRecordOut::getInboundNum)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            factoryNum= factoryNum.add(reduce1);
 
            // 构建当月统计项
            QualityStatisticsItem item = new QualityStatisticsItem();
            item.setDate(monthStart.format(monthFormatter)); // 日期显示为“年月”(如 2025-10)
 
            // 1. 供应商检验(类型0)- 合格数量
            BigDecimal supplierQualified = monthInspects.stream()
                    .map(ProcurementRecordStorage::getInboundNum)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            item.setSupplierNum(supplierQualified);
 
            // 3. 工厂检验(类型2)- 合格数量
            BigDecimal factoryQualified = monthInspects1.stream()
                    .map(ProcurementRecordOut::getInboundNum)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            item.setFactoryNum(factoryQualified);
 
            qualityStatisticsItems.add(item);
        }
        // 统计近4个月总数据(所有月份汇总)
        qualityStatisticsDto.setSupplierNum(supplierNum);
        qualityStatisticsDto.setFactoryNum(factoryNum);
        qualityStatisticsDto.setItem(qualityStatisticsItems);
 
        return qualityStatisticsDto;
    }
 
    @Override
    public Map<String, List<String>> productionStatistics() {
        // 获取最近四个月(当前月 + 前3个月)的时间范围
        LocalDate today = LocalDate.now();
        DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyy-MM"); // 年月格式化器
        Map<String, List<String>> result = new HashMap<>();
        List<String> months = new ArrayList<>(); // 存储年月(如 2025-12、2025-11)
        List<String> totalIncomeList = new ArrayList<>(); // 每月总收入
        List<String> totalExpenseList = new ArrayList<>(); // 每月总支出
        List<String> netIncomeList = new ArrayList<>(); // 每月净收入(收入-支出)
 
        // 步骤1:计算近4个月的年月列表(当前月、前1月、前2月、前3月)
        List<String> targetMonths = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            LocalDate currentMonth = today.minusMonths(i);
            String monthStr = currentMonth.format(monthFormatter);
            targetMonths.add(monthStr);
        }
        // 反转列表,确保顺序为「前3月 → 当前月」(可选,按需求调整顺序)
        Collections.reverse(targetMonths);
 
        // 步骤2:一次性查询近4个月所有收入数据,按“年月”分组汇总
        LocalDate fourMonthsAgo = today.minusMonths(3).withDayOfMonth(1); // 近4个月起始日(前3月1号)
        LocalDate currentMonthEnd = today.withDayOfMonth(today.lengthOfMonth()); // 当前月结束日
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        // 查询近4个月所有收入
        List<SalesLedgerWork> allIncomes = salesLedgerWorkMapper.selectList(
                Wrappers.<SalesLedgerWork>lambdaQuery()
                        .ge(SalesLedgerWork::getSchedulingDate, fourMonthsAgo.toString()) // 大于等于起始日
                        .le(SalesLedgerWork::getSchedulingDate, currentMonthEnd.toString()) // 小于等于结束日
        );
 
        // 待生产
        Map<String, BigDecimal> monthlyIncomeMap = allIncomes.stream()
                .filter(income -> income.getSchedulingNum() != null && income.getStatus().equals(1)) // 过滤空金额
                .collect(Collectors.groupingBy(
                        income -> {
                            // 将输入时间(字符串)转换为LocalDate,再格式化为年月
                            return income.getSchedulingDate().format(monthFormatter);
                        },
                        Collectors.reducing(BigDecimal.ZERO, SalesLedgerWork::getSchedulingNum, BigDecimal::add)
                ));
 
        // 生产中
        Map<String, BigDecimal> monthlyExpenseMap = allIncomes.stream()
                .filter(income -> income.getSchedulingNum() != null && income.getStatus().equals(2)) // 过滤空金额
                .collect(Collectors.groupingBy(
                        income -> {
                            // 将输入时间(字符串)转换为LocalDate,再格式化为年月
                            return income.getSchedulingDate().format(monthFormatter);
                        },
                        Collectors.reducing(BigDecimal.ZERO, SalesLedgerWork::getSchedulingNum, BigDecimal::add)
                ));
 
        // 已报工
        Map<String, BigDecimal> successIncomeMap = allIncomes.stream()
                .filter(income -> income.getSchedulingNum() != null && income.getStatus().equals(3)) // 过滤空金额
                .collect(Collectors.groupingBy(
                        income -> {
                            // 将输入时间(字符串)转换为LocalDate,再格式化为年月
                            return income.getSchedulingDate().format(monthFormatter);
                        },
                        Collectors.reducing(BigDecimal.ZERO, SalesLedgerWork::getSchedulingNum, BigDecimal::add)
                ));
 
        // 步骤4:循环4个目标月份,填充统计数据(无数据时默认为0)
        for (String month : targetMonths) {
            // 待生产
            BigDecimal totalIncome = monthlyIncomeMap.getOrDefault(month, BigDecimal.ZERO);
            // 生产中
            BigDecimal totalExpense = monthlyExpenseMap.getOrDefault(month, BigDecimal.ZERO);
            // 已报工
            BigDecimal netIncome =  successIncomeMap.getOrDefault(month, BigDecimal.ZERO);
 
            // 填充列表
            months.add(month);
            totalIncomeList.add(totalIncome.toString());
            totalExpenseList.add(totalExpense.toString());
            netIncomeList.add(netIncome.toString());
        }
 
        // 组装结果
        result.put("days", months); // 年月(如 ["2025-09", "2025-10", "2025-11", "2025-12"])
        result.put("totalIncome", totalIncomeList); // 待生产
        result.put("totalExpense", totalExpenseList); // 生产中
        result.put("netIncome", netIncomeList); // 已报工
 
        return result;
    }
}