package com.jeeplus.modules.sg.raiseCapitalDesign.utils;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.sg.financial.erpcredit.entity.*;
import com.jeeplus.modules.sg.financial.erpcredit.util.ExportUtilCalculate;
import com.jeeplus.modules.sg.financial.settlement.entity.MaintainData;
import com.jeeplus.modules.sg.picking.activiti.entity.MdSmallClass;
import com.jeeplus.modules.sg.picking.activiti.entity.MdTheClass;
import com.jeeplus.modules.sg.picking.activiti.entity.PickList;
import com.jeeplus.modules.sg.picking.activiti.entity.ShowList;
import com.jeeplus.modules.sg.raiseCapital.entity.RaiseCapital;
import com.jeeplus.modules.sg.raiseCapitalDesign.entity.CableSectionData;
import com.jeeplus.modules.sg.raiseCapitalDesign.entity.CableSectionInfo;
import com.jeeplus.modules.sg.raiseCapitalDesign.entity.InventoryData;
import com.jeeplus.modules.sg.raiseCapitalDesign.entity.InventoryInfo;
import com.jeeplus.modules.sg.raiseCapitalDesign.entity.VoltageData;
import com.jeeplus.modules.sg.raiseCapitalDesign.entity.VoltageInfo;
import com.jeeplus.modules.sys.utils.DictUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 写入表格中模板
 */
public class RaiseCapitalDesignExportUtil {


    //模块表
    public static void getSheetModular(Sheet sheet, List<ShowList> myList, List<PickList> newList, RaiseCapital raiseCapital, Workbook workbook) {
        if (sheet != null) {
            CellStyle cellStyleFont = getCellStyleFont(workbook);
            CellStyle cellStyle = getCellStyle(workbook);
            XSSFRow row = (XSSFRow) sheet.getRow(0);
            XSSFCell cell = row.createCell(1);
            cell.setCellValue(raiseCapital.getProjectName() + "物料清册");//标题
            int i = 0;
            for (int j = 0; j < newList.size(); j++) {
                i = i + 1;
                PickList pickList = newList.get(j);
                row = (XSSFRow) sheet.getRow(i);
                cell = row.createCell(1);
                cell.setCellValue(pickList.getName());
                List<MdTheClass> mdTheClassList = pickList.getMdTheClassList();
                for (int k = 0; k < mdTheClassList.size(); k++) {
                    MdTheClass mdTheClass = mdTheClassList.get(k);
                    String name = mdTheClass.getName();
                    if (null != name && !"".equals(name)) {
                        i = i + 1;
                        row = (XSSFRow) sheet.getRow(i);
                        cell = row.createCell(1);
                        cell.setCellValue(name);
                    }
                    List<MdSmallClass> mdSmallClassList = mdTheClass.getMdSmallClassList();
                    int cellNum = 1;
                    for (int l = 0; l < mdSmallClassList.size(); l++) {
                        MdSmallClass mdSmallClass = mdSmallClassList.get(l);
                        for (ShowList s :
                                myList) {
                            if (s.getId().equals(mdSmallClass.getId())) {
                                mdSmallClass.setNumber(s.getNumber().toString());
                            }
                        }
                        //小类每四次换一次行
                        if (l % 4 == 0) {
                            i = i + 1;
                            cellNum = 1;
                        }
                        row = (XSSFRow) sheet.getRow(i);
                        cell = row.createCell(cellNum);
                        cell.setCellValue(mdSmallClass.getName());
                        cellNum += 1;
                        cell = row.createCell(cellNum);
                        cell.setCellValue(mdSmallClass.getNumber());
                        cellNum += 1;
                        cell = row.createCell(cellNum);
                        cell.setCellValue(mdSmallClass.getMdUnit());
                        cellNum += 1;
                    }
                }
            }
            System.out.println(newList.toArray());
        }

    }

