maven
2025-10-14 cf6b1cf6fa8f7784c6d7c64b7326d4662bc3d4b3
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
package com.ruoyi.business.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.basic.entity.CoalInfo;
import com.ruoyi.basic.entity.CoalPlan;
import com.ruoyi.basic.entity.CoalValue;
import com.ruoyi.basic.entity.Customer;
import com.ruoyi.basic.mapper.CoalInfoMapper;
import com.ruoyi.basic.mapper.CoalPlanMapper;
import com.ruoyi.basic.mapper.CoalValueMapper;
import com.ruoyi.basic.mapper.CustomerMapper;
import com.ruoyi.business.dto.*;
import com.ruoyi.business.entity.*;
import com.ruoyi.business.mapper.*;
import com.ruoyi.business.service.ProductHomeService;
import com.ruoyi.common.core.domain.R;
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.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author :yys
 * @date : 2025/10/13 9:11
 */
@Service
@Slf4j
public class ProductHomeServiceImpl implements ProductHomeService {
 
    @Autowired
    private ProductionMapper productionMapper;
 
    @Autowired
    private ProductionSchedulingMapper productionSchedulingMapper;
 
    @Autowired
    private OfficialInventoryMapper officialInventoryMapper;
 
    @Autowired
    private SalesRecordMapper salesRecordMapper;
 
    @Autowired
    private CoalInfoMapper coalInfoMapper;
 
    @Autowired
    private CustomerMapper customerMapper;
 
    @Autowired
    private PurchaseRegistrationMapper purchaseRegistrationMapper;
 
    @Autowired
    private CoalValueMapper coalValueMapper;
 
    @Autowired
    private CoalPlanMapper coalPlanMapper;
 
    @Override
    public R<ProductionSchedulingStatisticsDto> productionSchedulingStatistics(DateQueryDto dto) {
        String start = dto.getEntryDateStart();
        String end = dto.getEntryDateEnd();
 
 
        // 使用 LambdaQueryWrapper 构建查询条件
        LambdaQueryWrapper<ProductionScheduling> wrapper = new LambdaQueryWrapper<>();
 
        if (start != null && !start.isEmpty() && end != null && !end.isEmpty()) {
            wrapper.between(ProductionScheduling::getSchedulingDate, LocalDate.parse(start, DateTimeFormatter.ISO_LOCAL_DATE), LocalDate.parse(end, DateTimeFormatter.ISO_LOCAL_DATE));
        }
 
        List<ProductionScheduling> schedulingList = productionSchedulingMapper.selectList(wrapper);
 
        ProductionSchedulingStatisticsDto result = new ProductionSchedulingStatisticsDto();
        BigDecimal totalScheduling = BigDecimal.ZERO;
        BigDecimal completedScheduling = BigDecimal.ZERO;
        BigDecimal pendingScheduling = BigDecimal.ZERO;
 
        for (ProductionScheduling scheduling : schedulingList) {
            BigDecimal num = scheduling.getSchedulingNum();
            if (num != null) {
                totalScheduling = totalScheduling.add(num);
                if (scheduling.getStatus() == 3) { // 已报工
                    completedScheduling = completedScheduling.add(num);
                } else if (scheduling.getStatus() == 1) { // 待生产
                    pendingScheduling = pendingScheduling.add(num);
                }
            }
        }
 
        result.setTotalOutput(totalScheduling);
        result.setCompletedScheduling(completedScheduling);
        result.setPendingScheduling(pendingScheduling);
 
        return R.ok(result);
    }
 
    @Override
    public R<List<ProductionScheduling>> productionSchedulingStatisticsList(DateQueryDto dto) {
        String start = dto.getEntryDateStart();
        String end = dto.getEntryDateEnd();
 
        // 使用 LambdaQueryWrapper 构建查询条件
        LambdaQueryWrapper<ProductionScheduling> wrapper = new LambdaQueryWrapper<>();
 
        if (start != null && !start.isEmpty() && end != null && !end.isEmpty()) {
            wrapper.between(ProductionScheduling::getSchedulingDate, LocalDate.parse(start, DateTimeFormatter.ISO_LOCAL_DATE), LocalDate.parse(end, DateTimeFormatter.ISO_LOCAL_DATE));
        }
 
        List<ProductionScheduling> schedulingList = productionSchedulingMapper.selectList(wrapper);
        // 通过煤种id获取煤种名称
        schedulingList.forEach(scheduling -> {
            CoalInfo coalInfo = coalInfoMapper.selectById(scheduling.getCoalId());
            scheduling.setCoalName(coalInfo.getCoal());
        });
        return R.ok(schedulingList);
    }
 
