package com.sw.mes.yb.wms.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(value = "requestUtilsWms")
@Log4j2
public class RequestWmsUtils {

    // 第三方WMS
    @Value("${spring.wmsServiceUrl}")
    private String wmsServiceUrl;

    // MCS中WMS
    @Value("${spring.wmsServiceUrl2}")
    private String wmsServiceUrl2;

    @Value("${spring.profiles.active}")
    private String active;

    @Autowired
    RestTemplate restTemplate;

    public ResultWmsDto postForWms(RequestWmsDto<T> requestDto) {
        ResponseEntity<String> result = null;
        ResultWmsDto resultDto = new ResultWmsDto();

        if (!active.equals("prod")){
            log.info("非生产环境，不请求wms");
            resultDto.setMessage("非生产环境，不请求wms");
            resultDto.setCode(200);

            return  resultDto;
        }
        Integer count = 0;
        while (true) {

            UUID uuid=UUID.randomUUID();
            log.info("【postForwms】【"+uuid+":】"+wmsServiceUrl + requestDto.getServiceUrl()+"【requestDto:】"+ JSONObject.toJSONString(requestDto));
            // 请求部分
            try {
                String wmsUrl = wmsServiceUrl;
                if(requestDto.getUrlTag() != null && requestDto.getUrlTag() == 2) {
                    wmsUrl = wmsServiceUrl2;
                }
                result = restTemplate.postForEntity(wmsUrl + requestDto.getServiceUrl(), requestDto.getData(), String.class);
                log.info("【postForwms】【"+uuid+":】【requestDto:】"+ JSONObject.toJSONString(result));

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

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

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

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

        return resultDto;
    }
}
