huminmin
5 小时以前 8b5ae4f644b951f7ab62c9037c8ac445b0b680a0
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
package com.ruoyi.sales.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.sales.dto.SalesTrendDto;
import com.ruoyi.sales.dto.StatisticsTableDto;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.mapper.ShippingInfoMapper;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import com.ruoyi.sales.pojo.ShippingInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author :yys
 * @date : 2026/1/15 9:41
 */
@Service
@Slf4j
public class MetricStatisticsServiceImpl {
 
    @Autowired
    private SalesLedgerMapper salesLedgerMapper;
 
    @Autowired
    private SalesLedgerProductMapper salesLedgerProductMapper;
 
    @Autowired
    private ShippingInfoMapper shippingInfoMapper;
 
    public AjaxResult total() {
        List<SalesLedger> salesLedgers = salesLedgerMapper.selectList(null);
        if(CollectionUtils.isEmpty(salesLedgers)) return AjaxResult.success(salesLedgers);
        Map<String, Object> map = new HashMap<>();
        // 销售额
        map.put("contractAmountTotal", salesLedgers.stream().map(SalesLedger::getContractAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
        // 订单数量
        map.put("total", new BigDecimal(salesLedgers.size()));
        // 发货率
        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<SalesLedgerProduct>()
                .eq(SalesLedgerProduct::getType, 1));
        map.put("shipRate", "0%");
        if(CollectionUtils.isEmpty(salesLedgerProducts)) return AjaxResult.success(map);
        // 发货数量
        long count = shippingInfoMapper.selectCount(new LambdaQueryWrapper<ShippingInfo>()
                .in(ShippingInfo::getSalesLedgerProductId, salesLedgerProducts.stream().map(SalesLedgerProduct::getId).collect(Collectors.toList()))
                .eq(ShippingInfo::getStatus,"已发货"));
        map.put("shipRate", String.format("%.2f", count * 100.0 / salesLedgerProducts.size()) + "%");
        return AjaxResult.success(map);
    }
 
    public AjaxResult statisticsTable(StatisticsTableDto statisticsTableDto) {
        Map<String, Object> map = new HashMap<>();
        Calendar calendar = Calendar.getInstance();
 
        // 结束时间默认是当前时间
        Date endDate = statisticsTableDto.getEntryDateEnd() != null ? statisticsTableDto.getEntryDateEnd() : new Date();
        statisticsTableDto.setEntryDateEnd(endDate);
 
        // 开始时间默认是12个月前
        Date startDate;
        if (statisticsTableDto.getEntryDateStart() != null) {
            startDate = statisticsTableDto.getEntryDateStart();
        } else {
            calendar.setTime(endDate);
            calendar.add(Calendar.MONTH, -11); // 减11个月,加上当前月共12个月
            calendar.set(Calendar.DAY_OF_MONTH, 1); // 设为月初
            startDate = calendar.getTime();
        }
        statisticsTableDto.setEntryDateStart(startDate);
 
        // 查询数据库获取有数据的月份
        List<SalesTrendDto> salesTrendDtos = salesLedgerMapper.statisticsTable(statisticsTableDto);
 
        // 创建月份到数据的映射
        Map<String, SalesTrendDto> trendMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(salesTrendDtos)) {
            for (SalesTrendDto dto : salesTrendDtos) {
                trendMap.put(dto.getMonth(), dto);
            }
        }
 
        // 生成月份列表
        List<String> dateList = new ArrayList<>();
        List<BigDecimal> orderCountList = new ArrayList<>();
        List<BigDecimal> salesAmountList = new ArrayList<>();
        List<BigDecimal> shippingRateList = new ArrayList<>();
 
        Calendar tempCalendar = Calendar.getInstance();
        tempCalendar.setTime(startDate);
        tempCalendar.set(Calendar.DAY_OF_MONTH, 1); // 确保从月初开始
 
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);
        endCalendar.set(Calendar.DAY_OF_MONTH, 1); // 确保到月末
 
        // 循环生成月份列表,直到达到结束月份
        while (!tempCalendar.after(endCalendar)) {
            String monthStr = String.format("%04d-%02d", tempCalendar.get(Calendar.YEAR), tempCalendar.get(Calendar.MONTH) + 1);
            dateList.add(monthStr);
 
            // 获取当前月份的数据,如果没有则使用默认值
            SalesTrendDto dto = trendMap.get(monthStr);
            if (dto != null) {
                orderCountList.add(new BigDecimal(dto.getOrderCount()));
                salesAmountList.add(dto.getSalesAmount() != null ? dto.getSalesAmount() : BigDecimal.ZERO);
                shippingRateList.add(new BigDecimal(String.valueOf(dto.getShipRate())));
            } else {
                orderCountList.add(BigDecimal.ZERO);
                salesAmountList.add(BigDecimal.ZERO);
                shippingRateList.add(BigDecimal.ZERO);
            }
 
            // 下一个月
            tempCalendar.add(Calendar.MONTH, 1);
        }
 
        map.put("dateList", dateList);
        map.put("orderCountList", orderCountList);
        map.put("salesAmountList", salesAmountList);
        map.put("shippingRateList", shippingRateList);
        return AjaxResult.success(map);
    }
}