maven
20 小时以前 88e995ccceb33b970fa00b349dd6292e2848bd3d
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
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.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 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;
 
    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 = salesLedgerProducts.stream()
                .filter(salesLedgerProduct -> salesLedgerProduct.getApproveStatus().equals(2))
                .count();
        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<>();
        if (statisticsTableDto.getEntryDateStart() == null || statisticsTableDto.getEntryDateEnd() == null) {
            Calendar calendar = Calendar.getInstance();
            // 结束时间默认是当前时间
            statisticsTableDto.setEntryDateEnd(new Date());
            // 开始时间默认是6个月前
            calendar.add(Calendar.MONTH, -6);
            statisticsTableDto.setEntryDateStart(calendar.getTime());
        }
        List<SalesTrendDto> salesTrendDtos = salesLedgerMapper.statisticsTable(statisticsTableDto);
        if(CollectionUtils.isEmpty(salesTrendDtos)) return AjaxResult.success(map);
        map.put("dateList", salesTrendDtos.stream().map(SalesTrendDto::getMonth).collect(Collectors.toList()));
        map.put("orderCountList", salesTrendDtos.stream().map(SalesTrendDto::getOrderCount).collect(Collectors.toList()));
        map.put("salesAmountList", salesTrendDtos.stream().map(SalesTrendDto::getSalesAmount).collect(Collectors.toList()));
        map.put("shippingRateList", salesTrendDtos.stream().map(SalesTrendDto::getShipRate).collect(Collectors.toList()));
        return AjaxResult.success(map);
    }
}