package com.benyun.project.third.longxiang.test;

import cn.hutool.core.map.MapUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.benyun.common.utils.JsonUtil;
import com.benyun.common.utils.MapUtils;
import com.benyun.common.utils.StringUtils;
import com.benyun.project.third.longxiang.domain.*;
import com.github.pagehelper.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

@Slf4j
public class InvokeTest {

    private final static String Token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Im1hbnl0cmVlLWlkIn0.eyJ1aWQiOiIiLCJhdWQiOlsiMEM4UzJGNjJHOVZLIl0sInVzZXJfbmFtZSI6bnVsbCwic2NvcGUiOlsiMFA4UDdKQUk5MlBTIl0sImV4cCI6MTcwMTg1NTg2MCwiaWF0IjoxNzAxODU0NjYwLCJwcm9qZWN0SWQiOiIwUDhQN0pBSTkyUFMiLCJqdGkiOiI3MTNmNWE5MC1hN2RmLTRhNDgtYWM0MS03NDgyYjFhNzNiMDgiLCJwZXJtaXNzaW9uSWRzIjpbXSwiY2xpZW50X2lkIjoiMEM4UzcwQVRYQzAwIn0.Lq9HOKsZjOfInY7ZNx5b3NsN2_LUYNoay9OShVc6YGKkHBGgEoq27UTvG-ALHrkYpDzWNwhARe4P4NPK2S_0zL2HDLDD3gJzij5ItktMiP4DUaWBqBpppSw5eHVO6Rx4xtbXxtYP2Uu8mvnJeAbRk0YJoHMwnNLsVqGKNfvfHiEV9HsYQjgjBSOsAwT_etBK41svqFz4PjaFZx4Sq5mOptNSwsjV1QRjcdF6jHN2Yc1rtIZROoajsAmHvoDyq-rORUIpfjhZA0azd8Uo3RXp701O30qj4e91uEggmAbp6dyLfGfrBv1FvxsKl2JXawFOfgf4xzcAUINMqS0nrX9quw";

    public static void main(String[] args) {
//        String code = getCode();
//        if(null==code){
//            return;
//        }
//        String token = getToken(code);
//        System.out.println(token);
        ResponseWmsCustomer owner = getOwner(Token);
        System.out.println(owner);

//        createOrder(null);
    }

    //获取code
    private static String getCode() {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("client_id", "0C8S70ATXC00")
                .addFormDataPart("project_id", "0P8P7JAI92PS")
                .addFormDataPart("redirect_uri", "http://127.0.0.1:8081")
                .addFormDataPart("response_type", "code")
                .addFormDataPart("state", "login")
                .build();
        Request request = new Request.Builder()
                .url("http://47.107.53.207:9019/ua/login_page")
                .method("POST", body)
                .addHeader("Authorization", "Bearer {{access_token}}")
                .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
                .build();
        try {
            Response response = client.newCall(request).execute();
            String string = response.body().string();
            if(200!=response.code()){
                ResponseCodeErrorMsg jsonToObject = JsonUtil.buildNonNullBinder().getJsonToObject(string, ResponseCodeErrorMsg.class);
                log.error("获取code失败"+jsonToObject.getErrors().get(0));
                return null;
            }
            ResponseCode jsonToObject = JsonUtil.buildNonNullBinder().getJsonToObject(string, ResponseCode.class);
            return jsonToObject.getAuthorize_code();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    //获取token
    private static String getToken(String authorize_code) {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("client_id", "0C8S70ATXC00")
                .addFormDataPart("client_secret", "123456")
                .addFormDataPart("redirect_uri", "http://127.0.0.1:8081")
                .addFormDataPart("authorize_code", authorize_code)
                .build();
        Request request = new Request.Builder()
                .url("http://47.107.53.207:9019/ua/access_token")
                .method("POST", body)
                .addHeader("Authorization", "Bearer {{access_token}}")
                .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
                .build();
        try {
            Response response = client.newCall(request).execute();
            String string = response.body().string();
            ResponseToken jsonToObject = JsonUtil.buildNonNullBinder().getJsonToObject(string, ResponseToken.class);
            return jsonToObject.getAccess_token();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //货主查询
    private static ResponseWmsCustomer getOwner(String token) {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\r\n    \"biz_content\": {\r\n        \"tenant_id\": \"000009\"\r\n    }\r\n}");
        Request request = new Request.Builder()
                .url("http://47.107.53.207:9019/oms/omsPartnerQuery")
                .method("POST", body)
                .addHeader("Authorization", "Bearer " + token)
                .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
                .addHeader("Content-Type", "application/json")
                .build();

        try {
            Response response = client.newCall(request).execute();
            String string = response.body().string();
            ResponseMsg jsonToObject = JsonUtil.buildNonNullBinder().getJsonToObject(string, ResponseMsg.class);
            List<HashMap<String, Object>> data = (List<HashMap<String, Object>>) jsonToObject.getData();
            if(null == data || data.size()==0){
                return null;
            }
            Map<String, Object> stringObjectHashMap = data.get(0);
            ResponseWmsCustomer wmsCustomer = (ResponseWmsCustomer)MapUtils.mapToObject(stringObjectHashMap, ResponseWmsCustomer.class);

            log.info("wmsCustomer:"+wmsCustomer);
            return wmsCustomer;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //订单创建
    private static void createOrder(OrderCreateParams order){
        RequestOrderDomain requestOrderDomain = new RequestOrderDomain();
        requestOrderDomain.setBiz_content(order);
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, JsonUtil.toJsonString(requestOrderDomain));
        Request request = new Request.Builder()
                .url("http://47.107.53.207:9019/oms/createOrder")
                .method("POST", body)
                .addHeader("Authorization", "Bearer " + Token)
                .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
                .addHeader("Content-Type", "application/json")
                .build();


        try {
            Response response = client.newCall(request).execute();
            String string = response.body().string();
            ResponseMsg jsonToObject = JsonUtil.buildNonNullBinder().getJsonToObject(string, ResponseMsg.class);
            List<HashMap<String, Object>> data = (List<HashMap<String, Object>>) jsonToObject.getData();
            log.info("data", data);
            if(null == data || data.size()==0){
            }
            Map<String, Object> stringObjectHashMap = data.get(0);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
