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.InputStream;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class XWPFTablesUtil {

    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 XWPFTablesUtil 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;
        List<List<String[]>> senTables = 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");
        senTables = (List<List<String[]>>) map.get("tables");
        XWPFDocument document = null;
        int lineTotal = 0;  //每页可以填充的行数
        int lineSize = 0; //每行的字数
        if (tables.get(0).get(0).length == 5){
            lineSize = 5;
            if (companyName.length() <= 15){
                lineTotal = 14;
            }else if (companyName.length() > 15 && companyName.length() < 30){
                lineTotal = 14;
            }else {
                lineTotal = 14;
            }
        }else {
            lineSize = 5;
            if (companyName.length() <= 15){
                lineTotal = 13;
            }else if (companyName.length() > 15 && companyName.length() < 30){
                lineTotal = 13;
            }else {
                lineTotal = 13;
            }
        }
        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));
        }

        List<List<String[]>> senTableList = new ArrayList<>();  //单耗计算表正常填充数据
        List<List<String[]>> senSubscript = new ArrayList<>(); //单耗计算表单位、符号填充数据
        int[] senUnit = EditWordZZUtil.getListUnit(senTables.get(0));
        for (List<String[]> senTable : senTables) {
            senSubscript.add(EditWordZZUtil.getListInfo(senTable));
            senTableList.add(EditWordZZUtil.replaceStr(senTable));
        }
        //表格数据的总行数
        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 remainLine = 0; //填充完用能统计表后剩下的行数
        Integer totalFlag = 0;
        List<Integer> senFlagAll = new ArrayList<>(); //单耗计算表的标志位，记录当前页的数据截取位置
        //填充到第几页
        totalFlag = flagAll.size();
        //当前页剩下的行数
        remainLine = lineTotal - total;
        //数据截取
        for (int i = 0; i < senTableList.size(); i++) {
            List<String[]> list = senTableList.get(i);
            for (int j = 0; j < list.size(); j++) {
                if (j > 1){
                    String[] str = list.get(j);
                    remainLine += str[0].length() / lineSize + (str[0].length() % lineSize != 0 ? 1 : 0);
                    if (remainLine + 4 >= lineTotal && j + 1 < list.size()){
                        senFlagAll.add(j);
                        remainLine = 0;
                    }
                }
            }
        }

        //计算总页数
        int pages = 0;
        if (flagAll.size() == 0 && senFlagAll.size() == 0){
            pages = 1;
        }else {
            pages = flagAll.size() + senFlagAll.size();
        }
        map.replace("total",pages);
        //按照总页数及标志位把数据拆分
        //记录上一次截取的位置
        int midCount = 0;
        int senMidCount = 0;
        if (pages ==  1){
            map.replace("total","1");
            map.replace("index","1");
            InputStream inputStream = minioTemplate.minio.getObject("jljc", "tables.docx");
            document = new XWPFDocument(OPCPackage.open(inputStream));
            List<XWPFParagraph> paragraphs = document.getParagraphs();
            EditWordZZUtil.setUnderLine(document,map,1);
            a: 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("tablex")){
                            XWPFParagraph paragraph1 = paragraphs.get(paragraphs.size() - 1);
                            paragraph1.removeRun(0);
                            paragraph1.createRun().setText((String) map.get("tableName1"));
                            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);
                            XWPFParagraph paragraph3 = document.createParagraph();
                            XWPFRun xwpfRun = paragraph3.createRun();
                            xwpfRun.addBreak();
                            XWPFRun run = paragraph3.createRun();
                            run.setText("单耗计算表");
                            run.setBold(true);
                            XWPFTable table1 = document.createTable();
                            CTTblWidth ctTblWidth1 = table1.getCTTbl().addNewTblPr().addNewTblW();
                            ctTblWidth1.setType(STTblWidth.PCT);
                            ctTblWidth1.setW(BigInteger.valueOf(4960));
                            //获取表头信息,处理表头信息
                            String[] strings1 = senTableList.get(0).get(0);
                            for (int k = 0; k < strings1.length; k++) {
                                if (k == 0){
                                    XWPFTableCell cell = table1.getRow(0).getCell(0);
                                    XWPFRun run1 = cell.addParagraph().createRun();
                                    run1.setText(strings1[k]);
                                    run1.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 = table1.getRow(0).addNewTableCell();
                                    XWPFRun run1 = cell.addParagraph().createRun();
                                    run1.setText(strings1[k]);
                                    run1.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));
                                    }
                                }
                            }
                            //处理表格数据信息
                            senTableList.get(0).remove(0);
                            break a;
                        }
                    }
                }
            }
            //处理表格数据信息
            int[] count = {0, 1};
            tableList.add(senTableList.get(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("用能统计表")){
                            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 == senUnit[0] || j == senUnit[1]){
                                if (!EditWordZZUtil.isChinese(senSubscript.get(0).get(k -1)[flag])){
                                    XWPFTableCell cell = cells.get(j);
                                    XWPFParagraph paragraph = cell.addParagraph();
                                    XWPFRun run1 = paragraph.createRun();
                                    run1.setText(senSubscript.get(0).get(k -1)[flag++],0);
                                    cell.removeParagraph(0);
                                    XWPFRun run2 = paragraph.createRun();
                                    if (flag < 3) {
                                        run2.setText(senSubscript.get(0).get(k -1)[flag++],0);
                                        run2.setSubscript(VerticalAlign.SUBSCRIPT);
                                    } else {
                                        run2.setText(senSubscript.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);
                                }
                            }
                        }
                    }
                }
                map.replace("tableName1","单耗计算表");
            }
            map.replace("tableName1","用能统计表");
            //综合能耗计算表最后一页添加行
            if (map.get("tableName1").equals("用能统计表")){
                XWPFTable xwpfTable = xwpfTables.get(0);
                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);
                }
            }
            map.replace("tableName1", "用能情况表");
            EditWordZZUtil.readWriteWord(document, map);
            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();
            XWPFParagraph paragraph1 = document.createParagraph();
            XWPFRun run2 = paragraph1.createRun();
            paragraph1.setAlignment(ParagraphAlignment.RIGHT);
            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<String[]>> senNomalList = new ArrayList<>();
                senNomalList.addAll(senTableList);
                List<List<String[]>> senUnitList = new ArrayList<>();
                senUnitList.addAll(senSubscript);

                //创建一个新的List来存储数据，进行数据截取
                List<List<String[]>> newNormalList = new ArrayList<List<String[]>>();
                List<List<String[]>> newUnitList = new ArrayList<List<String[]>>();

                List<List<String[]>> senNewNormalList = new ArrayList<List<String[]>>();
                List<List<String[]>> senNewUnitList = new ArrayList<List<String[]>>();

                if (i == 0){
                    List<String[]> strings = new ArrayList<>();
                    if (flagAll.size() == 0){
                        strings = nomalList.get(0).subList(1,nomalList.get(0).size());
                    }else {
                        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 = new ArrayList<>();
                    if (flagAll.size() == 0){
                        subList = unitList.get(0).subList(0, unitList.get(0).size());
                    }else {
                        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);
                        }
                    }
                    if (i == totalFlag){
                        List<String[]> list = senNomalList.get(0).subList(1, senFlagAll.get(i) + 1);
                        senMidCount = senFlagAll.get(i) + 1;
                        for (int j = 0; j < list.size(); j++) {
                            String[] str = list.get(j);
                            if (senNewNormalList.size() == 0){
                                List<String[]> newList = new ArrayList<>();
                                newList.add(str);
                                senNewNormalList.add(newList);
                            }else {
                                senNewNormalList.get(0).add(str);
                            }
                        }
                        List<String[]> senList = senUnitList.get(0).subList(0, senFlagAll.get(i));
                        for (int j = 0; j < senList.size(); j++) {
                            String[] str = senList.get(j);
                            if (senNewUnitList.size() == 0){
                                List<String[]> newList = new ArrayList<>();
                                newList.add(str);
                                newUnitList.add(newList);
                            }else {
                                senUnitList.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);
                        }
                    }
                    if (i == totalFlag){
//                        for (int j = 0; j < senMidCount; j++) {
//                            nomalList.get(0).remove(0);
//                        }
                        List<String[]> list = senNomalList.get(0);
                        for (int j = 0; j < list.size(); j++) {
                            String[] str = list.get(j);
                            if (senNewNormalList.size() == 0){
                                List<String[]> newList = new ArrayList<>();
                                newList.add(str);
                                senNewNormalList.add(newList);
                            }else {
                                senNewNormalList.get(0).add(str);
                            }
                        }
                        List<String[]> senList = senUnitList.get(0);
                        for (int j = 0; j < senList.size(); j++) {
                            String[] str = senList.get(j);
                            if (senNewUnitList.size() == 0){
                                List<String[]> newList = new ArrayList<>();
                                newList.add(str);
                                newUnitList.add(newList);
                            }else {
                                senUnitList.get(0).add(str);
                            }
                        }
                    }else if (i > totalFlag){
                        List<String[]> list = senNomalList.get(0).subList(senMidCount, senNomalList.get(0).size());
                        for (int j = 0; j < list.size(); j++) {
                            String[] str = list.get(j);
                            if (senNewNormalList.size() == 0){
                                List<String[]> newList = new ArrayList<>();
                                newList.add(str);
                                senNewNormalList.add(newList);
                            }else {
                                senNewNormalList.get(0).add(str);
                            }
                        }
                        List<String[]> senList = senUnitList.get(0).subList(senMidCount - 1, senUnitList.get(0).size());
                        for (int j = 0; j < senList.size(); j++) {
                            String[] str = senList.get(j);
                            if (senNewUnitList.size() == 0){
                                List<String[]> newList = new ArrayList<>();
                                newList.add(str);
                                senNewUnitList.add(newList);
                            }else {
                                senNewUnitList.get(0).add(str);
                            }
                        }
                    }
                }else {
                    if (i == flagAll.size()){
                        List<String[]> strings = nomalList.get(0).subList(midCount, nomalList.get(0).size());
                        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, unitList.get(0).size());
                        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);
                            }
                        }
                    }else if (i < flagAll.size()){
                        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);
                            }
                        }
                    }
                    if (i == totalFlag){
//                        senMidCount = senFlagAll.get(i) + 1;
                        List<String[]> list = senNomalList.get(0).subList(1, senFlagAll.get(i - totalFlag) + 1);
                        for (int j = 0; j < list.size(); j++) {
                            String[] str = list.get(j);
                            if (senNewNormalList.size() == 0){
                                List<String[]> newList = new ArrayList<>();
                                newList.add(str);
                                senNewNormalList.add(newList);
                            }else {
                                senNewNormalList.get(0).add(str);
                            }
                        }
                        List<String[]> senList = senUnitList.get(0).subList(0, senFlagAll.get(i - totalFlag));
                        for (int j = 0; j < senList.size(); j++) {
                            String[] str = senList.get(j);
                            if (senNewUnitList.size() == 0){
                                List<String[]> newList = new ArrayList<>();
                                newList.add(str);
                                senNewUnitList.add(newList);
                            }else {
                                senNewUnitList.get(0).add(str);
                            }
                        }
                    }else if (i > totalFlag){
                        List<String[]> list = senNomalList.get(0).subList(senMidCount, senFlagAll.get(i) + 1);
                        for (int j = 0; j < list.size(); j++) {
                            String[] str = list.get(j);
                            if (senNewNormalList.size() == 0){
                                List<String[]> newList = new ArrayList<>();
                                newList.add(str);
                                senNewNormalList.add(newList);
                            }else {
                                senNewNormalList.get(0).add(str);
                            }
                        }
                        List<String[]> senList = senUnitList.get(0).subList(senMidCount - 1, senFlagAll.get(i));
                        for (int j = 0; j < senList.size(); j++) {
                            String[] str = senList.get(j);
                            if (senNewUnitList.size() == 0){
                                List<String[]> newList = new ArrayList<>();
                                newList.add(str);
                                senNewUnitList.add(newList);
                            }else {
                                senNewUnitList.get(0).add(str);
                            }
                        }
                    }

                    if (i < flagAll.size()){
                        midCount = flagAll.get(i) + 1;
                    }
                    senMidCount = senFlagAll.get(i - 1) + 1;
                }
                //先操作第一页，不然没办法合并docx
                if (i == 0){
                    map.replace("index",i + 1);
                    InputStream inputStream = minioTemplate.minio.getObject("jljc", "tables.docx");
                    document = new XWPFDocument(OPCPackage.open(inputStream));
                    EditWordZZUtil.setUnderLine(document,map,1);
                    if (i == totalFlag){
                        List<XWPFParagraph> paragraphs = document.getParagraphs();
                        EditWordZZUtil.setUnderLine(document,map,1);
                        a: 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("tablex")){
                                        XWPFParagraph paragraph1 = paragraphs.get(paragraphs.size() - 1);
                                        paragraph1.removeRun(0);
                                        paragraph1.createRun().setText((String) map.get("tableName1"));
                                        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);
                                        XWPFParagraph paragraph3 = document.createParagraph();
                                        XWPFRun xwpfRun = paragraph3.createRun();
                                        xwpfRun.addBreak();
                                        XWPFRun run = paragraph3.createRun();
                                        run.setText("单耗计算表");
                                        run.setBold(true);
                                        XWPFTable table1 = document.createTable();
                                        CTTblWidth ctTblWidth1 = table1.getCTTbl().addNewTblPr().addNewTblW();
                                        ctTblWidth1.setType(STTblWidth.PCT);
                                        ctTblWidth1.setW(BigInteger.valueOf(4960));
                                        //获取表头信息,处理表头信息
                                        String[] strings1 = senTableList.get(0).get(0);
                                        for (int k = 0; k < strings1.length; k++) {
                                            if (k == 0){
                                                XWPFTableCell cell = table1.getRow(0).getCell(0);
                                                XWPFRun run1 = cell.addParagraph().createRun();
                                                run1.setText(strings1[k]);
                                                run1.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 = table1.getRow(0).addNewTableCell();
                                                XWPFRun run1 = cell.addParagraph().createRun();
                                                run1.setText(strings1[k]);
                                                run1.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));
                                                }
                                            }
                                        }
                                        //处理表格数据信息
