package com.xjrsoft.module.job;

import cn.hutool.db.Db;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.xjrsoft.XjrSoftApplication;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.mybatis.SqlRunnerAdapter;
import com.xjrsoft.common.utils.DatasourceUtil;
import com.xjrsoft.common.utils.SqlRunnerAdapterUtil;
import com.xjrsoft.module.base.entity.BaseClass;
import com.xjrsoft.module.base.entity.BaseGrade;
import com.xjrsoft.module.schedule.entity.JianyueData;
import com.xjrsoft.module.schedule.util.DataUtil;
import com.xjrsoft.module.schedule.util.ScheduleUtil;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author dzx
 * @date 2024/4/30
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = XjrSoftApplication.class)
class JianyuekbBaseDataTaskTest {


    @Test
    public void RefreshConnectionPool() {
        try {
            String sql = "SELECT distinct table_name FROM jianyue_data WHERE 1 = 1";
            List<Map<String, Object>> query = SqlRunnerAdapter.db().selectList(sql, JianyueData.class);
            Set<String> tables = new HashSet<>();
            for (Map<String, Object> queryMap : query) {
                String[] strings = SqlRunnerAdapterUtil.convertMapToStringArray(queryMap);
                tables.add(strings[0]);
            }
            sql = "SELECT * FROM jianyue_data WHERE 0 = 0";
            List<Map<String, Object>> list = SqlRunnerAdapter.db().selectList(sql, JianyueData.class);
            Map<String, Map<String, String>> dataMap = new HashMap<>();
            for (String table : tables) {
                Map<String, String> tableData = new HashMap<>();
                for (Map<String, Object> queryMap : list) {
                    JianyueData jianyueData = SqlRunnerAdapterUtil.convertMapToEntity(queryMap, JianyueData.class);
                    if(!table.equals(jianyueData.getTableName())){
                        continue;
                    }
                    tableData.put(jianyueData.getSourceId(), jianyueData.getJianyueId());
                }
                dataMap.put(table, tableData);
            }

        } catch (Exception e) {
        }
    }

    @Test
    public void RefreshConnectionPool2() {
        DataSource datasource = DatasourceUtil.getDataSource(GlobalConstant.DEFAULT_DATASOURCE_KEY);
        try {
            Db use = Db.use(datasource);
            String sql = "SELECT distinct table_name FROM jianyue_data WHERE 1 = 1";
            List<JianyueData> query = use.query(sql, JianyueData.class);
            Set<String> tables = new HashSet<>();
            for (JianyueData jianyueData : query) {
                tables.add(jianyueData.getTableName());
            }
            sql = "SELECT * FROM jianyue_data WHERE 0 = 0";
            List<JianyueData> list = use.query(sql, JianyueData.class);
            Map<String, Map<String, String>> dataMap = new HashMap<>();
            for (String table : tables) {
                Map<String, String> tableData = new HashMap<>();
                for (JianyueData jianyueData : list) {
                    if(!table.equals(jianyueData.getTableName())){
                        continue;
                    }
                    tableData.put(jianyueData.getSourceId(), jianyueData.getJianyueId());
                }
                dataMap.put(table, tableData);
            }
            DataUtil dataUtil = new DataUtil();
            //查询校区
            String tableName = "xjr_department";

            //推送年级
            JsonArray schoolDistrictData = dataUtil.getSchoolDistrictData();
            tableName = "base_grade";
            Map<String, String> grade = dataMap.get(tableName);
            //推送教职工
            tableName = "base_teacher";
            Map<String, String> teacherMap = dataMap.get(tableName);
            //推送教室
            tableName = "base_classroom";
            Map<String, String> classroomMap = dataMap.get(tableName);
            //推送行政班，先查询当前学期id
            String currenSemeter = dataUtil.getCurrenSemeter();
            tableName = "base_class";
            dataUtil.insertClass(tableName, grade, teacherMap, currenSemeter, dataMap.get(tableName), classroomMap);

        } catch (Exception e) {
        }
    }