    //模块清单
    public static void getSheetModuleList(Sheet sheet, List<ShowList> showLists, XSSFWorkbook workbook) {
        if (null != sheet) {
            CellStyle style = getCellStyleFont(workbook);
            CellStyle cellStyle = getCellStyle(workbook);
            XSSFRow row = (XSSFRow) sheet.createRow(1);
            XSSFCell cell = row.getCell(0);
            ShowList showList = null;
            for (int i = 0; i < showLists.size(); i++) {
                showList = showLists.get(i);
                row = (XSSFRow) sheet.createRow((int) i + 1);
                //线路第一行
                if (row.getCell(0) == null) {
                    cell = row.createCell(0);
                } else {
                    cell = row.getCell(0);
                }
                cell.setCellValue(showList.getMdName());//添加模块名称
                cell.setCellStyle(cellStyle);


                //线路第二行
                if (row.getCell(1) == null) {
                    cell = row.createCell(1);
                } else {
                    cell = row.getCell(1);
                }
                cell.setCellValue(showList.getBigClassification());//大分类
                cell.setCellStyle(cellStyle);

                //添加第3行
                if (row.createCell(2) == null) {
                    cell = row.createCell(2);
                } else {
                    cell = row.getCell(2);
                }
                cell.setCellValue(showList.getTheClassification());//中分类
                cell.setCellStyle(cellStyle);


                //添加第4行
                if (row.createCell(3) == null) {
                    cell = row.createCell(3);
                } else {
                    cell = row.getCell(3);
                }
                cell.setCellValue(showList.getSmallClassification());//小分类
                cell.setCellStyle(cellStyle);


                //第5行
                if (row.createCell(4) == null) {
                    cell = row.createCell(4);
                } else {
                    cell = row.getCell(4);
                }
                cell.setCellValue(showList.getMaterialCode());//物料编码
                cell.setCellStyle(cellStyle);


                //第6行
                if (row.createCell(5) == null) {
                    cell = row.createCell(5);
                } else {
                    cell = row.getCell(5);
                }
                cell.setCellValue(showList.getMaterialDescription());//物料描述
                cell.setCellStyle(cellStyle);


                //第7行
                if (row.createCell(6) == null) {
                    cell = row.createCell(6);
                } else {
                    cell = row.getCell(6);
                }
                cell.setCellValue(showList.getExtensionDescription());//扩展描述
                cell.setCellStyle(cellStyle);


                //第8行
                if (row.getCell(7) == null) {
                    cell = row.createCell(7);
                } else {
                    cell = row.getCell(7);
                }
                cell.setCellValue(showList.getMdUnits());//单位
                cell.setCellStyle(cellStyle);


                //添加第9行
                if (row.getCell(8) == null) {
                    cell = row.createCell(8);
                } else {
                    cell = row.getCell(8);
                }
                cell.setCellValue(showList.getMdTotalAmount());//数量
                cell.setCellStyle(cellStyle);

                //添加滴10行
                if (row.getCell(9) == null) {
                    cell = row.createCell(9);
                } else {
                    cell = row.getCell(9);
                }
                cell.setCellValue(showList.getMdSingleWeight());//单重
                cell.setCellStyle(cellStyle);


                //添加第11行
                if (row.getCell(10) == null) {
                    cell = row.createCell(10);
                } else {
                    cell = row.getCell(10);
                }
                cell.setCellValue(showList.getMdTotalWeight());//总重
                cell.setCellStyle(cellStyle);


                //添加第12行
                if (row.getCell(11) == null) {
                    cell = row.createCell(11);
                } else {
                    cell = row.getCell(11);
                }
                cell.setCellValue(showList.getMdTheParty());//甲乙供
                cell.setCellStyle(cellStyle);


                //添加第13行
                if (row.getCell(12) == null) {
                    cell = row.createCell(12);
                } else {
                    cell = row.getCell(12);
                }
                cell.setCellValue(showList.getMdText());
                cell.setCellStyle(cellStyle);


                //添加第14行
                if (row.getCell(13) == null) {
                    cell = row.createCell(13);
                } else {
                    cell = row.getCell(13);
                }
                cell.setCellValue(showList.getMdTotalAmount());
                cell.setCellStyle(cellStyle);

            }
        }
    }

