package com.ruoyi.admin.service.impl;
|
|
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> cityIds) {
|
if (null == cityIds || cityIds.isEmpty()) {
|
return new ArrayList<>();
|
}
|
List<UserTrendsVO> userTrendsList = baseMapper.userTrends(cityIds);
|
return null == userTrendsList || userTrendsList.isEmpty() ? new ArrayList<>() : userTrendsList;
|
}
|
|
@Override
|
public Object 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())) {
|
// 用户增长数
|
increaseNumber = lambdaQuery().ge(User::getCreateTime, startDateStr).le(User::getCreateTime, endDateStr)
|
.in(User::getCity, cityList).eq(User::getIsDelete, 0).count();
|
// 用户总交易额
|
totalMoney = orderService.totalMoneyByQuarter(startDateStr, endDateStr, cityList);
|
// 用户提现总额
|
withdrawalTotalMoney = withdrawService.withdrawalTotalMoney(cityList);
|
} 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;
|
}
|
}
|
|
}
|