package com.jeeplus.modules.sg.scheduleView.util;

import com.jeeplus.modules.sg.managementcenter.project.entity.Assignment;
import com.jeeplus.modules.sg.managementcenter.project.entity.BasicInformation;
import com.jeeplus.modules.sg.managementcenter.project.entity.DisclosurePlan;
import com.jeeplus.modules.sg.managementcenter.project.entity.Project;
import com.jeeplus.modules.sg.scheduleView.entity.DisView;
import com.jeeplus.modules.sg.scheduleView.entity.ReceiveView;
import com.jeeplus.modules.sg.scheduleView.entity.ScheduleView;
import org.apache.poi.ss.usermodel.Cell;
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.XSSFWorkbook;

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

public class ExportUtil {

    private  XSSFCellStyle style = null;

    public static ExportUtil getOne(){

        return new ExportUtil();
    }

    /**
     * 传入excel模板名称 获取workbook对象
     * @param fileName excel模板名称
     * @return
     */
    public  XSSFWorkbook getWorkbook(String fileName){
        InputStream inputStream = null;
        XSSFWorkbook workbook = null;
        try {
            String path = this.getClass().getResource("/").getPath() + "/freemarker/excelmodel/"+fileName+".xlsx";
            File file = new File(path);
            inputStream = new FileInputStream(file);// 将excel文件转为输入流
            workbook = new XSSFWorkbook(inputStream);// 创建个workbook，
        } catch (Exception e) {
            e.printStackTrace();
        }
        return workbook;
    }



    /**
     * 导出施工交底进度信息
     * @param response
     * @param workbook
     * @param list
     */
    public void exportDisclose(HttpServletResponse response, XSSFWorkbook workbook, List<ScheduleView> list){
        try {
            int rowNum = 1;
            Sheet sheet = workbook.getSheetAt(0);
            if(list!=null&&list.size()>0){
                for(int i=0;i<list.size();i++){
                    Row row = sheet.createRow(rowNum+i);
                    Cell cell0 = crateCell(workbook,row,0); //项目定义号
                    Cell cell1 = crateCell(workbook,row,1); //项目名称
                    Cell cell2 = crateCell(workbook,row,2); //储备项目名称
                    Cell cell3 = crateCell(workbook,row,3); //发起流程
                    Cell cell4 = crateCell(workbook,row,4); //项目经理交底内容
                    Cell cell5 = crateCell(workbook,row,5); //导入材料表和拆旧物资表
                    Cell cell6 = crateCell(workbook,row,6); //填写部分工程量信息
                    Cell cell7 = crateCell(workbook,row,7); //项目关闭
                    Cell cell8 = crateCell(workbook,row,8); //设计变更
                    Cell cell9 = crateCell(workbook,row,9); //政策处理
                    Cell cell10 = crateCell(workbook,row,10); //完成技术交底时间

                    ScheduleView view = list.get(i);
                    DisView disclose = view.getDisView();

                    cell0.setCellValue(view.getProjectId());
                    cell1.setCellValue(view.getProjectName());
                    cell2.setCellValue(view.getReserveProjectName());
                    if(disclose!=null){
                        cell3.setCellValue(disclose.getFullTimeStart());
                        cell4.setCellValue(disclose.getProjectManagerImport());
                        cell5.setCellValue(disclose.getUnitManagerJudge());
                        cell6.setCellValue(disclose.getProjectManagerJudge());
                        cell7.setCellValue(disclose.getProjectClosure());
                        cell8.setCellValue(disclose.getDesignChange());
                        cell9.setCellValue(disclose.getPolicyType());
                        cell10.setCellValue(disclose.getFinTime());
                    }
                }
            }
            String str = URLEncoder.encode("施工交底进度.xlsx", "UTF8");
            response.setHeader("Content-Disposition", "attachment;filename="+str);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            OutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出施工交底进度信息
     * @param response
     * @param workbook
     * @param list
     */
    public void exportRec(HttpServletResponse response, XSSFWorkbook workbook, List<ScheduleView> list){
        try {
            int rowNum = 1;
            Sheet sheet = workbook.getSheetAt(0);
            if(list!=null&&list.size()>0){
                for(int i=0;i<list.size();i++){
                    Row row = sheet.createRow(rowNum+i);
                    Cell cell0 = crateCell(workbook,row,0); //项目定义号
                    Cell cell1 = crateCell(workbook,row,1); //项目名称
                    Cell cell2 = crateCell(workbook,row,2); //储备项目名称
                    Cell cell3 = crateCell(workbook,row,3); //项目第几次领料
                    Cell cell4 = crateCell(workbook,row,4); //项目经理开领料单
                    Cell cell5 = crateCell(workbook,row,5); //经理填写项目模块
                    Cell cell6 = crateCell(workbook,row,6); //施工单位领导审核
                    Cell cell7 = crateCell(workbook,row,7); //施工单位物资专职审核
                    Cell cell8 = crateCell(workbook,row,8); //配网项目经理审核领料单
                    Cell cell9 = crateCell(workbook,row,9); //配网物资专职审核领料单
                    Cell cell10 = crateCell(workbook,row,10); //完成时间

                    ScheduleView view = list.get(i);
                    ReceiveView receive = view.getReceiveView();

                    cell0.setCellValue(view.getProjectId());
                    cell1.setCellValue(view.getProjectName());
                    cell2.setCellValue(view.getReserveProjectName());
                    if(receive!=null){
                        cell3.setCellValue(receive.getTheNumber());
                        cell4.setCellValue(receive.getOpenList());
                        cell5.setCellValue(receive.getMangagerFile());
                        cell6.setCellValue(receive.getMaLeaderReviewed());
                        cell7.setCellValue(receive.getConUnitReviewed());
                        cell8.setCellValue(receive.getConMangagerReviewed());
                        cell9.setCellValue(receive.getFullReviewed());
                        cell10.setCellValue(receive.getRecFinTime());
                    }
                }
            }
            String str = URLEncoder.encode("物资领料进度.xlsx", "UTF8");
            response.setHeader("Content-Disposition", "attachment;filename="+str);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            OutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




    /**
     * 创建Cell对象 同时设置style
     * @param workbook
     * @param row
     * @param num
     * @return
     */
    Cell crateCell(XSSFWorkbook workbook,Row row, int num){
        Cell cell = row.createCell(num);
        if(style == null){
            style =workbook.createCellStyle();
            style.setWrapText(true);
            style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        }
        cell.setCellStyle(style);
        return cell;
    }
}