    //领料清单
    public static void getSheetPickingList(Sheet sheet, List<ShowList> showLists, RaiseCapital raiseCapital, Workbook workbook) {
        if (sheet != null) {
            CellStyle style = getCellStyleFont(workbook);
            CellStyle cellStyle = getCellStyle(workbook);

            XSSFRow row = (XSSFRow) sheet.getRow(0);
            XSSFCell cell = row.getCell(0);
            cell.setCellValue(raiseCapital.getProjectName() + "物料清册");//标题

            row = (XSSFRow) sheet.getRow(1);
            cell = row.getCell(2);
            cell.setCellValue(raiseCapital.getProjectNo());//项目编号

            row = (XSSFRow) sheet.getRow(1);
            cell = row.getCell(6);
            cell.setCellValue(raiseCapital.getApprover());//批准人

            row = (XSSFRow) sheet.getRow(2);
            cell = row.getCell(2);
            cell.setCellValue(raiseCapital.getErpCode());//ERP编码

            row = (XSSFRow) sheet.getRow(2);
            cell = row.getCell(6);
            cell.setCellValue(raiseCapital.getReviewer());//审核人

            row = (XSSFRow) sheet.getRow(3);
            cell = row.getCell(2);
            cell.setCellValue(raiseCapital.getProjectName());//项目名称

            row = (XSSFRow) sheet.getRow(3);
            cell = row.getCell(6);
            cell.setCellValue(raiseCapital.getPreparedBy());//制单人

            row = (XSSFRow) sheet.getRow(4);
            cell = row.getCell(0);
            cell.setCellValue("甲方项目经理确认：" + raiseCapital.getManager());//甲方项目经理确认

            row = (XSSFRow) sheet.getRow(4);
            cell = row.getCell(3);
            cell.setCellValue("领用单位：" + raiseCapital.getReqUnit());//领用单位

            row = (XSSFRow) sheet.getRow(4);
            cell = row.getCell(6);
            cell.setCellValue(raiseCapital.getPreTime());//制单时间

            ShowList showList = null;
            for (int i = 0; i < showLists.size(); i++) {
                showList = showLists.get(i);
                row = (XSSFRow) sheet.createRow((int) i + 6);
                //第一列
                if (row.getCell(0) == null) {
                    cell = row.createCell(0);
                } else {
                    cell = row.getCell(0);
                }
                cell.setCellValue(i + 1);
                cell.setCellStyle(cellStyle);

                //第二列
                if (row.getCell(1) == null) {
                    cell = row.createCell(1);
                } else {
                    cell = row.getCell(1);
                }
                cell.setCellValue(showList.getMaterialCode());//物料编码
                cell.setCellStyle(cellStyle);

                //第三列
                if (row.getCell(2) == null) {
                    cell = row.createCell(2);
                } else {
                    cell = row.getCell(2);
                }
                cell.setCellValue(showList.getMaterialDescription());//物料名称
                cell.setCellStyle(cellStyle);


                //第四列
                if (row.getCell(3) == null) {
                    cell = row.createCell(3);
                } else {
                    cell = row.getCell(3);
                }
                cell.setCellValue(showList.getExtensionDescription());//扩展描述
                cell.setCellStyle(cellStyle);


                //第五列
                if (row.getCell(4) == null) {
                    cell = row.createCell(4);
                } else {
                    cell = row.getCell(4);
                }
                cell.setCellValue(showList.getMdUnits());//单位
                cell.setCellStyle(cellStyle);

                //第六列
                if (row.getCell(5) == null) {
                    cell = row.createCell(5);
                } else {
                    cell = row.getCell(5);
                }
                cell.setCellValue(showList.getMdTotalAmount());//总量
                cell.setCellStyle(cellStyle);

                //第七列
                if (row.getCell(6) == null) {
                    cell = row.createCell(6);
                } else {
                    cell = row.getCell(6);
                }
                cell.setCellValue(showList.getMdSingleWeight());//单重
                cell.setCellStyle(cellStyle);

                //第8列
                if (row.getCell(7) == null) {
                    cell = row.createCell(7);
                } else {
                    cell = row.getCell(7);
                }
                cell.setCellValue(showList.getMdSingleWeight());//总重
                cell.setCellStyle(cellStyle);

            }
        }
    }

