package org.jeecg.modules.app.controller;

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.SignCallbackDTO;
import org.jeecg.modules.app.esign.exception.EsignDemoException;
import org.jeecg.modules.app.service.IESignService;
import org.jeecg.modules.system.app.entity.AppContractSign;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@Tag(name = "E签宝相关接口")
@RestController
@RequestMapping("/app/esign")
public class ESignApi {
    @Resource
    private IESignService iESignService;
    /**
     * @Author SheepHy
     * @Description 查询用户授权状态
     * @Date 9:42 2025/7/18
     * @Param phoneNumber
     * @return boolean
     **/
    @GetMapping("/getUserIdentityInfo")
    @Operation(summary = "查询用户授权状态-APP")
    public Result<Boolean> getUserIdentityInfo(@RequestParam("familyId") @Schema(description="使用人ID")String familyId) throws EsignDemoException{
        return Result.OK(iESignService.getUserIdentityInfo(familyId));
    }

    /**
     * @Author SheepHy
     * @Description 获取授权链接
     * @Date 9:42 2025/7/18
     * @Param
     * @return
     **/
    @GetMapping("/getAuthUrl")
    @Operation(summary = "用户获取授权链接-APP")
    public Result<String> getAuthUrl(@RequestParam("familyId") @Schema(description="使用人ID")String familyId) throws EsignDemoException{
        return Result.OK(iESignService.getAuthUrl(familyId));
    }

    /**
     * @Author SheepHy
     * @Description 获取签署链接
     * @Date 14:02 2025/7/21
     * @Param
     * @return
     **/
    @GetMapping("/getSignUrl")
    @Operation(summary = "获取签署链接-APP")
    public Result<String> getSignUrl(@RequestParam("signFlowId") @Schema(description="文件标识")String signFlowId) throws EsignDemoException{
        return Result.OK(iESignService.getSignUrl(signFlowId));
    }

    /**
     * @Author SheepHy
     * @Description 发起签署
     * @Date 10:10 2025/7/21
     * @Param
     * @return
     **/
    @GetMapping("/createSign")
    @Operation(summary = "发起签署-APP")
    public Result<String> createSign(@RequestParam("orderCode") @Schema(description="订单编号")String orderCode,
                                     @RequestParam("familyId") @Schema(description="使用人ID")String familyId) throws EsignDemoException{
        return Result.OK(iESignService.createSign(orderCode,familyId));
    }

    /**
     * @Author SheepHy
     * @Description 签署回调处理
     * @Date 13:51 2025/8/6
     * @Param
     * @return
     **/
    @PostMapping("/signCallback")
    @Operation(summary = "签署回调处理")
    public Result<Boolean> signCallback(@RequestBody SignCallbackDTO rawBody, HttpServletRequest request) {
        return Result.OK(iESignService.signCallback(rawBody));
    }

    /**
     * @Author SheepHy
     * @Description 查询待签列表
     * @Date 14:37 2025/8/6
     * @Param
     * @return
     **/
    @GetMapping("/queryWaitSignList")
    @Operation(summary = "查询待签列表")
    public Result<List<AppContractSign>> queryWaitSignList(@RequestParam("orderId") @Schema(description="订单ID")String orderId){
        return Result.OK(iESignService.queryWaitSignList(orderId));
    }
    /**
     * @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));
    }

}
