package com.ruoyi.common.utils.http;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.StringUtils;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 飞瓜 okHttp3 接口数据抓取Utils
 */
public class OkHttpFGUtils {

    private static final Logger log = LoggerFactory.getLogger(OkHttpFGUtils.class);

    public  static  String sentPOST(String url,String param,String PHPSESSID){
        try {

            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, param);
            Request request = new Request.Builder()
                    .url(url)
                    .method("POST", body)
                    .addHeader("Accept", " text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
                    .addHeader("Accept-Encoding", " gzip, deflate")
                    .addHeader("Accept-Language", " zh-CN,zh;q=0.9")
                    .addHeader("Cache-Control", " max-age=0")
                    .addHeader("Connection", " keep-alive")
                    .addHeader("Cookie", " PHPSESSID="+PHPSESSID+"; body_collapsed=0")
                    .addHeader("Host", " 120.27.232.118")
                    .addHeader("Upgrade-Insecure-Requests", " 1")
                    .addHeader("User-Agent", " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36")
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();
            return response.body().toString();
        } catch (Exception e) {
            log.error("调用OkHttpFGUtils.sendPost Exception, url=" + url + ",param=" + param, e);
            return StringUtils.EMPTY;
        }
    }


    public static String sendGet(String url,String PHPSESSID){
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            Request request = new Request.Builder()
                    .url(url)
                    .addHeader("Accept", " text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
                    .addHeader("Accept-Encoding", " gzip, deflate")
                    .addHeader("Accept-Language", " zh-CN,zh;q=0.9")
                    .addHeader("Cache-Control", " max-age=0")
                    .addHeader("Connection", " keep-alive")
                    .addHeader("Cookie", " PHPSESSID="+PHPSESSID+"; body_collapsed=0")
                    .addHeader("Host", " 120.27.232.118")
                    .addHeader("Upgrade-Insecure-Requests", " 1")
                    .addHeader("User-Agent", " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36")
                    .build();
            Response response = client.newCall(request).execute();
            return response.body().toString();
        } catch (Exception e) {
            log.error("调用OkHttpFGUtils.sendGet Exception, url=" + url , e);
            return StringUtils.EMPTY;
        }
    }



    public static <object> void main(String[] args) {

        String URL = "http://120.27.232.118/api/v1/goodsDataOverView/salesTrendData";

//        List<SalesTrendData> resultList = new ArrayList<>();

        String result = HttpUtils.sendGet(URL,"periodType=90&fromDateCode=20230829&toDateCode=20231126&cateId=20004");

        Map maps = (Map) JSON.parse(result);
        for(Object map : maps.entrySet()){
            if(((Map.Entry)map).getKey().toString().equals("Data")){

                JSONArray dataJArray = JSONObject.parseArray(((Map.Entry)map).getValue().toString());
                List<Object> dataList = dataJArray;

                for (Object po:
                dataList) {
                    Map poMap = (Map) JSON.parse(po.toString());

                    poMap.get("DateCode").toString();


                }
            }
        }
    }


    /**
     * 封装返回数据类型
     * @param result
     * @return
     */
    public static Map<String, List<Object>> getSchoolMap(String result){

        List<Object> majorsList = new ArrayList<>();
        List<Object> indsList = new ArrayList<>();
        List<Object> rankingsList = new ArrayList<>();
        Map maps = (Map) JSON.parse(result);
        for(Object map : maps.entrySet()){
            if(((Map.Entry)map).getKey().toString().equals("data")){

                Map dataMap = (Map) JSON.parse( ((Map.Entry)map).getValue().toString());

                JSONArray inds = JSONObject.parseArray(dataMap.get("inds").toString());
                indsList = inds;

                JSONArray majors = JSONObject.parseArray(dataMap.get("majors").toString());
                majorsList = majors;

                JSONArray rankings = JSONObject.parseArray(dataMap.get("rankings").toString());
                rankingsList = rankings;

            }
        }
        Map<String, List<Object>> resultMap = new HashMap<>();

        resultMap.put("majorsList",majorsList);
        resultMap.put("indsList",indsList);
        resultMap.put("rankingsList",rankingsList);

        return resultMap;
    }



}
