package com.nantian.cloud.jljc.tools;

import com.nantian.sc.cloud.common.minio.service.MinioTemplate;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class XWPFTableUtil {
    private static final String FIRST_LINE = "检查人员签名：                                   记录人员签名：";
    private static final String DAYS_LINE  = "                                                                          " +
            "                      年  月  日";
    private static final String SEN_LINE  = "当事人意见：";
    private static final String LAST_LINE  = "              签名或盖章：";
    private static final String OTHER_LINE = "                                 年  月  日";
    private static final String NUMBER_JC = "监察编号：";

    @Autowired
    private MinioTemplate minio;

    private static XWPFTableUtil minioTemplate;

    @PostConstruct
    public void init() {
        minioTemplate = this;
        minioTemplate.minio = this.minio;
    }
    public static void XWPFTable(Map<String, Object> map, HttpServletResponse response) throws Exception {
        List<List<String[]>> tables = null;
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename="  + URLEncoder.encode("单耗计算表.docx", "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        String companyName = (String) map.get("companyName");
        tables = (List<List<String[]>>) map.get("tablex");
        XWPFDocument document = null;
        int lineTotal = 0;  //每页可以填充的行数
        int lineSize = 0; //每行的字数
        if (tables.get(0).get(0).length == 5){
            lineSize = 5;
            if (companyName.length() <= 15){
                lineTotal = 24;
            }else if (companyName.length() > 15 && companyName.length() < 30){
                lineTotal = 22;
            }else {
                lineTotal = 20;
            }
        }else {
            lineSize = 5;
            if (companyName.length() <= 15){
                lineTotal = 21;
            }else if (companyName.length() > 15 && companyName.length() < 30){
                lineTotal = 20;
            }else {
                lineTotal = 20;
            }
        }
        List<List<String[]>> tableList = new ArrayList<>();  //正常填充数据
        List<List<String[]>> subscript = new ArrayList<>();  //单位、符号填充数据
        int[] listUnit = EditWordZZUtil.getListUnit(tables.get(0));
        for (List<String[]> strings : tables) {
            subscript.add(EditWordZZUtil.getListInfo(strings));
            tableList.add(EditWordZZUtil.replaceStr(strings));
        }
        //表格数据的总行数
        int total = 0;
        List<Integer> flagAll = new ArrayList<>(); //标志位，记录当前页的数据截取到哪
        for (int i = 0; i < tableList.size(); i++) {
            List<String[]> strings = tableList.get(i);
            for (int j = 1; j < strings.size(); j++) {
                String[] str = strings.get(j);
                total += str[0].length() / lineSize + (str[0].length() % lineSize != 0 ? 1 : 0);
                if (total >= lineTotal && j + 1 < strings.size()){
                    flagAll.add(j);
                    total = 0;
                }
            }
        }
        //计算总页数
        int pages = flagAll.size() + 1;
        map.replace("total",pages);
        //按照总页数及标志位把数据拆分
        //记录上一次截取的位置
        int midCount = 0;
        if (pages ==  1){
            map.replace("total","1");
            map.replace("index","1");
            InputStream inputStream = minioTemplate.minio.getObject("jljc", "table.docx");
            document = new XWPFDocument(OPCPackage.open(inputStream));
            List<XWPFParagraph> paragraphs = document.getParagraphs();
            EditWordZZUtil.setUnderLine(document,map,1);
            for (XWPFParagraph paragraph : paragraphs) {
                List<XWPFRun> runs = paragraph.getRuns();
                for (int j = 0; j < runs.size(); j++) {
                    if (runs.get(j).getText(0) != null){
                        if (runs.get(j).getText(0).contains("jcNumber")){
                            XWPFParagraph xwpfParagraph = paragraphs.get(0);
                            int size = xwpfParagraph.getRuns().size();
                            for (int k = 0; k < size - 1; k++) {
                                xwpfParagraph.removeRun(1);
                            }
                            XWPFRun run = xwpfParagraph.createRun();
                            run.setText(NUMBER_JC);
                            run.setBold(true);
                            XWPFRun run1 = xwpfParagraph.createRun();
                            run1.setText((String) map.get("text"));
                        }
                        if (runs.get(0).getText(0).contains("tables")){
                            XWPFParagraph paragraph1 = paragraphs.get(paragraphs.size() - 1);
                            paragraph1.removeRun(0);
                            XWPFTable table = document.createTable();
                            CTTblWidth ctTblWidth = table.getCTTbl().addNewTblPr().addNewTblW();
                            ctTblWidth.setType(STTblWidth.PCT);
                            ctTblWidth.setW(BigInteger.valueOf(4960));
                            //获取表头信息,处理表头信息
                            String[] strings = tableList.get(0).get(0);
                            for (int k = 0; k < strings.length; k++) {
                                if (k == 0){
                                    XWPFTableCell cell = table.getRow(0).getCell(0);
                                    XWPFRun run = cell.addParagraph().createRun();
                                    run.setText(strings[j]);
                                    run.setBold(true);
                                    cell.removeParagraph(0);
                                    CTTc ctTc = cell.getCTTc();
                                    CTTcPr ctTcPr = ctTc.addNewTcPr();
                                    ctTcPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                                    ctTc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER );
//                                    ctTcPr.addNewTcW().setW(BigInteger.valueOf(1700));
                                }else {
                                    XWPFTableCell cell = table.getRow(0).addNewTableCell();
                                    XWPFRun run = cell.addParagraph().createRun();
                                    run.setText(strings[k]);
                                    run.setBold(true);
                                    cell.removeParagraph(0);
                                    CTTc ctTc = cell.getCTTc();
                                    CTTcPr ctTcPr = ctTc.addNewTcPr();
                                    ctTcPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                                    ctTc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER );
                                    if (strings[k].equals("符号") || strings[k].equals("单位")){
                                        ctTcPr.addNewTcW().setW(BigInteger.valueOf(800));
                                        ctTcPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                                        ctTc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER );
                                    } else {
                                        ctTcPr.addNewTcW().setW(BigInteger.valueOf(1800));
                                    }
                                }
                            }
                            //处理表格数据信息
                            tableList.get(0).remove(0);
                            int[] count = {0};
                            EditWordZZUtil.readWriteWord(document, map, count, tableList);
                        }
                    }
                }
            }
            List<XWPFTable> xwpfTables = document.getTables();
            int flag = 0;
            for (XWPFTable xwpfTable : xwpfTables) {
                List<XWPFTableRow> rows = xwpfTable.getRows();
                for (int k = 1; k < rows.size(); k++) {
                    List<XWPFTableCell> cells = rows.get(k).getTableCells();
                    for (int j = 0; j < cells.size(); j++) {
                        if (map.get("tableName1").equals("用能统计表") || map.get("tableName1").equals("")){
                            if (j == listUnit[0]){
                                if (!EditWordZZUtil.isChinese(subscript.get(0).get(k -1)[flag])){
                                    XWPFTableCell cell = cells.get(j);
                                    XWPFParagraph paragraph = cell.addParagraph();
                                    XWPFRun run1 = paragraph.createRun();
                                    run1.setText(subscript.get(0).get(k -1)[flag++],0);
                                    cell.removeParagraph(0);
                                    XWPFRun run2 = paragraph.createRun();
                                    if (flag < 1) {
                                        run2.setText(subscript.get(0).get(k -1)[flag++],0);
                                        run2.setSubscript(VerticalAlign.SUBSCRIPT);
                                    } else {
                                        run2.setText(subscript.get(0).get(k -1)[flag],0);
                                        run2.setSubscript(VerticalAlign.SUBSCRIPT);
                                        flag = 0;
                                    }
                                    cell.getCTTc().addNewTcPr().addNewVAlign().setVal(STVerticalJc.CENTER);
                                    cell.getCTTc().getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
                                }
                            }
                        } else {
                            if (j == listUnit[0] || j == listUnit[1]){
                                if (!EditWordZZUtil.isChinese(subscript.get(0).get(k -1)[flag])){
                                    XWPFTableCell cell = cells.get(j);
                                    XWPFParagraph paragraph = cell.addParagraph();
                                    XWPFRun run1 = paragraph.createRun();
                                    run1.setText(subscript.get(0).get(k -1)[flag++],0);
                                    cell.removeParagraph(0);
                                    XWPFRun run2 = paragraph.createRun();
                                    if (flag < 3) {
                                        run2.setText(subscript.get(0).get(k -1)[flag++],0);
                                        run2.setSubscript(VerticalAlign.SUBSCRIPT);
                                    } else {
                                        run2.setText(subscript.get(0).get(k -1)[flag],0);
                                        run2.setSubscript(VerticalAlign.SUBSCRIPT);
                                        flag = 0;
                                    }
                                    cell.getCTTc().addNewTcPr().addNewVAlign().setVal(STVerticalJc.CENTER);
                                    cell.getCTTc().getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
                                }
                            }
                        }
                    }
                }
                //综合能耗计算表最后一页添加行
                if (map.get("tableName1").equals("") || map.get("tableName1").equals("用能统计表")){
                    if (tables.get(0).get(0).length == 5){
                        String[] str = {"综合能耗合计", "", "tce", (String) map.get("amount"),""};
                        XWPFTableRow row = xwpfTable.createRow();
                        List<XWPFTableCell> cells = row.getTableCells();
                        for (int j = 0; j < cells.size(); j++) {
                            XWPFTableCell cell = cells.get(j);
                            XWPFRun run = cell.addParagraph().createRun();
                            run.setText(str[j]);
                            cell.removeParagraph(0);
                            CTTc ctTc = cell.getCTTc();
                            CTTcPr ctTcPr = ctTc.addNewTcPr();
                            ctTcPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                            ctTc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER );
                        }
                        EditWordZZUtil.mergeCellsHorizontal(xwpfTable,xwpfTable.getRows().size() - 1,0,1);
                        EditWordZZUtil.mergeCellsHorizontal(xwpfTable,xwpfTable.getRows().size() - 1,3,4);
                    }else {
                        String[] str = {"综合能耗合计", "","", "tce", (String) map.get("amount"),""};
                        XWPFTableRow row = xwpfTable.createRow();
                        List<XWPFTableCell> cells = row.getTableCells();
                        for (int j = 0; j < cells.size(); j++) {
                            XWPFTableCell cell = cells.get(j);
                            XWPFRun run = cell.addParagraph().createRun();
                            run.setText(str[j]);
                            cell.removeParagraph(0);
                            CTTc ctTc = cell.getCTTc();
                            CTTcPr ctTcPr = ctTc.addNewTcPr();
                            ctTcPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                            ctTc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER );
                        }
                        EditWordZZUtil.mergeCellsHorizontal(xwpfTable,xwpfTable.getRows().size() - 1,0,2);
                        EditWordZZUtil.mergeCellsHorizontal(xwpfTable,xwpfTable.getRows().size() - 1,4,5);
                    }
                }
            }
            XWPFRun run = document.createParagraph().createRun();
            run.setText("（以下空白）");