    @Test
    void test3() throws Exception {
        long timestamp = System.currentTimeMillis();
        //生成签名
        String sign = ScheduleUtil.createSign(timestamp);

        System.out.println(timestamp);

        System.out.println(sign);
    }

    @Test
    void deleteData() throws Exception {
        DataSource datasource = DatasourceUtil.getDataSource(GlobalConstant.DEFAULT_DATASOURCE_KEY);
        Db use = Db.use(datasource);

        String url  = ScheduleUtil.apiUrl + "Class/page";

        JsonObject pageJson = new JsonObject();
        pageJson.addProperty("pageSize", 120);
        pageJson.addProperty("pageIndex", 1);
        JsonParser parser = new JsonParser();


        String ids = "[\"CLA000000975\",\"CLA000001011\",\"CLA000000997\",\"CLA000000998\",\"CLA000000999\",\"CLA000001000\",\"CLA000001001\",\"CLA000001002\",\"CLA000001003\",\"CLA000001004\",\"CLA000001005\",\"CLA000000972\",\"CLA000000973\",\"CLA000000974\",\"CLA000000989\",\"CLA000000990\",\"CLA000001007\",\"CLA000001008\",\"CLA000001009\",\"CLA000001010\",\"CLA000001012\",\"CLA000000987\",\"CLA000000988\",\"CLA000000991\",\"CLA000000992\",\"CLA000000993\",\"CLA000000994\",\"CLA000000995\",\"CLA000000980\",\"CLA000000981\",\"CLA000000982\",\"CLA000000983\",\"CLA000000996\",\"CLA000000984\",\"CLA000000985\",\"CLA000000986\",\"CLA000001006\",\"CLA000000977\",\"CLA000000978\",\"CLA000000979\",\"CLA000000976\",\"CLA000001017\",\"CLA000001018\",\"CLA000001057\",\"CLA000001043\",\"CLA000001044\",\"CLA000001045\",\"CLA000001046\",\"CLA000001047\",\"CLA000001048\",\"CLA000001049\",\"CLA000001013\",\"CLA000001014\",\"CLA000001015\",\"CLA000001034\",\"CLA000001052\",\"CLA000001053\",\"CLA000001054\",\"CLA000001055\",\"CLA000001056\",\"CLA000001032\",\"CLA000001033\",\"CLA000001035\",\"CLA000001036\",\"CLA000001037\",\"CLA000001038\",\"CLA000001039\",\"CLA000001040\",\"CLA000001041\",\"CLA000001024\",\"CLA000001025\",\"CLA000001026\",\"CLA000001027\",\"CLA000001028\",\"CLA000001042\",\"CLA000001029\",\"CLA000001030\",\"CLA000001031\",\"CLA000001050\",\"CLA000001051\",\"CLA000001019\",\"CLA000001020\",\"CLA000001021\",\"CLA000001022\",\"CLA000001023\",\"CLA000001016\"]";

        JsonArray paramJson = parser.parse(ids).getAsJsonArray();

        System.out.println(paramJson.toString());
        long timestamp = System.currentTimeMillis();
        url  = ScheduleUtil.apiUrl + "class/Del";
        String sign = ScheduleUtil.createSign(timestamp);

        String doPost = ScheduleUtil.doPost(url, paramJson.toString(), sign, timestamp);
        System.out.println(doPost);
    }

