package com.sw.mes.yb.fls.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.UUID;


/**
 * @Author: GaoXinJian
 * @Date: 2022-01-12 15:12
 */

@Component
@Log4j2
public class RequestUtil {
    @Value("${spring.flsServiceUrl}")
    private String flsServiceUrl;
    @Value("${spring.profiles.active}")
    private String active;
    @Autowired
    RestTemplate restTemplate;

    public ResultDto postForFls(RequestDto<T> requestDto, String url) {
        ResponseEntity<String> result = null;
        ResultDto resultDto = new ResultDto();
        if (!active.equals("prod")) {
            log.info("非生产环境，不请求fls");
            resultDto.setMessage("请求成功");
            resultDto.setStatus(100);
            return resultDto;
        }
        Integer count = 0;
        while (true) {
            UUID uuid=UUID.randomUUID();
            log.info("【postForFls】【" + uuid + ":】" + url + requestDto.getServiceUrl() + "【requestDto:】" + JSONObject.toJSONString(requestDto));
            // 请求部分
            try {
                result = restTemplate.postForEntity(url + requestDto.getServiceUrl(), requestDto.getData(), String.class);
                log.info("【postForFls】【" + uuid + ":】【requestDto:】" + JSONObject.toJSONString(result));

                if (result.getStatusCodeValue() == 200) {
                    //请求成功
                    if (StringUtils.isBlank(result.getBody())) {
                        resultDto.setMessage("请求成功，返回信息为空");
                        resultDto.setStatus(result.getStatusCodeValue());
                        count++;
                        if (count > 2) {
                            break;
                        }
                        continue;
                    }

                    // 返回结果，解析部分
                    try {
                        resultDto = JSON.parseObject(result.getBody(), ResultDto.class);
                        break;
                    } catch (Exception e) {
                        resultDto.setMessage("【postForFls】请求成功，解析返回结果发生错误，返回信息：[" + result.getBody() + "]" + "，错误信息：" + e.getMessage());
                        resultDto.setStatus(500);
                    }
                    break;
                } else {
                    //请求失败
                    resultDto.setMessage("【postForFls】请求失败");
                    resultDto.setStatus(result.getStatusCodeValue());
                }

            } catch (Exception e) {
                // 发生异常，存储异常信息
                resultDto.setMessage("【postForFls】请求发生错误：" + e.getMessage());
                resultDto.setStatus(500);
                count++;
               log.error("【postForFls】【"+uuid+":】【requestDto:】"+ JSONObject.toJSONString(result));

                if (count > 2) {
                    break;
                }
                continue;
            }
        }

        return resultDto;
    }
}