//                                        senTableList.get(0).remove(0);
                                        break a;
                                    }
                                }
                            }
                        }
                        //处理表格数据信息
                        int[] count = {0, 1};
                        newNormalList.add(senNewNormalList.get(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("用能统计表")){
                                        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 == senUnit[0] || j == senUnit[1]){
                                            if (j == senUnit[0] || j == senUnit[1]){
                                                if (!EditWordZZUtil.isChinese(senSubscript.get(0).get(k -1)[flag])){
                                                    XWPFTableCell cell = cells.get(j);
                                                    XWPFParagraph paragraph = cell.addParagraph();
                                                    XWPFRun run1 = paragraph.createRun();
                                                    run1.setText(senSubscript.get(0).get(k -1)[flag++],0);
                                                    cell.removeParagraph(0);
                                                    XWPFRun run2 = paragraph.createRun();
                                                    if (flag < 3) {
                                                        run2.setText(senSubscript.get(0).get(k -1)[flag++],0);
                                                        run2.setSubscript(VerticalAlign.SUBSCRIPT);
                                                    } else {
                                                        run2.setText(senSubscript.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);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            map.replace("tableName1","单耗计算表");
                        }
                        map.replace("tableName1","用能统计表");
                        //综合能耗计算表最后一页添加行
                        if (map.get("tableName1").equals("用能统计表")){
                            XWPFTable xwpfTable = xwpfTables.get(0);
                            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);
                            }
                        }

                    }else {
                        List<XWPFParagraph> paragraphs = document.getParagraphs();
                        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("tablex")){
                                        XWPFParagraph paragraph1 = paragraphs.get(paragraphs.size() - 1);
                                        paragraph1.removeRun(0);
                                        paragraph1.createRun().setText((String) map.get("tableName1"));
                                        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("用能统计表")){
                                        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);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    EditWordZZUtil.readWriteWord(document, map);
                    XWPFRun run = document.createParagraph().createRun();
                    run.setText("（以下空白）");
//                    run.addBreak();
                    XWPFRun run1 = document.createParagraph().createRun();
                    run1.setText(FIRST_LINE);
                    run1.setBold(true);
//            run1.addBreak();
                    XWPFParagraph paragraph1 = document.createParagraph();
                    XWPFRun run2 = paragraph1.createRun();
                    paragraph1.setAlignment(ParagraphAlignment.RIGHT);
                    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", "tables.docx");
                    XWPFDocument xwpfDocument = new XWPFDocument(OPCPackage.open(inputStream));
                    EditWordZZUtil.setUnderLine(xwpfDocument,map,1);
                    if (i < totalFlag){
                        List<XWPFParagraph> paragraphs = xwpfDocument.getParagraphs();
                        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("tablex")){
                                        XWPFParagraph paragraph1 = paragraphs.get(paragraphs.size() - 1);
                                        paragraph1.removeRun(0);
                                        paragraph1.createRun().setText((String) map.get("tableName1"));
                                        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 );
                                            }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);
                                    }
                                }
                            }
                        }
                    }else if (i == totalFlag){
                        //先填充第一个表格
                        List<XWPFParagraph> paragraphs = xwpfDocument.getParagraphs();
                        a: 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("tablex")){
                                        XWPFParagraph paragraph1 = paragraphs.get(paragraphs.size() - 1);
                                        paragraph1.removeRun(0);
                                        paragraph1.createRun().setText((String) map.get("tableName1"));
                                        XWPFTable table = xwpfDocument.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 );
                                                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);
                                        XWPFParagraph paragraph3 = xwpfDocument.createParagraph();
                                        XWPFRun xwpfRun = paragraph3.createRun();
                                        xwpfRun.addBreak();
                                        XWPFRun run = paragraph3.createRun();
                                        run.setText("单耗计算表");
                                        run.setBold(true);
                                        XWPFTable table1 = xwpfDocument.createTable();
                                        CTTblWidth ctTblWidth1 = table1.getCTTbl().addNewTblPr().addNewTblW();
                                        ctTblWidth1.setType(STTblWidth.PCT);
                                        ctTblWidth1.setW(BigInteger.valueOf(4960));
                                        //获取表头信息,处理表头信息
                                        String[] strings1 = senTableList.get(0).get(0);
                                        for (int k = 0; k < strings1.length; k++) {
                                            if (k == 0){
                                                XWPFTableCell cell = table1.getRow(0).getCell(0);
                                                XWPFRun run1 = cell.addParagraph().createRun();
                                                run1.setText(strings1[j]);
                                                run1.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 = table1.getRow(0).addNewTableCell();
                                                XWPFRun run1 = cell.addParagraph().createRun();
                                                run1.setText(strings1[k]);
                                                run1.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));
                                                }
                                            }
                                        }
                                        //处理表格数据信息
