package com.game.business.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.game.business.domain.*;
import com.game.business.dto.*;
import com.game.business.service.*;
import com.game.business.vo.AppAgentTeamVo;
import com.game.business.vo.AppUserAgentJournalIndexVO;
import com.game.business.vo.AppUserDetailVo;
import com.game.business.vo.AppUserTeamTranserRecordVo;
import com.game.common.annotation.RepeatSubmit;
import com.game.common.constant.CacheConstants;
import com.game.common.constant.finance.FinTranType1;
import com.game.common.constant.finance.FinTranType2;
import com.game.common.constant.finance.FinTranType3;
import com.game.common.core.controller.BaseController;
import com.game.common.core.domain.HttpRet;
import com.game.common.core.domain.HttpRetPageArr;
import com.game.common.core.domain.R;
import com.game.common.core.page.TableDataInfo;
import com.game.common.core.redis.RedisCache;
import com.game.common.utils.MessageUtils;
import com.game.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/business/agent")
@Api(value = "AppGameBettingController", description = "用户代理表接口", tags = {"用户代理表接口"})
public class AppAgentController extends BaseController {

    @Autowired
    private IAppUserCountService appUserCountService;

    @Autowired
    private IAppUserService appUserService;

    @Autowired
    private IAppUserAgentService appUserAgentService;

    @Autowired
    private IFinTranRecordService finTranRecordService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IAppGameCommissionService appGameCommissionService;

    @Autowired
    private IAppUsersCashAccountService appUsersCashAccountService;

    @Autowired
    private IShopBusinessOrderService shopBusinessOrderService;

    @Autowired
    private IShopSubOrderService shopSubOrderService;

    @Autowired
    private IAppGameBettingService appGameBettingService;

    @Autowired
    private IAppUsersChargeService appUsersChargeService;

    @Autowired
    private IAppUsersCashrecordService appUsersCashrecordService;


