package com.red.circle.other.adapter.app.team;

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.common.business.dto.cmd.app.AppFlowCmd;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.app.dto.clientobject.TeamBdMemberCO;
import com.red.circle.other.app.dto.clientobject.team.*;
import com.red.circle.other.app.dto.clientobject.team.bd.BdDetailItemCO;
import com.red.circle.other.app.dto.cmd.activity.LotteryWithdrawCmd;
import com.red.circle.other.app.dto.cmd.team.*;
import com.red.circle.other.app.dto.cmd.team.bd.BdDetailsQryCmd;
import com.red.circle.other.app.service.team.TeamBdHistoryService;
import com.red.circle.other.app.service.team.TeamBdService;
import com.red.circle.other.app.service.TeamBdBillService;
import com.red.circle.other.app.dto.cmd.TeamBdMemberBillCmd;
import com.red.circle.other.app.dto.clientobject.TeamBdMemberBillCO;
import com.red.circle.other.infra.database.mongo.entity.team.bd.MessageInviteGroup;
import java.util.List;

import com.red.circle.other.inner.model.cmd.team.bd.BdTeamPageQryCmd;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * BD团队 前端控制器.
 * </p>
 *
 * @author pengshigang
 * @eo.api-type http
 * @eo.groupName 团队服务.BD团队
 * @eo.path /team/bd
 * @since 2022-06-07
 */
