package com.yn.bftl.thirdparty.modules.contract.controller;

import com.yn.bftl.thirdparty.modules.contract.service.ContractLockService;
import com.yn.bftl.thirdparty.modules.contract.vo.CallVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

/**
 * $CallBackController 契约锁回调
 *
 * @author chendehuihuo
 * @date 2022-07-26 17:50
 */
@Api(value = "ContractLockController", tags = "公共端 - 契约锁操作")
@Validated
@RestController
@RequestMapping("/contract/call")
public class ContractLockCallBackController {

    @Resource
    private ContractLockService contractLockService;

    /**
     * 契约锁 - 合同回调执行
     *
     * @param
     * @return
     * @author chendehuihuo
     * @date 2022-07-27 09:32
     */
    @PostMapping(value = "/back")
    public String callBack(
            @RequestParam("signature") String signature,
            @RequestParam("timestamp") String timestamp,
            HttpServletRequest request
    ) {
        return contractLockService.callBack(signature, timestamp, request);
    }

    /**
     * 契约锁 - 企业认证回调执行
     *
     * @param
     * @return
     * @author chendehuihuo
     * @date 2022-07-27 09:32
     */
    @PostMapping(value = "/back/company")
    public CallVo companyCallback(
            @RequestParam("status") Integer status,
            @RequestParam("status") Integer actionEvent,
            @RequestParam("requestId") String requestId,
            @RequestParam("authInfo") String authInfo
    ) {
        // status	认证结果状态：0（“认证中”）、1（“认证成功”）、2（“认证失败”）
        // actionEvent	认证行为状态：0（“提交基本信息”）、1（“基本信息审核通过”）、2（“基础信息审核失败”）、4（“授权书审核失败”）、7（“认证成功”）
        return contractLockService.companyCallback(status, actionEvent, requestId, authInfo);
    }

    @ApiOperation("获取合同详情url")
    @GetMapping("/getDetailUrl/{id}")
    public String getDetailUrl(@PathVariable Long id) {
        return contractLockService.getDetailUrl(id);
    }

    @ApiOperation("获取合同 预签署/签署 url")
    @GetMapping("/getPageUrl/{id}")
    public String getPageUrl(@PathVariable Long id) {
        return contractLockService.show(id, false).getLink();
    }

    @ApiOperation(value = "发起合同")
    @GetMapping(value = "/send/{id}")
    public Boolean send(@PathVariable Long id) {
        contractLockService.send(id, false);
        return true;
    }

    @ApiOperation("获取收据详情url")
    @GetMapping("/receipt/getPageUrl/{id}")
    public String getReceiptPageUrl(@PathVariable("id") Long contractId) {
        return contractLockService.detailUrl(contractId);
    }

    @ApiOperation("由pc触发收据盖章")
    @GetMapping("/receipt_seal")
    public void generateReceipt() {
        contractLockService.generateReceipt();
    }

    @ApiOperation("获取契约锁合同详情url")
    @GetMapping("/detail_url/{id}")
    public String detailUrl(@PathVariable Long id) {
        return contractLockService.detailUrl(id);
    }

    @ApiOperation(value = "下载合同文档")
    @GetMapping(value = "/download")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", required = true, name = "documentId", value = "文档id", dataType = "Long", dataTypeClass = Long.class)
    })
    public void download(@NotNull Long documentId, HttpServletResponse response) {
        contractLockService.download(documentId, response);
    }

    @ApiOperation(value = "下载合同与附属文件")
    @GetMapping(value = "/download/zip")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", required = true, name = "contractId", value = "合同id", dataType = "Long", dataTypeClass = Long.class)
    })
    public void downloadZip(@NotNull Long contractId, HttpServletResponse response) {
        contractLockService.downloadZip(contractId, response);
    }

    @ApiOperation(value = "回调-邀请")
    @PostMapping(value = "/back/invite")
    public CallVo inviteCallback(@RequestParam("inviteId") String inviteId, @RequestParam("result") String result,
                                 @RequestParam("companyName") String companyName, @RequestParam("registerNo") String registerNo) {
        return contractLockService.inviteCallback(inviteId, result, companyName, registerNo);
    }
}

