package com.ruoyi.spider.ie;

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

import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.spider.utils.DBUtil;
import com.ruoyi.spider.utils.HttpClientUtil;
import com.ruoyi.spider.utils.UsignUtils;

import cn.hutool.core.lang.Dict;
import cn.hutool.db.Entity;
import okhttp3.Headers;

public class AbstraceService {
    public Logger logger = LoggerFactory.getLogger(getClass());
    public static String host = "https://questions.csdhe.com";
//    public static String host = "https://120.26.169.242";
    /** 省份 */
//    public static Integer provinceId = 5604;

    public String getHost() {
        return host;
    }

    List<Entity> find(Entity where) {
        return DBUtil.find(null, where);
    }

    Long count(Entity where) {
        return DBUtil.count(null, where);
    }

    void insertOrUpdate(Entity record, String... keys) {
        DBUtil.insertOrUpdate(null, record, keys);
    }

    void updateById(Entity record) {
        DBUtil.updateById(null, record);
    }

    void update(Entity record, Entity where) {
        DBUtil.update(null, record, where);
    }


    /**
     * 掌上高考
     * @param requestApi
     * @param params
     * @param isSign
     * @return
     */
    public JSONObject getZsgkJson(String requestApi, Dict params,Boolean isSign) {
        if (isSign){
            String safeSign = UsignUtils.getZsgkUsign(requestApi);
            if (StringUtils.isNotBlank(safeSign)){
                requestApi = requestApi+"&signsafe="+safeSign;
            }
        }
        String res = HttpClientUtil.get(requestApi, params);
        return JSONObject.parseObject(res);
    }

    /**
     * 非授权登录请求
     *
     * @param api
     * @param params
     * @return
     */
    public JSONObject getJson(String api, Dict params) {
        String url = host + api;
        long time = System.currentTimeMillis() / 1000;
        params.set("timestrap", "" + time);
        params.set("sign", UsignUtils.getUsignAll2(url, JSON.toJSONString(params)));
        String res = HttpClientUtil.get(url, params);
        return JSONObject.parseObject(res);
    }

    /**
     * 授权登录请求
     *
     * @param api
     * @param params
     * @param authorization
     * @return
     */
    public JSONObject getJson(String api, Dict params, String authorization) {
        String url = host + api;
        long time = System.currentTimeMillis() / 1000;
        params.set("timestrap", "" + time);
        params.set("sign", UsignUtils.getUsignAll2(url, JSON.toJSONString(params)));
        Headers headers = Headers.of("authorization", authorization);
        String res = HttpClientUtil.get(url, "UTF-8", params, headers);
        return JSONObject.parseObject(res);
    }

    public JSONObject postJson(String api, Dict params, Map<String, String> data, String authorization) {
        String url = host + api;
        long time = System.currentTimeMillis() / 1000;
        params.set("timestrap", "" + time);
        params.set("sign", UsignUtils.getUsignAll2(url, JSON.toJSONString(params)));
        Headers headers = null == authorization ? null : Headers.of("authorization", authorization);
        String res = HttpClientUtil.post(url, params, data, headers);
        return JSONObject.parseObject(res);
    }

    public Dict commonParams() {
        Dict params = Dict.create().set("app_type", "senioriup");
        params.set("good_app_type", "senioriup");
        params.set("platform", "android");
        params.set("deviceid", "senioriup2d3011f34b0a8dc8").set("channel", "ali");
        params.set("version", "v1.6.6").set("oaid", "2d3011f34b0a8dc8");
        return params;
    }

    public static void main(String[] args) {
        Dict params = Dict.create().set("app_type", "senioriup");
        params.set("good_app_type", "senioriup").set("province_id", "5604");
        long time = System.currentTimeMillis() / 1000;
        params.set("platform", "android").set("timestrap", "" + time);
        params.set("deviceid", "senioriup2d3011f34b0a8dc8").set("channel", "ali");
        params.set("version", "v1.6.6").set("oaid", "2d3011f34b0a8dc8");
        params.set("enrollment_scope_ids", "1,2");
        /**
         * params.set("recruit_student_type_ids", "1"); params.set("double_high_type_ids", "").set("star_json", "");
         * params.set("nature_ids", "") .set("major_id", ""); params.set("grade_ids", "").set("type_ids", ""); params.set("from_module",
         * "");
         **/
        params.set("page", "1");
        params.set("subject_id", "1402").set("cate_id", "1399");
        String url = host + "/api/v1.6.5/college";
        params.set("sign", UsignUtils.getUsignAll2(url, JSON.toJSONString(params)));
        String res = HttpClientUtil.get(url, params);
        JSONObject data = JSONObject.parseObject(res).getJSONObject("data");
        JSONArray list = data.getJSONObject("list").getJSONArray("data");
        System.out.println(data.getString("total"));
        System.out.println(list.size());
        System.out.println(list.getJSONObject(0));

    }

}
