package com.game.business.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.game.common.annotation.Excel;
import com.game.common.core.domain.BaseEntity;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiModel;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户代理表对象 app_user_agent
 *
 * @author game
 * @date 2024-06-25
 */
@ApiModel(value = "app_user_agent", description = "用户代理表")
@TableName(value= "app_user_agent")
@Data
public class AppUserAgent
        {
private static final long serialVersionUID=1L;

    /**  */
    @ApiModelProperty(value = "")
    @TableId(value = "id" , type = IdType.AUTO)
    private Long id;

    /** 用户id */
    @ApiModelProperty(value = "用户id")
    @Excel(name = "用户id")
    @TableField(value = "user_id")
    private Long userId;

    /** 上级id */
    @ApiModelProperty(value = "上级id")
    @Excel(name = "上级id")
    @TableField(value = "pid")
    private Long pid;

    /** 直属下级id集合 */
    @ApiModelProperty(value = "直属下级id集合")
    @Excel(name = "直属下级id集合")
    @TableField(value = "subset")
    private String subset;

    /** 顶级代理ID */
    @ApiModelProperty(value = "顶级代理ID")
    @Excel(name = "顶级代理ID")
    @TableField(value = "top_id")
    private Long topId;

    /** 直播分成比例 */
    @ApiModelProperty(value = "直播分成比例")
    @Excel(name = "直播分成比例")
    @TableField(value = "live_rate")
    private Double liveRate;

    /** 保底分红比例 */
    @ApiModelProperty(value = "保底分红比例")
    @Excel(name = "保底分红比例")
    @TableField(value = "dividend_guarantee_rate")
    private Double dividendGuaranteeRate;

    /** 审核状态 0 待审核 1 审核/自动 2 未通过  */
    @ApiModelProperty(value = "审核状态 0 待审核 1 审核/自动 2 未通过 ")
    @Excel(name = "审核状态 0 待审核 1 审核/自动 2 未通过 ")
    @TableField(value = "audit_status")
    private String auditStatus;

    /** 创建时间 */
    @ApiModelProperty(value = "创建时间")
    @TableField(value = "create_time")
    private Date createTime;

    /** 更新时间 */
    @ApiModelProperty(value = "更新时间")
    @TableField(value = "update_time")
    private Date updateTime;

    @TableField(exist = false)
    private Double teamWinLose = 0.00;

    @TableField(exist = false)
    private List<AppUserAgent> childrenList = new ArrayList<>();

    @ApiModelProperty(value = "修改类型 0：直播分红比例  1：保底分红比例")
    @TableField(exist = false)
    private Integer upType;

    public static AppUserAgent buildTree(List<AppUserAgent> appUserAgentList,Long parentId,List<AppUserCount> appUserCountList){
        AppUserAgent appUserAgent = new AppUserAgent();
        if(null != appUserAgentList && appUserAgentList.size() > 0){
            for(AppUserAgent child : appUserAgentList){
                if(child.getUserId().equals(parentId)){
                    appUserAgent = child;
                    appUserAgent.setTeamWinLose(deal(appUserCountList,appUserAgent.getUserId()));
                    break;
                }
            }
            buildChildrenList(appUserAgent,appUserAgentList,appUserCountList);
        }
        return appUserAgent;
    }

    public static void buildChildrenList(AppUserAgent appUserAgent,List<AppUserAgent> appUserAgentList,List<AppUserCount> appUserCountList){
        for(AppUserAgent appUserAgentChild : appUserAgentList){
             if(appUserAgentChild.getPid().equals(appUserAgent.getUserId())){
                appUserAgentChild.setTeamWinLose(deal(appUserCountList,appUserAgentChild.getUserId()));
                appUserAgent.getChildrenList().add(appUserAgentChild);
                buildChildrenList(appUserAgentChild,appUserAgentList,appUserCountList);
            }
        }
    }

    private static double deal(List<AppUserCount> appUserCountList,Long userId){
       return appUserCountList.stream().filter(e->e.getUserId().equals(userId)).mapToDouble(e->{
            return e.getGameLoseAmount() - (e.getGameWinAmount() - (e.getGameBetting() - e.getGameLoseAmount())) - e.getGameCommission() - e.getRechargeGive();
        }).sum();
    }

    //递归用户盈亏
    public static double calMoney(AppUserAgent appUserAgent,double money){
        money += appUserAgent.getTeamWinLose();
        if(null != appUserAgent.getChildrenList() && appUserAgent.getChildrenList().size() > 0){
            for(AppUserAgent child : appUserAgent.getChildrenList()){
                money = calMoney(child,money);
            }
        }
        return money;
    }



}