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

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.contract.AuthBean;
import com.gree.mall.manager.bean.contract.WebsitContractSignRecordBean;
import com.gree.mall.manager.bean.contract.WebsitContractSignRecordCount;
import com.gree.mall.manager.bean.contract.WebsitContractSignVO;
import com.gree.mall.manager.enums.contract.SignContractStatusEnum;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.contract.WebsitContractSignLogic;
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.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@Api(value = "网点合同签约API", tags ={"网点合同签约API"} )
@RequestMapping("/websit/contract/sign/")
public class WebsitContractSignController {

    @Resource
    WebsitContractSignLogic websitContractSignLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "网点合同签约-列表")
    public ResponseHelper<IPage<WebsitContractSignVO>> list(@RequestBody ZfireParamBean zfireParamBean) {
        IPage<WebsitContractSignVO> list = websitContractSignLogic.list(zfireParamBean);
        return ResponseHelper.success(list, new TypeReference<WebsitContractSignVO>() {
        });
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "网点合同签约-导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //2.查询要导出的内容
        IPage<WebsitContractSignVO> list = websitContractSignLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(list.getRecords(), zfireParamBean.getExportFields(), request, response);
    }

    @PostMapping("/count")
    @ApiOperation(value = "网点合同签约-统计")
    public ResponseHelper<WebsitContractSignRecordCount> count() {
        WebsitContractSignRecordCount count = websitContractSignLogic.count();
        return ResponseHelper.success(count);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "网点合同签约-详情")
    public ResponseHelper<WebsitContractSignRecordBean> detail(@RequestParam String id) {
        WebsitContractSignRecordBean detail = websitContractSignLogic.detail(id);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/cancel")
    @ApiOperation(value = "网点合同签约-失效")
    public ResponseHelper cancel(@RequestParam String id) {
        websitContractSignLogic.cancel(id);
        return ResponseHelper.success();
    }

    @PostMapping("/sign")
    @ApiOperation(value = "网点合同签约-签约")
    public ResponseHelper<AuthBean> sign(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            @ApiParam(value = "返回地址", required = true) @RequestParam String returnUrl) throws Exception {
        AuthBean bean = websitContractSignLogic.sign(id, returnUrl);
        return ResponseHelper.success(bean);
    }

    @PostMapping("/confirm")
    @ApiOperation(value = "网点合同签约-审核")
    public ResponseHelper confirm(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            @ApiParam(value = "状态 OK=通过 REJECT=驳回", required = true) @RequestParam SignContractStatusEnum status,
            @ApiParam(value = "备注") @RequestParam(required = false) String remark) {
        websitContractSignLogic.confirm(id, status, remark);
        return ResponseHelper.success();
    }
}