    //架空导线计算表
    public static void getSheetVoltageList(Sheet sheet, VoltageData voltageData, RaiseCapital raiseCapital, Workbook workbook) {
        if (sheet != null) {
            CellStyle style = getCellStyleFont(workbook);
            CellStyle cellStyle = getCellStyle(workbook);

            XSSFRow row = (XSSFRow) sheet.getRow(0);
            XSSFCell cell = row.getCell(0);

            List<VoltageInfo> highVoltageList = voltageData.getHighVoltageList();
            List<VoltageInfo> lowVoltageList = voltageData.getLowVoltageList();

            for (VoltageInfo highVoltage : highVoltageList) {
                switch (highVoltage.getVoltageName()){
                    case "JKLYJ-10/240":
                        disposeRowData(2,sheet,highVoltage,cellStyle);
                        break;
                    case "JKLYJ-10/150":
                        disposeRowData(3,sheet,highVoltage,cellStyle);
                        break;
                    case "JKLYJ-10/50":
                        disposeRowData(4,sheet,highVoltage,cellStyle);
                        break;
                }

            }

            for (VoltageInfo lowVoltage : lowVoltageList) {
                switch (lowVoltage.getVoltageName()){
                    case "JKLYJ-120":
                        disposeRowData(6,sheet,lowVoltage,cellStyle);
                        break;
                    case "JKLYJ-70":
                        disposeRowData(7,sheet,lowVoltage,cellStyle);
                        break;
                    case "JKLGYJ-120/20":
                        disposeRowData(8,sheet,lowVoltage,cellStyle);
                        break;
                    case "JKYJ-120":
                        disposeRowData(9,sheet,lowVoltage,cellStyle);
                        break;
                    case "JKYJ-70":
                        disposeRowData(10,sheet,lowVoltage,cellStyle);
                        break;
                    case "JKYJ-35":
                        disposeRowData(11,sheet,lowVoltage,cellStyle);
                        break;
                    case "JKYJ-16":
                        disposeRowData(12,sheet,lowVoltage,cellStyle);
                        break;
                }

            }
        }
    }
    //拆旧材料清单计算表
    public static void getSheetInventoryList(Sheet sheet, InventoryData inventoryData, RaiseCapital raiseCapital, Workbook workbook) {
        if (sheet != null) {
            CellStyle style = getCellStyleFont(workbook);
            CellStyle cellStyle = getCellStyle(workbook);

            XSSFRow row = (XSSFRow) sheet.getRow(0);
            XSSFCell cell = row.getCell(0);

            List<InventoryInfo> inventoryList = inventoryData.getInventoryList();

            for (InventoryInfo inventoryInfo : inventoryList) {
                if (inventoryInfo.getName().equals("10kV电力电缆")){
                    switch (inventoryInfo.getSpecifications()){
                        case "YJV22-8.7/15-3&times;400":
                            disposeinventoryRowData(1,sheet,inventoryInfo,cellStyle);
                            break;
                        case "YJV22-8.7/15-3&times;300":
                            disposeinventoryRowData(2,sheet,inventoryInfo,cellStyle);
                            break;
                        case "YJV22-8.7/15-3&times;95":
                            disposeinventoryRowData(3,sheet,inventoryInfo,cellStyle);
                            break;
                        case "YJV22-8.7/15-3&times;35":
                            disposeinventoryRowData(4,sheet,inventoryInfo,cellStyle);
                            break;
                    }
                }
                if (inventoryInfo.getName().equals("10kV架空导线")){
                    switch (inventoryInfo.getSpecifications()){
                        case "JKLYJ-240":
                            disposeinventoryRowData(5,sheet,inventoryInfo,cellStyle);
                            break;
                        case "JKLYJ-185":
                            disposeinventoryRowData(6,sheet,inventoryInfo,cellStyle);
                            break;
                        case "JKLYJ-150":
                            disposeinventoryRowData(7,sheet,inventoryInfo,cellStyle);
                            break;
                        case "LGJ-240":
                            disposeinventoryRowData(8,sheet,inventoryInfo,cellStyle);
                            break;
                        case "LGJ-185":
                            disposeinventoryRowData(9,sheet,inventoryInfo,cellStyle);
                            break;
                        case "LGJ-150":
                            disposeinventoryRowData(10,sheet,inventoryInfo,cellStyle);
                            break;
                    }
                }
                if (inventoryInfo.getName().equals("10kV电杆")){
                    switch (inventoryInfo.getSpecifications()){
                        case "钢管杆":
                            disposeinventoryRowData(11,sheet,inventoryInfo,cellStyle);
                            break;
                        case "宅基塔":
                            disposeinventoryRowData(12,sheet,inventoryInfo,cellStyle);
                            break;
                        case "&Phi;190&times;15":
                            disposeinventoryRowData(13,sheet,inventoryInfo,cellStyle);
                            break;
                        case "&Phi;190&times;12":
                            disposeinventoryRowData(14,sheet,inventoryInfo,cellStyle);
                            break;
                    }
                }
                if (inventoryInfo.getName().equals("10kV电缆分支箱")){
                    switch (inventoryInfo.getSpecifications()){
                        case "1进1出":
                            disposeinventoryRowData(15,sheet,inventoryInfo,cellStyle);
                            break;
                        case "1进2出":
                            disposeinventoryRowData(16,sheet,inventoryInfo,cellStyle);
                            break;
                        case "1进3出":
                            disposeinventoryRowData(17,sheet,inventoryInfo,cellStyle);
                            break;
                    }
                }
                if (inventoryInfo.getName().equals("10kV电缆环网柜")){
                    switch (inventoryInfo.getSpecifications()){
                        case "2进2出":
                            disposeinventoryRowData(18,sheet,inventoryInfo,cellStyle);
                            break;
                        case "2进3出":
                            disposeinventoryRowData(19,sheet,inventoryInfo,cellStyle);
                            break;
                    }
                }
                if (inventoryInfo.getName().equals("10kV变压器")){
                    switch (inventoryInfo.getSpecifications()){
                        case "50kVA":
                            disposeinventoryRowData(20,sheet,inventoryInfo,cellStyle);
                            break;
                        case "315kVA":
                            disposeinventoryRowData(21,sheet,inventoryInfo,cellStyle);
                            break;
                    }
                }
                if (inventoryInfo.getName().equals("10kV柱上设备")){
                    switch (inventoryInfo.getSpecifications()){
                        case "断路器开关":
                            disposeinventoryRowData(22,sheet,inventoryInfo,cellStyle);
                            break;
                        case "SF6开关":
                            disposeinventoryRowData(23,sheet,inventoryInfo,cellStyle);
                            break;
                        case "熔断器":
                            disposeinventoryRowData(24,sheet,inventoryInfo,cellStyle);
                            break;
                    }
                }
                if (inventoryInfo.getName().equals("0.4kV电气设备")){
                    if (inventoryInfo.getSpecifications().equals("低压综合配电箱")){
                        switch (inventoryInfo.getCompany()){
                            case "套":
                                disposeinventoryRowData(25,sheet,inventoryInfo,cellStyle);
                                break;
                            case "面":
                                disposeinventoryRowData(26,sheet,inventoryInfo,cellStyle);
                                break;
                        }
                    }
                }
                if (inventoryInfo.getName().equals("0.4kV电缆")){
                    disposeinventoryRowData(27,sheet,inventoryInfo,cellStyle);
                }
                if (inventoryInfo.getName().equals("铁附件")){
                    disposeinventoryRowData(28,sheet,inventoryInfo,cellStyle);
                }
                if (inventoryInfo.getName().equals("10kV箱变")){
                    switch (inventoryInfo.getNum()){
                        case "11":
                            disposeinventoryRowData(29,sheet,inventoryInfo,cellStyle);
                            break;
                        case "12":
                            disposeinventoryRowData(30,sheet,inventoryInfo,cellStyle);
                            break;
                    }
                }

            }
        }
    }

