package com.hz.pm.api.irs;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
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 com.hz.pm.api.common.util.Md5Utils;
import com.hz.pm.api.common.util.RefreshKeyUtil;
import com.hz.pm.api.gov.model.req.ProjectPushReq;
import com.hz.pm.api.irs.model.dto.ForwardDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * @Classname CoreTest
 * @Description
 * @Date 2023/8/31 17:21
 * @Author PoffyZhang
 */
@Slf4j
public class CoreTest  {//extends AppTests

//    @Value("${irs.core-biz.appKey}")
    private String appKey = "A331101453557202109017383";

//    @Value("${irs.core-biz.appSecret}")
    private String appSecret = "496f0f2a19994f76b4fd9dae087366c7";

//    @Value("${irs.core-biz.url}")
    private String coreBizUrl = "https://interface.zjzwfw.gov.cn/gateway/api/001008012012001/dataSharing/Fc3re2cq7r64Qfa7.htm";

//    @Value("${irs.core-biz.interfaceName}")
    private String interfaceName = "Fc3re2cq7r64Qfa7";

    @Resource
    private IRefreshTokenService refreshTokenService;

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

    @Test
    public void searchCoreBiz() {
        long requestTime = System.currentTimeMillis();
        String businessName = "";
        String orgCode = "";
        Integer limit = 10;
        Integer page = 1;

        String secret = refreshTokenService.refreshToken(appKey, appSecret, govRequestTokenUrl, govRefreshTokenUrl,
                 HttpMethod.POST.name());
//        String secret = "15345863c0fb4a85a28b2b467e5e6253";
//        String secret = appSecret;
//        String secret = RefreshKeyUtil.getRequestSecret(appKey, appSecret,requestTime);
//        String secret = RefreshKeyUtil.refreshSecret(appKey, "78cde959148b4335b8eba556f9c8c7bf",requestTime);
        System.out.println("secret:" + secret);
        HttpComponentsClientHttpRequestFactory factory = HttpUtil.generateHttpRequestFactory();
        RestTemplate restTemplate;
        if(Objects.nonNull(factory)){
            restTemplate = new RestTemplate(factory);
        }else{
            restTemplate = new RestTemplate();
        }
        ForwardDTO dto = new ForwardDTO();
        Map<String,Object> map = Maps.newHashMap();
        map.put("dingCode",orgCode);
        map.put("matterName",(StringUtils.isNotBlank(businessName)?businessName:StringUtils.EMPTY));
        map.put("useState",1);
        map.put("limit",limit);
        map.put("page",page);
        map.put("appKey",appKey);
        dto.setData(map);
        dto.setUrl(coreBizUrl);
        dto.setSecret(secret);
        dto.setAppKey(appKey);
        dto.setSecret(secret);
        dto.setRequestTime(requestTime);
        String sign = Md5Utils.hash(appKey + secret + requestTime);
        dto.setSign(sign);
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        HttpEntity<ForwardDTO> formEntity = new HttpEntity<ForwardDTO>(dto, headers);
        String url = "http://10.53.168.41:38088/open/api/v1/project-receive/forward";
        ResponseEntity<String> forEntity = restTemplate.postForEntity(url,formEntity, String.class);
        String body = forEntity.getBody();
        System.out.println(body);
    }
}
