hjl
2024-07-05 428519bd1056dd90cd4589dbf85b380e403ff254
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
package com.ruoyi.admin.service.impl;
 
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.admin.entity.User;
import com.ruoyi.admin.mapper.UserMapper;
import com.ruoyi.admin.request.UserDataCountRequest;
import com.ruoyi.admin.service.OrderService;
import com.ruoyi.admin.service.UserService;
import com.ruoyi.admin.service.WithdrawService;
import com.ruoyi.admin.vo.UserDataCountVO;
import com.ruoyi.admin.vo.UserTrendsVO;
import com.ruoyi.common.core.constant.OrderConstants;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.Month;
import java.time.Year;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
 
/**
 * <p>
 * 用户列表 服务实现类
 * </p>
 *
 * @author hjl
 * @since 2024-05-29
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
 
    @Resource
    private OrderService orderService;
    @Resource
    private WithdrawService withdrawService;
 
    @Override
    public List<UserTrendsVO> userTrends(List<String> city) {
        List<UserTrendsVO> userTrendsList = baseMapper.userTrends(city);
        return null == userTrendsList || userTrendsList.isEmpty() ? new ArrayList<>() : userTrendsList;
    }
 
    @Override
    public UserDataCountVO userDataCount(UserDataCountRequest userDataCountRequest) {
        List<String> cityList = userDataCountRequest.getCityList();
        List<User> userList = lambdaQuery().eq(User::getIsDelete, 0).list();
        // 用户总量
        Integer userNumber = null == userList ? 0 : userList.size();
        // 计算当前季度时间所包含时间
        int currentYear = Year.now().getValue();
        // 获取当前月份
        Month currentMonth = LocalDate.now().getMonth();
        // 计算当前季度的开始时间和结束时间
        LocalDate startDate = LocalDate.of(currentYear, getStartMonthOfQuarter(currentMonth), 1);
        LocalDate endDate = startDate.plusMonths(2).with(TemporalAdjusters.lastDayOfMonth());
        String startDateStr = String.valueOf(startDate);
        String endDateStr = String.valueOf(endDate);
        // 结果数据封装
        Long increaseNumber;
        BigDecimal totalMoney;
        BigDecimal withdrawalTotalMoney;
        if (OrderConstants.QUARTER.equals(userDataCountRequest.getCountType())) {
            // 用户增长数
            LambdaQueryChainWrapper<User> wrapper = lambdaQuery().ge(User::getCreateTime, startDateStr)
                    .le(User::getCreateTime, endDateStr);
            if (!cityList.isEmpty()) {
                wrapper.in(User::getCity, cityList);
            }
            increaseNumber = wrapper.eq(User::getIsDelete, 0).count();
            // 用户总交易额
            totalMoney = orderService.totalMoneyByQuarter(startDateStr, endDateStr, cityList);
            // 用户提现总额
            withdrawalTotalMoney = withdrawService.withdrawalTotalMoney(cityList,startDateStr,endDateStr);
        } else if (OrderConstants.YEAR.equals(userDataCountRequest.getCountType())) {
            // 用户增长数
            increaseNumber = baseMapper.increaseNumberByYear(cityList);
            // 用户总交易额
            totalMoney = orderService.totalMoneyByYear(cityList);
            // 用户提现总额
            withdrawalTotalMoney = withdrawService.withdrawalTotalMoneyByYear(cityList);
        } else if (OrderConstants.MONTH.equals(userDataCountRequest.getCountType())) {
            // 用户增长数
            increaseNumber = baseMapper.increaseNumberByMonth(cityList);
            // 用户总交易额
            totalMoney = orderService.totalMoneyByMonth(cityList);
            // 用户提现总额
            withdrawalTotalMoney = withdrawService.withdrawalTotalMoneyByMonth(cityList);
        } else {
            // 数量初始化
            increaseNumber = 0L;
            totalMoney = BigDecimal.ZERO;
            withdrawalTotalMoney = BigDecimal.ZERO;
        }
        // 查询结果判断
        increaseNumber = null == increaseNumber ? 0L : increaseNumber;
        totalMoney = null == totalMoney ? BigDecimal.ZERO : totalMoney;
        withdrawalTotalMoney = null == withdrawalTotalMoney ? BigDecimal.ZERO : withdrawalTotalMoney;
        return new UserDataCountVO(userNumber, increaseNumber, totalMoney, withdrawalTotalMoney);
    }
 
    /**
     * 根据当前月份获取当前季度的开始月份
     */
    private static Month getStartMonthOfQuarter(Month currentMonth) {
        if (currentMonth.compareTo(Month.JANUARY) >= 0 && currentMonth.compareTo(Month.MARCH) <= 0) {
            return Month.JANUARY;
        } else if (currentMonth.compareTo(Month.APRIL) >= 0 && currentMonth.compareTo(Month.JUNE) <= 0) {
            return Month.APRIL;
        } else if (currentMonth.compareTo(Month.JULY) >= 0 && currentMonth.compareTo(Month.SEPTEMBER) <= 0) {
            return Month.JULY;
        } else {
            return Month.OCTOBER;
        }
    }
 
}