    @Override
    public R<List<OfficialInventory>> productionSchedulingInventoryList(DateQueryDto dto) {
        String start = dto.getEntryDateStart();
        String end = dto.getEntryDateEnd();
 
 
        // 使用 LambdaQueryWrapper 构建查询条件
        LambdaQueryWrapper<OfficialInventory> wrapper = new LambdaQueryWrapper<>();
 
        if (start != null && !start.isEmpty() && end != null && !end.isEmpty()) {
            wrapper.between(OfficialInventory::getRegistrationDate, LocalDate.parse(start, DateTimeFormatter.ISO_LOCAL_DATE), LocalDate.parse(end, DateTimeFormatter.ISO_LOCAL_DATE));
        }
 
        List<OfficialInventory> schedulingList = officialInventoryMapper.selectList(wrapper);
        schedulingList.forEach(scheduling -> {
            CoalInfo coalInfo = coalInfoMapper.selectById(scheduling.getCoalId());
            scheduling.setCoalName(coalInfo.getCoal());
        });
        return R.ok(schedulingList);
    }
 
    @Override
    public R<MaterialStatisticsDto> materialStatistics(DateQueryDto dto) {
        MaterialStatisticsDto result = new MaterialStatisticsDto();
        // 获取今日开始时间,结束时间
        LocalDateTime todayStart = LocalDateTime.of(
                LocalDateTime.now().toLocalDate(),
                LocalTime.MIN
        );
        LocalDateTime todayEnd = LocalDateTime.of(
                LocalDateTime.now().toLocalDate(),
                LocalTime.MAX
        );
        // 使用 LambdaQueryWrapper 构建查询条件
        LambdaQueryWrapper<OfficialInventory> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(OfficialInventory::getRegistrationDate, todayStart, todayEnd);
        // 获取今日入库总量
        List<OfficialInventory> officialInventories = officialInventoryMapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(officialInventories)) {
            BigDecimal todayInboundTotal = officialInventories.stream()
                    .map(OfficialInventory::getInventoryQuantity)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            result.setTodayInboundTotal(todayInboundTotal);
 
        }
        // 获取当前库存总量
        R<List<OfficialInventory>> listR = productionSchedulingInventoryList(dto);
        if (R.isSuccess(listR)) {
            List<OfficialInventory> officialInventoriesList = listR.getData();
            if (!CollectionUtils.isEmpty(officialInventoriesList)) {
                BigDecimal todayOutboundTotal = officialInventoriesList.stream()
                        .map(OfficialInventory::getInventoryQuantity)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                result.setTodayOutboundTotal(todayOutboundTotal);
            }
        }
        // 获取今日出库总量
        // 使用 LambdaQueryWrapper 构建查询条件
        LambdaQueryWrapper<SalesRecord> wrapperSalesRecord = new LambdaQueryWrapper<>();
        wrapperSalesRecord.between(SalesRecord::getSaleDate, todayStart, todayEnd);
        // 获取今日入库总量
        List<SalesRecord> salesRecords = salesRecordMapper.selectList(wrapperSalesRecord);
        if (!CollectionUtils.isEmpty(salesRecords)) {
            BigDecimal todayInboundTotal = salesRecords.stream()
                    .map(SalesRecord::getInventoryQuantity)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            result.setTodayOutboundTotal(todayInboundTotal);
 
        }
        return R.ok(result);
    }
 
    @Override
    public R<List<ItemListDto>> coalTypeDistribution(DateQueryDto dto) {
        String start = dto.getEntryDateStart();
        String end = dto.getEntryDateEnd();
        // 获取煤种列表
        List<CoalInfo> coalInfoList = coalInfoMapper.selectList(null);
 
        // 使用 LambdaQueryWrapper 构建查询条件
        LambdaQueryWrapper<SalesRecord> wrapper = new LambdaQueryWrapper<>();
 
        if (start != null && !start.isEmpty() && end != null && !end.isEmpty()) {
            wrapper.between(SalesRecord::getRegistrationDate, LocalDate.parse(start, DateTimeFormatter.ISO_LOCAL_DATE), LocalDate.parse(end, DateTimeFormatter.ISO_LOCAL_DATE));
        }
        List<SalesRecord> salesRecords = salesRecordMapper.selectList(wrapper);
        // 根据煤种进行分组,并计算占比
        Map<Long, List<SalesRecord>> coalTypeMap = salesRecords.stream()
                .collect(Collectors.groupingBy(SalesRecord::getCoalId));
 
        List<ItemListDto> itemListDtos = coalInfoList.stream().map(coalInfo -> {
            ItemListDto itemListDto = new ItemListDto();
            itemListDto.setName(coalInfo.getCoal());
 
            // 计算该煤种的销售记录总数量
            List<SalesRecord> records = coalTypeMap.getOrDefault(coalInfo.getId(), Collections.emptyList());
            BigDecimal totalQuantity = records.stream()
                    .map(SalesRecord::getInventoryQuantity)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
 
            itemListDto.setValue(totalQuantity);
            return itemListDto;
        }).collect(Collectors.toList());
 
        // 计算所有煤种的销售总量
        BigDecimal totalAll = itemListDtos.stream()
                .map(ItemListDto::getValue)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
 
        // 设置占比
        for (ItemListDto item : itemListDtos) {
            if (totalAll.compareTo(BigDecimal.ZERO) > 0) {
                BigDecimal percentage = item.getValue().multiply(BigDecimal.valueOf(100))
                        .divide(totalAll, 2, RoundingMode.HALF_UP);
                item.setPercent(percentage);
            } else {
                item.setPercent(BigDecimal.ZERO);
            }
        }
 
        return R.ok(itemListDtos);
    }
 
    @Override
    public R<List<ItemListDto>> originDistribution(DateQueryDto dto) {
        String start = dto.getEntryDateStart();
        String end = dto.getEntryDateEnd();
        // 获取客户列表
        List<Customer> coalInfoList = customerMapper.selectList(null);
 
        // 使用 LambdaQueryWrapper 构建查询条件
        LambdaQueryWrapper<SalesRecord> wrapper = new LambdaQueryWrapper<>();
 
        if (start != null && !start.isEmpty() && end != null && !end.isEmpty()) {
            wrapper.between(SalesRecord::getRegistrationDate, LocalDate.parse(start, DateTimeFormatter.ISO_LOCAL_DATE), LocalDate.parse(end, DateTimeFormatter.ISO_LOCAL_DATE));
        }
        List<SalesRecord> salesRecords = salesRecordMapper.selectList(wrapper);
        // 根据煤种进行分组,并计算占比
        Map<Long, List<SalesRecord>> coalTypeMap = salesRecords.stream()
                .collect(Collectors.groupingBy(SalesRecord::getCustomerId));
 
        List<ItemListDto> itemListDtos = coalInfoList.stream().map(coalInfo -> {
            ItemListDto itemListDto = new ItemListDto();
            itemListDto.setName(coalInfo.getCustomerName());
 
            // 计算该煤种的销售记录总数量
            List<SalesRecord> records = coalTypeMap.getOrDefault(coalInfo.getId(), Collections.emptyList());
            BigDecimal totalQuantity = records.stream()
                    .map(SalesRecord::getInventoryQuantity)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
 
            itemListDto.setValue(totalQuantity);
            return itemListDto;
        }).collect(Collectors.toList());
 
        // 计算所有煤种的销售总量
        BigDecimal totalAll = itemListDtos.stream()
                .map(ItemListDto::getValue)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
 
        // 设置占比
        for (ItemListDto item : itemListDtos) {
            if (totalAll.compareTo(BigDecimal.ZERO) > 0) {
                BigDecimal percentage = item.getValue().multiply(BigDecimal.valueOf(100))
                        .divide(totalAll, 2, RoundingMode.HALF_UP);
                item.setPercent(percentage);
            } else {
                item.setPercent(BigDecimal.ZERO);
            }
        }
 
        return R.ok(itemListDtos);
    }
 
    @Override
    public R<List<CarDto>> carCodeDistribution(DateQueryDto dto) {
        String start = dto.getEntryDateStart();
        String end = dto.getEntryDateEnd();
 
        // 使用 LambdaQueryWrapper 构建查询条件
        LambdaQueryWrapper<PurchaseRegistration> wrapper = new LambdaQueryWrapper<>();
 
        if (start != null && !start.isEmpty() && end != null && !end.isEmpty()) {
            wrapper.between(PurchaseRegistration::getRegistrationDate, LocalDate.parse(start, DateTimeFormatter.ISO_LOCAL_DATE), LocalDate.parse(end, DateTimeFormatter.ISO_LOCAL_DATE));
        }
        List<PurchaseRegistration> purchaseRegistrations = purchaseRegistrationMapper.selectList(wrapper);
        // 通过车牌号分组获取次数,计算总量
        Map<String, List<PurchaseRegistration>> carMap = purchaseRegistrations.stream()
                .collect(Collectors.groupingBy(PurchaseRegistration::getLicensePlate));
        List<CarDto> carDtos = carMap.entrySet().stream().map(entry -> {
            CarDto carDto = new CarDto();
            carDto.setCode(entry.getKey());
            carDto.setCount(entry.getValue().size());
            carDto.setTotal(entry.getValue().stream()
                    .map(PurchaseRegistration::getPurchaseQuantity)
                    .reduce(BigDecimal.ZERO, BigDecimal::add));
            return carDto;
        }).collect(Collectors.toList());
        return R.ok(carDtos);
    }
 
    @Override
    public R<List<PurchaseRegistration>> recentTransaction(DateQueryDto dto) {
        String start = dto.getEntryDateStart();
        String end = dto.getEntryDateEnd();
 
        // 使用 LambdaQueryWrapper 构建查询条件
        LambdaQueryWrapper<PurchaseRegistration> wrapper = new LambdaQueryWrapper<>();
 
        if (start != null && !start.isEmpty() && end != null && !end.isEmpty()) {
            wrapper.between(PurchaseRegistration::getRegistrationDate, LocalDate.parse(start, DateTimeFormatter.ISO_LOCAL_DATE), LocalDate.parse(end, DateTimeFormatter.ISO_LOCAL_DATE));
        }
        List<PurchaseRegistration> purchaseRegistrations = purchaseRegistrationMapper.selectList(wrapper);
        purchaseRegistrations.forEach(purchaseRegistration -> {
            purchaseRegistration.setCoalName(coalInfoMapper.selectById(purchaseRegistration.getCoalId()).getCoal());
        });
        return R.ok(purchaseRegistrations);
    }
 
    @Override
    public R<List<ItemListDto>> heatValueDistribution(DateQueryDto dto) {
        String start = dto.getEntryDateStart();
        String end = dto.getEntryDateEnd();
        LambdaQueryWrapper<OfficialInventory> officialInventoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (start != null && !start.isEmpty() && end != null && !end.isEmpty()) {
            officialInventoryLambdaQueryWrapper.between(OfficialInventory::getRegistrationDate, LocalDate.parse(start, DateTimeFormatter.ISO_LOCAL_DATE), LocalDate.parse(end, DateTimeFormatter.ISO_LOCAL_DATE));
        }
        List<OfficialInventory> officialInventories = officialInventoryMapper.selectList(officialInventoryLambdaQueryWrapper);
        Map<String, BigDecimal> heatValueMap = new HashMap<>();
        BigDecimal totalQuantity = BigDecimal.ZERO;
        for (OfficialInventory officialInventory : officialInventories) {
            List<CoalValue> coalValues = coalValueMapper.selectList(new LambdaQueryWrapper<CoalValue>()
                    .eq(CoalValue::getPlanId, officialInventory.getId())
                    .eq(CoalValue::getFieldName, "发热量"));
            if (coalValues != null && !coalValues.isEmpty()) {
                // 根据发热量值进行分类统计
 
                heatValueMap.put("4000以下", BigDecimal.ZERO);
                heatValueMap.put("4000-4500", BigDecimal.ZERO);
                heatValueMap.put("4500-5000", BigDecimal.ZERO);
                heatValueMap.put("5000-5500", BigDecimal.ZERO);
                heatValueMap.put("5500-6000", BigDecimal.ZERO);
                heatValueMap.put("6000+", BigDecimal.ZERO);
 
                for (CoalValue coalValue : coalValues) {
                    BigDecimal heatValue = BigDecimal.valueOf(Long.parseLong(coalValue.getCoalValue()));
                    if (heatValue == null) continue;
 
                    if (heatValue.compareTo(BigDecimal.valueOf(4000)) < 0) {
                        heatValueMap.put("4000以下", heatValueMap.get("4000以下").add(officialInventory.getInventoryQuantity()));
                    } else if (heatValue.compareTo(BigDecimal.valueOf(4000)) >= 0 && heatValue.compareTo(BigDecimal.valueOf(4500)) < 0) {
                        heatValueMap.put("4000-4500", heatValueMap.get("4000-4500").add(officialInventory.getInventoryQuantity()));
                    } else if (heatValue.compareTo(BigDecimal.valueOf(4500)) >= 0 && heatValue.compareTo(BigDecimal.valueOf(5000)) < 0) {
                        heatValueMap.put("4500-5000", heatValueMap.get("4500-5000").add(officialInventory.getInventoryQuantity()));
                    } else if (heatValue.compareTo(BigDecimal.valueOf(5000)) >= 0 && heatValue.compareTo(BigDecimal.valueOf(5500)) < 0) {
                        heatValueMap.put("5000-5500", heatValueMap.get("5000-5500").add(officialInventory.getInventoryQuantity()));
                    } else if (heatValue.compareTo(BigDecimal.valueOf(5500)) >= 0 && heatValue.compareTo(BigDecimal.valueOf(6000)) < 0) {
                        heatValueMap.put("5500-6000", heatValueMap.get("5500-6000").add(officialInventory.getInventoryQuantity()));
                    } else {
                        heatValueMap.put("6000+", heatValueMap.get("6000+").add(officialInventory.getInventoryQuantity()));
                    }
                }
 
 
            }
        }
        // 计算总量
        totalQuantity = heatValueMap.values().stream().reduce(BigDecimal.ZERO, BigDecimal::add);
        // 将结果转换为 ItemListDto 列表
        BigDecimal finalTotalQuantity = totalQuantity;
        List<ItemListDto> itemListDtos = heatValueMap.entrySet().stream()
                .map(entry -> {
                    ItemListDto itemListDto = new ItemListDto();
                    itemListDto.setName(entry.getKey());
                    itemListDto.setValue(entry.getValue());
                    if (finalTotalQuantity.compareTo(BigDecimal.ZERO) > 0) {
                        BigDecimal percentage = entry.getValue().multiply(BigDecimal.valueOf(100))
                                .divide(finalTotalQuantity, 2, RoundingMode.HALF_UP);
                        itemListDto.setPercent(percentage);
                    }
                    return itemListDto;
                })
                .collect(Collectors.toList());
        return R.ok(itemListDtos);
    }
 
    @Override
    public R<ReportStatisticsDto> reportStatistics(DateQueryDto dto) {
        ReportStatisticsDto reportStatisticsDto = new ReportStatisticsDto();
        String start = dto.getEntryDateStart();
        String end = dto.getEntryDateEnd();
        LambdaQueryWrapper<OfficialInventory> officialInventoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (start != null && !start.isEmpty() && end != null && !end.isEmpty()) {
            officialInventoryLambdaQueryWrapper.between(OfficialInventory::getRegistrationDate, LocalDate.parse(start, DateTimeFormatter.ISO_LOCAL_DATE), LocalDate.parse(end, DateTimeFormatter.ISO_LOCAL_DATE));
        }
        List<OfficialInventory> officialInventories = officialInventoryMapper.selectList(officialInventoryLambdaQueryWrapper);
        if(!CollectionUtils.isEmpty(officialInventories)){
            BigDecimal reduce = officialInventories.stream()
                    .map(OfficialInventory::getInventoryQuantity)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            List<CoalValue> coalValues = coalValueMapper.selectList(new LambdaQueryWrapper<CoalValue>()
                    .in(CoalValue::getPlanId, officialInventories.stream().map(OfficialInventory::getId).collect(Collectors.toList()))
                    .eq(CoalValue::getFieldName, "发热量"));
            BigDecimal totalQuantity = coalValues.stream()
                    .map(coalValue -> BigDecimal.valueOf(Long.parseLong(coalValue.getCoalValue())))
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
 
 
            long count = coalValues.stream()
                    .filter(coalValue -> BigDecimal.valueOf(Long.parseLong(coalValue.getCoalValue())).compareTo(BigDecimal.valueOf(5000)) >= 0)
                    .count();
 
 
            if (!coalValues.isEmpty()){
                // 单位热值耗煤量 = reduce * 1000 / totalQuantity
                BigDecimal heatValue = reduce.multiply(BigDecimal.valueOf(1000)).divide(totalQuantity, 2, RoundingMode.HALF_UP);
                reportStatisticsDto.setUnitHeatValue(heatValue);
                // 平均发热量
                reportStatisticsDto.setAverageFuel(totalQuantity.divide(BigDecimal.valueOf(coalValues.size()), 2, RoundingMode.HALF_UP));
                // 整体达标率(发热量≥5000大卡)
                BigDecimal wholeStandardRate = BigDecimal.valueOf(count).divide(BigDecimal.valueOf(coalValues.size()), 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
                reportStatisticsDto.setWholeStandardRate(wholeStandardRate);
            }else{
                reportStatisticsDto.setAverageFuel(BigDecimal.ZERO);
                reportStatisticsDto.setWholeStandardRate(BigDecimal.ZERO);
                reportStatisticsDto.setUnitHeatValue(BigDecimal.ZERO);
            }
        }
        return R.ok(reportStatisticsDto);
    }
 
    @Override
    public R<List<ItemListDto>> reportTrend(DateQueryDto dto) {
        List<LocalDate> lastFiveDaysQuery = getLastFiveDaysQuery();
        List<ItemListDto> itemListDtos = new ArrayList<>();
        for (LocalDate s : lastFiveDaysQuery) {
            ItemListDto itemListDto = new ItemListDto();
            itemListDto.setName(s.toString());
            LambdaQueryWrapper<OfficialInventory> officialInventoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
            // 在s后面拼上00:00:00
            officialInventoryLambdaQueryWrapper.eq(OfficialInventory::getRegistrationDate, s);
            List<OfficialInventory> officialInventories = officialInventoryMapper.selectList(officialInventoryLambdaQueryWrapper);
            if(!CollectionUtils.isEmpty(officialInventories)){
                List<CoalValue> coalValues = coalValueMapper.selectList(new LambdaQueryWrapper<CoalValue>()
                        .in(CoalValue::getPlanId, officialInventories.stream().map(OfficialInventory::getId).collect(Collectors.toList()))
                        .eq(CoalValue::getFieldName, "发热量"));
                // 整体达标率(发热量≥5000大卡)
                long count = coalValues.stream()
                        .filter(coalValue -> BigDecimal.valueOf(Long.parseLong(coalValue.getCoalValue())).compareTo(BigDecimal.valueOf(5000)) >= 0)
                        .count();
                // 判断除数不能为零的情况
                if (!coalValues.isEmpty()) {
                    BigDecimal wholeStandardRate = BigDecimal.valueOf(count).divide(BigDecimal.valueOf(coalValues.size()), 2, RoundingMode.HALF_UP);
                    itemListDto.setValue(wholeStandardRate);
                }else{
                    itemListDto.setValue(BigDecimal.ZERO);
                }
 
            }
            itemListDtos.add(itemListDto);
        }
        return R.ok(itemListDtos);
    }
 
    @Override
    public R<List<ItemListDto>> coalTypeHeatValueComparison(DateQueryDto dto) {
        List<LocalDate> lastFiveDaysQuery = getLastFiveDaysQuery();
        List<ItemListDto> itemListDtos = new ArrayList<>();
        for (LocalDate s : lastFiveDaysQuery) {
            ItemListDto itemListDto = new ItemListDto();
            itemListDto.setName(s.toString());
            LambdaQueryWrapper<OfficialInventory> officialInventoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
            // 在s后面拼上00:00:00
            officialInventoryLambdaQueryWrapper.eq(OfficialInventory::getRegistrationDate, s);
            List<OfficialInventory> officialInventories = officialInventoryMapper.selectList(officialInventoryLambdaQueryWrapper);
            if(!CollectionUtils.isEmpty(officialInventories)){
                List<CoalValue> coalValues = coalValueMapper.selectList(new LambdaQueryWrapper<CoalValue>()
                        .in(CoalValue::getPlanId, officialInventories.stream().map(OfficialInventory::getId).collect(Collectors.toList()))
                        .eq(CoalValue::getFieldName, "发热量"));
                // 发热量(求和)
                long count = coalValues.stream()
                        .mapToLong(coalValue -> Long.parseLong(coalValue.getCoalValue()))
                        .sum();
                itemListDto.setValue(BigDecimal.valueOf(count));
            }
            itemListDtos.add(itemListDto);
        }
        return R.ok(itemListDtos);
    }
 
    @Override
    public R<List<ItemListDto>> processingRateAnalysis(DateQueryDto dto) {
        List<LocalDate> lastFiveDaysQuery = getLastFiveDaysQuery();
        List<ItemListDto> itemListDtos = new ArrayList<>();
        for (LocalDate s : lastFiveDaysQuery) {
            ItemListDto itemListDto = new ItemListDto();
            itemListDto.setName(s.toString());
            // 获取采购数量
            LambdaQueryWrapper<PurchaseRegistration> purchaseRegistrationLambdaQueryWrapper = new LambdaQueryWrapper<>();
            // 在s后面拼上00:00:00
            purchaseRegistrationLambdaQueryWrapper.eq(PurchaseRegistration::getRegistrationDate, s);
            List<PurchaseRegistration> purchaseRegistrations = purchaseRegistrationMapper.selectList(purchaseRegistrationLambdaQueryWrapper);
            if(!CollectionUtils.isEmpty(purchaseRegistrations)){
                BigDecimal totalQuantity = purchaseRegistrations.stream()
                        .map(PurchaseRegistration::getPurchaseQuantity)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                // 获取正式库存数量
                LambdaQueryWrapper<OfficialInventory> officialInventoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
                // 在s后面拼上00:00:00
                officialInventoryLambdaQueryWrapper.eq(OfficialInventory::getRegistrationDate, s);
                List<OfficialInventory> officialInventories = officialInventoryMapper.selectList(officialInventoryLambdaQueryWrapper);
                if(!CollectionUtils.isEmpty(officialInventories)){
                    BigDecimal officialInventoryQuantity = officialInventories.stream()
                            .map(OfficialInventory::getInventoryQuantity)
                            .reduce(BigDecimal.ZERO, BigDecimal::add);
                    // 加工得率 = 正式库存数量/采购数量
                    if(!totalQuantity.equals(BigDecimal.ZERO)){
                        itemListDto.setValue(officialInventoryQuantity.divide(totalQuantity, 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)));
                    }else{
                        itemListDto.setValue(BigDecimal.ZERO);
                    }
                }
            }
            itemListDtos.add(itemListDto);
        }
        return R.ok(itemListDtos);
    }
 
    @Override
    public R<List<ItemListDto>> costStructure(DateQueryDto dto) {
        List<ItemListDto> itemListDtos = new ArrayList<>();
        LocalDate now = LocalDate.now();
        LocalDate startDate = now.minusDays(4);
        LambdaQueryWrapper<Production> productionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productionLambdaQueryWrapper.between(Production::getCreateTime, startDate, now);
        List<Production> productions = productionMapper.selectList(productionLambdaQueryWrapper);
        ItemListDto itemListDto = new ItemListDto();
        itemListDto.setName("人工成本");
        itemListDto.setValue(productions.stream()
                .map(Production::getLaborCost)
                .reduce(BigDecimal.ZERO, BigDecimal::add));
        itemListDtos.add(itemListDto);
        ItemListDto itemListDtoOne = new ItemListDto();
        itemListDtoOne.setName("能耗成本");
        itemListDtoOne.setValue(productions.stream()
                .map(Production::getEnergyConsumptionCost)
                .reduce(BigDecimal.ZERO, BigDecimal::add));
        itemListDtos.add(itemListDtoOne);
        return R.ok(itemListDtos);
    }
 
    /**
     * 获取最近五天的时间集合
     */
    private List<LocalDate> getLastFiveDaysQuery() {
        LocalDate now = LocalDate.now();
        LocalDate startDate = now.minusDays(4);
        List<LocalDate> dates = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            LocalDate date = startDate.plusDays(i);
            dates.add(date);
        }
        return dates;
    }
}