package org.dromara.mp.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RequestMapping("/demo")
public class dome {

    @PostMapping(value = "/testV3")
    public String testV3()throws Exception
    {
//        String apiUrl = "http://192.168.124.5:8090/openAPI/contractSign/v3/becomeSilentSaveSign"; // 替换为实际的API地址
        String apiUrl = "https://openapi.signzg.com/openAPI/contractSign/v3/becomeSilentSaveSign"; // 替换为实际的API地址
        // 构建请求参数对象
        BecomeSilentSaveSingleSignatureDto becomeSilentSaveSignDto = new BecomeSilentSaveSingleSignatureDto();
        becomeSilentSaveSignDto.setApplicationId("2205026100");//应用id
        becomeSilentSaveSignDto.setBusinessId("95d6eb9ae14b4b6f8770467a37132c82");//合作方ID
        becomeSilentSaveSignDto.setContractName("测试合同9220");//合同名称
        becomeSilentSaveSignDto.setLaunchType("1"); //发起类型 0-个人发起 1-企业发起
        becomeSilentSaveSignDto.setLaunchSubject("13839900475");//发起用户手机号
        becomeSilentSaveSignDto.setLaunchEnterpriseName("郑州沃薪科技有限公司");//发起企业名称(launchType为1企业发起合同时必传)
        becomeSilentSaveSignDto.setSignEnterpriseName("郑州沃薪科技有限公司");//静默签署企业名

//        becomeSilentSaveSignDto.setSignEnterpriseName("常州钢构建设工程有限公司厦门分公司");//静默签署企业名
        becomeSilentSaveSignDto.setCustomerOriginationNumber("8374673663338222");//客户发起时定义的编号
        becomeSilentSaveSignDto.setInputValues("郑州沃薪科技有限公司,孙泽星,41152119850526,王舟舟,郑州沃薪科技有限公司,郑州沃薪科技有限公司,郑州沃薪科技有限公司,郑州沃薪科技有限公司,2024-05-31");//输入内容设置模板签署位置时要签署的内容    多个用逗号分隔
        becomeSilentSaveSignDto.setContractModelNum("2024053111351717126553386040");//模板编号
        becomeSilentSaveSignDto.setSignatureType("0");
        List<Map<String,String>> data=new ArrayList<>();
        Map<String,String> map = new HashMap<>();
        map.put("userName","孙泽星");//经办人姓名
        map.put("userPhone","15981832697");//经办人手机号
        map.put("type","0");//类型 0-个人 1-企业
        map.put("signMode","1");//签署印章 1用户手绘章 2用户模板章 3企业章  4合同章 5法人章，多个用逗号分隔
        // map.put("type","1");//类型 0-个人 1-企业
        // map.put("signMode","3");//签署印章 1用户手绘章 2用户模板章 3企业章  4合同章 5法人章，多个用逗号分隔
        // map.put("enterpriseName","贵州省七进制科技有限公司");//企业名称(类型type为1时必传)
        data.add(map);
        String s = JSONObject.toJSONString(data);
        becomeSilentSaveSignDto.setSignatoryInfos(s);//签署方相关信息

        String response = callThirdPartyApi(apiUrl, becomeSilentSaveSignDto);


        System.out.println(response);
        return null;

    }


    public static void main(String[] args) throws Exception{
        dome d = new dome();
        d.testV3();
    }



