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

import com.google.common.collect.Lists;
import com.red.circle.console.app.dto.clienobject.team.TeamBillCycleCO;
import com.red.circle.console.app.service.app.team.TeamBillBackService;
import com.red.circle.console.infra.annotations.OpsOperationReqLog;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.ResponseErrorCode;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.inner.model.cmd.team.TeamBillCycleBackQryCmd;
import com.red.circle.other.inner.model.cmd.team.TeamBillExportCmd;
import com.red.circle.other.inner.model.cmd.team.TeamBillRemarkCmd;
import com.red.circle.other.inner.model.cmd.team.TeamBillRemarkIdCmd;
import com.red.circle.other.inner.model.cmd.team.TeamBillStatusChangeCmd;
import com.red.circle.other.inner.model.cmd.team.TeamCalculatorWorkCmd;
import com.red.circle.other.inner.model.dto.agency.agency.TeamCalculatorWorkResultDTO;
import com.red.circle.other.inner.model.dto.agency.agency.TeamRemarkDTO;
import com.red.circle.other.inner.enums.team.TeamBillCycleStatus;
import com.red.circle.tool.core.text.StringUtils;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
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.RestController;

/**
 * 团队账单服务.
 *
 * @author pengliang on 2022/12/13
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/team/bill")
public class TeamBillRestController extends BaseController {

  private final TeamBillBackService teamBillBackService;

  /**
   * 列表查询.
   */
  @GetMapping("/list")
  public List<TeamBillCycleCO> listByTeamId(TeamBillCycleBackQryCmd query) {
    return teamBillBackService.listBillTable(query);
  }

  /**
   * 添加备注.
   */
  @PostMapping("/add/internal-remarks")
  public TeamRemarkDTO addRemarks(@RequestBody @Validated TeamBillRemarkCmd param) {
    param.setCreateUser(param.getReqUserId());
    return teamBillBackService.addInternalRemarks(param);
  }

  /**
   * 添加备注.
   */
  @PostMapping("/del/internal-remarks")
  public void delRemarks(@RequestBody @Validated TeamBillRemarkIdCmd param) {
    teamBillBackService.delInternalRemarks(param);
  }

  /**
   * 修改备注.
   */
  @OpsOperationReqLog("修改备注")
  @PostMapping("/remarks")
  public void updateRemarks(@RequestBody @Validated TeamBillRemarkCmd param) {
    param.setCreateUser(param.getReqUserId());
    teamBillBackService.updateRemarks(param);
  }

  /**
   * 修改账单状态.
   */
  @PostMapping("/update-status")
  public void updateBillStatus(
      @RequestBody @Validated TeamBillStatusChangeCmd param) {
    param.setUpdateUser(param.getReqUserId());
    teamBillBackService.updateBillStatus(param);
  }

  /**
   * 计算团队账单工作情况.
   */
  @GetMapping("/calculator")
  public TeamCalculatorWorkResultDTO calculatorWork(
      @Validated TeamCalculatorWorkCmd param) {
    return teamBillBackService.calculatorWork(param);
  }

  /**
   * 导出主播账单数据.
   */
  @OpsOperationReqLog(value = "导出-导出主播账单数据", ignoreRequestParam = true)
  @GetMapping("/excel")
  public void excel(HttpServletResponse response, @Validated TeamBillExportCmd param)
      throws IOException {

    ResponseAssert.isTrue(ResponseErrorCode.REQUEST_PARAMETER_ERROR,
        Objects.nonNull(param.getMonthDate()));

    if (StringUtils.isNotBlank(param.getBillStatusStr())) {
      String[] str = param.getBillStatusStr().split(",");

      List<TeamBillCycleStatus> statusList = Lists.newArrayList();
      for (String status : str) {
        statusList.add(TeamBillCycleStatus.valueOf(status));
      }
      param.setBillStatus(statusList);
    }

    teamBillBackService.export(response, param);
  }

}
