package com.xjrsoft.xjrsoftboot;


import com.xjrsoft.common.enums.TimeNumberEnum;
import com.xjrsoft.common.enums.TimePeriodEnum;
import com.xjrsoft.module.courseTable.dto.CourseTableParse;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.usermodel.*;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.util.*;

public class WordTest {
    @Test
    public void WordTest() throws IOException {
        InputStream is = new FileInputStream("/Users/fanxp/Downloads/2023年春期 班级总课程表.docx");
        XWPFDocument doc = new XWPFDocument(is);

        List<XWPFParagraph> paras = doc.getParagraphs(); //将得到包含段落列表
        //获取标题
        for (XWPFParagraph para : paras) {
            System.out.println(para.getText());
            System.out.println("--------------------");
        }
        //获取文档中所有的表格
        List<XWPFTable> tables = doc.getTables();

        List<XWPFTableRow> rows;

        List<XWPFTableCell> cells;

        for (XWPFTable table : tables) {
            rows = table.getRows();
            for (XWPFTableRow row : rows) {
                //获取行对应的单元格
                cells = row.getTableCells();
                for (XWPFTableCell cell : cells) {
                    System.out.println(cell.getText());
                }
                System.out.println("--------------------");
            }
            break;
        }
        close(is);
    }

    @Test
    public void WordTest1() throws IOException {
        InputStream is = new FileInputStream("/Users/fanxp/Downloads/2023年春期 班级总课程表.docx");
        List<CourseTableParse> result = courseTableParses(is);

        System.out.println(result);
    }


    public List<CourseTableParse> courseTableParses(InputStream inputStream) throws IOException {
        XWPFDocument doc = new XWPFDocument(inputStream);

        List<XWPFParagraph> paras = doc.getParagraphs();
        String semester = null;
        List<String> cNames = new ArrayList<>();
        //获取标题
        for (int i = 0; i < paras.size(); i++) {
            String txt = paras.get(i).getText();
            if (i == 0) {
                semester = txt;
                continue;
            }
            txt = txt.replaceAll("总课程表", "").replace("\n", "").trim();
            if (txt != null && !txt.equals("") && !txt.equals(semester)) {
                cNames.add(txt);
            }
        }
        List<CourseTableParse> result = new ArrayList<>();
        //获取文档中所有的表格
        List<XWPFTable> tables = doc.getTables();
        int tNum = 0;
        for (XWPFTable table : tables) {
            int rNum = 0;
            String timePeriod = null;
            List<String> weeks = new ArrayList<>();
            List<XWPFTableRow> rows = table.getRows();
            for (XWPFTableRow row : rows) {
                //获取行对应的单元格
                List<XWPFTableCell> cells = row.getTableCells();
                String timeNumber = null;
                for (int i = 0; i < cells.size(); i++) {
                    String cellText = cells.get(i).getText();
                    if (cellText.equals("") || rNum < 1) continue;

                    if (rNum == 1) {
                        weeks.add(cellText);
                        continue;
                    }
                    if (i == 0) {
                        timePeriod = cellText;
                        continue;
                    }
                    if (i == 1) {
                        timeNumber = cellText;
                        continue;
                    }
                    List<XWPFParagraph> cParagraph = cells.get(i).getParagraphs();
                    CourseTableParse item = new CourseTableParse();
                    item.setSemester(semester);
                    item.setTimePeriod(TimePeriodEnum.getCode(timePeriod));
                    item.setTimeNumber(TimeNumberEnum.getCode(timeNumber));
                    String week = weeks.get(Math.max(i - 2, 0));
                    item.setWeeksCn(week);
                    item.setClassName(cNames.get(tNum));
                    for (int j = 0; j < cParagraph.size(); j++) {
                        cellText = cParagraph.get(j).getText().trim();
                        switch (j) {
                            case 0:
                                item.setCourseName(cellText);
                                break;
                            case 1:
                                item.setTeacherName(cellText);
                                break;
                            case 2:
                                item.setSiteName(cellText);
                                break;
                        }
                    }
                    result.add(item);
                    System.out.println(item);
                }
                rNum++;
            }
            tNum++;
        }
        inputStream.close();
        return result;
    }

    /**
     * 关闭输入流
     *
     * @param is
     */

    private void close(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
