package com.jeeplus.modules.sg.balancedlibrary.materialReportDetails.utils;

import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.modules.sg.balancedlibrary.materialReportDetails.entity.LikuDetail;
import com.jeeplus.modules.sg.balancedlibrary.materialReportDetails.entity.MaterialReportDetails;
import com.jeeplus.modules.sg.financial.settlement.entity.CostCheck;
import com.jeeplus.modules.sg.financial.settlement.entity.ProjectBudget;
import com.jeeplus.modules.sg.financial.settlement.util.MyExportUtil;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ExportUtil {
    private XSSFCellStyle style = null;

    private XSSFCellStyle redStyle = null;

    XSSFWorkbook workbook = null;

    public ExportUtil(){
        try {
            String path = this.getClass().getResource("/").getPath() + "/freemarker/excelmodel/materialReport.xlsx";
            File file = new File(path);
            InputStream inputStream = new FileInputStream(file);// 将excel文件转为输入流
            workbook = new XSSFWorkbook(inputStream);// 创建个workbook，
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 导出费用检查管理
     * @param response
     */
    public void exportDetails(HttpServletResponse response, List<MaterialReportDetails> reportList, List<LikuDetail> likuList){
        OutputStream outputStream = null;
        try {
            int rowNum = 2;
            //处理利库汇总情况
            Sheet sheet1 = workbook.getSheetAt(0);
            if(reportList!=null&&reportList.size()>0){
                for(int i=0;i<reportList.size();i++){
                    MaterialReportDetails report = reportList.get(i);
                    //当利库数量不为0时 标红
                    Double likuAmount = report.getNumberOfNiches();
                    int color = 0;
                    if(likuAmount>0) color=1;

                    Row row = sheet1.createRow(rowNum+i);
                    Cell cell0 = crateCell(workbook,row,0,color); //上报地区
                    Cell cell1 = crateCell(workbook,row,1,color); //储备项目名称
                    Cell cell2 = crateCell(workbook,row,2,color); //物料编码
                    Cell cell3 = crateCell(workbook,row,3,color); //物料名称
                    Cell cell4 = crateCell(workbook,row,4,color); //扩展编码
                    Cell cell5 = crateCell(workbook,row,5,color); //扩展描述
                    Cell cell6 = crateCell(workbook,row,6,color); //单位
                    Cell cell7 = crateCell(workbook,row,7,color); //总量
                    Cell cell8 = crateCell(workbook,row,8,color); //单重
                    Cell cell9 = crateCell(workbook,row,9,color); //总重
                    Cell cell10 = crateCell(workbook,row,10,color); //甲乙供
                    Cell cell11 = crateCell(workbook,row,11,color); //利库数量
                    Cell cell12 = crateCell(workbook,row,12,color); //申请数量


                    cell0.setCellValue(getString(report.getReportingArea()));
                    cell1.setCellValue(getString(report.getNameOfReserve()));
                    cell2.setCellValue(getString(report.getMaterialCode()));
                    cell3.setCellValue(getString(report.getMaterialName()));
                    cell4.setCellValue(getString(report.getExtendedCoding()));
                    cell5.setCellValue(getString(report.getExtendedDescription()));
                    cell6.setCellValue(getString(report.getUnit()));
                    cell7.setCellValue(getDouble(report.getTotal()));
                    cell8.setCellValue(getString(report.getUnitWeight()));
                    cell9.setCellValue(getString(report.getTotalWeight()));
                    cell10.setCellValue(getString(report.getSupplied()));
                    cell11.setCellValue(getDouble(report.getNumberOfNiches()));
                    cell12.setCellValue(getDouble(report.getNumberOfApplications()));
                }
            }
            //利库明细情况
            Sheet sheet2 = workbook.getSheetAt(1);
            if(likuList!=null&&likuList.size()>0){
                for(int i=0;i<likuList.size();i++){
                    LikuDetail liku = likuList.get(i);
                    int color = 0;

                    Row row = sheet2.createRow(rowNum+i);
                    Cell cell0 = crateCell(workbook,row,0,color); //上报地区
                    Cell cell1 = crateCell(workbook,row,1,color); //储备项目名称
                    Cell cell2 = crateCell(workbook,row,2,color); //物料编码
                    Cell cell3 = crateCell(workbook,row,3,color); //物料名称
                    Cell cell4 = crateCell(workbook,row,4,color); //扩展编码
                    Cell cell5 = crateCell(workbook,row,5,color); //扩展描述
                    Cell cell6 = crateCell(workbook,row,6,color); //单位
                    Cell cell7 = crateCell(workbook,row,7,color); //总量
                    Cell cell8 = crateCell(workbook,row,8,color); //单重
                    Cell cell9 = crateCell(workbook,row,9,color); //总重
                    Cell cell10 = crateCell(workbook,row,10,color); //甲乙供
                    Cell cell11 = crateCell(workbook,row,11,color); //利库数量
                    Cell cell12 = crateCell(workbook,row,12,color); //申请数量
                    Cell cell13 = crateCell(workbook,row,13,color); //利库类型
                    Cell cell14 = crateCell(workbook,row,14,color); //利库批次号
                    Cell cell15 = crateCell(workbook,row,15,color); //利库库存地
                    Cell cell16 = crateCell(workbook,row,16,color); //利库明细数量


                    cell0.setCellValue(getString(liku.getReportingArea()));
                    cell1.setCellValue(getString(liku.getNameOfReserve()));
                    cell2.setCellValue(getString(liku.getMaterialCode()));
                    cell3.setCellValue(getString(liku.getMaterialName()));
                    cell4.setCellValue(getString(liku.getExtendedCoding()));
                    cell5.setCellValue(getString(liku.getExtendedDescription()));
                    cell6.setCellValue(getString(liku.getUnit()));
                    cell7.setCellValue(getDouble(liku.getTotal()));
                    cell8.setCellValue(getString(liku.getUnitWeight()));
                    cell9.setCellValue(getString(liku.getTotalWeight()));
                    cell10.setCellValue(getString(liku.getSupplied()));
                    //当利库数量=利库明细数量 表明这条记录只有一个利库明细数据
                    if(liku.getNumberOfNiches().equals(liku.getNumberNiches())){
                        cell11.setCellValue(getDouble(liku.getNumberOfNiches()));
                        cell12.setCellValue(getDouble(liku.getNumberOfApplications()));
                    }
                    cell13.setCellValue(getString(liku.getComparisonType()));
                    cell14.setCellValue(getString(liku.getBatch()));
                    cell15.setCellValue(getString(liku.getWarehouse()));
                    //段长类要加第几段
                    if(liku.getSegBelongs() == null){
                        cell16.setCellValue(getDouble(liku.getNumberNiches()));
                    }else{
                        cell16.setCellValue(liku.getNumberNiches()+" 第"+liku.getSegBelongs()+"段");
                    }

                }
            }

            String fileName = "物资上报利库情况表" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            String str = URLEncoder.encode(fileName, "UTF8");
            response.setHeader("Content-Disposition", "attachment;filename="+str);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    /**
     * 创建Cell对象 同时设置style
     * @param workbook
     * @param row
     * @param num
     * @param color 设置颜色 1：红色
     * @return
     */
    Cell crateCell(XSSFWorkbook workbook,Row row, int num,int color){
        Cell cell = row.createCell(num);
        if(style == null){
            style = workbook.createCellStyle();
            style.setWrapText(true);
            style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
            style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        }
        if(redStyle == null){
            redStyle = workbook.createCellStyle();
            redStyle.setWrapText(true);
            redStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
            redStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
            XSSFFont font = workbook.createFont();
            font.setColor(Font.COLOR_RED);
            redStyle.setFont(font);
        }
        if(color==1){
            cell.setCellStyle(redStyle);
        }else{
            cell.setCellStyle(style);
        }
        return cell;
    }



    Cell crateCellLeft(XSSFWorkbook workbook,Row row, int num){
        Cell cell = row.createCell(num);
        if(style == null){
            style = workbook.createCellStyle();
            style.setWrapText(true);
        }
        cell.setCellStyle(style);
        return cell;
    }

    Double getDouble(Double num) {
        Double returnDouble = 0.0;
        if (null!=num) {
            returnDouble = num;
        }
        return returnDouble;
    }

    String getString (String obj) {
        String returnStr = "";
        if (null!=obj) {
            returnStr = obj;
        }
        return returnStr;
    }

}