//                                        senTableList.get(0).remove(0);
                                        break a;
                                    }
                                }
                            }
                        }
                        //处理表格数据信息
                        int[] count = {0, 1};
                        newNormalList.add(senNewNormalList.get(0));
                        EditWordZZUtil.readWriteWord(xwpfDocument, map, count, newNormalList);
                        List<XWPFTable> xwpfTables = xwpfDocument.getTables();
                        int flag = 0;
                        b: 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("用能统计表")){
                                        if (j == listUnit[0]){
                                            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 == senUnit[0] || j == senUnit[1]){
//                                            flag = 0;
                                            XWPFTableCell cell = cells.get(j);
                                            XWPFParagraph paragraph = cell.addParagraph();
                                            XWPFRun run1 = paragraph.createRun();
                                            run1.setText(senNewUnitList.get(0).get(k -1)[flag++],0);
                                            cell.removeParagraph(0);
                                            XWPFRun run2 = paragraph.createRun();
                                            if (flag < 3){
                                                run2.setText(senNewUnitList.get(0).get(k -1)[flag++],0);
                                                run2.setSubscript(VerticalAlign.SUBSCRIPT);
                                            }else {
                                                run2.setText(senNewUnitList.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);
                                        }
                                    }
//                                    map.replace("tableName1","单耗计算表");
//                                    continue b;
                                }
                            }
                            map.replace("tableName1","单耗计算表");
                        }
                        //综合能耗计算表最后一页添加行
                        map.replace("tableName1","用能统计表");
                        if (map.get("tableName1").equals("用能统计表")){
                            XWPFTable xwpfTable = xwpfTables.get(0);
                            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);
                            }
                        }
                        map.replace("tableName1", "用能情况表");
                    }else {
                        map.replace("tableName1", "单耗计算表");
                        List<XWPFParagraph> paragraphs = xwpfDocument.getParagraphs();
                        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("tablex")){
                                        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 = senTableList.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[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 );
                                            }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));
                                                }
                                            }
                                        }
                                        int[] count = {0};
                                        EditWordZZUtil.readWriteWord(xwpfDocument, map, count, senNewNormalList);
                                    }
                                }
                            }
                        }
                        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 (j == senUnit[0] || j == senUnit[1]){
                                        XWPFTableCell cell = cells.get(j);
                                        XWPFParagraph paragraph = cell.addParagraph();
                                        XWPFRun run1 = paragraph.createRun();
                                        run1.setText(senNewUnitList.get(0).get(k -1)[flag++],0);
                                        cell.removeParagraph(0);
                                        XWPFRun run2 = paragraph.createRun();
                                        if (flag < 3){
                                            run2.setText(senNewUnitList.get(0).get(k -1)[flag++],0);
                                            run2.setSubscript(VerticalAlign.SUBSCRIPT);
                                        }else {
                                            run2.setText(senNewUnitList.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);
                                    }
                                }
                            }
                        }
                    }
                    EditWordZZUtil.readWriteWord(xwpfDocument,map);
                    XWPFRun run = xwpfDocument.createParagraph().createRun();
                    run.setText("（以下空白）");
//                    run.addBreak();
//                    EditWordZZUtil.readWriteWord(document,map);
                    XWPFRun run1 = xwpfDocument.createParagraph().createRun();
                    run1.setText(FIRST_LINE);
                    run1.setBold(true);
//            run1.addBreak();
                    XWPFParagraph paragraph1 = xwpfDocument.createParagraph();
                    XWPFRun run2 = paragraph1.createRun();
                    paragraph1.setAlignment(ParagraphAlignment.RIGHT);
                    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);
//        run5.addBreak();
                    xwpfDocument.removeBodyElement(7);
//                    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();
//                        }
//                    }

//                    XWPFRun run1 = xwpfDocument.createParagraph().createRun();


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

}
