package com.hz.pm.api.irs;

import com.alibaba.fastjson.JSON;
import com.hz.pm.api.common.util.CryptUtils;
import com.hz.pm.api.common.util.HttpUtil;
import liquibase.util.MD5Util;
import org.junit.Test;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

/**
 * @Classname IrsTest2
 * @Description
 * @Date 2023/7/20 10:48
 * @Author PoffyZhang
 */
public class IrsTest2 {

    @Test
    public void test() throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String appSecret = "e066f496194540afa829c66983d4710e";
        String appkey = "8fb070a1f1194b2ebffdc6f596bb8a96";
        String dataName = URLEncoder.encode("丽水市大数据","UTF-8");
        long timestamp = System.currentTimeMillis();
        String sign = CryptUtils.MD5Encode(appkey + appSecret + timestamp);
        String url = "https://interface.ls.local/a/api/shared/1689239219101?data_name=" + dataName + "&appKey=" + appkey +
                "&sign=" + sign + "&requestTime=" + timestamp;
        RestTemplate restTemplate = new RestTemplate();
        System.out.println("url:" + url);
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);

        System.out.println(JSON.toJSONString(forEntity));
    }

    @Test
    public void refreshToken() throws NoSuchAlgorithmException {
        long timestamp = System.currentTimeMillis();
        String appSecret = "e066f496194540afa829c66983d4710e";
        String appKey = "8fb070a1f1194b2ebffdc6f596bb8a96";
        String sign = CryptUtils.MD5Encode(appKey + appSecret + timestamp);
        HttpComponentsClientHttpRequestFactory factory = HttpUtil.generateHttpRequestFactory();
        RestTemplate restTemplate;
        if(Objects.nonNull(factory)){
            restTemplate = new RestTemplate(factory);
        }else{
            restTemplate = new RestTemplate();
        }
        String url = "https://interface.ls.local/a/api/refreshTokenKey?appKey=" + appKey +
                "&requestTime=" + timestamp + "&sign=" + sign;
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
        System.out.println(forEntity.getBody());
    }

    @Test
    public void test3() throws NoSuchAlgorithmException {
        String timestamp = "1689841393364";
        String appSecret = "f12a6f2e9fc143fcbe4622295db097fd";
        String appkey = "8fb070a1f1194b2ebffdc6f596bb8a96";
        String sign1 = MD5Util.computeMD5(appkey + appSecret + timestamp);
        String sign2 = CryptUtils.MD5Encode(appkey + appSecret + timestamp);
        System.out.println(sign1);
        System.out.println(sign2);
    }

    @Test
    public void test4(){
        long timestamp = 1690354109541L;

        long now = System.currentTimeMillis();

        System.out.println( (timestamp - now)/1000 + "s");

    }
}