    /**
     * 架空导线表数据处理
     * @param rowLineNumber
     * @param sheet
     * @param voltageInfo
     * @param cellStyle
     */
    private static void disposeRowData(Integer rowLineNumber, Sheet sheet, VoltageInfo voltageInfo, CellStyle cellStyle){

        XSSFRow row = (XSSFRow) sheet.getRow(rowLineNumber);
        XSSFCell cell = row.getCell(1);
        //第二列
        if (row.getCell(1) == null) {
            cell = row.getCell(1);
        } else {
            cell = row.getCell(1);
        }
        cell.setCellValue(voltageInfo.getSingleCircuitPath());//单回路路径

        //第三列
        if (row.getCell(2) == null) {
            cell = row.getCell(2);
        } else {
            cell = row.getCell(2);
        }
        cell.setCellValue(voltageInfo.getDoubleCircuitPath());//物料名称

        //第四列
        if (row.getCell(3) == null) {
            cell = row.getCell(3);
        } else {
            cell = row.getCell(3);
        }
        if(StringUtils.isNotBlank(voltageInfo.getSingleLong())){
            cell.setCellValue(voltageInfo.getSingleLong());//扩展描述
        }else{
            cell.setCellValue("0");//扩展描述
        }

    }

    private static void disposeinventoryRowData(Integer rowLineNumber, Sheet sheet, InventoryInfo inventoryInfo, CellStyle cellStyle){

        XSSFRow row = (XSSFRow) sheet.getRow(rowLineNumber);
        XSSFCell cell = row.getCell(1);
        //第二列
        if (row.getCell(4) == null) {
            cell = row.getCell(4);
        } else {
            cell = row.getCell(4);
        }
        cell.setCellValue(inventoryInfo.getQuantity());//数量

        //第三列
        if (row.getCell(5) == null) {
            cell = row.getCell(5);
        } else {
            cell = row.getCell(5);
        }
        if(StringUtils.isNotBlank(inventoryInfo.getRemarks())){
            cell.setCellValue(inventoryInfo.getRemarks());//备注
        }else{
            cell.setCellValue("");//备注
        }


    }


