package com.prolog.rdc.tms.g7;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.prolog.common.util.ToolKits;
import com.prolog.rdc.tms.g7.constant.Constants;
import com.prolog.rdc.tms.g7.constant.ContentType;
import com.prolog.rdc.tms.g7.constant.HttpHeader;
import com.prolog.rdc.tms.g7.constant.HttpSchema;
import com.prolog.rdc.tms.g7.enums.Method;
import com.prolog.rdc.tms.g7.util.MessageDigestUtil;
import com.prolog.rdc.tms.model.dto.g7.CurrentTemperatureDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

@Component
@Slf4j
public class G7Conn {

    // token地址
    @Value("${prolog.interface.g7.accessId}")
    private String ACCESS_ID;
    // APP密钥
    @Value("${prolog.interface.g7.secretKey}")
    private String SECRET_KEY;
    //API域名
    @Value("${prolog.interface.g7.baseurl}")
    private String BaseURL;

    public JSONObject connect(Map<String, String> querys,String path) throws Exception {
        Map<String, String> headers = new HashMap<String, String>();
        headers.put(HttpHeader.HTTP_HEADER_G7_TIMESTAMP, "" + System.currentTimeMillis());
        Request request = new Request(Method.GET, HttpSchema.HTTP + BaseURL, path, ACCESS_ID, SECRET_KEY, Constants.DEFAULT_TIMEOUT);
        request.setHeaders(headers);
        //请求的query
        request.setQueries(querys);
        //调用服务端
        Response response = Client.execute(request);
        if (200 == response.getStatusCode()) {
            JSONObject object = JSONObject.parseObject(response.getBody());
            log.info("返回结果:{}",object);
            return object;
        }
        return null;
    }

    public JSONObject post(Map<String, String> querys,String path) throws Exception {
        Map<String, String> headers = new HashMap<String, String>();
        headers.put(HttpHeader.HTTP_HEADER_G7_TIMESTAMP, "" + System.currentTimeMillis());
        headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_JSON);
        headers.put(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.base64AndMD5(JSON.toJSONString(querys)));
        Request request = new Request(Method.POST_JSON, HttpSchema.HTTP + BaseURL, path, ACCESS_ID, SECRET_KEY, Constants.DEFAULT_TIMEOUT);
        request.setHeaders(headers);
        //请求的query
//        request.setQueries(querys);
        request.setJsonStrBody(JSON.toJSONString(querys));
        //调用服务端
        Response response = Client.execute(request);
        if (200 == response.getStatusCode()) {
            JSONObject object = JSONObject.parseObject(response.getBody());
            log.info("返回结果:{}",object);
            return object;
        }
        return null;
    }

//    public static void main(String[] args) throws Exception {
////        Map<String, String> headers = new HashMap<String, String>();
////        headers.put(HttpHeader.HTTP_HEADER_G7_TIMESTAMP, "" + System.currentTimeMillis());
////        headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_JSON);
////
//////        String path = "/v1/coldchain/daily/temperatureCurrents";
//////        String path = "/v1/device/truck/history_location_by_gpsno";
////        String path = "/v1/device/equipment/simple_currents_by_gpsnos";
////        Map<String, String> querys = Maps.newHashMap();
//////        querys.put("gpsnos", "90037836");
////
////        querys.put("gpsnos", "71017854");
//////        querys.put("from", DateUtil.formatDateTime(DateUtil.offsetDay(DateUtil.date(), -1)));
//////        querys.put("to",DateUtil.now());
////
//////        querys.put("gpsnos", "71017854");
////
////        Request request = new Request(Method.POST_JSON, HttpSchema.HTTP + "openapi.huoyunren.com", path, "bzzf13", "dc1uoqdcfdbtzhw4mkudga6n8xh8zbzf", Constants.DEFAULT_TIMEOUT);
////        request.setHeaders(headers);
////        //请求的query
//////        request.setQueries(querys);
//////        request.setBodies(querys);
////        headers.put(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.base64AndMD5(JSON.toJSONString(querys)));
////        request.setJsonStrBody(JSON.toJSONString(querys));
////        //调用服务端
////        Response response = Client.execute(request);
////        System.out.println(JSON.toJSONString(response));
////
////        Map<String, String> headers = new HashMap<String, String>();
////        headers.put(HttpHeader.HTTP_HEADER_G7_TIMESTAMP, "" + System.currentTimeMillis());
////        headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_JSON);
//
//        Map<String, String> headers = new HashMap<String, String>();
//        headers.put(HttpHeader.HTTP_HEADER_G7_TIMESTAMP, "" + System.currentTimeMillis());
//        String path = "/v1/coldchain/daily/temperatureCurrents";
////        String path = "/v1/device/truck/history_location_by_gpsno";
//        Map<String, String> querys = Maps.newHashMap();
//        querys.put("gpsnos", "71017854");
////        querys.put("from", DateUtil.formatDateTime(DateUtil.offsetDay(DateUtil.date(), -1)));
////        querys.put("to",DateUtil.now());
//        Request request = new Request(Method.GET, HttpSchema.HTTP + "openapi.huoyunren.com", path, "bzzf13", "dc1uoqdcfdbtzhw4mkudga6n8xh8zbzf", Constants.DEFAULT_TIMEOUT);
//        request.setHeaders(headers);
//        //请求的query
//        request.setQueries(querys);
//        //调用服务端
//        Response response = Client.execute(request);
//        System.out.println(JSON.toJSONString(response));
//        if (200 == response.getStatusCode()) {
//            JSONObject object = JSONObject.parseObject(response.getBody());
//            log.info("返回结果:{}",object);
//        }
//    }

}
