package com.xjrsoft.xjrsoftboot;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.alibaba.excel.EasyExcel;
import com.xjrsoft.XjrSoftApplication;
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.BaseCourseSubject;
import com.xjrsoft.module.base.entity.BaseGrade;
import com.xjrsoft.module.base.entity.BaseSemester;
import com.xjrsoft.module.courseTable.service.ICourseTableService;
import com.xjrsoft.module.textbook.entity.SubjectGroup;
import com.xjrsoft.module.textbook.entity.Textbook;
import com.xjrsoft.module.textbook.entity.TextbookClassRelation;
import com.xjrsoft.module.textbook.service.ITextbookService;
import com.yomahub.liteflow.util.JsonUtil;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.sql.DataSource;
import java.io.File;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dzx
 * @date 2024/2/17
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = XjrSoftApplication.class)
public class TextBookTest {
    @Autowired
    private ITextbookService textbookService;
    @Test
    void test() throws SQLException {
        String filePath = "D:\\workspace\\其他\\教材管理\\副本2024年春期教材征订表（2024.1.22）交书店改.xlsx";
        File file = new File(filePath);
        List<Map<Integer, Object>> excelDataList = EasyExcel.read(file).sheet().headRowNumber(1).doReadSync();
        DataSource datasource = DatasourceUtil.getDataSource(GlobalConstant.DEFAULT_DATASOURCE_KEY);
        Db use = Db.use(datasource);

        // 班级
        String sql = "SELECT * FROM base_class WHERE delete_mark = 0";
        List<BaseClass> classList = use.query(sql, BaseClass.class);
        Map<String, Long> classMap = new HashMap<>();
        for (BaseClass baseClass : classList) {
            classMap.put(baseClass.getName(), baseClass.getId());
        }

        //学科组
        sql = "SELECT * FROM subject_group WHERE delete_mark = 0";
        List<SubjectGroup> subjectGroupList = use.query(sql, SubjectGroup.class);
        Map<String, Long> subjectGroupMap = new HashMap<>();
        for (SubjectGroup el : subjectGroupList) {
            subjectGroupMap.put(el.getGroupName(), el.getId());
        }

        //课程 base_course_subject
        sql = "SELECT * FROM base_course_subject WHERE delete_mark = 0";
        List<BaseCourseSubject> courseSubjectList = use.query(sql, BaseCourseSubject.class);
        Map<String, Long> courseSubjectMap = new HashMap<>();
        for (BaseCourseSubject el : courseSubjectList) {
            courseSubjectMap.put(el.getName(), el.getId());
        }

        //年级
        sql = "SELECT * FROM base_grade WHERE delete_mark = 0";
        List<BaseGrade> gaseGradeList = use.query(sql, BaseGrade.class);
        Map<String, Long> gaseGradeMap = new HashMap<>();
        for (BaseGrade el : gaseGradeList) {
            gaseGradeMap.put(el.getName(), el.getId());
        }

        //学期
        sql = "SELECT * FROM base_semester WHERE delete_mark = 0";
        List<BaseSemester> baseSemesterList = use.query(sql, BaseSemester.class);
        Map<String, Long> baseSemesterMap = new HashMap<>();
        for (BaseSemester el : baseSemesterList) {
            baseSemesterMap.put(el.getName(), el.getId());
        }

        Long id = 1758702787978899458L;
        Long createUserId = 1000000000000000000L;
        Date date = new Date();
        for (Map<Integer, Object> dataMap : excelDataList) {
            System.out.println(dataMap.toString());
            Textbook textbook = new Textbook();
//            textbook.setId(Long.parseLong(dataMap.get(1).toString()));
            textbook.setIssn(dataMap.get(2).toString());
            textbook.setBookName(dataMap.get(3).toString());
            textbook.setTextbookType(dataMap.get(5).toString());
            textbook.setPublishingHouse(dataMap.get(6).toString());
            if(dataMap.get(7) != null){
                textbook.setEditorInChief(dataMap.get(7).toString());
            }

            if(dataMap.get(8) != null){
                textbook.setPrice(BigDecimal.valueOf(Double.valueOf(dataMap.get(8).toString())));
            }

            Object yesno = dataMap.get(9);
            if(yesno != null){
                textbook.setIsTextbookPlan(yesno.toString());
            }
            if(dataMap.get(13) != null){
                textbook.setSubjectGroupId(subjectGroupMap.get(dataMap.get(13).toString()));
            }
            if(dataMap.get(11) != null){
                textbook.setGradeId(gaseGradeMap.get(dataMap.get(11).toString()));
            }

            textbook.setBaseSemesterId(1754815042567057408L);
            if(dataMap.get(10) != null){
                textbook.setCourseSubjectId(courseSubjectMap.get(dataMap.get(10).toString()));
            }

            textbook.setCreateUserId(createUserId);
            textbook.setCreateDate(date);
            textbook.setDeleteMark(0);
            textbook.setEnabledMark(1);

            Object classObj = dataMap.get(12);
            List<TextbookClassRelation> relationList = new ArrayList<>();
            if(classObj != null){
                String[] classNames = classObj.toString().split("、");

                for (String className : classNames) {
                    TextbookClassRelation classRelation = new TextbookClassRelation();
                    classRelation.setCreateDate(date);
                    classRelation.setCreateUserId(createUserId);
                    classRelation.setDeleteMark(0);
                    classRelation.setEnabledMark(1);
                    classRelation.setClassId(classMap.get(className));
                    relationList.add(classRelation);
                }

            }
            textbook.setTextbookClassRelationList(relationList);
            textbookService.add(textbook);

        }
    }
}

