package com.santos.iost.dao;

import com.alibaba.fastjson.JSON;
import com.santos.iost.utils.HttpClientUtil;
import com.santos.iost.utils.HttpUtil;
import com.santos.iost.utils.JsonUtil;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class SoSsDao {
    public  String getTokenUrl = "http://123.58.109.39:86/K3API/Token/Create?authorityCode=635e8cd049f1407db2b25b97738d02c7d686307e003c6213";
    String requestUrl = "http://123.58.109.39:86";
    public static String token;

    Map<String, String> hreader = new HashMap<>();

//    @PostConstruct
    public void init() throws IOException {
        String s = HttpUtil.get(getTokenUrl);
        Map map = JSON.parseObject(s, Map.class);
        String D = map.get("Data").toString();
        Map mapDate = JSON.parseObject(D, Map.class);
        this.token = mapDate.get("Token").toString();
        System.out.println(token);

    }

    public SoSsDao() throws Exception {
        this.hreader.put("Content-Type", "application/json");
    }
//请求主体
    public String requestK3Api(String getMaterialUrl, String paramsMap) throws Exception {
        System.out.println("====head====="+paramsMap+"====head====");
//        String post = HttpUtil.post(requestUrl + getMaterialUrl + "?token=" + token, paramsMap, hreader);//以弃用
        String post = HttpClientUtil.doPostJson(requestUrl + getMaterialUrl + "?token=" + token, paramsMap);
        System.out.println(token);
//        System.out.println("================response=============="+post+"=================response===================");
        return post;
    }
    public String requestK3ApiDetail(String getMaterialUrl, String paramsMap) throws Exception {
        System.out.println("====Detail====="+paramsMap+"====Detail====");
//        String post = HttpUtil.post(requestUrl + getMaterialUrl + "?token=" + token, paramsMap, hreader);
        String post = HttpClientUtil.doPostJson(requestUrl + getMaterialUrl + "?token=" + token, paramsMap);
//        System.out.println(token);
//        System.out.println("================response=============="+post+"=================response===================");
        return post;
    }
    //
    public String setK3Api(String getMaterialUrl, String paramsMap) throws Exception {
        System.out.println("====head====="+paramsMap+"====head====");
        String post = HttpClientUtil.doPostJson(requestUrl + getMaterialUrl + "?token=" + token, paramsMap);
        System.out.println(token);
//        System.out.println("================response=============="+post+"=================response===================");
        return post;
    }



}
