package org.jeecg.modules.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.app.dto.SealInfoDTO;
import org.jeecg.modules.app.esign.exception.EsignDemoException;
import org.jeecg.modules.app.service.IESignService;
import org.jeecg.modules.system.app.dto.AppContractInfoDTO;
import org.jeecg.modules.system.app.dto.ContractSignDTO;
import org.jeecg.modules.system.app.entity.AppContractInfo;
import org.jeecg.modules.system.app.vo.ContractSignVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Tag(name = "web管理E签宝相关接口")
@RestController
@RequestMapping("/api/esign")
public class ESignController {
    @Resource
    private IESignService iESignService;

    /**
     * @Author SheepHy
     * @Description 查询企业授权状态
     * @Date 13:33 2025/7/18
     * @Param
     * @return
     **/
    @GetMapping("/getOrgIdentityInfos")
    @Operation(summary = "查询企业授权状态-PC")
    public Result<Boolean> getOrgIdentityInfos(@RequestParam("orgCode") @Schema(description="企业编号")String orgCode) throws EsignDemoException {
        return Result.OK(iESignService.getOrgIdentityInfos(orgCode));
    }

    /**
     * @Author SheepHy
     * @Description 企业获取授权链接
     * @Date 13:33 2025/7/18
     * @Param
     * @return
     **/
    @GetMapping("/getOrgAuthUrls")
    @Operation(summary = "企业获取授权链接-PC")
    public Result<String> getOrgAuthUrls(@RequestParam("orgCode") @Schema(description="企业编号")String orgCode,
                                         @RequestParam("phoneNumber") @Schema(description="联系人手机号")String phoneNumber) throws EsignDemoException{
        return Result.OK(iESignService.getOrgAuthUrls(orgCode,phoneNumber));
    }

    /**
     * @Author SheepHy
     * @Description 上传合同底稿
     * @Date 9:54 2025/7/21
     * @Param
     * @return
     **/
    @PostMapping("/uploadContractTemplate")
    @Operation(summary = "上传合同底稿-PC")
    public Result<Boolean> uploadContractTemplate(@RequestBody AppContractInfoDTO appContractInfoDTO) throws Exception{
        return Result.OK(iESignService.uploadContractTemplate(appContractInfoDTO));
    }

    /**
     * @Author SheepHy
     * @Description 制作模板
     * @Date 14:47 2025/7/21
     * @Param
     * @return
     **/
    @GetMapping("/createTemplate")
    @Operation(summary = "制作模板-PC")
    public Result<String> createTemplate(@RequestParam("fileId") @Schema(description="文件标识")String fileId) throws EsignDemoException{
        return Result.OK(iESignService.createTemplate(fileId));
    }

    /**
     * @Author SheepHy
     * @Description 编辑合同
     * @Date 15:01 2025/7/21
     * @Param
     * @return
     **/
    @GetMapping("/editContract")
    @Operation(summary = "编辑合同-PC")
    public Result<String> editContract(@RequestParam("docTemplateId") @Schema(description="模板ID")String docTemplateId) throws EsignDemoException{
        return Result.OK(iESignService.editContract(docTemplateId));
    }

    /**
     * @Author SheepHy
     * @Description 下载已签署文件及附属材料
     * @Date 14:05 2025/7/28
     * @Param
     * @return
     **/
    @GetMapping("/fileDownloadUrl")
    @Operation(summary = "下载已签署文件及附属材料-PC")
    public Result<List<Map<String,String>>> fileDownloadUrl(@RequestParam("signFlowId") @Schema(description="文件标识")String signFlowId) throws EsignDemoException{
        return Result.OK(iESignService.fileDownloadUrl(signFlowId));
    }

    /**
     * @Author SheepHy
     * @Description 合同列表查询
     * @Date 15:09 2025/7/28
     * @Param
     * @return
     **/
    @GetMapping("/queryContractInfo")
    @Operation(summary = "合同列表查询-PC")
    public Result<List<AppContractInfo>> queryContractInfo(@RequestParam(name = "contractName", required = false) @Schema(description="合同名称")String contractName){
        return Result.OK(iESignService.queryContractInfo(contractName));
    }

    /**
     * @Author SheepHy
     * @Description 查询企业内部印章
     * @Date 11:46 2025/7/31
     * @Param
     * @return
     **/
    @GetMapping("/orgOwnSeal")
    @Operation(summary = "查询企业内部印章-PC")
    public Result<List<SealInfoDTO>> orgOwnSeal(@RequestParam(name = "orgCode") @Schema(description="E签宝组织ID")String orgCode){
        return Result.OK(iESignService.orgOwnSeal(orgCode));
    }

    /**
     * @Author SheepHy
     * @Description 跨企业印章授权
     * @Date 15:29 2025/7/31
     * @Param
     * @return
     **/
    @GetMapping("/orgSealsExternal")
    @Operation(summary = "跨企业印章授权-PC")
    public Result<String> orgSealsExternal(@RequestParam("orgCode")@Schema(description="企业编号")String orgCode,
                                            @RequestParam("sealId")@Schema(description="签章ID")String sealId) throws EsignDemoException{
        return Result.OK(iESignService.orgSealsExternal(orgCode, sealId));
    }

    /**
     * @Author SheepHy
     * @Description 获取合同预览页
     * @Date 13:36 2025/8/1
     * @Param
     * @return
     **/
    @GetMapping("/templatePreviewUrl")
    @Operation(summary = "获取合同预览页-PC")
    public Result<String> templatePreviewUrl(@RequestParam("docTemplateId")@Schema(description="合同模板ID")String docTemplateId) throws EsignDemoException{
        return Result.OK(iESignService.templatePreviewUrl(docTemplateId));
    }

    /**
     * @Author SheepHy
     * @Description 获取用户签署合同列表
     * @Date 14:37 2025/8/6
     * @Param
     * @return
     **/
    @PostMapping("/queryUserSignContractList")
    @Operation(summary = "获取用户签署合同列表")
    public Result<IPage<ContractSignVO>> queryUserSignContractList(@RequestBody ContractSignDTO contractSignDTO){
        return Result.OK(iESignService.queryUserSignContractList(contractSignDTO));
    }
}
