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

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 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.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.List;

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


    //模块表
    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 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;
    }

}