//            run.addBreak();
            EditWordZZUtil.readWriteWord(document,map);
            XWPFRun run1 = document.createParagraph().createRun();
            run1.setText(FIRST_LINE);
            run1.setBold(true);
            run1.addBreak();
            XWPFRun run2 = document.createParagraph().createRun();
            run2.setText(DAYS_LINE);
//            run2.addBreak();
            XWPFRun run3 = document.createParagraph().createRun();
            run3.setText(SEN_LINE);
            run3.setBold(true);
            run3.addBreak();
            XWPFParagraph paragraph = document.createParagraph();
            XWPFRun run4 = paragraph.createRun();
            run4.setText(LAST_LINE);
            run4.setBold(true);
            XWPFRun run5 = paragraph.createRun();
            run5.setText(OTHER_LINE);
//        run5.addBreak();
            document.removeBodyElement(7);
            document.write(outputStream);
            if (null != outputStream){
                outputStream.close();
            }
        }else {
            for (int i = 0; i < pages; i++) {
                List<List<String[]>> nomalList = new ArrayList<>();
                nomalList.addAll(tableList);
                List<List<String[]>> unitList = new ArrayList<>();
                unitList.addAll(subscript);

                //创建一个新的List来存储数据
                List<List<String[]>> newNormalList = new ArrayList<List<String[]>>();
//                newNormalList.add(null);
                List<List<String[]>> newUnitList = new ArrayList<List<String[]>>();
//                newUnitList.add(null);
                if (i == 0){
                    List<String[]> strings = nomalList.get(0).subList(1, flagAll.get(i) + 1);
                    midCount = flagAll.get(i) + 1;
                    for (int j = 0; j < strings.size(); j++) {
                        String[] str = strings.get(j);
                        if(newNormalList.size() == 0){
                            List<String[]> newList = new ArrayList<>();
                            newList.add(str);
                            newNormalList.add(newList);
                        }else{
                            newNormalList.get(0).add(str);
                        }
                    }
                    List<String[]> subList = unitList.get(0).subList(0, flagAll.get(i));
                    for (int j = 0; j < subList.size(); j++) {
                        String[] str = subList.get(j);
                        if(newUnitList.size() == 0){
                            List<String[]> newList = new ArrayList<>();
                            newList.add(str);
                            newUnitList.add(newList);
                        }else{
                            newUnitList.get(0).add(str);
                        }
                    }
                }else if (i + 1 == pages){
                    for (int j = 0; j < midCount; j++) {
                        nomalList.get(0).remove(0);
                    }
                    List<String[]> strings = nomalList.get(0);
                    for (int j = 0; j < strings.size(); j++) {
                        String[] str = strings.get(j);
                        if(newNormalList.size() == 0){
                            List<String[]> newList = new ArrayList<>();
                            newList.add(str);
                            newNormalList.add(newList);
                        }else{
                            newNormalList.get(0).add(str);
                        }
                    }
                    for (int j = 0; j < midCount - 1; j++) {
                        unitList.get(0).remove(0);
                    }
                    List<String[]> subList = unitList.get(0);
                    for (int j = 0; j < subList.size(); j++) {
                        String[] str = subList.get(j);
                        if(newUnitList.size() == 0){
                            List<String[]> newList = new ArrayList<>();
                            newList.add(str);
                            newUnitList.add(newList);
                        }else{
                            newUnitList.get(0).add(str);
                        }
                    }
                }else {
                    List<String[]> strings = nomalList.get(0).subList(midCount, flagAll.get(i) + 1);
                    for (int j = 0; j < strings.size(); j++) {
                        String[] str = strings.get(j);
                        if(newNormalList.size() == 0){
                            List<String[]> newList = new ArrayList<>();
                            newNormalList.add(newList);
                            newNormalList.get(0).add(str);
                        }else{
                            newNormalList.get(0).add(str);
                        }
                    }
                    List<String[]> subList = unitList.get(0).subList(midCount - 1, flagAll.get(i));
                    for (int j = 0; j < subList.size(); j++) {
                        String[] str = subList.get(j);
                        if(newUnitList.size() == 0){
                            List<String[]> newList = new ArrayList<>();
                            newUnitList.add(newList);
                            newUnitList.get(0).add(str);
                        }else{
                            newUnitList.get(0).add(str);
                        }
                    }
                    midCount = flagAll.get(i) + 1;
                }
                //先操作第一页，不然没办法合并docx
                if (i == 0){
                    map.replace("index",i + 1);
                    InputStream inputStream = minioTemplate.minio.getObject("jljc", "table.docx");
                    document = new XWPFDocument(OPCPackage.open(inputStream));
                    List<XWPFParagraph> paragraphs = document.getParagraphs();
                    EditWordZZUtil.setUnderLine(document,map,1);
                    for (XWPFParagraph paragraph : paragraphs) {
                        List<XWPFRun> runs = paragraph.getRuns();
                        for (int j = 0; j < runs.size(); j++) {
                            if (runs.get(j).getText(0) != null){
                                if (runs.get(j).getText(0).contains("jcNumber")){
                                    XWPFParagraph xwpfParagraph = paragraphs.get(0);
                                    int size = xwpfParagraph.getRuns().size();
                                    for (int k = 0; k < size - 1; k++) {
                                        xwpfParagraph.removeRun(1);
                                    }
                                    XWPFRun run = xwpfParagraph.createRun();
                                    run.setText(NUMBER_JC);
                                    run.setBold(true);
                                    XWPFRun run1 = xwpfParagraph.createRun();
                                    run1.setText((String) map.get("text"));
                                }
                                if (runs.get(0).getText(0).contains("tables")){
                                    XWPFParagraph paragraph1 = paragraphs.get(paragraphs.size() - 1);
                                    paragraph1.removeRun(0);
                                    XWPFTable table = document.createTable();
                                    CTTblWidth ctTblWidth = table.getCTTbl().addNewTblPr().addNewTblW();
                                    ctTblWidth.setType(STTblWidth.PCT);
                                    ctTblWidth.setW(BigInteger.valueOf(4960));
                                    //获取表头信息,处理表头信息
                                    String[] strings = tableList.get(0).get(0);
                                    for (int k = 0; k < strings.length; k++) {
                                        if (k == 0){
                                            XWPFTableCell cell = table.getRow(0).getCell(0);
                                            XWPFRun run = cell.addParagraph().createRun();
                                            run.setText(strings[j]);
                                            run.setBold(true);
                                            cell.removeParagraph(0);
                                            CTTc ctTc = cell.getCTTc();
                                            CTTcPr ctTcPr = ctTc.addNewTcPr();
                                            ctTcPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                                            ctTc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER );
                                        }else {
                                            XWPFTableCell cell = table.getRow(0).addNewTableCell();
                                            XWPFRun run = cell.addParagraph().createRun();
                                            run.setText(strings[k]);
                                            run.setBold(true);
                                            cell.removeParagraph(0);
                                            CTTc ctTc = cell.getCTTc();
                                            CTTcPr ctTcPr = ctTc.addNewTcPr();
                                            ctTcPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                                            ctTc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER );
//                                            ctTcPr.addNewTcW().setW(BigInteger.valueOf(1700));
                                            if (strings[k].equals("符号") || strings[k].equals("单位")){
                                                ctTcPr.addNewTcW().setW(BigInteger.valueOf(800));
                                                ctTcPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                                                ctTc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER );
                                            } else {
                                                ctTcPr.addNewTcW().setW(BigInteger.valueOf(1800));
                                            }
                                        }
                                    }
                                    int[] count = {0};
                                    EditWordZZUtil.readWriteWord(document, map, count, newNormalList);
                                }
                            }
                        }
                    }
                    List<XWPFTable> xwpfTables = document.getTables();
                    int flag = 0;
                    for (XWPFTable xwpfTable : xwpfTables) {
                        List<XWPFTableRow> rows = xwpfTable.getRows();
                        for (int k = 1; k < rows.size(); k++) {
                            List<XWPFTableCell> cells = rows.get(k).getTableCells();
                            for (int j = 0; j < cells.size(); j++) {
                                if(map.get("tableName1").equals("用能统计表") || map.get("tableName1").equals("")){
                                    if(j == listUnit[0]){
                                        if(!EditWordZZUtil.isChinese(newUnitList.get(0).get(k -1)[flag])){
                                        XWPFTableCell cell = cells.get(j);
                                        XWPFParagraph paragraph = cell.addParagraph();
                                        XWPFRun run1 = paragraph.createRun();
                                        run1.setText(newUnitList.get(0).get(k -1)[flag++],0);
                                        cell.removeParagraph(0);
                                        XWPFRun run2 = paragraph.createRun();
                                        if (flag < 1){
                                            run2.setText(newUnitList.get(0).get(k -1)[flag++],0);
                                            run2.setSubscript(VerticalAlign.SUBSCRIPT);
                                        }else {
                                            run2.setText(newUnitList.get(0).get(k -1)[flag],0);
                                            run2.setSubscript(VerticalAlign.SUBSCRIPT);
                                            flag = 0;
                                            }
                                            cell.getCTTc().addNewTcPr().addNewVAlign().setVal(STVerticalJc.CENTER);
                                            cell.getCTTc().getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
                                        }
                                    }
                                }else {
                                    if (j == listUnit[0] || j == listUnit[1]){
                                        XWPFTableCell cell = cells.get(j);
                                        XWPFParagraph paragraph = cell.addParagraph();
                                        XWPFRun run1 = paragraph.createRun();
                                        run1.setText(newUnitList.get(0).get(k -1)[flag++],0);
                                        cell.removeParagraph(0);
                                        XWPFRun run2 = paragraph.createRun();
                                        if (flag < 3){
                                            run2.setText(newUnitList.get(0).get(k -1)[flag++],0);
                                            run2.setSubscript(VerticalAlign.SUBSCRIPT);
                                        }else {
                                            run2.setText(newUnitList.get(0).get(k -1)[flag],0);
                                            run2.setSubscript(VerticalAlign.SUBSCRIPT);
                                            flag = 0;
                                        }
                                        cell.getCTTc().addNewTcPr().addNewVAlign().setVal(STVerticalJc.CENTER);
                                        cell.getCTTc().getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
                                    }
                                }
                            }
                        }
                    }
                    XWPFRun run = document.createParagraph().createRun();
                    run.setText("（以下空白）");
