package com.hz.pm.api.irs;

import com.ningdatech.irs.service.IRefreshTokenService;
import com.hz.pm.api.AppTests;
import com.hz.pm.api.common.util.CryptUtils;
import com.hz.pm.api.common.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

/**
 * @Classname irsTest3
 * @Description
 * @Date 2023/7/25 9:52
 * @Author PoffyZhang
 */
@Slf4j
public class irsTest3 extends AppTests {

    @Resource
    private IRefreshTokenService refreshTokenService;

    @Value("${irs.interface-local-refresh.request-token-url}")
    private String requestTokenUrl;
    @Value("${irs.interface-local-refresh.refresh-token-url}")
    private String refreshTokenUrl;

    @Value("${irs.interface-refresh.request-token-url}")
    private String govRequestTokenUrl;
    @Value("${irs.interface-refresh.refresh-token-url}")
    private String govRefreshTokenUrl;

    @Test
    public void test1() throws NoSuchAlgorithmException {
        long timestamp = System.currentTimeMillis();
        String appSecret = "496f0f2a19994f76b4fd9dae087366c7";
        String appKey = "A331101453557202109017383";
        String dataName = "大数据管理局";
        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/shared/1689239219101?" +
                "data_name=" + dataName + "&appKey=" + appKey + "&sign=" + sign + "&requestTime=" + timestamp;

        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
        System.out.println(forEntity.getBody());
    }

    @Test
    public void test2() throws NoSuchAlgorithmException {
        long timestamp = System.currentTimeMillis();
        String appSecret = "496f0f2a19994f76b4fd9dae087366c7";
        String appKey = "A331101453557202109017383";
        String method = "POST";
        String secret = refreshTokenService.refreshToken(appKey,appSecret,govRequestTokenUrl,govRefreshTokenUrl,method);
        String sign = CryptUtils.MD5Encode(appKey + secret + timestamp);
        HttpComponentsClientHttpRequestFactory factory = HttpUtil.generateHttpRequestFactory();
        RestTemplate restTemplate;
        if(Objects.nonNull(factory)){
            restTemplate = new RestTemplate(factory);
        }else{
            restTemplate = new RestTemplate();
        }
        String appId = "A331123467587202307014177";
        String updateTime = "2023-07-26 00:00:00";
        String capCode = timestamp + updateTime + appId;
        String url = "https://interface.zjzwfw.gov.cn/gateway/api/proxy/001003001029/" +
                "dataSharing/5crv7i4727abLCv0.htm?requestTime=" + timestamp +
                "&sign=" + sign + "&appKey=" + appKey +
                "&capCode=abc&appId=abc&capTime=" + timestamp +
                "&appId=" + appId + "&updateTime=" + updateTime;
        log.info("url :{}",url);
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
        log.info(forEntity.getBody());
    }

}