    /**
     * 电缆分段表
     * @param sheet
     * @param cableSectionData
     * @param raiseCapital
     * @param workbook
     */
    public static void getSheetCableList(Sheet sheet, CableSectionData cableSectionData, RaiseCapital raiseCapital, Workbook workbook) {
        if (sheet != null) {
            CellStyle cellStyle = getNewCellStyleFont(workbook);
            CellStyle cellStyleNoAlignment = getNewCellStyleFontNoAlignment(workbook);//不居中
            CellStyle cellStyleNoBoldweight = getNewCellStyleFontNoBoldweight(workbook);//不加粗

            XSSFRow row = (XSSFRow) sheet.getRow(0);
            XSSFCell cell = row.getCell(0);

            List<CableSectionInfo> cableFourHundredList = cableSectionData.getCableFourHundredList();    //YJV22-3×400mm²
            List<CableSectionInfo> cableThreeHundredList = cableSectionData.getCableThreeHundredList();   //YJV22-3×300mm²
            List<CableSectionInfo> cableTwoHundredFortyList = cableSectionData.getCableTwoHundredFortyList();    //YJV22-3×240mm²
            List<CableSectionInfo> cableHundredAndTwentyList = cableSectionData.getCableHundredAndTwentyList();   //YJV22-3×120mm²
            List<CableSectionInfo> cableSeventyList = cableSectionData.getCableSeventyList();       //YJV22-3×70mm²
            //将五个list的长度进行排序
            int[] a=new int[]{cableFourHundredList.size(),cableThreeHundredList.size(),cableTwoHundredFortyList.size(),cableHundredAndTwentyList.size(),cableSeventyList.size()};
            //将数组按小到大的顺序排列，最后的一个为最大值
            Arrays.sort(a);
            //获取最大的一个list长度，作为最终的行号
            int num=a[a.length-1];

            //获取电缆分段的系数
            String cableFactor = DictUtils.getDictLabel("cableFactor", "designCalculateCoefficient", "");

            for (int i = 1; i <= num; i++) {
                row = (XSSFRow) sheet.createRow((int) i + 1);
                CableSectionInfo cableFourHundred = null;   //3×400mm²
                if(cableFourHundredList.size()>=i){
                    cableFourHundred = cableFourHundredList.get(i-1);
                    //对数据进行处理
                    disposeCableRowData(row,0,cableFourHundred,cellStyle,cellStyleNoBoldweight);
                }else{
                    disposeCableRowNoData(row,0,cableFactor,cellStyle,cellStyleNoBoldweight);
                }
                CableSectionInfo cableThreeHundred = null;  //3×300mm²
                if(cableThreeHundredList.size()>=i){
                    cableThreeHundred = cableThreeHundredList.get(i-1);
                    disposeCableRowData(row,8,cableThreeHundred,cellStyle,cellStyleNoBoldweight);
                }else{
                    disposeCableRowNoData(row,8,cableFactor,cellStyle,cellStyleNoBoldweight);
                }
                CableSectionInfo cableTwoHundredForty = null;   //3×240mm²
                if(cableTwoHundredFortyList.size()>=i){
                    cableTwoHundredForty = cableTwoHundredFortyList.get(i-1);
                    disposeCableRowData(row,16,cableTwoHundredForty,cellStyle,cellStyleNoBoldweight);
                }else{
                    disposeCableRowNoData(row,16,cableFactor,cellStyle,cellStyleNoBoldweight);
                }
                CableSectionInfo cableHundredAndTwenty = null;  //3×120mm²
                if(cableHundredAndTwentyList.size()>=i){
                    cableHundredAndTwenty = cableHundredAndTwentyList.get(i-1);
                    disposeCableRowData(row,24,cableHundredAndTwenty,cellStyle,cellStyleNoBoldweight);
                }else{
                    disposeCableRowNoData(row,24,cableFactor,cellStyle,cellStyleNoBoldweight);
                }
                CableSectionInfo cableSeventy = null;       //3×70mm²
                if(cableSeventyList.size()>=i){
                    cableSeventy = cableSeventyList.get(i-1);
                    disposeCableRowData(row,32,cableSeventy,cellStyle,cellStyleNoBoldweight);
                }else{
                    disposeCableRowNoData(row,32,cableFactor,cellStyle,cellStyleNoBoldweight);
                }
            }

            //处理总数行
            row = (XSSFRow) sheet.createRow(num + 2);
            disposeCableRowOverallLength(row,num + 2,0,cellStyle,cellStyleNoAlignment,sheet,cableSectionData.getCableFourHundredListTotal());
            disposeCableRowOverallLength(row,num + 2,8,cellStyle,cellStyleNoAlignment,sheet,cableSectionData.getCableThreeHundredListTotal());
            disposeCableRowOverallLength(row,num + 2,16,cellStyle,cellStyleNoAlignment,sheet,cableSectionData.getCableTwoHundredFortyListTotal());
            disposeCableRowOverallLength(row,num + 2,24,cellStyle,cellStyleNoAlignment,sheet,cableSectionData.getCableHundredAndTwentyListTotal());
            disposeCableRowOverallLength(row,num + 2,32,cellStyle,cellStyleNoAlignment,sheet,cableSectionData.getCableSeventyListTotal());
        }
    }

