package com.gree.mall.manager.controller.websit;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.websit.SettlementOrderDetail;
import com.gree.mall.manager.bean.websit.WithdrawalOrderDetail;
import com.gree.mall.manager.bean.websit.WithdrawalOrderVO;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.websit.WithdrawalOrderLogic;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;

@Slf4j
@RestController
@Api(value = "提现单", tags = {"提现单"})
@RequestMapping(value = "/withdrawal", produces = "application/json; charset=utf-8")
public class WithdrawalOrderController {

    @Autowired
    private WithdrawalOrderLogic withdrawalOrderLogic;


    @ZfireList
    @PostMapping("/list/pageV2")
    @ApiOperation(value = "提现单列表")
    public ResponseHelper<IPage<WithdrawalOrderVO>> pageV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<WithdrawalOrderVO> page = withdrawalOrderLogic.pageV2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<WithdrawalOrderVO>() {});
    }

    @PostMapping("/pageExport")
    @ApiOperation(value = "提现单导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<WithdrawalOrderVO> page = withdrawalOrderLogic.pageV2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }


    @PostMapping("/detail")
    @ApiOperation("提现单详情")
    public ResponseHelper<WithdrawalOrderDetail> detail(
            @ApiParam(value = "提现id" , required = true) @RequestParam(required = true) String id,

            HttpServletRequest request
    ) throws ParseException {
        WithdrawalOrderDetail  withdrawalOrderDetail = withdrawalOrderLogic.detail(id);
        return ResponseHelper.success(withdrawalOrderDetail);
    }

    @PostMapping("/update")
    @ApiOperation("设为已发")
    public ResponseHelper update(
            @ApiParam(value = "提现id" , required = true) @RequestParam(required = true) String id,
            @ApiParam(value = "转账凭证" , required = false) @RequestParam(required = false) String certImg,
            @ApiParam(value = "备注" , required = false) @RequestParam(required = false) String remark,
            HttpServletRequest request
    ) throws ParseException {
        withdrawalOrderLogic.update(id,certImg,remark);
        return ResponseHelper.success();
    }


}
