package cn.pluss.platform.controller;

import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.cash.CashService;
import cn.pluss.platform.entity.Cash;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.entity.WxBankCode;
import cn.pluss.platform.util.*;
import cn.pluss.platform.wxBankCode.WxBankCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/cash")
@Api(value = "Cash控制类", tags = { "Cash接口" })
public class CashController {

  private final CashService cashService;

  private final WxBankCodeService wxBankCodeService;

  @GetMapping({ "", "/v3" })
  @ApiOperation(tags = { "页面-提现记录" }, value = "获取分页提现记录", notes = "返回分页过后的数据", httpMethod = "GET")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "page", value = "查询页码", paramType = "query", dataType = "Integer", defaultValue = "0"),
      @ApiImplicitParam(name = "size", value = "每页数据量", paramType = "query", dataType = "Integer", defaultValue = "0"),
      @ApiImplicitParam(name = "type", value = "提现类型  0粉丝分润   1推广分润      99其他 ", paramType = "type", dataType = "Integer", required = true),
      @ApiImplicitParam(name = "status", value = "提现状态  0 申请提现  1 提现成功  2 提现失败", paramType = "status", dataType = "Integer"),
      @ApiImplicitParam(name = "merchantCode", value = "商家Code", paramType = "merchantCode", dataType = "String", required = true), })
  public Result<PageInfo<Cash>> list(@RequestParam(defaultValue = "1") Integer page,
      @RequestParam(defaultValue = "10") Integer size, Integer type, Integer status, String merchantCode,
      String userId) {

    Map<String, Object> map = new HashMap<>();
    map.put("pageSize", size);
    map.put("offset", (page - 1) * size);

    if (StringUtil.isEmpty(userId)) {
      return ResultGenerator.genFailResult("无用户id");
    }

    // map.put("type", type);
    map.put("userId", userId);
    List<Cash> cashList = cashService.queryCashPage(map);
    Integer count = cashService.queryCashPageCount(map);
    // 返回参数
    PageInfo<Cash> pageInfo = new PageInfo<>(count.longValue(), (long) StringUtil.getPageCount(count, size),
        page.longValue(), size.longValue(), cashList);
    return ResultGenerator.genSuccessResult(pageInfo);
  }

  @PostMapping
  @ApiOperation(tags = { "页面-提现" }, value = "提现接口", notes = "添加新的提现记录", httpMethod = "POST")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "Cash", value = "待添加的Cash实例", paramType = "body", dataType = "Cash", required = true) })
  public Result<Object> add(@RequestBody Cash cash) {
    cashService.apiSaveCash(cash);
    return ResultGenerator.genSuccessResult();
  }

  @PostMapping("/v2")
  public Result<Object> addV2(@RequestBody Cash cash) {
    cashService.apiSaveCashV2(cash);
    return ResultGenerator.genSuccessResult();
  }

  @GetMapping("/v3/preDetail")
  public Result<Object> detail(Cash cash) {
    Cash result = cashService.cashDetail(cash);
    return ResultGenerator.genSuccessResult(result);
  }

  @PostMapping("/v3")
  public Result<Object> addV3(@RequestBody Cash cash) {
    cashService.apiSaveCashV3(cash);
    return ResultGenerator.genSuccessResult();
  }

}
