package com.jwipc.portal;

import com.jwipc.contract.logic.ContractLogic;
import com.jwipc.sys.client.SequenceClient;
import com.jwipc.test.BaseTest;
import okhttp3.*;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
public class ContractTest  extends BaseTest {
    @Autowired
    private ContractLogic contractLogic;
    private SequenceClient sequenceClient;

    @Test
    public void testContract() {
        contractLogic.sendSign("534");

    }

    //测试并发
    @Test
    @Transactional
    public void te111() {
        String aa =  sequenceClient.createSequenceNo("1111");

        //等待一个小时
        try {
            Thread.sleep(3600000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    @Test
    @Transactional
    public void te112() {
        String aa =  sequenceClient.createSequenceNo("2222");
        System.out.println(aa);
    }

    @Test
    public void te() {
        OkHttpClient client = new OkHttpClient();

        // JSON消息体
        String jsonBody = "{\"signerInfos\":[{\"signNode\":1,\"organizationCode\":\"a8eaa73758c4476cb4065b49cb8c2480\",\"allowAddAttachment\":0,\"userType\":1,\"signMode\":0,\"autoSign\":0,\"sealInfos\":[{\"fileKey\":\"$f6e5891c-a067-4e41-9338-72350e21b24c$2222561460\",\"signConfigs\":[{\"signFieldType\":0,\"sealId\":\"1780482834373406721\",\"allowMove\":0,\"keywordInfo\":{\"keyword\":\"甲方\",\"keywordIndex\":-1},\"edgeScope\":0,\"addSignDate\":false,\"signatureType\":\"COMMON-SEAL\",\"signType\":\"KEYWORD-SIGN\",\"handEnable\":false}]}],\"userCode\":\"zhoux\",\"signOrder\":1},{\"signNode\":2,\"organizationCode\":\"971cb2d189034cda888b92298d78ecca\",\"allowAddAttachment\":0,\"sealTypeCode\":\"\",\"userType\":2,\"signMode\":0,\"autoSign\":0,\"sealInfos\":[{\"fileKey\":\"$f6e5891c-a067-4e41-9338-72350e21b24c$2222561460\",\"signConfigs\":[{\"signFieldType\":0,\"allowMove\":0,\"keywordInfo\":{\"keyword\":\"乙方\",\"keywordIndex\":-1},\"edgeScope\":0,\"addSignDate\":false,\"signatureType\":\"COMMON-SEAL\",\"signType\":\"KEYWORD-SIGN\",\"handEnable\":false}]}],\"userCode\":\"HZTGXXKJYXGS\",\"signOrder\":2}],\"signFiles\":[{\"fileKey\":\"$f6e5891c-a067-4e41-9338-72350e21b24c$2222561460\"}],\"redirectUrl\":\"www.baidu.com\",\"subject\":\"材料类合同20240415-001\",\"remark\":\"\",\"readComplete\":1,\"businessTypeCode\":\"fb5c9f2c27e6cfd5ed5bd7514fa8b1a4\",\"initiatorInfo\":{\"userCode\":\"zhoux\",\"organizationCode\":\"a8eaa73758c4476cb4065b49cb8c2480\"},\"businessNo\":\"contract\",\"chargingType\":1,\"signNotifyUrl\":\"\",\"advertisement\":0,\"manualConfig\":{\"startMode\":0,\"finishMode\":0}}";

        // 创建RequestBody对象
        RequestBody body = RequestBody.create(
                MediaType.parse("application/json; charset=utf-8"), jsonBody);

        // 创建Request对象
        Request request = new Request.Builder()
                .url("http://eqbtest.jwipc.com:8199/esign-signs/v1/signFlow/createAndStart") // 设置URL
                .post(body) // 设置POST方法和消息体
                .addHeader("Content-Type", "application/json") // 设置请求头
                .addHeader("X-timevale-project-id", "1000001") // 可以添加其他请求头
                .addHeader("X-timevale-signature", "bcba351d866b01bfcd59e6ecaeb9a40bc2cfc2176295d8c20e89cc7e3046f74c") // 可以添加其他请求头
                .addHeader("Accept", "*/*") // 可以添加其他请求头
                .build();

        try (Response response = client.newCall(request).execute()) {
            // 处理响应
            if (response.isSuccessful()) {
                // 请求成功
                String responseBody = response.body().string();
                System.out.println(responseBody);
            } else {
                // 请求失败
                System.out.println("Error: " + response.message());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