    /**
     * 首页
     */
    @PostMapping(value = "/index")
    @ApiOperation(value = "用户代理统计", notes = "用户代理统计")
    public HttpRet<AppUserAgentJournalIndexVO> index(@RequestBody AgentIndexDTO agentIndexDto)
    {
        Date now = DateUtil.date();
        Long userId = SecurityUtils.getUserId();
        if(null != agentIndexDto.getUserId()){
            userId = agentIndexDto.getUserId();
        }
        AppUserAgent appUserAgent = appUserAgentService.selectInfo(userId);
        long topUserId = userId;
        if(null != appUserAgent && appUserAgent.getPid() > 0){
            topUserId = appUserAgent.getTopId();
        }
        AppUserAgentJournalIndexVO vo = new AppUserAgentJournalIndexVO();
        Date curDate = DateUtil.date();
        if(agentIndexDto.getType() == 1){
//            curDate = DateUtil.offsetMonth(curDate,-1); //上个月
            //上一个周期
            curDate = DateUtil.offsetDay(curDate,-15);
        }
        String formatP = "yyyy-MM-dd";
        /*Date beginDate = DateUtil.beginOfMonth(curDate);
        Date endDate = DateUtil.endOfMonth(curDate);*/
        int curDay = Integer.parseInt(DateUtil.format(curDate,"dd"));

        Date weekBegin = null;
        Date weekend = null;
        if(curDay <= 15){
            weekBegin = DateUtil.beginOfMonth(curDate);
            weekend = DateUtil.parse(DateUtil.format(curDate,"yyyy-MM-".concat("15").concat(" 23:59:59")),"yyyy-MM-dd HH:mm:ss");
        }else{
            weekBegin = DateUtil.parse(DateUtil.format(curDate,"yyyy-MM-".concat("16").concat(" 00:00:00")),"yyyy-MM-dd HH:mm:ss");
            weekend = DateUtil.endOfMonth(curDate);
        }

        List<AppUserCount> appUserCountList = appUserCountService.getAppUserCountNew(userId,DateUtil.format(weekBegin,formatP),DateUtil.format(weekend,formatP));
        AppUserAgent queryAppUserAgent = new AppUserAgent();
        queryAppUserAgent.setPid(userId);
        //查询用户下级代理
        List<AppUserAgent> appUserAgentList = appUserAgentService.selectAllAgentList(userId,null,null,null);
        if(null == appUserCountList){
            appUserCountList = new ArrayList<>();
        }
        //处理下级代理树形列表
        AppUserAgent teamUser = AppUserAgent.buildTree(appUserAgentList,userId,appUserCountList);
        //佣金
        double money = 0;
        if(null != appUserCountList && appUserCountList.size() > 0){
//            List<AppUserCount> finalAppUserCountList = appUserCountList;
            /*money = appUserAgentList.stream().mapToDouble(appUserAgent1 -> {
                return finalAppUserCountList.stream().mapToDouble(e->{
                    return (e.getGameLoseAmount() - e.getGameWinAmount() - e.getGameCommission() - e.getRechargeGive()) * appUserAgent1.getDividendGuaranteeRate()/100;
                }).sum();
            }).sum();*/
            /*if(teamUser.getChildrenList() != null && teamUser.getChildrenList().size()>0) {
                money = teamUser.getChildrenList().stream().mapToDouble(e -> {
                    double winLose = 0;
                    winLose = AppUserAgent.calMoney(e, winLose);
                    if (winLose > 0) {
                        return BigDecimal.valueOf(winLose * e.getDividendGuaranteeRate() / 100).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                    } else {
                        return 0;
                    }
                }).sum();
            }*/
//            money = dealChildel(teamUser,money);

            //计算当前用户分红
            double winLose = appUserCountList.stream().mapToDouble(e->{
               return e.getGameLoseAmount() - (e.getGameWinAmount() - (e.getGameBetting() - e.getGameLoseAmount())) - e.getGameCommission() - e.getRechargeGive() - e.getRechargeCommission() - e.getWithdrawalCommission();
            }).sum();

            if(winLose > 0){
                money +=  BigDecimal.valueOf(winLose * teamUser.getDividendGuaranteeRate() / 100).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
            }

        }


        /*if(null != appUserAgent.getDividendGuaranteeRate()){
            money = money * appUserAgent.getDividendGuaranteeRate() / 100 ;
        }else{
            money = 0;
        }*/
        //预计分红
        vo.setMyCommission(BigDecimal.valueOf(money>0?money:0).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
        vo.setLiveCommission(BigDecimal.valueOf(appUserCountList.stream().mapToDouble(e->e.getLiveCommission()).sum()).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
        vo.setGameCommission(BigDecimal.valueOf(appUserCountList.stream().mapToDouble(e->e.getGameCommission()).sum()).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
        //团队人数
        vo.setTeamUser(appUserAgentList.stream().collect(Collectors.toList()).size());
        //团队充值
        vo.setTeamRecharge(BigDecimal.valueOf(appUserCountList.stream().mapToDouble(e->{
            return e.getRechargeAmount();
        }).sum()).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
        //团队充值赠送金额
        vo.setRechargeGive(BigDecimal.valueOf(appUserCountList.stream().mapToDouble(e->e.getRechargeGive()).sum()).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
        //团队充值手续费
        vo.setRechargeCommission(BigDecimal.valueOf(appUserCountList.stream().mapToDouble(e->e.getRechargeCommission()).sum()).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
        //团队提现手续费
        vo.setWithdrawalCommission(BigDecimal.valueOf(appUserCountList.stream().mapToDouble(e->e.getWithdrawalCommission()).sum()).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
        //团队投注
        vo.setTeamBetting(BigDecimal.valueOf(appUserCountList.stream().mapToDouble(e->e.getGameBetting()).sum()).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
        //团队直播消费
        vo.setTeamLive(BigDecimal.valueOf(appUserCountList.stream().mapToDouble(e->e.getLiveUseAmount()).sum()).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
        //团队投注赢
        vo.setTeamWin(BigDecimal.valueOf(appUserCountList.stream().mapToDouble(
                e->{
                    return e.getGameWinAmount();
                }
        ).sum()).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
        //团队投注输
        vo.setTeamLose(BigDecimal.valueOf(appUserCountList.stream().mapToDouble(e->e.getGameLoseAmount()).sum()).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
        //活跃人数
        vo.setActiveUser(appUserCountList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()-> new TreeSet<>(Comparator.comparing(f->f.getUserId()))), ArrayList::new)).size());
        //昨日新增
        Date yesterDay = DateUtil.offsetDay(DateUtil.date(),-1);
        vo.setYesterdayUser(appUserAgentList.stream().filter(e->
                compareTime(e.getCreateTime(),yesterDay,yesterDay)
        ).collect(Collectors.toList()).size());
        vo.setMonthUser(appUserAgentList.stream().filter(e->
                compareTime(e.getCreateTime(),DateUtil.beginOfMonth(new Date()),DateUtil.endOfMonth(new Date()))
        ).collect(Collectors.toList()).size());

        //直播收益
        Long finalUserId = userId;
        vo.setLiveProfit(BigDecimal.valueOf(appUserCountList.stream().filter(e->e.getUserId().equals(finalUserId)).mapToDouble(e->e.getLiveProfit()).sum()).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
        //直播邀请奖励
        vo.setLiveInvite(BigDecimal.valueOf(appUserCountList.stream().filter(e->e.getUserId().equals(finalUserId)).mapToDouble(e->e.getLiveInvite()).sum()).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());

        return HttpRet.success(MessageUtils.message("11022"),vo);
    }

    /**
     * 修改直播分成、分红保底
     */
    /*@PostMapping(value = "/updateSetting")
    @ApiOperation(value = "修改直播分成、分红保底", notes = "修改直播分成、分红保底")
    public HttpRet<Boolean> updateSetting(@RequestBody AppAgentEditDto editDto)
    {
        return appUserAgentService.updateSetting(editDto);
    }*/

    /**
     * 团队列表
     */
    @PostMapping(value = "/teamList")
    @ApiOperation(value = "团队列表", notes = "团队列表")
    public HttpRetPageArr<AppAgentTeamVo> teamList(@RequestBody AppAgentTeamDto teamDto)
    {

//        startPageBySize(10);//每页最多10条
        List<AppAgentTeamVo> list = appUserAgentService.teamList(teamDto);
        return HttpRetPageArr.success(MessageUtils.message("11022"),getDataTable(list));
    }

    /**
     * 团队转账
     */
    @PostMapping(value = "/transfer")
    @ApiOperation(value = "团队转账", notes = "团队转账")
    @RepeatSubmit(message = "频繁操作")
    public HttpRet<Boolean> transfer(@RequestBody @Validated AppUserTeamTranserDto transerDto)
    {
        Long userId = SecurityUtils.getUserId();
       //查询当前用户代理信息
       LambdaQueryWrapper<AppUserAgent> queryWrapper = new LambdaQueryWrapper<>();
       queryWrapper.eq(AppUserAgent::getUserId,userId);
       queryWrapper.eq(AppUserAgent::getAuditStatus,1);
       AppUserAgent appUserAgent = appUserAgentService.getOne(queryWrapper);
       if(null == appUserAgent){
           return HttpRet.fail("转账失败，受益人不是代理");
       }
//       if(appUserAgent.getPid().longValue() == 0 || isTwoLevel(appUserAgent.getPid())){
           List<AppUserAgent> appUserAgentList = appUserAgentService.selectAllAgentList(userId,transerDto.getToUserId(),null,null);
           if(null == appUserAgentList || appUserAgentList.size() < 2){
               return HttpRet.fail("转账失败，只能给团队成员转账");
           }
      /* }else{
            if(!isTeam(userId,transerDto.getToUserId())){
                return HttpRet.fail("转账失败，只能给下级成员转账");
            }
       }*/
       return finTranRecordService.transfer(transerDto);
    }

    /**
     * 金币转账
     */
    @PostMapping(value = "/coinTransfer")
    @ApiOperation(value = "金币转账", notes = "金币转账")
    @RepeatSubmit(message = "频繁操作")
    public HttpRet<Boolean> coinTransfer(@RequestBody @Validated AppUserTeamTranserDto transerDto)
    {
       return finTranRecordService.coinTransfer(transerDto);
    }

    /**
     * 团队转账记录
     */
    @PostMapping(value = "/transferRecord")
    @ApiOperation(value = "团队转账记录(分页 链接上拼pageSize和pageNum)", notes = "团队转账记录（分页 链接上拼pageSize和pageNum）")
    public HttpRetPageArr<AppUserTeamTranserRecordVo> transferRecord(@RequestBody @Validated AppUserTeamTranserRecordDto transerDto)
    {
        startPage();
        List<FinTranRecord> list = finTranRecordService.transferRecord(transerDto);
        TableDataInfo<FinTranRecord> tableDataInfo = getDataTable(list);
        TableDataInfo resultDataInfo = new TableDataInfo();
        BeanUtils.copyProperties(tableDataInfo,resultDataInfo);
        if(null != tableDataInfo.getRows() && tableDataInfo.getRows().size() > 0){
            List<AppUser> appUsers = null;
            if(0 == transerDto.getType()){
                //转出
                appUsers = appUserService.selectListByIds(
                        tableDataInfo.getRows().stream().map(e->e.getToUid()).collect(Collectors.toList())
                );
            }else{
                //转入
                appUsers = appUserService.selectListByIds(
                        tableDataInfo.getRows().stream().map(e->e.getFromUid()).collect(Collectors.toList())
                );
            }
            Map<String,AppUser> userMap = new HashMap<>();
            if(null != appUsers && appUsers.size() > 0){
                userMap = appUsers.stream().collect(Collectors.toMap(e->{return String.valueOf(e.getUserid());}, Function.identity(),(k1,k2)->k2));
            }
            Map<String, AppUser> finalUserMap = userMap;
            resultDataInfo.setRows(tableDataInfo.getRows().stream().map(e->{
                AppUserTeamTranserRecordVo vo = new AppUserTeamTranserRecordVo();
                vo.setMoney(e.getDiamondCoinChange());
                if(e.getCurrencyType() == 2){
                    vo.setMoney(e.getCoinChange());
                }
                vo.setTime(e.getCreateTime());
                AppUser appUser = null;
                if(0 == transerDto.getType()){
                    //转出
                    long userId = e.getToUid();
                    appUser = finalUserMap.get(String.valueOf(userId));
                    vo.setUserId(userId);
                    if(null != appUser){
                        vo.setNickName(appUser.getUsername());
                    }
                }else{
                    //转入
                    long userId = e.getFromUid();
                    appUser = finalUserMap.get(String.valueOf(userId));
                    vo.setUserId(userId);
                    if(null != appUser){
                        vo.setNickName(appUser.getUsername());
                    }
                }
                return vo;
            }).collect(Collectors.toList()));
        }else{
            resultDataInfo.setRows(new ArrayList());
        }
        return HttpRetPageArr.success(MessageUtils.message("11022"),resultDataInfo);
    }

    /**
     * 查询用户详情
     */
    @GetMapping(value = "/userDetail")
    @ApiOperation(value = "查询用户详情", notes = "查询用户详情")
    public R<AppUserDetailVo> selectSuperList(Long userId)
    {
        AppUserDetailVo appUserDetailVo = new AppUserDetailVo();
        if(null == userId){
            return R.ok(appUserDetailVo);
        }
        AppUser appUser = appUserService.selectAppUserByUserid(userId);
        if(null == appUser){
            return R.ok(appUserDetailVo);
        }
        List<AppUserAgent> list = appUserAgentService.selectSuperList(userId);
        appUserDetailVo.setSuperUserList(list);
        appUserDetailVo.setDownUserList(appUserAgentService.selectAllAgentList(userId,null,null,null));
        AppUserAgent appUserAgent = null;
        if(null != list && list.size() > 0){
            List<AppUserAgent> curList = list.stream().filter(e->e.getUserId().equals(userId)).collect(Collectors.toList());
            appUserAgent = null!=curList?curList.get(0):null;
        }
        if(null != appUserAgent) {
            appUserDetailVo.setLiveRate(appUserAgent.getLiveRate());
            appUserDetailVo.setDividendGuaranteeRate(appUserAgent.getDividendGuaranteeRate());
        }

        List<AppGameCommission> gameCommissionList = appGameCommissionService.getByIds(Arrays.asList(userId));
        appUserDetailVo.setGameCommissionList(gameCommissionList);

        Date nowDate = new Date();
        int curDay = Integer.parseInt(DateUtil.format(nowDate,"dd"));

        Date beginDate = null;
        Date endDate = null;
        if(curDay <= 15){
            beginDate = DateUtil.beginOfMonth(nowDate);
            endDate = DateUtil.parse(DateUtil.format(nowDate,"yyyy-MM-".concat("15").concat(" 23:59:59")),"yyyy-MM-dd HH:mm:ss");
        }else{
            beginDate = DateUtil.parse(DateUtil.format(nowDate,"yyyy-MM-".concat("15").concat(" 00:00:00")),"yyyy-MM-dd HH:mm:ss");
            endDate = DateUtil.endOfMonth(nowDate);
        }
        //游戏流水
        List<AppGameBetting> gameBettingList = appGameBettingService.selectListByDate(userId,
                DateUtil.format(beginDate,"yyyy-MM-dd").concat(" 00:00:00"),
                DateUtil.format(endDate,"yyyy-MM-dd").concat(" 23:59:59"),
                null,null,null,null);
        //用户总投注流水
        appUserDetailVo.setTotalBetting(appGameBettingService.sumBettingAmount(userId,0,null,null,null));
        //用户金币总投注流水
        appUserDetailVo.setTotalCoinBetting(appGameBettingService.sumBettingAmount(userId,1,null,null,null));

        //当日游戏投注
        appUserDetailVo.setTodayBetting(gameBettingList.stream().filter(e->{
           return DateUtil.format(e.getCreateTime(),"yyyy-MM-dd").equals(DateUtil.format(nowDate,"yyyy-MM-dd")) &&
                   e.getBettingType().intValue() == 0;
        }).mapToDouble(AppGameBetting::getBettingAmount).sum());
        appUserDetailVo.setTodayCoinBetting(gameBettingList.stream().filter(e->{
           return DateUtil.format(e.getCreateTime(),"yyyy-MM-dd").equals(DateUtil.format(nowDate,"yyyy-MM-dd")) &&
                   e.getBettingType().intValue() ==1;
        }).mapToDouble(AppGameBetting::getBettingAmount).sum());

        //本周游戏投注
        appUserDetailVo.setWeekBetting(gameBettingList.stream().filter(e->e.getBettingType().intValue() == 0)
                .mapToDouble(AppGameBetting::getBettingAmount).sum());
        appUserDetailVo.setWeekCoinBetting(gameBettingList.stream().filter(e->e.getBettingType().intValue() == 1)
                .mapToDouble(AppGameBetting::getBettingAmount).sum());

        //当日游戏赢
        appUserDetailVo.setTodayWin(gameBettingList.stream().filter(e->{
            return DateUtil.format(e.getCreateTime(),"yyyy-MM-dd").equals(DateUtil.format(nowDate,"yyyy-MM-dd")) &&
                    e.getIsWinning().intValue() == 1 && e.getBettingType().intValue() == 0;
        }).mapToDouble(e->{return (e.getBettingMultiple() * e.getBettingAmount()) - e.getBettingAmount();}).sum());
        appUserDetailVo.setTodayCoinWin(gameBettingList.stream().filter(e->{
            return DateUtil.format(e.getCreateTime(),"yyyy-MM-dd").equals(DateUtil.format(nowDate,"yyyy-MM-dd")) &&
                    e.getIsWinning().intValue() == 1 && e.getBettingType().intValue() == 1;
        }).mapToDouble(e->{return (e.getBettingMultiple() * e.getBettingAmount()) - e.getBettingAmount();}).sum());

        //本周游戏赢
        appUserDetailVo.setWeekWin(gameBettingList.stream().filter(e->{
            return e.getIsWinning().intValue() == 1 && e.getBettingType().intValue() == 0;
        }).mapToDouble(e->{return (e.getBettingMultiple() * e.getBettingAmount()) - e.getBettingAmount();}).sum());
        appUserDetailVo.setWeekCoinWin(gameBettingList.stream().filter(e->{
            return e.getIsWinning().intValue() == 1 && e.getBettingType().intValue() == 1;
        }).mapToDouble(e->{return (e.getBettingMultiple() * e.getBettingAmount()) - e.getBettingAmount();}).sum());

        //当日游戏输
        appUserDetailVo.setTodayLose(gameBettingList.stream().filter(e->{
            return DateUtil.format(e.getCreateTime(),"yyyy-MM-dd").equals(DateUtil.format(nowDate,"yyyy-MM-dd")) &&
                    e.getIsWinning().intValue() == 2 && e.getBettingType().intValue() == 0;
        }).mapToDouble(e->{return e.getBettingAmount();}).sum());
        appUserDetailVo.setTodayCoinLose(gameBettingList.stream().filter(e->{
            return DateUtil.format(e.getCreateTime(),"yyyy-MM-dd").equals(DateUtil.format(nowDate,"yyyy-MM-dd")) &&
                    e.getIsWinning().intValue() == 2 && e.getBettingType().intValue() == 1;
        }).mapToDouble(e->{return e.getBettingAmount();}).sum());

        //本周游戏输
        appUserDetailVo.setWeekLose(gameBettingList.stream().filter(e->{
            return e.getIsWinning().intValue() == 2 && e.getBettingType().intValue() == 0;
        }).mapToDouble(e->{return e.getBettingAmount();}).sum());
        appUserDetailVo.setWeekCoinLose(gameBettingList.stream().filter(e->{
            return e.getIsWinning().intValue() == 2 && e.getBettingType().intValue() == 1;
        }).mapToDouble(e->{return e.getBettingAmount();}).sum());

        List<AppUsersCharge> appUsersChargeList = appUsersChargeService.getChargeList(userId,
                DateUtil.format(beginDate,"yyyy-MM-dd").concat(" 00:00:00"),
                DateUtil.format(endDate,"yyyy-MM-dd").concat(" 23:59:59"));
        //总充值
        appUserDetailVo.setTotalCharge(appUsersChargeService.sumCharge(userId));
        //当日充值
        appUserDetailVo.setTodayCharge(appUsersChargeList.stream().filter(e->{
           return DateUtil.format(e.getAddtime(),"yyyy-MM-dd").equals(DateUtil.format(nowDate,"yyyy-MM-dd"));
        }).mapToDouble(e->{return e.getCoin().doubleValue();}).sum());

        //本周充值
        appUserDetailVo.setWeekCharge(appUsersChargeList.stream().mapToDouble(e->{return e.getCoin().doubleValue();}).sum());

        //总提现
        appUserDetailVo.setTotalWithdraw(appUsersCashrecordService.sumWithdraw(userId));

        //提现
        List<AppUsersCashrecord> appUsersCashrecords = appUsersCashrecordService.getUserCashList(userId,
                DateUtil.format(beginDate,"yyyy-MM-dd").concat(" 00:00:00"),
                DateUtil.format(endDate,"yyyy-MM-dd").concat(" 23:59:59"));
        //当日提现
        appUserDetailVo.setTodayWithdraw(appUsersCashrecords.stream().filter(e->{
            return DateUtil.format(e.getAddtime(),"yyyy-MM-dd").equals(DateUtil.format(nowDate,"yyyy-MM-dd"));
        }).mapToDouble(e->{return e.getVotes().doubleValue();}).sum());

        //本周提现
        appUserDetailVo.setWeekWithdraw(appUsersCashrecords.stream().mapToDouble(e->{return e.getVotes().doubleValue();}).sum());


        //礼物
        Date giftBeginTime = DateUtil.offsetDay(DateUtil.date(),-30);
        Date giftEndTime = DateUtil.date();
        LambdaQueryWrapper<FinTranRecord> finTranWrapper = new LambdaQueryWrapper<>();
        finTranWrapper.eq(FinTranRecord::getTranType1, FinTranType1.U_Income_Coin.getType());
        finTranWrapper.eq(FinTranRecord::getTranType2, FinTranType2.REWARD_Income.getType());
        finTranWrapper.eq(FinTranRecord::getTranType3, FinTranType3.RECOMMEND_ADD_AMOUNT.getType());
        finTranWrapper.eq(FinTranRecord::getUid,userId);
        finTranWrapper.between(FinTranRecord::getCreateTime,DateUtil.format(giftBeginTime,"yyyy-MM-dd").concat(" 00:00:00"),
                DateUtil.format(giftEndTime,"yyyy-MM-dd").concat(" 23:59:59"));
        List<FinTranRecord> giftList = finTranRecordService.selecListByWrapper(finTranWrapper);
        if(null == giftList){
            giftList = new ArrayList<>();
        }
        appUserDetailVo.setTotalGift(giftList.stream().mapToDouble(FinTranRecord::getCoinChange).sum());
        appUserDetailVo.setTodayGift(giftList.stream().filter(e->DateUtil.format(DateUtil.date(),
                "yyyy-MM-dd").equals(DateUtil.format(e.getCreateTime(),"yyyy-MM-dd"))).mapToDouble(FinTranRecord::getCoinChange).sum());
        Date finalEndDate = endDate;
        Date finalBeginDate = beginDate;
        appUserDetailVo.setWeekGift(giftList.stream().filter(e->e.getCreateTime().getTime() >= finalBeginDate.getTime() &&
                e.getCreateTime().getTime() <= finalEndDate.getTime()).mapToDouble(FinTranRecord::getCoinChange).sum());


       /* AppUser queryIpUser = new AppUser();
        queryIpUser.setIpaddr(appUser.getIpaddr());
        List<AppUser> ipUserList = appUserService.selectAppUserList(queryIpUser);
        appUserDetailVo.setIpUserList(ipUserList);*/

        /*AppUsersCashAccount queryCashAccount = new AppUsersCashAccount();
        queryCashAccount.setUid(userId);
        List<AppUsersCashAccount> usersCashAccounts = appUsersCashAccountService.selectAppUsersCashAccountList(queryCashAccount);
        appUserDetailVo.setUsersCashAccounts(usersCashAccounts);*/

        /*ShopBusinessOrder queryOrder = new ShopBusinessOrder();
        queryOrder.setUid(userId);
        List<ShopBusinessOrder> businessOrders = shopBusinessOrderService.selectShopBusinessOrderList(queryOrder);
        if(null != businessOrders && businessOrders.size() > 0){
            businessOrders.forEach(e->{
                ShopSubOrder query = new ShopSubOrder();
                query.setBusinessOrderId(e.getId());
                List<ShopSubOrder> shopSubOrders = shopSubOrderService.selectShopSubOrderList(query);
                if(null != shopSubOrders && shopSubOrders.size() > 0){
                    String productNames = "";
                    for(ShopSubOrder shopSubOrder : shopSubOrders){
                        if(StringUtils.isNotBlank(productNames)){
                            productNames = productNames.concat(",");
                        }
                        productNames = productNames.concat(shopSubOrder.getGoodsName());
                    }
                    e.setProductNames(productNames);
                }
            });
        }
        appUserDetailVo.setBusinessOrders(businessOrders);*/
        appUserDetailVo.setUserId(userId);
        return R.ok(appUserDetailVo,MessageUtils.message("11022"));
    }

    //查询用户是否为下级代理
    private boolean isTeam(Long pid,Long userId){
        //查询当前用户代理信息
        LambdaQueryWrapper<AppUserAgent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppUserAgent::getUserId,userId);
        queryWrapper.eq(AppUserAgent::getAuditStatus,1);
        AppUserAgent appUserAgent = appUserAgentService.getOne(queryWrapper);
        if(null != appUserAgent && appUserAgent.getPid().longValue() == pid){
            return true;
        }
        return false;
    }

    /**
     * 查询是否为第二级代理
     * */
    private boolean isTwoLevel(Long userId){
        LambdaQueryWrapper<AppUserAgent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppUserAgent::getUserId,userId);
        AppUserAgent appUserAgent = appUserAgentService.getOne(queryWrapper);
        if(appUserAgent.getPid().longValue() == 0){
            return true;
        }
        return false;
    }

    private boolean compareTime(Date curTime,Date beginTime,Date endTime){
        if(null == curTime){
            return false;
        }
        beginTime = DateUtil.parse(DateUtil.format(beginTime,"yyyy-MM-dd").concat(" 00:00:00"));
        endTime = DateUtil.parse(DateUtil.format(endTime,"yyyy-MM-dd").concat(" 23:59:59"));
        return curTime.getTime() >= beginTime.getTime() && curTime.getTime() <= endTime.getTime();
    }

    public double dealChildel(AppUserAgent appUserAgent,double money){
        double winlose = 0;
        winlose = AppUserAgent.calMoney(appUserAgent,winlose);
        if(winlose > 0){
            money += winlose * appUserAgent.getDividendGuaranteeRate() / 100;
        }
        if(null != appUserAgent.getChildrenList() && appUserAgent.getChildrenList().size() > 0){
            for(AppUserAgent child : appUserAgent.getChildrenList()){
                money = dealChildel(child,money);
            }
        }
        return money;
    }
}
