1
zj
2024-06-03 09206aedcfdf30050123e99f2af0a192ebad1de4
src/main/java/com/nq/service/impl/SiteAdminServiceImpl.java
@@ -1,19 +1,16 @@
package com.nq.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nq.dao.SiteAdminMapper;
import com.nq.dao.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nq.common.ServerResponse;
import com.nq.dao.UserMapper;
import com.nq.dao.UserRechargeMapper;
import com.nq.dao.UserWithdrawMapper;
import com.nq.pojo.SiteAdmin;
import com.nq.pojo.UserRecharge;
import com.nq.pojo.*;
import com.nq.service.IAgentUserService;
@@ -29,6 +26,7 @@
import com.nq.service.IUserWithdrawService;
import com.nq.utils.ConverterUtil;
import com.nq.utils.PropertiesUtil;
import com.nq.utils.SymmetricCryptoUtil;
@@ -41,14 +39,17 @@
import java.math.BigDecimal;
import java.util.Date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.nq.vo.agent.AgentUserNodeVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
@@ -58,6 +59,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xmlunit.util.Convert;
@Service("iSiteAdminServiceImpl")
@@ -103,6 +105,11 @@
    @Resource
    UserMapper userMapper;
    @Autowired
    UserWithdrawMapper userWithdrawMapper;
    @Autowired
    AgentUserMapper agentUserMapper;
    public ServerResponse login(String adminPhone, String adminPwd, String verifyCode, HttpServletRequest request) {
@@ -369,6 +376,257 @@
    }
    @Override
    public ServerResponse moneyCount(String agentId,String startTime, String entTime) {
        BigDecimal todayRechargeAmount = BigDecimal.ZERO;//今日充值金额
        BigDecimal todayWithdrawAmount = BigDecimal.ZERO;//今日提现金额
        Long todayRegister = 0L;//今日注册
        Long todayWithdraw = 0L;//今日充值总人数
        BigDecimal rechargeTotalAmount = BigDecimal.ZERO;//总充值金额
        BigDecimal withdrawalTotalAmount = BigDecimal.ZERO;//总提现金额
        Map<String, Object> map = new HashMap<>();
        map.put("todayRechargeAmount", todayRechargeAmount);
        map.put("todayWithdrawAmount", todayWithdrawAmount);
        map.put("todayRegister", todayRegister);
        map.put("todayWithdraw", todayWithdraw);
        map.put("rechargeTotalAmount", rechargeTotalAmount);
        map.put("withdrawalTotalAmount", withdrawalTotalAmount);
        //今日开始结束时间
        LocalDate currentDate  = LocalDate.now();
        LocalDate  nextDay = currentDate.plusDays(1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String start = currentDate.format(formatter);
        String end = nextDay.format(formatter);
        List<Integer> userIds  = null;
        if(StringUtils.isNotEmpty(agentId)){
            //当前代理
            AgentUser agentUser = agentUserMapper.selectById(agentId);
            if(null == agentUser){
                return ServerResponse.createBySuccess(map);
            }
            List<AgentUser> lowerAgentUsers = agentUserMapper.selectList(new LambdaQueryWrapper<AgentUser>());
            AgentUserNodeVO userNodeVO = ConverterUtil.convert(agentUser, AgentUserNodeVO.class);
            List<AgentUserNodeVO> agentUserNodeVOS = ConverterUtil.convertToList(lowerAgentUsers, AgentUserNodeVO.class);
            List<AgentUserNodeVO> nodeJson = getNodeJson(userNodeVO, agentUserNodeVOS);
            userNodeVO.setChildList(nodeJson);
            //当前节点和子节点的id
            List<Integer>  ids  =  getAllChildrenIds(userNodeVO);
            //查询代理id下面所有的用户
            List<User> users = userMapper.selectList(new LambdaQueryWrapper<User>().in(User::getAgentId, ids));
            userIds = users.stream().map(User::getId).collect(Collectors.toList());
        }
        //今日充值
        List<UserRecharge> todayRecharges = userRechargeMapper.selectList(new LambdaQueryWrapper<UserRecharge>().eq(UserRecharge::getOrderStatus, 1)
                .ge(UserRecharge::getPayTime, start)
                .le(UserRecharge::getPayTime, end)
                .in(CollectionUtil.isNotEmpty(userIds),UserRecharge::getUserId,userIds));
        //今日提现
        List<UserWithdraw> todayWithdraws = userWithdrawMapper.selectList(new LambdaQueryWrapper<UserWithdraw>().eq(UserWithdraw::getWithStatus, 1)
                .ge(UserWithdraw::getTransTime, start)
                .le(UserWithdraw::getTransTime, end)
                .in(CollectionUtil.isNotEmpty(userIds),UserWithdraw::getUserId,userIds));
        //总充值
        List<UserRecharge> userRecharges = userRechargeMapper.selectList(new LambdaQueryWrapper<UserRecharge>().eq(UserRecharge::getOrderStatus, 1)
                .ge(StringUtils.isNotBlank(startTime), UserRecharge::getPayTime, startTime)
                .le(StringUtils.isNotBlank(entTime), UserRecharge::getPayTime, entTime)
                .in(CollectionUtil.isNotEmpty(userIds),UserRecharge::getUserId,userIds));
        //总提现
        List<UserWithdraw> userWithdraws = userWithdrawMapper.selectList(new LambdaQueryWrapper<UserWithdraw>().eq(UserWithdraw::getWithStatus, 1)
                .ge(StringUtils.isNotBlank(startTime), UserWithdraw::getTransTime, startTime)
                .le(StringUtils.isNotBlank(entTime), UserWithdraw::getTransTime, entTime)
                .in(CollectionUtil.isNotEmpty(userIds),UserWithdraw::getUserId,userIds));
        //今日注册数量
        todayRegister = userMapper.selectCount(new LambdaQueryWrapper<User>()
                .ge(User::getRegTime, start)
                .le(User::getRegTime, end)
                .in(CollectionUtil.isNotEmpty(userIds),User::getId,userIds));
        for (UserRecharge userRecharge : todayRecharges) {
            todayRechargeAmount = todayRechargeAmount.add(userRecharge.getPayAmt());
        }
        for (UserWithdraw userWithdraw : todayWithdraws) {
            todayWithdrawAmount = todayWithdrawAmount.add(userWithdraw.getWithAmt());
        }
        for (UserRecharge userRecharge : userRecharges) {
            rechargeTotalAmount = rechargeTotalAmount.add(userRecharge.getPayAmt());
        }
        for (UserWithdraw userWithdraw : userWithdraws) {
            withdrawalTotalAmount = withdrawalTotalAmount.add(userWithdraw.getWithAmt());
        }
        List<UserRecharge> distinctCustomers = todayRecharges.stream()
                .collect(Collectors.toMap(UserRecharge::getUserId, c -> c, (c1, c2) -> c1))
                .values().stream()
                .collect(Collectors.toList());
        todayWithdraw = Long.valueOf(distinctCustomers.size());
        map.put("todayRechargeAmount", todayRechargeAmount);
        map.put("todayWithdrawAmount", todayWithdrawAmount);
        map.put("todayRegister", todayRegister);
        map.put("todayWithdraw", todayWithdraw);
        map.put("rechargeTotalAmount", rechargeTotalAmount);
        map.put("withdrawalTotalAmount", withdrawalTotalAmount);
        return ServerResponse.createBySuccess(map);
    }
    @Override
    public ServerResponse totalAgencyFunds(String agentId) {
        BigDecimal inTotalAmount = BigDecimal.ZERO;
        BigDecimal usTotalAmount = BigDecimal.ZERO;
        BigDecimal inTotalWithdrawAmount = BigDecimal.ZERO;
        BigDecimal usTotalWithdrawAmount = BigDecimal.ZERO;
        Map<String, Object> map = new HashMap<>();
        Long todayRegister = 0L;//今日注册
        map.put("todayRegister", todayRegister);
        //入金
        map.put("inTotalAmount", inTotalAmount);
        map.put("usTotalAmount", usTotalAmount);
        //出金
        map.put("inTotalWithdrawAmount", inTotalWithdrawAmount);
        map.put("usTotalWithdrawAmount", usTotalWithdrawAmount);
        //查询所有代理用户(管理员)
        List<AgentUser> agentUsers = agentUserMapper.selectList(new LambdaQueryWrapper<AgentUser>()
                .eq(AgentUser::getAgentLevel,0)
                .eq(StringUtils.isNotBlank(agentId),AgentUser::getId,agentId));
        List<AgentUser> lowerAgentUsers = agentUserMapper.selectList(new LambdaQueryWrapper<AgentUser>()
                .ne(AgentUser::getAgentLevel,0));
        if(CollectionUtil.isEmpty(agentUsers)){
            return ServerResponse.createBySuccess(map);
        }
        //今日开始结束时间
        LocalDate currentDate  = LocalDate.now();
        LocalDate  nextDay = currentDate.plusDays(1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String start = currentDate.format(formatter);
        String end = nextDay.format(formatter);
        for (AgentUser agentUser : agentUsers) {
            AgentUserNodeVO userNodeVO = ConverterUtil.convert(agentUser, AgentUserNodeVO.class);
            List<AgentUserNodeVO> agentUserNodeVOS = ConverterUtil.convertToList(lowerAgentUsers, AgentUserNodeVO.class);
            List<AgentUserNodeVO> nodeJson = getNodeJson(userNodeVO, agentUserNodeVOS);
            userNodeVO.setChildList(nodeJson);
            //当前节点和子节点的id
            List<Integer>  ids  =  getAllChildrenIds(userNodeVO);
            //查询代理id下面所有的用户
            List<User> users = userMapper.selectList(new LambdaQueryWrapper<User>().in(User::getAgentId, ids));
            //查询用户充值
            List<Integer> userIds = users.stream().map(User::getId).collect(Collectors.toList());
            if(CollectionUtil.isEmpty(userIds)){
                continue;
            }
            //今日注册数量
            todayRegister = userMapper.selectCount(new LambdaQueryWrapper<User>()
                    .ge(User::getRegTime, start)
                    .le(User::getRegTime, end)
                    .in(CollectionUtil.isNotEmpty(userIds),User::getId,userIds));
            List<UserRecharge> userRecharges = userRechargeMapper.selectList(new LambdaQueryWrapper<UserRecharge>()
                    .eq(UserRecharge::getOrderStatus, 1)
                    .in(UserRecharge::getUserId,userIds));
            Map<String, List<UserRecharge>> typeList = userRecharges.stream().collect(Collectors.groupingBy(UserRecharge::getAssetsType));
            List<UserRecharge> inUserRecharge = typeList.get("IN");
            List<UserRecharge> usUserRecharge = typeList.get("US");
            if(CollectionUtil.isNotEmpty(inUserRecharge)){
                for (UserRecharge userRecharge : inUserRecharge) {
                    inTotalAmount = inTotalAmount.add(userRecharge.getPayAmt());
                }
            }
            if(CollectionUtil.isNotEmpty(usUserRecharge)){
                for (UserRecharge userRecharge : usUserRecharge) {
                    usTotalAmount = usTotalAmount.add(userRecharge.getPayAmt());
                }
            }
            //总提现
            List<UserWithdraw> userWithdraws = userWithdrawMapper.selectList(new LambdaQueryWrapper<UserWithdraw>()
                    .eq(UserWithdraw::getWithStatus, 1)
                    .in(CollectionUtil.isNotEmpty(userIds),UserWithdraw::getUserId,userIds));
            Map<String, List<UserWithdraw>> typeWithdrawsList = userWithdraws.stream().collect(Collectors.groupingBy(UserWithdraw::getAssetsType));
            List<UserWithdraw> inUserWithdraws = typeWithdrawsList.get("IN");
            List<UserWithdraw> usUserWithdraws = typeWithdrawsList.get("US");
            if(CollectionUtil.isNotEmpty(inUserWithdraws)){
                for (UserWithdraw userWithdraw : inUserWithdraws) {
                    inTotalWithdrawAmount = inTotalWithdrawAmount.add(userWithdraw.getWithAmt());
                }
            }
            if(CollectionUtil.isNotEmpty(usUserWithdraws)){
                for (UserWithdraw userWithdraw : usUserWithdraws) {
                    usTotalWithdrawAmount = usTotalWithdrawAmount.add(userWithdraw.getWithAmt());
                }
            }
        }
        map.put("todayRegister", todayRegister);
        map.put("inTotalAmount", inTotalAmount);
        map.put("usTotalAmount", usTotalAmount);
        map.put("inTotalWithdrawAmount", inTotalWithdrawAmount);
        map.put("usTotalWithdrawAmount", usTotalWithdrawAmount);
        return ServerResponse.createBySuccess(map);
    }
    public  static  List<Integer>  getAllChildrenIds(AgentUserNodeVO  parent)  {
        List<Integer>  allChildrenIds  =  new  ArrayList<>();
        getAllChildrenIdsHelper(parent,  allChildrenIds);
        return  allChildrenIds;
    }
    private  static  void  getAllChildrenIdsHelper(AgentUserNodeVO  node,  List<Integer>  allChildrenIds)  {
        allChildrenIds.add(node.getId());
        if(CollectionUtil.isNotEmpty(node.getChildList())){
            for  (AgentUserNodeVO  child  :  node.getChildList())  {
                getAllChildrenIdsHelper(child,  allChildrenIds);
            }
        }
    }
    public List<AgentUserNodeVO> getNodeJson(AgentUserNodeVO agentUser, List<AgentUserNodeVO> nodes){
        //当前层级当前点下的所有子节点
        List<AgentUserNodeVO> childList = getChildNodes(agentUser.getId(),nodes);
        List<AgentUserNodeVO> list = new ArrayList<>();
        childList.forEach(f->{
            List<AgentUserNodeVO> childs = getNodeJson(f,nodes);  //递归调用该方法
            if(!childs.isEmpty()) {
                f.setChildList(childs);
            }
            list.add(f);
        });
        return list;
    }
    /**
     * 获取当前节点的所有子节点
     * @param nodeId
     * @param nodes
     * @return
     */
    public List<AgentUserNodeVO> getChildNodes(Integer nodeId, List<AgentUserNodeVO> nodes){
        List<AgentUserNodeVO> list = new ArrayList<>();
        nodes.forEach(f->{
            if(f.getParentId().equals(nodeId)){
                list.add(f);
            }
        });
        return list;
    }
    public String getDate(){
        LocalDate currentDate  =  LocalDate.now();
        DateTimeFormatter  formatter  =  DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return currentDate.format(formatter);
    }
    public SiteAdmin findAdminByName(String name) {
        return this.siteAdminMapper.findAdminByName(name);