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


import com.alibaba.excel.EasyExcel;
import com.red.circle.console.app.dto.clienobject.user.UserBankBalanceCO;
import com.red.circle.console.app.dto.clienobject.user.UserBankRunningWaterCO;
import com.red.circle.console.app.dto.clienobject.user.UserBankWithdrawGoldApplyCO;
import com.red.circle.console.app.dto.clienobject.user.UserBankWithdrawMoneyApplyCO;
import com.red.circle.console.app.excel.model.bank.UserBankBalanceReadModel;
import com.red.circle.console.app.service.app.user.UserBankBalanceBackService;
import com.red.circle.framework.core.dto.CommonCommand;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.wallet.inner.model.cmd.ApprovalMoneyApplyCmd;
import com.red.circle.wallet.inner.model.cmd.UserBankBalanceQryCmd;
import com.red.circle.wallet.inner.model.cmd.UserBankMoneyCmd;
import com.red.circle.wallet.inner.model.cmd.UserBankRunningWaterQryCmd;
import com.red.circle.wallet.inner.model.cmd.UserBankTransferCmd;
import com.red.circle.wallet.inner.model.cmd.UserBankWithdrawGoldApplyQryCmd;
import com.red.circle.wallet.inner.model.cmd.UserBankWithdrawMoneyApplyExportQryCmd;
import com.red.circle.wallet.inner.model.cmd.UserBankWithdrawMoneyApplyQryCmd;
import com.red.circle.wallet.inner.model.enums.UserBankWaterEvent;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
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.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 用户银行账户 前端控制器.
 * </p>
 *
 * @author pengshigang on 2023/2/16
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/user-bank-balance", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserBankBalanceRestController extends BaseController {

  private final UserBankBalanceBackService userBankBalanceBackService;

  /**
   * 用户余额列表.
   */
  @GetMapping("/page")
  public List<UserBankBalanceCO> page(UserBankBalanceQryCmd query) {
    return userBankBalanceBackService.listUserBankBalance(query);
  }

  /**
   * 用户余额列表-导出.
   */
  @GetMapping("/export")
  public void export(HttpServletResponse response, String sysOrigin) throws IOException {
    userBankBalanceBackService.exportUserBankBalance(response, sysOrigin);

  }

  /**
   * 下载用户工资导入模板.
   */
  @GetMapping("/template-export")
  public void templateExport(HttpServletResponse response, String sysOrigin) throws IOException {
    List<UserBankBalanceReadModel> list = new ArrayList<>();
    /*list.add(new UserBankBalanceReadModel()
        .setSysOrigin(sysOrigin)
        .setUserId("test")
        .setType("0")
        .setAmount("1")
        .setRemark("这是一条Demo数据")
    );*/
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setCharacterEncoding("utf-8");
    String fileName = URLEncoder.encode("批量处理工资模板", StandardCharsets.UTF_8);
    String sheetName = URLEncoder.encode("ImportUserBankAccount", StandardCharsets.UTF_8);
    response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
    EasyExcel.write(response.getOutputStream(), UserBankBalanceReadModel.class)
        .sheet(sheetName)
        .doWrite(list);
  }


  /**
   * 用户工资-导入.
   */
  @PostMapping(value = "/import")
  public void importUserBankBalance(@RequestParam("file") MultipartFile file, CommonCommand param) {
    userBankBalanceBackService.importUserBankBalance(file, param);
  }


  /**
   * 用户银行流水.
   */
  @GetMapping("/running-water-page")
  public List<UserBankRunningWaterCO> runningWaterPage(
      UserBankRunningWaterQryCmd query) {
    return userBankBalanceBackService.pageUserBankRunningWater(query);
  }

  /**
   * 获得用户银行账户.
   */
  @GetMapping("/bank/balance/{userId}")
  public BigDecimal getBankBalance(@PathVariable Long userId) {
    return userBankBalanceBackService.getBankBalance(userId);
  }

  /**
   * 发送用户银行账户现金.
   */
  @PostMapping("/send-money")
  public void sendMoney(@RequestBody @Validated UserBankMoneyCmd param) {
    param.setOperationUserId(param.getReqUserId());
    param.setEvent(UserBankWaterEvent.SYSTEM_COMPENSATE);
    userBankBalanceBackService.sendMoney(param);

  }

  /**
   * 扣除用户银行账户现金.
   */
  @PostMapping("/deduct-money")
  public void deductMoney(@RequestBody @Validated UserBankMoneyCmd param) {
    param.setOperationUserId(param.getReqUserId());
    param.setEvent(UserBankWaterEvent.SYSTEM_DEDUCT);
    userBankBalanceBackService.deductMoney(param);

  }

  /**
   * 用户银行账户现金转账.
   */
  @PostMapping("/transfer-money")
  public void transferMoney(@RequestBody @Validated UserBankTransferCmd param) {
    param.setOperationUserId(param.getReqUserId());
    userBankBalanceBackService.transferMoney(param);

  }

  /**
   * 创建用户银行现金账户.
   */
  @PostMapping("/create-bank-balance")
  public void createBankBalance(@RequestBody @Validated UserBankMoneyCmd param) {
    param.setOperationUserId(param.getReqUserId());
    param.setEvent(UserBankWaterEvent.CREATE_ACCOUNT);
    userBankBalanceBackService.createBankBalance(param);

  }

  /**
   * 用户银行卡提现金币申请记录.
   */
  @GetMapping("/withdraw-gold-apply-page")
  public List<UserBankWithdrawGoldApplyCO> pageWithdrawGoldApply(
      UserBankWithdrawGoldApplyQryCmd query) {
    return userBankBalanceBackService.pageUserBankWithdrawGoldApply(query);
  }

  /**
   * 用户银行卡提现现金申请记录.
   */
  @GetMapping("/withdraw-money-apply-page")
  public List<UserBankWithdrawMoneyApplyCO> pageWithdrawMoneyApply(
      UserBankWithdrawMoneyApplyQryCmd query) {
    return userBankBalanceBackService.pageUserBankWithdrawMoneyApply(query);
  }

  /**
   * 用户银行卡提现现金申请记录-导出.
   */
  @GetMapping("/withdraw-money-apply/export")
  public void exportWithdrawMoneyApply(HttpServletResponse response,
      UserBankWithdrawMoneyApplyExportQryCmd query) {
    userBankBalanceBackService.exportUserBankWithdrawMoneyApply(response, query);
  }

  /**
   * 用户银行卡现金提现申请记录.
   */
  @GetMapping("/withdraw-money-apply/info")
  public UserBankWithdrawMoneyApplyCO getUserBankWithdrawMoneyApply(
      @RequestParam Long id) {
    return userBankBalanceBackService.getUserBankWithdrawMoneyApply(id);
  }

  /**
   * 审核用户银行卡提现现金申请.
   */
  @PostMapping("/approval-money-apply")
  public void approvalMoneyApply(
      @RequestBody @Validated ApprovalMoneyApplyCmd param) {

    param.setUpdateUser(param.getReqUserId());
    userBankBalanceBackService.approvalMoneyApply(param);

  }

  /**
   * 计算流水金额.
   */
  @GetMapping("/calculate/total")
  public String calculateTotal(UserBankRunningWaterQryCmd query) {
    return userBankBalanceBackService.calculateTotal(query);
  }


}