@Slf4j
@RestController
@RequestMapping(value = "/team/bd", produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class TeamBdRestController extends BaseController {

  private final TeamBdService teamBdService;
  private final TeamBdBillService teamBdBillService;
  private final TeamBdHistoryService teamBdHistoryService;

  /**
   * 获得bd角色.
   *
   * @eo.name 获得bd角色.
   * @eo.url /role
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/role")
  public String getBdRole(AppExtCommand cmd) {
    return teamBdService.getBdRole(cmd);
  }

  /**
   * BD进入系统.
   *
   * @eo.name BD进入系统.
   * @eo.url /entry-sys
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/entry-sys")
  public EntryBdCO bdEntrySys(AppExtCommand cmd) {
    return teamBdService.bdEntrySys(cmd);
  }

  /**
   * BD Leader名下BD列表.
   *
   * @eo.name BD Leader名下BD列表.
   * @eo.url /leader-bd/list
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/leader-bd/list")
  public List<TeamDbMemberCO> listBdMembers(AppFlowCmd cmd) {
    return teamBdService.listBdMembers(cmd);
  }

  /**
   * BD名下代理分页.
   *
   * @eo.name BD名下代理分页.
   * @eo.url /bd-agent/list
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/bd-agent/list")
  public List<TeamDbAgentMemberCO> pageBdTeam(AppFlowCmd cmd) {
    return teamBdService.listBdAgent(cmd);
  }

  /**
   * BD名下代理情况统计.
   *
   * @eo.name BD名下代理情况统计.
   * @eo.url /agent-count
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/agent-count")
  public BdAgentCountCO agentCount(AppExtCommand cmd) {
    return teamBdService.getBdAgentCount(cmd);
  }

    /**
     * BD名下代理情况统计(上个月)
     */
    @GetMapping("/agent-count-last")
    public BdAgentCountCO agentCountLastMonth(AppExtCommand cmd) {
        return teamBdService.getBdAgentLastCount(cmd);
    }

  /**
   * 是否为BD.
   *
   * @eo.name 是否为BD.
   * @eo.url /check
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/check")
  public Boolean checkBD(AppExtCommand cmd) {
    return teamBdService.checkBD(cmd);
  }

  /**
   * 邀请成为BD-邀请BD.
   *
   * @eo.name 邀请成为BD-邀请BD.
   * @eo.url /invite-bd
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/invite-bd")
  public void inviteBd(@RequestBody @Validated DbLeaderInviteBdCmd cmd) {
    teamBdService.inviteBd(cmd);
  }

  /**
   * 获取被Bd邀请消息-邀请BD.
   *
   * @eo.name 获取被Bd邀请消息-邀请BD.
   * @eo.url /invite-bd-message
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/invite-bd-message")
  public List<BdInviteMessageCO> listInviteBdMessage(BdInviteQryCmd cmd) {
    cmd.setGroup(MessageInviteGroup.BD_LEADER_INVITE_BD.name());
    return teamBdService.listBdInviteMessage(cmd);
  }

  /**
   * 获取邀请Bd的消息-邀请BD.
   *
   * @eo.name 获取被Bd邀请消息-邀请BD.
   * @eo.url /invite-bd-message
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/invite-bd-message-own")
  public List<BdInviteMessageCO> listInviteBdMessageOwn(BdInviteQryCmd cmd) {
    cmd.setGroup(MessageInviteGroup.BD_LEADER_INVITE_BD.name());
    return teamBdService.listBdInviteMessageOwn(cmd);
  }

  /**
   * 邀请BD-撤销
   */
  @PostMapping("/invite-bd-cancel/{id}")
  public void inviteBdCancel(@PathVariable("id") String id) {
    teamBdService.inviteAgentCancel(id);
  }

    /**
     * 管理员邀请充值代理.
     *
     * @eo.name 管理员邀请充值代理.
     * @eo.url /invite-recharge-agent
     * @eo.method post
     * @eo.request-type json
     */
    @PostMapping("/invite-recharge-agent")
    public void inviteRechargeAgent(@RequestBody @Validated DbLeaderInviteBdCmd cmd) {
      teamBdService.inviteRechargeAgent(cmd);
    }

    /**
     * 获取被管理员邀请消息-邀请充值代理.
     *
     * @eo.name 获取被管理员邀请消息-邀请充值代理.
     * @eo.url /invite-recharge-agent-message
     * @eo.method get
     * @eo.request-type formdata
     */
    @GetMapping("/invite-recharge-agent-message")
    public List<BdInviteMessageCO> listInviteRechargeAgentMessage(BdInviteQryCmd cmd) {
      return teamBdService.listInviteRechargeAgentMessage(cmd);
    }

    /**
     * 获取邀请充值代理的消息-邀请充值代理.
     *
     * @eo.name 获取邀请充值代理的消息-邀请充值代理.
     * @eo.url /invite-recharge-agent-message-own
     * @eo.method get
     * @eo.request-type formdata
     */
    @GetMapping("/invite-recharge-agent-message-own")
    public List<BdInviteMessageCO> listInviteRechargeAgentMessageOwn(BdInviteQryCmd cmd) {
      return teamBdService.listInviteRechargeAgentMessageOwn(cmd);
    }

    /**
     * 邀请充值代理-撤销.
     *
     * @eo.name 邀请充值代理-撤销.
     * @eo.url /invite-recharge-agent-cancel/{id}
     * @eo.method post
     * @eo.request-type formdata
     */
    @PostMapping("/invite-recharge-agent-cancel/{id}")
    public void inviteRechargeAgentCancel(@PathVariable("id") String id) {
      teamBdService.inviteRechargeAgentCancel(id);
    }

    /**
     * 处理邀请-邀请充值代理.
     *
     * @eo.name 处理邀请-邀请充值代理.
     * @eo.url /invite-recharge-agent-message-process
     * @eo.method post
     * @eo.request-type json
     */
    @PostMapping("/invite-recharge-agent-message-process")
    public void processInviteRechargeAgentMessage(@RequestBody @Validated DbInviteMessageProcessCmd cmd) {
      teamBdService.processAdminInviteRechargeAgentMessage(cmd);
    }


  /**
   * 处理邀请-邀请BD.
   *
   * @eo.name 处理邀请-邀请BD.
   * @eo.url /invite-bd-message-process
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/invite-bd-message-process")
  public void processInviteBdMessage(@RequestBody @Validated DbInviteMessageProcessCmd cmd) {
    teamBdService.processBdLeaderInviteMessage(cmd);
  }

//  ===================================================================================


  /**
   * 邀请BDLEADER
   *
   * @eo.name 邀请成为BD-邀请BD.
   * @eo.url /invite-bd
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/invite-bdleader")
  public void inviteBdLeader(@RequestBody @Validated DbLeaderInviteBdCmd cmd) {
    teamBdService.inviteBdleader(cmd);
  }

  /**
   * 获取被BDLEADER邀请消息
   *
   * @eo.name 获取被BDLEADER邀请消息-邀请BD.
   * @eo.url /invite-bd-message
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/invite-bdleader-message")
  public List<BdInviteMessageCO> listInviteBdleaderMessage(BdInviteQryCmd cmd) {
    cmd.setGroup(MessageInviteGroup.BD_LEADER_INVITE_BD_LEADER.name());
    return teamBdService.listBdInviteMessage(cmd);
  }

  /**
   * 获取邀请BDLEADER的消息
   *
   * @eo.name 获取被BDLEADER邀请消息-邀请BD.
   * @eo.url /invite-bd-message
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/invite-bdleader-message-own")
  public List<BdInviteMessageCO> listInviteBdleaderMessageOwn(BdInviteQryCmd cmd) {
    cmd.setGroup(MessageInviteGroup.BD_LEADER_INVITE_BD_LEADER.name());
    return teamBdService.listBdInviteMessageOwn(cmd);
  }

  /**
   * 邀请BDLEADER-撤销
   */
  @PostMapping("/invite-bdleader-cancel/{id}")
  public void inviteBdleaderCancel(@PathVariable("id") String id) {
    teamBdService.inviteAgentCancel(id);
  }


  /**
   * 处理邀请BDLEADER.
   *
   * @eo.name 处理邀请-邀请BD.
   * @eo.url /invite-bd-message-process
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/invite-bdleader-message-process")
  public void processInviteBdleaderMessage(@RequestBody @Validated DbInviteMessageProcessCmd cmd) {
    teamBdService.processBdLeaderInviteMessage2(cmd);
  }

//  ===================================================================================

  /**
   * Bd邀请成为代理-邀请代理.
   *
   * @eo.name Bd邀请成为代理-邀请代理.
   * @eo.url /invite-agent
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/invite-agent")
  public void inviteAgent(@RequestBody @Validated DbInviteAgentCmd cmd) {
    teamBdService.inviteAgent(cmd);
  }

  /**
   * bd 邀请代理-撤销
   */
  @PostMapping("/invite-agent-cancel/{id}")
  public void inviteAgentCancel(@PathVariable("id") String id) {
    teamBdService.inviteAgentCancel(id);
  }

  /**
   * 获取被Bd邀请消息-邀请代理.
   *
   * @eo.name 获取被Bd邀请消息-邀请代理.
   * @eo.url /invite-message
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/invite-message")
  public List<BdInviteMessageCO> listBdInviteMessage(BdInviteQryCmd cmd) {
    cmd.setGroup(MessageInviteGroup.BD_INVITE_TEAM.name());
    return teamBdService.listBdInviteMessage(cmd);
  }

  /**
   * 获取邀请消息记录-邀请代理.
   */
  @GetMapping("/invite-message-own")
  public List<BdInviteMessageCO> listBdInviteMessageOwn(BdInviteQryCmd cmd) {
    cmd.setGroup(MessageInviteGroup.BD_INVITE_TEAM.name());
    return teamBdService.listBdInviteMessageOwn(cmd);
  }

  /**
   * 处理邀请-邀请代理.
   *
   * @eo.name 处理邀请-邀请代理.
   * @eo.url /invite-message-process
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/invite-message-process")
  public void processBdInviteMessage(@RequestBody @Validated DbInviteMessageProcessCmd cmd) {
    teamBdService.processBdInviteMessage(cmd);
  }

    /**
     * BD 列表接口
     */
  @GetMapping("/page2")
  public PageResult<BdPageCO> bdPage(BdTeamPageQryCmd qryCmd) {
      return teamBdService.bdPage(qryCmd);
  }

  // ==================== BD Center 账单相关接口 ====================

  /**
   * 查询BD成员账单列表.
   *
   * @param cmd 查询命令
   * @return 成员账单列表
   * @eo.name 查询BD成员账单列表
   * @eo.url /member-bill/list
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/member-bill/list")
  public TeamBdMemberCO queryMemberBillList(@Validated TeamBdMemberBillCmd cmd) {
    return teamBdBillService.queryMemberBillList(cmd);
  }

  /**
   * 提交提现申请.
   *
   * @eo.name 提交提现申请.
   * @eo.url /withdraw/apply
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/withdraw/apply")
  public void applyWithdraw(@RequestBody @Validated LotteryWithdrawCmd cmd) {
    teamBdBillService.applyWithdraw(cmd);
  }

  /**
   * 查询BD历史记录
   *
   * @param cmd 查询命令
   * @return BD历史记录汇总
   * @eo.name 查询BD历史记录
   * @eo.url /history
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/history")
  public BdHistorySummaryCO queryBdHistory(@Validated BdHistoryQryCmd cmd) {
    return teamBdHistoryService.queryBdHistory(cmd);
  }

  /**
   * 查询BD历史记录详情（More页面）
   *
   * @param cmd 查询命令
   * @return BD历史记录详情
   * @eo.name 查询BD历史记录详情
   * @eo.url /history/more
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/history/more")
  public BdHistoryMoreCO queryBdHistoryMore(@Validated BdHistoryMoreQryCmd cmd) {
    return teamBdHistoryService.queryBdHistoryMore(cmd);
  }

  /**
   * 查询BD Leader历史记录
   *
   * @param cmd 查询命令
   * @return BD Leader历史记录汇总
   * @eo.name 查询BD Leader历史记录
   * @eo.url /leader/history
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/leader/history")
  public BdLeaderHistorySummaryCO queryBdLeaderHistory(@Validated BdLeaderHistoryQryCmd cmd) {
    return teamBdHistoryService.queryBdLeaderHistory(cmd);
  }

  /**
   * 查询BD Leader历史记录详情（More页面）
   *
   * @param cmd 查询命令
   * @return BD Leader历史记录详情
   * @eo.name 查询BD Leader历史记录详情
   * @eo.url /leader/history/more
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/leader/history/more")
  public BdLeaderHistoryMoreCO queryBdLeaderHistoryMore(@Validated BdLeaderHistoryMoreQryCmd cmd) {
    return teamBdHistoryService.queryBdLeaderHistoryMore(cmd);
  }

  /**
   * 查询BD Details（聚合所有交易记录）
   *
   * @param cmd 查询命令
   * @return BD Details 分页结果
   * @eo.name 查询BD Details
   * @eo.url /withdraw/details
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/withdraw/details")
  public List<BdDetailItemCO> queryBdDetails(@Validated BdDetailsQryCmd cmd) {
    return teamBdBillService.queryBdDetails(cmd);
  }

}
