package com.santos.iost.dao;

import com.alibaba.fastjson.JSON;
import com.santos.iost.utils.HttpUtil;
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);
        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 = HttpUtil.post(requestUrl + getMaterialUrl + "?token=" + token, paramsMap, hreader);
        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);
//        System.out.println(token);
//        System.out.println("================response=============="+post+"=================response===================");
        return post;
    }
}
