package com.weaver.seconddev.chapanda.beisen.util;

import com.alibaba.fastjson.JSONObject;
import com.weaver.seconddev.chapanda.beisen.constant.Constants;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;

@Component
public class Esb2BeisenUnRelateRequestUtil {
    private final static Logger log = LoggerFactory.getLogger(Esb2BeisenUnRelateRequestUtil.class);

    @Autowired
    Token2BeiSenUtil token2BeiSenUtil ;
    public String unRelateJobRequirement(String requirementId){

        String dataCode = "";
        String token = token2BeiSenUtil.getToken();
        if(StringUtils.isNotBlank(token)){
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            JSONObject bodyJson = new JSONObject();
            int operationType = 1;
            bodyJson.put("operationType",operationType);
            bodyJson.put("id",requirementId);

            RequestBody body = RequestBody.create(mediaType, bodyJson.toJSONString());
            try {
                Request request = new Request.Builder()
                        .url(Constants.beishenHost+Constants.unRelateRequirementUrl)
                        .method("POST", body)
                        .addHeader("Authorization", "Bearer "+token)
                        .addHeader("Content-Type", "application/json")
                        .build();
                Response response = client.newCall(request).execute();

                int code = response.code();
                String bodyData = response.body().string();
                log.error("code:{}",code);
                log.error("bodyData:{}",bodyData);
                if(code == 200){
                    if(StringUtils.isNotBlank(bodyData)){
                        JSONObject dataJson = JSONObject.parseObject(bodyData);
                        if(dataJson.containsKey("code")){
                            dataCode = dataJson.getString("code");
                        }
                    }
                }else{
                    log.error("bodyData:"+bodyData);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return dataCode;
    }
}
