package com.friday.custody.service.trustService.XingYe.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.friday.custody.service.trustService.XingYe.common.XYConstants;
import com.friday.custody.service.basicService.InvokeRecordService;
import com.friday.custody.service.basicService.PullDataFailureLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author K0051721
 * @Date: 2022/3/22
 */
@Slf4j
@Service("xYHttpUtil")
public class XYHttpUtil {


    @Autowired
    private PullDataFailureLogService pullDataFailureLogService;

    @Autowired
    private InvokeRecordService invokeRecordService;

    public HashMap sendMessage(String api) {
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setReadTimeout(120*1000);
        requestFactory.setConnectTimeout(120*1000);
        RestTemplate restTemplate = new RestTemplate(requestFactory);

        try {
            Thread.sleep(1000l);
        } catch (InterruptedException e) {
            log.error("xYHttpUtil 睡眠被打断了!",e);
            Thread.currentThread().interrupt();
        }

        log.info("兴业XYHttpUtil的sendMessage入参情况为api={}", api);
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        String urlWithParam = XYConstants.URL+api;
        HttpEntity<Map<String,Object>> httpEntity = new HttpEntity<>(null, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(urlWithParam, httpEntity, String.class);
        HashMap map = JSONObject.parseObject(responseEntity.getBody(), HashMap.class);
        log.info("获取兴业数据:{},api:{}", JSON.toJSONString(map),api);
        return map;
    }
}