//                    run.addBreak();
                    EditWordZZUtil.readWriteWord(document,map);
                    XWPFRun run1 = document.createParagraph().createRun();
                    run1.setText(FIRST_LINE);
                    run1.setBold(true);
                    run1.addBreak();
                    XWPFRun run2 = document.createParagraph().createRun();
                    run2.setText(DAYS_LINE);
//                    run2.addBreak();
                    XWPFRun run3 = document.createParagraph().createRun();
                    run3.setText(SEN_LINE);
                    run3.setBold(true);
                    run3.addBreak();
                    XWPFParagraph paragraph = document.createParagraph();
                    XWPFRun run4 = paragraph.createRun();
                    run4.setText(LAST_LINE);
                    run4.setBold(true);
                    XWPFRun run5 = paragraph.createRun();
                    run5.setText(OTHER_LINE);
//        run5.addBreak();
                    document.removeBodyElement(7);
                    if (null != inputStream){
                        inputStream.close();
                    }
                }else {
                    //循环插入
                    map.replace("index",i + 1);
                    InputStream inputStream = minioTemplate.minio.getObject("jljc", "table.docx");
                    XWPFDocument xwpfDocument = new XWPFDocument(OPCPackage.open(inputStream));
                    List<XWPFParagraph> paragraphs = xwpfDocument.getParagraphs();
                    EditWordZZUtil.setUnderLine(xwpfDocument,map,1);
                    for (XWPFParagraph paragraph : paragraphs) {
                        List<XWPFRun> runs = paragraph.getRuns();
                        for (int j = 0; j < runs.size(); j++) {
                            if (runs.get(j).getText(0) != null){
                                if (runs.get(j).getText(0).contains("jcNumber")){
                                    XWPFParagraph xwpfParagraph = paragraphs.get(0);
                                    int size = xwpfParagraph.getRuns().size();
                                    for (int k = 0; k < size - 1; k++) {
                                        xwpfParagraph.removeRun(1);
                                    }
                                    XWPFRun run = xwpfParagraph.createRun();
                                    run.setText(NUMBER_JC);
                                    run.setBold(true);
                                    XWPFRun run1 = xwpfParagraph.createRun();
                                    run1.setText((String) map.get("text"));
//                                    runs.remove(0);
                                }
                                if (runs.get(0).getText(0).contains("tables")){
                                    XWPFParagraph paragraph1 = paragraphs.get(paragraphs.size() - 1);
                                    paragraph1.removeRun(0);
                                    XWPFTable table = xwpfDocument.createTable();
                                    CTTblWidth ctTblWidth = table.getCTTbl().addNewTblPr().addNewTblW();
                                    ctTblWidth.setType(STTblWidth.PCT);
                                    ctTblWidth.setW(BigInteger.valueOf(4960));
                                    //获取表头信息,处理表头信息
                                    String[] strings = tables.get(0).get(0);
                                    for (int k = 0; k < strings.length; k++) {
                                        if (k == 0){
                                            XWPFTableCell cell = table.getRow(0).getCell(0);
                                            XWPFRun run = cell.addParagraph().createRun();
                                            run.setText(strings[j]);
                                            run.setBold(true);
                                            cell.removeParagraph(0);
                                            CTTc ctTc = cell.getCTTc();
                                            CTTcPr ctTcPr = ctTc.addNewTcPr();
                                            ctTcPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                                            ctTc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER );
//                                            ctTcPr.addNewTcW().setW(BigInteger.valueOf(1700));
                                        }else {
                                            XWPFTableCell cell = table.getRow(0).addNewTableCell();
                                            XWPFRun run = cell.addParagraph().createRun();
                                            run.setText(strings[k]);
                                            run.setBold(true);
                                            cell.removeParagraph(0);
                                            CTTc ctTc = cell.getCTTc();
                                            CTTcPr ctTcPr = ctTc.addNewTcPr();
                                            ctTcPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                                            ctTc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER );
                                            if (strings[k].equals("符号") || strings[k].equals("单位")){
                                                ctTcPr.addNewTcW().setW(BigInteger.valueOf(800));
                                                ctTcPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                                                ctTc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER );
                                            } else {
                                                ctTcPr.addNewTcW().setW(BigInteger.valueOf(1700));
                                            }
                                        }
                                    }
                                    int[] count = {0};
                                    EditWordZZUtil.readWriteWord(xwpfDocument, map, count, newNormalList);
                                }
                            }
                        }
                    }
                    List<XWPFTable> xwpfTables = xwpfDocument.getTables();
                    int flag = 0;
                    for (XWPFTable xwpfTable : xwpfTables) {
                        List<XWPFTableRow> rows = xwpfTable.getRows();
                        for (int k = 1; k < rows.size(); k++) {
                            List<XWPFTableCell> cells = rows.get(k).getTableCells();
                            for (int j = 0; j < cells.size(); j++) {
                                if (map.get("tableName1").equals("用能统计表") || map.get("tableName1").equals("")){
                                    if (j == listUnit[0]){
                                        if(!EditWordZZUtil.isChinese(newUnitList.get(0).get(k -1)[flag])){
                                            XWPFTableCell cell = cells.get(j);
                                            XWPFParagraph paragraph = cell.addParagraph();
                                            XWPFRun run1 = paragraph.createRun();
                                            run1.setText(newUnitList.get(0).get(k -1)[flag++],0);
                                            cell.removeParagraph(0);
                                            XWPFRun run2 = paragraph.createRun();
                                            if (flag < 1){
                                                run2.setText(newUnitList.get(0).get(k -1)[flag++],0);
                                                run2.setSubscript(VerticalAlign.SUBSCRIPT);
                                            }else {
                                                run2.setText(newUnitList.get(0).get(k -1)[flag],0);
                                                run2.setSubscript(VerticalAlign.SUBSCRIPT);
                                                flag = 0;
                                            }
                                            cell.getCTTc().addNewTcPr().addNewVAlign().setVal(STVerticalJc.CENTER);
                                            cell.getCTTc().getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
                                        }
                                    }
                                }else {
                                    if (j == listUnit[0] || j == listUnit[1]){
                                        XWPFTableCell cell = cells.get(j);
                                        XWPFParagraph paragraph = cell.addParagraph();
                                        XWPFRun run1 = paragraph.createRun();
                                        run1.setText(newUnitList.get(0).get(k -1)[flag++],0);
                                        cell.removeParagraph(0);
                                        XWPFRun run2 = paragraph.createRun();
                                        if (flag < 3){
                                            run2.setText(newUnitList.get(0).get(k -1)[flag++],0);
                                            run2.setSubscript(VerticalAlign.SUBSCRIPT);
                                        }else {
                                            run2.setText(newUnitList.get(0).get(k -1)[flag],0);
                                            run2.setSubscript(VerticalAlign.SUBSCRIPT);
                                            flag = 0;
                                        }
                                        cell.getCTTc().addNewTcPr().addNewVAlign().setVal(STVerticalJc.CENTER);
                                        cell.getCTTc().getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
                                    }
                                }
                            }
                        }
                        //综合能耗计算表最后一页添加行
                        if (i + 1 == pages){
                            if (map.get("tableName1").equals("") || map.get("tableName1").equals("用能统计表")){
                                if (tables.get(0).get(0).length == 5){
                                    String[] str = {"综合能耗合计", "", "tce", (String) map.get("amount"),""};
                                    XWPFTableRow row = xwpfTable.createRow();
                                    List<XWPFTableCell> cells = row.getTableCells();
                                    for (int j = 0; j < cells.size(); j++) {
                                        XWPFTableCell cell = cells.get(j);
                                        XWPFRun run = cell.addParagraph().createRun();
                                        run.setText(str[j]);
                                        cell.removeParagraph(0);
                                        CTTc ctTc = cell.getCTTc();
                                        CTTcPr ctTcPr = ctTc.addNewTcPr();
                                        ctTcPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                                        ctTc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER );
                                    }
                                    EditWordZZUtil.mergeCellsHorizontal(xwpfTable,xwpfTable.getRows().size() - 1,0,1);
                                    EditWordZZUtil.mergeCellsHorizontal(xwpfTable,xwpfTable.getRows().size() - 1,3,4);
                                } else {
                                    String[] str = {"综合能耗合计", "","", "tce", (String) map.get("amount"),""};
                                    XWPFTableRow row = xwpfTable.createRow();
                                    List<XWPFTableCell> cells = row.getTableCells();
                                    for (int j = 0; j < cells.size(); j++) {
                                        XWPFTableCell cell = cells.get(j);
                                        XWPFRun run = cell.addParagraph().createRun();
                                        run.setText(str[j]);
                                        cell.removeParagraph(0);
                                        CTTc ctTc = cell.getCTTc();
                                        CTTcPr ctTcPr = ctTc.addNewTcPr();
                                        ctTcPr.addNewVAlign().setVal(STVerticalJc.CENTER);
                                        ctTc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER );
                                    }
                                    EditWordZZUtil.mergeCellsHorizontal(xwpfTable,xwpfTable.getRows().size() - 1,0,2);
                                    EditWordZZUtil.mergeCellsHorizontal(xwpfTable,xwpfTable.getRows().size() - 1,4,5);
                                }
                            }
                        }
                    }
                    XWPFRun run = xwpfDocument.createParagraph().createRun();
                    run.setText("（以下空白）");