    /**
     *
     * @param row   行信息
     * @param lineNum   行数
     * @param columnNum 起始列
     * @param cellStyle 截至列
     * @param sheet
     */
    private static void disposeCableRowOverallLength(XSSFRow row, Integer lineNum, Integer columnNum, CellStyle cellStyle, CellStyle cellStyleNoAlignment, Sheet sheet,String cableTotal){
        XSSFCell cell = row.getCell(1);

        if (row.getCell(columnNum) == null) {
            cell = row.createCell(columnNum);
        } else {
            cell = row.getCell(columnNum);
        }
        cell.setCellValue("总长（km)：");
        cell.setCellStyle(cellStyleNoAlignment);

        //第二列
        if (row.getCell(columnNum+1) == null) {
            cell = row.createCell(columnNum+1);
        } else {
            cell = row.getCell(columnNum+1);
        }
        cell.setCellValue("");//空行
        cell.setCellStyle(cellStyleNoAlignment);

        //第三列
        if (row.getCell(columnNum+2) == null) {
            cell = row.createCell(columnNum+2);
        } else {
            cell = row.getCell(columnNum+2);
        }
        cell.setCellValue("");//空行
        cell.setCellStyle(cellStyleNoAlignment);

        //第四列
        if (row.getCell(columnNum+3) == null) {
            cell = row.createCell(columnNum+3);
        } else {
            cell = row.getCell(columnNum+3);
        }
        cell.setCellValue("");//空行
        cell.setCellStyle(cellStyleNoAlignment);

        //第五列
        if (row.getCell(columnNum+4) == null) {
            cell = row.createCell(columnNum+4);
        } else {
            cell = row.getCell(columnNum+4);
        }
        cell.setCellValue("");//空行
        cell.setCellStyle(cellStyleNoAlignment);

        //第六列
        if (row.getCell(columnNum+5) == null) {
            cell = row.createCell(columnNum+5);
        } else {
            cell = row.getCell(columnNum+5);
        }
        cell.setCellValue("");//系数
        cell.setCellStyle(cellStyleNoAlignment);


        if (row.getCell(columnNum+6) == null) {
            cell = row.createCell(columnNum+6);
        } else {
            cell = row.getCell(columnNum+6);
        }
        cell.setCellValue(cableTotal);//总长度
        cell.setCellStyle(cellStyle);

        // 合并日期占两行(4个参数，分别为起始行，结束行，起始列，结束列)
        // 行和列都是从0开始计数，且起始结束都会合并
        // 这里是合并excel中日期的两行为一行
        CellRangeAddress region = new CellRangeAddress(lineNum, lineNum, columnNum, columnNum + 5);
        sheet.addMergedRegion(region);
    }

    /**
     * 电缆分段表数据处理
     * @param row
     * @param columnNum
     * @param cableData
     * @param cellStyle
     */
    private static void disposeCableRowData(XSSFRow row, Integer columnNum, CableSectionInfo cableData, CellStyle cellStyle, CellStyle cellStyleNoBoldweight){

        XSSFCell cell = row.getCell(1);
        //第一列
        if (row.getCell(columnNum) == null) {
            cell = row.createCell(columnNum);
        } else {
            cell = row.getCell(columnNum);
        }
        if(cableData != null){
            cell.setCellValue(cableData.getDestination());//400起讫点
            cell.setCellStyle(cellStyleNoBoldweight);
        }

        //第二列
        if (row.getCell(columnNum+1) == null) {
            cell = row.createCell(columnNum+1);
        } else {
            cell = row.getCell(columnNum+1);
        }
        if(cableData != null){
            cell.setCellValue(cableData.getPathLength());//400路径长（m）
            cell.setCellStyle(cellStyleNoBoldweight);
        }

        //第三列
        if (row.getCell(columnNum+2) == null) {
            cell = row.createCell(columnNum+2);
        } else {
            cell = row.getCell(columnNum+2);
        }
        if(cableData != null){
            cell.setCellValue(cableData.getUpperRod());//400上杆（m）
            cell.setCellStyle(cellStyleNoBoldweight);
        }

        //第四列
        if (row.getCell(columnNum+3) == null) {
            cell = row.createCell(columnNum+3);
        } else {
            cell = row.getCell(columnNum+3);
        }
        if(cableData != null){
            cell.setCellValue(cableData.getArrival());//400进站（m）
            cell.setCellStyle(cellStyleNoBoldweight);
        }

        //第五列
        if (row.getCell(columnNum+4) == null) {
            cell = row.createCell(columnNum+4);
        } else {
            cell = row.getCell(columnNum+4);
        }
        if(cableData != null){
            cell.setCellValue(cableData.getIncomingEquipment());//400进设备（m）
            cell.setCellStyle(cellStyleNoBoldweight);
        }

        //第六列
        if (row.getCell(columnNum+5) == null) {
            cell = row.createCell(columnNum+5);
        } else {
            cell = row.getCell(columnNum+5);
        }
        if(cableData != null){
            cell.setCellValue(cableData.getCoefficient());//400系数
            cell.setCellStyle(cellStyle);
        }

        //第七列
        if (row.getCell(columnNum+6) == null) {
            cell = row.createCell(columnNum+6);
        } else {
            cell = row.getCell(columnNum+6);
        }
        if(cableData != null){
            cell.setCellValue(cableData.getElectricalChief());//400电气长（m）
            cell.setCellStyle(cellStyle);
        }

        if(null != columnNum && 32 != columnNum){
            //第8列
            if (row.getCell(columnNum+7) == null) {
                cell = row.createCell(columnNum+7);
            } else {
                cell = row.getCell(columnNum+7);
            }
            cell.setCellValue("");//空行
            cell.setCellStyle(cellStyle);
        }
    }