    @PostMapping(value = "/testV4")
    public String testV4()throws Exception
    {
//        String apiUrl = "http://192.168.124.5:8090/openAPI/contractSign/v4/becomeSilentSaveSign"; // 替换为实际的API地址

        String apiUrl = "https://openapi.signzg.com/openAPI/contractSign/v2/becomeSilentSaveSign";
        //String apiUrl = "https://openapi.signzg.com/openAPI/contractSign/v4/becomeSilentSaveSign"; // 替换为实际的API地址
        // 构建请求参数对象
        BecomeSilentSaveSingleSignatureDto becomeSilentSaveSignDto = new BecomeSilentSaveSingleSignatureDto();
        becomeSilentSaveSignDto.setApplicationId("2205026100");//应用id
        becomeSilentSaveSignDto.setBusinessId("95d6eb9ae14b4b6f8770467a37132c82");//合作方ID
        becomeSilentSaveSignDto.setContractName("测试合同92");//合同名称
        becomeSilentSaveSignDto.setLaunchType("1"); //发起类型 0-个人发起 1-企业发起
        becomeSilentSaveSignDto.setLaunchSubject("18137182576");//发起用户手机号
        becomeSilentSaveSignDto.setLaunchEnterpriseName("郑州沃薪科技有限公司");//发起企业名称(launchType为1企业发起合同时必传)
        becomeSilentSaveSignDto.setCustomerOriginationNumber("83746736681111223");//客户发起时定义的编号
        becomeSilentSaveSignDto.setInputValues("");//输入内容设置模板签署位置时要签署的内容    多个用逗号分隔
        becomeSilentSaveSignDto.setSignatureType("0");
//        becomeSilentSaveSignDto.setInputValues("");//输入内容设置模板签署位置时要签署的内容    多个用逗号分隔
//        becomeSilentSaveSignDto.setContractModelNum("2024041717141713345299606662");//模板编号
        becomeSilentSaveSignDto.setContractModelNum("2024041717141713345299606662");//模板编号


//        2024041717001713344451988901
        PcSignatoryDto pc = new PcSignatoryDto();
        pc.setUserName("罗啊啊");//姓名
        pc.setUserPhone("15981832697");//手机号
        pc.setEnterpriseName("常州钢构建设分公司");//公司名称
        pc.setSignMode("1");//1用户手绘章 2用户模板章 3企业章  4合同章 5法人章
//        pc.setSignLevel(1);//签署顺序
        pc.setType(0);//类型 0-个人 1-企业


        PcSignatoryDto pc1 = new PcSignatoryDto();
        pc1.setUserName("孙**");//姓名
        pc1.setUserPhone("1563**090");//手机号
        pc1.setSignMode("1");//1用户手绘章 2用户模板章 3企业章  4合同章 5法人章
        pc1.setSignLevel(2);//签署顺序
        pc1.setType(0);//类型 0-个人 1-企业


        PcSignatoryDto pc2 = new PcSignatoryDto();
        pc2.setUserName("魏**");//姓名
        pc2.setUserPhone("1563**090");//手机号
        pc2.setSignMode("1");//1用户手绘章 2用户模板章 3企业章  4合同章 5法人章
        pc2.setSignLevel(3);//签署顺序
        pc2.setType(0);//类型 0-个人 1-企业
        List<PcSignatoryDto> enterpriseSignDtoList=new ArrayList<>();
        enterpriseSignDtoList.add(pc);
//        enterpriseSignDtoList.add(pc1);
//        enterpriseSignDtoList.add(pc2);
//        becomeSilentSaveSignDto.setSignatoryInfos(enterpriseSignDtoList);

        String response = callThirdPartyApi(apiUrl, becomeSilentSaveSignDto);
        System.out.println(response);


        return null;
    }


    public static String callThirdPartyApi(String apiUrl, BecomeSilentSaveSingleSignatureDto becomeSilentSaveSignDto) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(apiUrl);

        ObjectMapper objectMapper = new ObjectMapper();
        String requestBody = objectMapper.writeValueAsString(becomeSilentSaveSignDto);

        System.out.println(requestBody);
        StringEntity requestEntity = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
        httpPost.setEntity(requestEntity);

        CloseableHttpResponse response = httpClient.execute(httpPost);
        try {
            HttpEntity entity = response.getEntity();
            BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
            StringBuilder responseBuilder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                responseBuilder.append(line);
            }
            return responseBuilder.toString();
        } finally {
            response.close();
        }
    }





}



@Data
class BecomeSilentSaveSingleSignatureDto{

    private String applicationId;

    private String businessId;

    private String launchType;

    private String launchSubject;

    private String launchEnterpriseName;

    private String customerOriginationNumber;
    private String contractModelNum;
    private String signEnterpriseName;
    private String contractName;
    private String signatoryInfos;

    private String inputValues;

    private String signatureType;




//    private List<PcSignatoryDto> signatoryInfos;




}


@Data
class PcSignatoryDto{

    private String userName;

    private String userPhone;

    private String enterpriseName;

    private String signMode;

    private Integer signLevel;

    private Integer type;
    private String signType;

}