    @Test
    void updateGrade() throws Exception {
        String url  = ScheduleUtil.apiUrl + "EduYear/page";
        JsonObject paramJson = new JsonObject();
        paramJson.addProperty("pageSize", 40);
        paramJson.addProperty("pageIndex", 1);

        long timestamp = System.currentTimeMillis();
        //生成签名
        String sign = ScheduleUtil.createSign(timestamp);

        String doPost = ScheduleUtil.doPost(url, paramJson.toString(), sign, timestamp);
        System.out.println(doPost);

//        JsonParser parser = new JsonParser();
//        JsonObject jsonObject = parser.parse(doPost).getAsJsonObject();
//        JsonArray jsonArray = jsonObject.get("data").getAsJsonObject().get("dataList").getAsJsonArray();
//
//        url = ScheduleUtil.apiUrl + "eduyear/update";
//        for (JsonElement jsonElement : jsonArray) {
//            JsonObject dataOne = jsonElement.getAsJsonObject();
//            dataOne.addProperty("schoolDistrictId", "48e0a5ae-6d0b-34dd-f31a-3a12feca72d1");
//
//            timestamp = System.currentTimeMillis();
//            //生成签名
//            sign = ScheduleUtil.createSign(timestamp);
//            String result = ScheduleUtil.doPost(url, dataOne.toString(), sign, timestamp);
//
//            System.out.println(result);
//        }
    }

    @Test
    void insertGrade() throws Exception {
        String url  = ScheduleUtil.apiUrl + "eduyear/create";
        JsonObject paramJson = new JsonObject();
        paramJson.addProperty("pageSize", 40);
        paramJson.addProperty("pageIndex", 1);

        long timestamp = System.currentTimeMillis();
        //生成签名
        String sign = ScheduleUtil.createSign(timestamp);

        DataSource datasource = DatasourceUtil.getDataSource(GlobalConstant.DEFAULT_DATASOURCE_KEY);
        Db use = Db.use(datasource);

        JsonParser jsonParser = new JsonParser();

        Map<String, String> idMap = new HashMap<>();
        //删除学生
        String sql = "SELECT * FROM jianyue_data WHERE table_name = 'xjr_department'";
        List<JianyueData> gradeList = use.query(sql, JianyueData.class);
        for (JianyueData jianyueData : gradeList) {
            if("48e0a5ae-6d0b-34dd-f31a-3a12feca72d1".equals(jianyueData.getJianyueId())){
                continue;
            }

            sql = "select * from base_grade where delete_mark = 0 and status = 1";
            List<BaseGrade> list = use.query(sql, BaseGrade.class);
            for (BaseGrade baseGrade : list) {
                url = ScheduleUtil.apiUrl + "eduyear/create";
                JsonObject dataJson = new JsonObject();
                dataJson.addProperty("schoolDistrictId", jianyueData.getJianyueId());
                dataJson.addProperty("period", 4);
                dataJson.addProperty("startYear", baseGrade.getTitle().replaceAll("年", ""));
                dataJson.addProperty("extendId", baseGrade.getId().toString());
                dataJson.addProperty("year", 3);

                //获取时间戳
                timestamp = System.currentTimeMillis();
                //生成签名
                sign = ScheduleUtil.createSign(timestamp);

                String result = ScheduleUtil.doPost(url, dataJson.toString(), sign, timestamp);

                JsonObject resultJson = jsonParser.parse(result).getAsJsonObject();

                idMap.put(jianyueData.getSourceId() + "_" + baseGrade.getId().toString(), resultJson.get("data").getAsString());
            }
        }
        DataUtil dataUtil = new DataUtil();
        dataUtil.insertRecord("base_grade", idMap);
    }

    @Test
    void selectSchoolDistrict() throws Exception {
        String url  = ScheduleUtil.apiUrl + "SchoolDistrict/page";
        JsonObject paramJson = new JsonObject();
        paramJson.addProperty("pageSize", 40);
        paramJson.addProperty("pageIndex", 1);

        long timestamp = System.currentTimeMillis();
        //生成签名
        String sign = ScheduleUtil.createSign(timestamp);

        String doPost = ScheduleUtil.doPost(url, paramJson.toString(), sign, timestamp);
        System.out.println(doPost);

    }

