package com.xjrsoft.xjrsoftboot;

import cn.hutool.db.Db;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.utils.DatasourceUtil;
import com.xjrsoft.module.base.entity.BaseClass;
import com.xjrsoft.module.base.entity.BaseGrade;
import com.xjrsoft.module.base.entity.BaseMajorSet;
import com.xjrsoft.module.system.entity.DictionaryDetail;
import com.xjrsoft.module.teacher.entity.XjrUser;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.sql.DataSource;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@SpringBootTest
public class DataMaintenanceTest {

    @Test
    public void maintenanceStudent(){
        //读取excel的数据
        List<StudentInfo> studentInfoList = new ArrayList<>();
        String fileName = "C:\\Users\\大数据与最优化研究所\\Desktop\\公司-沈祖山\\学生信息20240222.xls";
        EasyExcel.read(fileName, StudentInfo.class, new PageReadListener<StudentInfo>(studentInfoList::addAll)).sheet().doRead();

        //读取数据库的数据
        List<XjrUser> xjrUserList = new ArrayList<>();
        List<BaseGrade> baseGradeList = new ArrayList<>();
        List<BaseMajorSet> baseMajorSetList = new ArrayList<>();
        List<BaseClass> baseClassList = new ArrayList<>();
        List<DictionaryDetail> studentTypeList = new ArrayList<>();
        List<DictionaryDetail> stduyStatusList = new ArrayList<>();
        DataSource datasource = DatasourceUtil.getDataSource(GlobalConstant.DEFAULT_DATASOURCE_KEY);
        try {
            Db use = Db.use(datasource);
            //学生用户id和身份证
            String listXjrUserSql = "select t.credential_number,t.id from xjr_user t left join xjr_user_role_relation t1 on t1.user_id = t.id where t1.role_id = 3 and t.delete_mark = 0 and t.enabled_mark = 1";
            xjrUserList = use.query(listXjrUserSql, XjrUser.class);
            //年级id和年级name
            String listGradeSql = "select t.id,t.name from base_grade t";
            baseGradeList = use.query(listGradeSql, BaseGrade.class);
            //在读专业方向id和在读专业方向name
            String listMajorSeteSql = "select t.id,t.name from base_major_set t";
            baseMajorSetList = use.query(listMajorSeteSql, BaseMajorSet.class);
            //班级id和班级name
            String listClassSql = "select t.id,t.name from base_class t";
            baseClassList = use.query(listClassSql, BaseClass.class);
            //学生类型code和学生类型name
            String liststudentTypeSql = "select t.code,t.name from xjr_dictionary_detail t LEFT JOIN xjr_dictionary_item t1 on t1.id = t.item_id where t.delete_mark = 0 and t.enabled_mark = 1 and t1.`code` = 'student_type'";
            studentTypeList = use.query(liststudentTypeSql, DictionaryDetail.class);
            //就读方式code和就读方式name
            String liststudentStatusSql = "select t.code,t.name from xjr_dictionary_detail t LEFT JOIN xjr_dictionary_item t1 on t1.id = t.item_id where t.delete_mark = 0 and t.enabled_mark = 1 and t1.`code` = 'stduy_status'";
            stduyStatusList = use.query(liststudentStatusSql, DictionaryDetail.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<Object> propertyList1 = studentInfoList.stream()
                .map(studentInfo -> getProperty(studentInfo, "sfzh"))
                .collect(Collectors.toList());

        List<Object> propertyList2 = xjrUserList.stream()
                .map(xjrUser -> getProperty(xjrUser, "credentialNumber"))
                .collect(Collectors.toList());

        List<Object> differentProperties = new ArrayList<>(propertyList1);
        differentProperties.removeAll(propertyList2);

        System.out.println("集合一中有，集合二中没有的的属性值:");
        //differentProperties.forEach(System.out::println);

        List<Object> differentProperties1 = new ArrayList<>(propertyList2);
        differentProperties1.removeAll(propertyList1);

        //System.out.println("集合二中有，集合一中没有的的属性值:");
        //differentProperties1.forEach(System.out::println);

        //已有学生数据身份证号和用户id的映射
        Map<String, Long> credentialNumberAndUserId = new HashMap<>();
        for (XjrUser xjrUser : xjrUserList){
            if(xjrUser.getCredentialNumber() != null){
                credentialNumberAndUserId.put(xjrUser.getCredentialNumber(), xjrUser.getId());
            }
        }

        //年级id和年级name的映射
        Map<String, Long> gradeByNameAndIdMap = new HashMap<>();
        for (BaseGrade baseGrade : baseGradeList){
            if(baseGrade.getName() != null){
                gradeByNameAndIdMap.put(baseGrade.getName(), baseGrade.getId());
            }
        }

        //在读专业方向id和在读专业方向name的映射
        Map<String, Long> baseMajorSetByNameAndIdMap = new HashMap<>();
        for (BaseMajorSet baseMajorSet : baseMajorSetList){
            if(baseMajorSet.getName() != null){
                baseMajorSetByNameAndIdMap.put(baseMajorSet.getName(), baseMajorSet.getId());
            }
        }

        //班级id和班级name的映射
        Map<String, Long> baseClassByNameAndIdMap = new HashMap<>();
        for (BaseClass baseClass : baseClassList){
            if(baseClass.getName() != null){
                baseClassByNameAndIdMap.put(baseClass.getName(), baseClass.getId());
            }
        }

        //学生类型code和学生类型name的映射
        Map<String, String> studentTypeByNameAndCodeMap = new HashMap<>();
        for (DictionaryDetail dictionaryDetail : studentTypeList){
            if(dictionaryDetail.getName() != null){
                studentTypeByNameAndCodeMap.put(dictionaryDetail.getName(), dictionaryDetail.getCode());
            }
        }

        //就读方式code和就读方式name的映射
        Map<String, String> stduyStatusByNameAndCodeMap = new HashMap<>();
        for (DictionaryDetail dictionaryDetail : stduyStatusList){
            if(dictionaryDetail.getName() != null){
                stduyStatusByNameAndCodeMap.put(dictionaryDetail.getName(), dictionaryDetail.getCode());
            }
        }

        //处理所有数据的信息
        //添加学生家庭成员信息，添加的为监护人
        StringBuilder modifyBSFMSqlSb = new StringBuilder();
        modifyBSFMSqlSb.append("# base_student_family_member新增监护人电话 \n");
        modifyBSFMSqlSb.append("INSERT INTO base_student_family_member (id, create_user_id, create_date, delete_mark, enabled_mark, user_id, name, mobile, is_guardian) \nVALUES ");
        //学生学籍信息修改
        StringBuilder modifyBSSRSqlSb = new StringBuilder();
        modifyBSSRSqlSb.append("# base_student_school_roll学生学籍信息修改\nBEGIN;\n");
        //学生学号信息修改
        StringBuilder modifyBSSqlSb = new StringBuilder();
        modifyBSSqlSb.append("# base_student学生学号信息修改 \nBEGIN;\n");

        long BSFMId = 456789123462290L;
        for (int i = 0; i < studentInfoList.size(); i++) {
            StudentInfo studentInfo = studentInfoList.get(i);
            if(differentProperties.contains(studentInfo.getSfzh())
                    || credentialNumberAndUserId.get(studentInfo.getSfzh()) == null
                    || studentInfo.getJhrdh().isEmpty()){
                continue;
            }
            modifyBSFMSqlSb.append("(");
            modifyBSFMSqlSb.append(++BSFMId);
            modifyBSFMSqlSb.append(", 1000000000000000000, '2024-02-23', 0, 1, ");
            modifyBSFMSqlSb.append(credentialNumberAndUserId.get(studentInfo.getSfzh()));
            modifyBSFMSqlSb.append(", '', '");
            modifyBSFMSqlSb.append(studentInfo.getJhrdh());
            modifyBSFMSqlSb.append("', 1),\n");

            if(differentProperties.contains(studentInfo.getSfzh()) || studentInfo.getJhrdh().isEmpty()){
                continue;
            }

            modifyBSSRSqlSb.append("UPDATE base_student_school_roll SET roll_number = '"
                    + studentInfo.getXjh()
                    + "', archives_number = '"
                    + studentInfo.getXsdabh()
                    + "', ");
            if(gradeByNameAndIdMap.get(studentInfo.getBjnj()) != null){
                modifyBSSRSqlSb.append("grade_id = "
                                    + (gradeByNameAndIdMap.get(studentInfo.getBjnj()))
                                    + ", ");

            }
            if(baseMajorSetByNameAndIdMap.get(studentInfo.getZdzyfx()) != null){
                modifyBSSRSqlSb.append("major_set_id = "
                        + baseMajorSetByNameAndIdMap.get(studentInfo.getZdzyfx())
                        + ", ");
            }
            if(baseClassByNameAndIdMap.get(studentInfo.getBjmc()) != null){
                modifyBSSRSqlSb.append("class_id = "
                        + baseClassByNameAndIdMap.get(studentInfo.getBjmc())
                        + ", ");
            }

            modifyBSSRSqlSb.append( "student_type = '"
                    + studentTypeByNameAndCodeMap.get(studentInfo.getXslb())
                    + "', stduy_status = '"
                    + stduyStatusByNameAndCodeMap.get(studentInfo.getJdfs())
                    + "' WHERE user_id = "
                    + credentialNumberAndUserId.get(studentInfo.getSfzh())
                    + ";\n");

            if(credentialNumberAndUserId.get(studentInfo.getSfzh()) != null){
                modifyBSSqlSb.append("UPDATE base_student SET student_id = '"
                        + studentInfo.getXh()
                        + "' WHERE user_id = "
                        + credentialNumberAndUserId.get(studentInfo.getSfzh())
                        + ";\n");
            }
        }
        modifyBSSRSqlSb.append("COMMIT;\n");
        modifyBSSqlSb.append("COMMIT;\n");
        //System.err.println(modifyBSFMSqlSb.toString());
        if (modifyBSFMSqlSb.length() > 0) {
            char lastChar = modifyBSFMSqlSb.charAt(modifyBSFMSqlSb.length() - 2);
            if(lastChar == ','){
                modifyBSFMSqlSb.deleteCharAt(modifyBSFMSqlSb.length() - 2);
            }
            //System.out.println("最后一个字符是：" + lastChar);
            //System.out.println(lastChar == ',');
        }
        modifyBSFMSqlSb.append(";\n\n");
        modifyBSSRSqlSb.append("\n\n");
        modifyBSSqlSb.append("\n\n");

        String filePath = "C:\\Users\\大数据与最优化研究所\\Desktop\\公司-沈祖山\\output.sql";

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))){
            writer.write(modifyBSFMSqlSb.toString());
            writer.write(modifyBSSRSqlSb.toString());
            writer.write(modifyBSSqlSb.toString());
            //System.out.println("StringBuilder content has been written to the file: " + filePath);
        } catch (IOException e) {
            //System.err.println("Error writing to the file: " + e.getMessage());
        }
    }

    public static Object getProperty(Object object, String propertyName) {
        try {
            return object.getClass().getMethod("get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1)).invoke(object);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
