Cui Zhi Feng
2024-10-23 569408eff91dda1d123a3b9bc6686ca9f2fe3c65
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
package com.mzl.flower.service.statistics;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.dto.request.flower.FlowerQueryDTO;
import com.mzl.flower.dto.request.payment.OrderItemSalesQueryDTO;
import com.mzl.flower.dto.request.payment.OrderQueryDTO;
import com.mzl.flower.dto.response.flower.FlowerListDTO;
import com.mzl.flower.dto.response.statistics.FlowerStatisticsDTO;
import com.mzl.flower.dto.response.statistics.SaleStatisticsDTO;
import com.mzl.flower.entity.payment.Order;
import com.mzl.flower.entity.supplier.Supplier;
import com.mzl.flower.mapper.flower.FlowerMapper;
import com.mzl.flower.mapper.payment.*;
import com.mzl.flower.mapper.supplier.SupplierMapper;
import com.mzl.flower.service.BaseService;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
 
@Slf4j
@Service
@Transactional
public class StatisticsService extends BaseService {
 
    @Autowired
    private OrderMapper orderMapper;
 
    @Autowired
    private OrderItemMapper orderItemMapper;
 
    @Autowired
    private FlowerMapper flowerMapper;
 
    @Autowired
    private SupplierMapper supplierMapper;
 
    @Autowired
    private OrderItemSalesMapper orderItemSalesMapper;
 
    public SaleStatisticsDTO getSaleStatistics(String date){
        if(StringUtils.isEmpty(date)){
            throw new ValidationException("日期不能为空");
        }
        LocalDate localDate = parseLocalDate(date);
        if(localDate == null){
            throw new ValidationException("日期无效");
        }
 
        LocalDateTime end = localDate.atTime(17, 0, 0);
        LocalDateTime begin = end.plusDays(-1);
 
        SaleStatisticsDTO dto = new SaleStatisticsDTO();
        BigDecimal a = orderMapper.getOrderSaleAmount(begin, end);
        Integer c = orderItemMapper.getFlowerSaleNum(begin, end);
 
        dto.setSaleAmount(a);
        dto.setSaleFlowerCount(c);
 
        return dto;
    }
 
    public FlowerStatisticsDTO getFlowerStatistics(){
        FlowerStatisticsDTO dto = new FlowerStatisticsDTO();
 
        Page page = new Page(1, 1);
        FlowerQueryDTO q = new FlowerQueryDTO();
        List<String> statusList = new ArrayList<>();
        statusList.add(Constants.FLOWER_STATUS.PENDING.name());
        statusList.add(Constants.FLOWER_STATUS.UP.name());
        statusList.add(Constants.FLOWER_STATUS.OFF.name());
        statusList.add(Constants.FLOWER_STATUS.FORCE_OFF.name());
        q.setStatusList(statusList);
        flowerMapper.selectFlowerList(page, q);
        dto.setFlowerCount((int)page.getTotal());
 
        Integer sc = supplierMapper.selectCount(new QueryWrapper<Supplier>()
                .eq("is_enabled", 1)
                .eq("status", "P")
                .eq("deleted", 0));
        dto.setSupplierCount(sc);
 
        Integer oc = orderMapper.selectCount(new QueryWrapper<Order>()
                .eq("deleted", 0)
                .isNotNull("payment_time")
                .isNull("cancel_time")
                .isNull("refund_time"));
        dto.setOrderCount(oc);
 
        page = new Page(1, 1);
        OrderQueryDTO oq = new OrderQueryDTO();
        oq.setStatusBackend(Constants.ORDER_STATUS_BACKEND.SEND.name());
        orderMapper.selectOrderList(page, oq);
        dto.setOrderSendCount((int)page.getTotal());
 
        page = new Page(1, 1);
        q = new FlowerQueryDTO();
        statusList = new ArrayList<>();
        statusList.add(Constants.FLOWER_STATUS.UP.name());
        q.setStatusList(statusList);
        flowerMapper.selectFlowerList(page, q);
        dto.setFlowerUpCount((int)page.getTotal());
 
        page = new Page(1, 1);
        q = new FlowerQueryDTO();
        statusList = new ArrayList<>();
        statusList.add(Constants.FLOWER_STATUS.PENDING.name());
        q.setStatusList(statusList);
        flowerMapper.selectFlowerList(page, q);
        dto.setFlowerPendingCount((int)page.getTotal());
 
        page = new Page(1, 1);
        OrderItemSalesQueryDTO sq = new OrderItemSalesQueryDTO();
        sq.setStatus(Constants.ORDER_SALES_STATUS.PENDING.name());
        orderItemSalesMapper.selectItemSalesList(page, sq);
        dto.setOrderSalesCount((int)page.getTotal());
 
        return dto;
    }
}