package com.ruoyi.spider.ie;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

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

/**
 * 专业
 * @author duzj
 *
 */
public class MajorService extends AbstraceService {
    private String tableName = "csdhe_major";

    /**
     * 一级大类
     */
    public void syncMajor() {
        Dict params = commonParams();
        JSONObject res = getJson("/api/v1/college/major", params);
        JSONArray arr = res.getJSONArray("data");
        for (int i = 0; i < arr.size(); i++) {
            Entity record = Entity.create(tableName).parseBean(arr.getJSONObject(i));
            record.set("level", 1);
            insertOrUpdate(record, "id");
            getLv23(record.getLong("id"));
        }
    }

    /***
     * 二,三级分类
     * 
     * @param lv1Id
     */
    private void getLv23(Long lv1Id) {
        Dict params = commonParams();
        JSONObject res = getJson("/api/v1/college/major/" + lv1Id + "/children", params);
        JSONArray arr = res.getJSONArray("data");
        for (int i = 0; i < arr.size(); i++) {
            JSONObject row = arr.getJSONObject(i);
            Long lv2Id = row.getLong("id");
            Entity record = Entity.create(tableName);
            record.set("pid", lv1Id).set("id", lv2Id);
            record.set("level", 2).set("title", row.getString("title"));
            insertOrUpdate(record, "id");
            JSONArray children = row.getJSONArray("children");
            for (int j = 0; j < children.size(); j++) {
                JSONObject item = children.getJSONObject(j);
                insertOrUpdate(Entity.create(tableName).parseBean(item), "id");
                getDetail(item.getLong("id"));
            }
        }
    }

    /***
     * 专业详情
     * 
     * @param id
     */
    private void getDetail(Long id) {
        JSONObject res = getJson("/api/v1.6.5/college/major/" + id, commonParams());
        JSONObject info = res.getJSONObject("data");
        Entity record = Entity.create(tableName);
        record.set("id", id).set("count", info.get("school_count"));
        for (String key : new String[] {"target", "subject", "job", "ability", "vocational", "zhongzhi", "benke"}) {
            record.set(key, info.getString("content_" + key));
        }
        for (String key : new String[] {"status", "created_at", "updated_at"}) {
            record.set(key, info.get(key));
        }
        // 单招院校

        // 相关专业
        JSONArray refs = info.getJSONArray("relation_majors");
        List<Long> ids = new ArrayList<>(refs.size());
        for (int i = 0; i < refs.size(); i++) {
            ids.add(refs.getJSONObject(i).getLong("id"));
        }
        record.set("relations", JSON.toJSONString(new LinkedHashSet<>(ids)));
        updateById(record);
    }

    public static void main(String[] args) {
        MajorService service = new MajorService();
        service.syncMajor();
    }
}
