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

import com.red.circle.console.app.dto.clienobject.team.TeamDetailsCO;
import com.red.circle.console.app.dto.clienobject.team.TeamMemberCO;
import com.red.circle.console.app.dto.clienobject.team.TeamMemberWorkCO;
import com.red.circle.console.app.dto.clienobject.team.TeamTableCO;
import com.red.circle.console.app.service.app.team.TeamMemberBackService;
import com.red.circle.console.app.service.app.team.TeamProfileBackService;
import com.red.circle.console.infra.annotations.OpsOperationReqLog;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.inner.model.cmd.gift.SumAnchorTargetQryCmd;
import com.red.circle.other.inner.model.cmd.team.MemberTeamChangeCmd;
import com.red.circle.other.inner.model.cmd.team.TeamBatchStatusFromCmd;
import com.red.circle.other.inner.model.cmd.team.TeamBindBdCmd;
import com.red.circle.other.inner.model.cmd.team.TeamChangeOwnerCmd;
import com.red.circle.other.inner.model.cmd.team.TeamContactCmd;
import com.red.circle.other.inner.model.cmd.team.TeamContactIdCmd;
import com.red.circle.other.inner.model.cmd.team.TeamCreateCmd;
import com.red.circle.other.inner.model.cmd.team.TeamMemberAddCmd;
import com.red.circle.other.inner.model.cmd.team.TeamMemberBackQryCmd;
import com.red.circle.other.inner.model.cmd.team.TeamMemberIdCmd;
import com.red.circle.other.inner.model.cmd.team.TeamMemberRemarksCmd;
import com.red.circle.other.inner.model.cmd.team.TeamMemberTargetResetCmd;
import com.red.circle.other.inner.model.cmd.team.TeamMemberWorkQryCmd;
import com.red.circle.other.inner.model.cmd.team.TeamRemarkCmd;
import com.red.circle.other.inner.model.cmd.team.TeamRemarkIdCmd;
import com.red.circle.other.inner.model.cmd.team.TeamTableQryCmd;
import com.red.circle.other.inner.model.cmd.team.TeamUpdateCmd;
import com.red.circle.other.inner.model.dto.agency.agency.TeamContactDTO;
import com.red.circle.other.inner.model.dto.agency.agency.TeamRemarkDTO;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import java.math.BigDecimal;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author pengliang on 2022/12/7
 */
@RestController
@RequestMapping(value = "/team", produces = MediaType.APPLICATION_JSON_VALUE)
@AllArgsConstructor
public class TeamManagerRestController extends BaseController {

  private final TeamMemberBackService teamMemberBackService;
  private final TeamProfileBackService teamBackManagerService;

  /**
   * 团队列表.
   */
  @GetMapping("/list")
  public List<TeamTableCO> listTeamTable(@Validated TeamTableQryCmd query) {
    return teamBackManagerService.listTeam(query);
  }

  /**
   * 团队列表.
   */
  @GetMapping("/details")
  public TeamDetailsCO getTeamDetails(@RequestParam Long teamId) {
    return teamBackManagerService.getTeamDetails(teamId);
  }

  /**
   * 创建团队.
   */
  @PostMapping("/create")
  public void createTeam(@RequestBody @Validated TeamCreateCmd cmd) {
    cmd.setCreateUser(getReqUserId());
    teamBackManagerService.createTeam(cmd);
  }

  /**
   * 修改团队资料.
   */
  @OpsOperationReqLog("修改团队资料")
  @PostMapping("/update-profile")
  public void updateTeam(@RequestBody @Validated TeamUpdateCmd cmd) {
    cmd.setUpdateUser(getReqUserId());
    teamBackManagerService.updateTeam(cmd);
  }

  /**
   * 修改团队状态.
   */
  @Deprecated
  @OpsOperationReqLog(" 修改团队状态")
  @PostMapping("/update-status")
  public void updateTeamStatus(
      @RequestBody @Validated TeamBatchStatusFromCmd cmd) {
    cmd.setUpdateUser(getReqUserId());
    teamBackManagerService.updateBatchTeamStatus(cmd);
  }

  /**
   * 删除团队.
   */
  @OpsOperationReqLog("删除团队")
  @PostMapping("/del")
  public void delTeam(@RequestBody List<Long> teamIds) {
    teamBackManagerService.delTeam(teamIds);
  }

  /**
   * 添加团队备注.
   */
  @PostMapping("/add-remarks")
  public TeamRemarkDTO addTeamRemarks(@RequestBody @Validated TeamRemarkCmd cmd) {
    cmd.setCreateUser(getReqUserId());
    return teamBackManagerService.addTeamRemarks(cmd);
  }