    @Test
    void updateClass() throws Exception {
        String url  = ScheduleUtil.apiUrl + "EduYear/page";
        JsonObject paramJson = new JsonObject();
        paramJson.addProperty("pageSize", 40);
        paramJson.addProperty("pageIndex", 1);

        long timestamp = System.currentTimeMillis();
        //生成签名
        String sign = ScheduleUtil.createSign(timestamp);

        String doPost = ScheduleUtil.doPost(url, paramJson.toString(), sign, timestamp);


        JsonParser parser = new JsonParser();
        JsonObject jsonObject = parser.parse(doPost).getAsJsonObject();
        JsonArray jsonArray = jsonObject.get("data").getAsJsonObject().get("dataList").getAsJsonArray();

        DataSource datasource = DatasourceUtil.getDataSource(GlobalConstant.DEFAULT_DATASOURCE_KEY);
        Db use = Db.use(datasource);
        String sql = "SELECT * FROM jianyue_data WHERE table_name = 'base_grade'";
        List<JianyueData> gradeList = use.query(sql, JianyueData.class);
        Map<String, String> gradeMap = new HashMap<>();
        for (JianyueData jianyueData : gradeList) {
            gradeMap.put(jianyueData.getSourceId(), jianyueData.getJianyueId());
        }

        url  = ScheduleUtil.apiUrl + "class/update";
        paramJson = new JsonObject();
        paramJson.addProperty("pageSize", 200);
        paramJson.addProperty("pageIndex", 1);

        timestamp = System.currentTimeMillis();
        //生成签名
        sign = ScheduleUtil.createSign(timestamp);

        doPost = ScheduleUtil.doPost(url, paramJson.toString(), sign, timestamp);

        sql = "SELECT * FROM base_class WHERE delete_mark = 0 and is_graduate = 1";
        List<BaseClass> dataList = use.query(sql, BaseClass.class);
        Map<String, BaseClass> classMap = new HashMap<>();
        for (BaseClass baseClass : dataList) {
            classMap.put(baseClass.getId().toString(), baseClass);
        }

        sql = "SELECT * FROM jianyue_data WHERE table_name = 'base_class'";
        List<JianyueData> classList = use.query(sql, JianyueData.class);
        Map<String, BaseClass> classMap2 = new HashMap<>();
        Map<String, String> classMap3 = new HashMap<>();
        for (JianyueData jianyueData : classList) {
            classMap2.put(jianyueData.getJianyueId(), classMap.get(jianyueData.getSourceId()));
            classMap3.put(jianyueData.getSourceId(), jianyueData.getJianyueId());
        }

        sql = "SELECT * FROM jianyue_data WHERE table_name = 'base_classroom'";
        List<JianyueData> classroomList = use.query(sql, JianyueData.class);
        Map<String, String> classroomMap = new HashMap<>();
        for (JianyueData jianyueData : classroomList) {
            classroomMap.put(jianyueData.getSourceId(), jianyueData.getJianyueId());
        }

        url  = ScheduleUtil.apiUrl + "class/update";
        DataUtil dataUtil = new DataUtil();
        String currenSemeter = dataUtil.getCurrenSemeter();

        for (BaseClass baseClass : classMap2.values()) {
            if(baseClass == null){
                continue;
            }
            paramJson = new JsonObject();
            paramJson.addProperty("name", baseClass.getName());

            paramJson.addProperty("semesterSerialNo", currenSemeter);
            paramJson.addProperty("eduYearSerialNo", gradeMap.get(baseClass.getOrgId() + "_" + baseClass.getGradeId().toString()));
            paramJson.addProperty("extendId", baseClass.getId());
            paramJson.addProperty("classRoomSerialNo", classroomMap.get(baseClass.getClassroomId().toString()));
            paramJson.addProperty("serialNo", classMap3.get(baseClass.getId().toString()));

            timestamp = System.currentTimeMillis();
            //生成签名
            sign = ScheduleUtil.createSign(timestamp);
            String result = ScheduleUtil.doPost(url, paramJson.toString(), sign, timestamp);

            System.out.println(baseClass.getName() + " -> " + result);
        }
    }


}