//                    run.addBreak();
                    int remainTotal = 0;
                    if (i + 1 == pages){
                        List<String[]> strings = newNormalList.get(0);
                        //最后一页填充的行数
                        for (int j = 0; j < strings.size(); j++) {
                            remainTotal += strings.get(j)[0].length() / lineSize + (strings.get(j)[0].length() % lineSize != 0 ? 1 : 0);
                        }
                        for (int j = 0; j < lineTotal - remainTotal - 2; j++) {
                            run.addBreak();
                        }
                    }
                    EditWordZZUtil.readWriteWord(xwpfDocument,map);
                    XWPFRun run1 = xwpfDocument.createParagraph().createRun();
                    run1.setText(FIRST_LINE);
                    run1.setBold(true);
                    run1.addBreak();
                    XWPFRun run2 = xwpfDocument.createParagraph().createRun();
                    run2.setText(DAYS_LINE);
//                    run2.addBreak();
                    XWPFRun run3 = xwpfDocument.createParagraph().createRun();
                    run3.setText(SEN_LINE);
                    run3.setBold(true);
                    run3.addBreak();
                    XWPFParagraph paragraph = xwpfDocument.createParagraph();
                    XWPFRun run4 = paragraph.createRun();
                    run4.setText(LAST_LINE);
                    run4.setBold(true);
                    XWPFRun run5 = paragraph.createRun();
                    run5.setText(OTHER_LINE);
                    xwpfDocument.removeBodyElement(7);

                    MergeDocx mergeDocx = new MergeDocx();
                    document = mergeDocx.mergeWord(document,xwpfDocument);
                    if (null != inputStream){
                        inputStream.close();
                    }
                }
            }
            document.write(outputStream);
        }
        if (null != outputStream){
            outputStream.close();
        }
    }

}