    /**
     * 电缆分段表数据处理(无数据行)
     * @param row
     * @param columnNum
     * @param coefficient   系数
     * @param cellStyle
     */
    private static void disposeCableRowNoData(XSSFRow row, Integer columnNum, String coefficient, CellStyle cellStyle, CellStyle cellStyleNoBoldweight){

        XSSFCell cell = row.getCell(1);
        //第一列
        if (row.getCell(columnNum) == null) {
            cell = row.createCell(columnNum);
        } else {
            cell = row.getCell(columnNum);
        }
        cell.setCellValue("");//空行
        cell.setCellStyle(cellStyleNoBoldweight);

        //第二列
        if (row.getCell(columnNum+1) == null) {
            cell = row.createCell(columnNum+1);
        } else {
            cell = row.getCell(columnNum+1);
        }
        cell.setCellValue("");//空行
        cell.setCellStyle(cellStyleNoBoldweight);

        //第三列
        if (row.getCell(columnNum+2) == null) {
            cell = row.createCell(columnNum+2);
        } else {
            cell = row.getCell(columnNum+2);
        }
        cell.setCellValue("");//空行
        cell.setCellStyle(cellStyleNoBoldweight);

        //第四列
        if (row.getCell(columnNum+3) == null) {
            cell = row.createCell(columnNum+3);
        } else {
            cell = row.getCell(columnNum+3);
        }
        cell.setCellValue("");//空行
        cell.setCellStyle(cellStyleNoBoldweight);

        //第五列
        if (row.getCell(columnNum+4) == null) {
            cell = row.createCell(columnNum+4);
        } else {
            cell = row.getCell(columnNum+4);
        }
        cell.setCellValue("");//空行
        cell.setCellStyle(cellStyleNoBoldweight);

        //第六列
        if (row.getCell(columnNum+5) == null) {
            cell = row.createCell(columnNum+5);
        } else {
            cell = row.getCell(columnNum+5);
        }
        cell.setCellValue(coefficient);//系数
        cell.setCellStyle(cellStyle);

        //第七列
        if (row.getCell(columnNum+6) == null) {
            cell = row.createCell(columnNum+6);
        } else {
            cell = row.getCell(columnNum+6);
        }
        cell.setCellValue("0");//电气长（m）
        cell.setCellStyle(cellStyle);

        if(null != columnNum && 32 != columnNum){
            //第8列
            if (row.getCell(columnNum+7) == null) {
                cell = row.createCell(columnNum+7);
            } else {
                cell = row.getCell(columnNum+7);
            }
            cell.setCellValue("");//空行
            cell.setCellStyle(cellStyle);
        }
    }


    //设置 字体边框
    public static CellStyle getCellStyleFont(Workbook workbook) {
        XSSFFont font = (XSSFFont) workbook.createFont();
        font.setFontHeightInPoints((short) 12);//设置字体大小
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
        style.setFont(font);
        return style;
    }

    //设置边框
    public static CellStyle getCellStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
        return style;
    }

    //设置边框
    public static CellStyle getCellStyleAlign(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
        return style;
    }

    //设置 字体边框
    public static CellStyle getNewCellStyleFont(Workbook workbook) {
        XSSFFont font = (XSSFFont) workbook.createFont();
        font.setFontHeightInPoints((short) 11);//设置字体大小
        font.setFontName("仿宋_GB2312");
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
        style.setFont(font);
        return style;
    }

    //设置 字体边框
    public static CellStyle getNewCellStyleFontNoAlignment(Workbook workbook) {
        XSSFFont font = (XSSFFont) workbook.createFont();
        font.setFontHeightInPoints((short) 11);//设置字体大小
        font.setFontName("仿宋_GB2312");
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示
        CellStyle style = workbook.createCellStyle();
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
        style.setFont(font);
        return style;
    }

    //设置 字体边框
    public static CellStyle getNewCellStyleFontNoBoldweight(Workbook workbook) {
        XSSFFont font = (XSSFFont) workbook.createFont();
        font.setFontHeightInPoints((short) 11);//设置字体大小
        font.setFontName("仿宋_GB2312");
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 居中
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
        style.setFont(font);
        return style;
    }

}