  /**
   * 删除团队备注.
   */
  @OpsOperationReqLog("删除团队备注")
  @PostMapping("/del-remarks")
  public void delTeamRemarks(@RequestBody @Validated TeamRemarkIdCmd cmd) {
    teamBackManagerService.delTeamRemarks(cmd);
  }

  /**
   * 添加团队联系方式.
   */
  @PostMapping("/add-contact")
  public TeamContactDTO addTeamContact(
      @RequestBody @Validated TeamContactCmd cmd) {
    cmd.setCreateUser(getReqUserId());
    return teamBackManagerService.addTeamContact(cmd);
  }

  /**
   * 删除团队联系方式.
   */
  @OpsOperationReqLog("删除团队联系方式")
  @PostMapping("/del-contact")
  public void delTeamContact(@RequestBody @Validated TeamContactIdCmd cmd) {
    teamBackManagerService.delTeamContact(cmd);
  }

  /**
   * 团队成员列表.
   */
  @GetMapping("/member/list/{teamId}")
  public List<TeamMemberCO> listTeamMember(@PathVariable("teamId") Long teamId) {
    return teamMemberBackService.listTeamMember(teamId);
  }

  /**
   * 成员列表.
   */
  @GetMapping("/member/list")
  public List<TeamMemberCO> listTeamMember(TeamMemberBackQryCmd query) {
    return teamMemberBackService.listTeamMember(query);
  }

  /**
   * 添加成员.
   */
  @OpsOperationReqLog("添加成员")
  @PostMapping("/member/add")
  public void addTeamMember(@RequestBody @Validated TeamMemberAddCmd cmd) {
    cmd.setCreateUser(getReqUserId());
    teamMemberBackService.addTeamMember(cmd);
  }

  /**
   * 移除成员.
   */
  @OpsOperationReqLog("移除成员")
  @PostMapping("/member/remove")
  public void removeTeamMember(@RequestBody @Validated TeamMemberIdCmd cmd) {
    cmd.setUpdateUser(getReqUserId());
    teamMemberBackService.removeTeamMember(cmd);
  }

  /**
   * 修改成员备注.
   */
  @OpsOperationReqLog("修改成员备注")
  @PostMapping("/member/update-remark")
  public void updateTeamMemberRemarks(
      @RequestBody @Validated TeamMemberRemarksCmd cmd) {
    teamMemberBackService.updateTeamMemberRemarks(cmd);
  }

  /**
   * 团队成员归属变更.
   */
  @OpsOperationReqLog("团队成员归属变更")
  @PostMapping("/member/team-change")
  public TeamMemberCO updateMemberTeam(
      @RequestBody @Validated MemberTeamChangeCmd param) {
    param.setUpdateUser(getReqUserId());
    return teamMemberBackService.updateMemberTeam(param);
  }

  /**
   * 查询当前成员修正幸运礼物后的目标情况.
   */
  @GetMapping("/check-target-lucky-gift")
  public BigDecimal checkTargetLuckyGift(@Validated SumAnchorTargetQryCmd query) {
    return teamMemberBackService.checkTargetLuckyGift(query);
  }

  /**
   * 检查当前成员目标情况.
   */
  @GetMapping("/check-target")
  public BigDecimal checkTarget(@Validated SumAnchorTargetQryCmd query) {
    return teamMemberBackService.checkTarget(query);
  }

  /**
   * 重置用户目标.
   */
  @PostMapping("/reset-target")
  public void resetUnpaidBillMemberTarget(
      @RequestBody @Validated TeamMemberTargetResetCmd cmd) {
    cmd.setUpdateUser(getReqUserId());
    teamMemberBackService.resetUnpaidBillMemberTarget(cmd);
  }

  /**
   * 团队成员工作情况.
   */
  @GetMapping("/member/work")
  public List<TeamMemberWorkCO> listMemberWork(@Validated TeamMemberWorkQryCmd query) {
    return teamMemberBackService.listMemberWork(query);
  }

  /**
   * 将团队绑定到指定BD名下.
   */
  @OpsOperationReqLog("将团队绑定到指定BD名下")
  @PostMapping("/bind/bd")
  public void bindBd(@RequestBody @Validated TeamBindBdCmd param) {
    param.setUpdateUserBackId(getReqUserId());
    teamBackManagerService.bindBd(param);
  }

  /**
   * 变更团长(务必同区域或者团长不是主播代理).
   */
  @OpsOperationReqLog("变更团长(务必同区域或者团长不是主播代理)")
  @PostMapping("/switch/own")
  public UserProfileDTO changeTeamOwnUser(
      @RequestBody @Validated TeamChangeOwnerCmd cmd) {
    cmd.setBackUpdateUserId(getReqUserId());
    return teamMemberBackService.changeTeamOwnUser(cmd);
  }


}

