package com.xjrsoft.module.xycxedu.util;

import cn.hutool.http.HttpUtil;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;

/**
 * @author dzx
 * @date 2024/7/17
 */
@Slf4j
public class ApiUtil {

    private final static int schoolid = 189;
    private final static String sign = "888";
    private final static String accessid = "86a5d02ae9f78470";

    private final static String host = "https://openapi.xycxedu.com";


    public static JsonArray getExamList() {
        String url = host + "/exam/list?schoolid=" + schoolid + "&accessid=" + accessid + "&sign=" + sign;
        String result = HttpUtil.get(url);
        JsonParser parser = new JsonParser();
        JsonObject asJsonObject = parser.parse(result).getAsJsonObject();
        if(asJsonObject.get("data").isJsonNull()){
            return new JsonArray();
        }
        JsonArray jsonArray = asJsonObject.get("data").getAsJsonArray();
        return jsonArray;
    }

    public static JsonArray getStudentScoreByCourse(Integer examid) {
        long timeMillis = System.currentTimeMillis();
        String url = host + "/exam/totalscore?schoolid=" + schoolid + "&accessid=" + accessid + "&sign=" + sign + "&examid=" + examid + "&timestamp=" + timeMillis;
        String result = HttpUtil.get(url);
        JsonParser parser = new JsonParser();
        JsonObject asJsonObject = parser.parse(result).getAsJsonObject();
        if(asJsonObject.get("data").isJsonNull()){
            return new JsonArray();
        }
        JsonArray jsonArray = asJsonObject.get("data").getAsJsonArray();
        return jsonArray;
    }

    /**
     * 根据考试id获取考试下的科目信息
     * @param milexamid
     * @return
     */
    public static JsonArray getCourseByExam(Long milexamid) {
        long timeMillis = System.currentTimeMillis();
        String url = host + "/exam/course?schoolid=" + schoolid + "&accessid=" + accessid + "&sign=" + sign + "&milexamid=" + milexamid + "&timestamp=" + timeMillis;
        String result = HttpUtil.get(url);
        JsonParser parser = new JsonParser();
        JsonObject asJsonObject = parser.parse(result).getAsJsonObject();
        if(asJsonObject.get("data").isJsonNull()){
            return new JsonArray();
        }
        JsonArray jsonArray = asJsonObject.get("data").getAsJsonArray();
        return jsonArray;
    }

}
