package com.jeeplus.modules.sg.managementcenter.project.util;

import com.github.abel533.echarts.code.X;
import com.jeeplus.modules.sg.managementcenter.project.entity.*;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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 static ExportUtil util = null;

    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 exportBasic(HttpServletResponse response, XSSFWorkbook workbook, List<Project> list){
        try {
            int index = 1;//第一列序号
            int rowNum = 6;//数据起始列
            Sheet sheet = workbook.getSheetAt(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); //网格编号
                Cell cell11 = crateCell(workbook,row,11); //单元格编号
                Cell cell12 = crateCell(workbook,row,12); //项目储备编号
                Cell cell13 = crateCell(workbook,row,13); //电网储备编号
                Cell cell14 = crateCell(workbook,row,14); //ERP编号
                Cell cell15 = crateCell(workbook,row,15); //建设必要性及内容
                Cell cell16 = crateCell(workbook,row,16); //变电站名称
                Cell cell17 = crateCell(workbook,row,17); //线路名称
                Cell cell18 = crateCell(workbook,row,18); //项目类型
                Cell cell19 = crateCell(workbook,row,19); //是否迎峰度夏项目
                Cell cell20 = crateCell(workbook,row,20); //项目基本说明
                Cell cell21 = crateCell(workbook,row,21); //工区备注

                Project project = list.get(i);
                BasicInformation export = project.getBasicInformation();

                cell0.setCellValue(String.valueOf(index++));
                cell1.setCellValue(project.getReserveProjectName());
                cell2.setCellValue(project.getProjectName());
                cell3.setCellValue(project.getProjectId());
                if(export!=null){
                    cell4.setCellValue(export.getMunicipalCompany());
                    cell5.setCellValue(export.getCountyCompany());
                    cell6.setCellValue(export.getProjectNature());
                    cell7.setCellValue(export.getDemandUnit());
                    cell8.setCellValue(export.getOperationUnit());
                    cell9.setCellValue(export.getPowerSupplyArea());
                    cell10.setCellValue(export.getGridNumber());
                    cell11.setCellValue(export.getCellNumber());
                    cell12.setCellValue(export.getProjectReserveNo());
                    cell13.setCellValue(export.getGridProjectNo());
                    cell14.setCellValue(export.getErpNo());
                    cell15.setCellValue(export.getNecessityContent());
                    cell16.setCellValue(export.getSubStationName());
                    cell17.setCellValue(export.getLineName());
                    cell18.setCellValue(export.getProjectType());
                    cell19.setCellValue(export.getWhetherSummer());
                    cell20.setCellValue(export.getBasicDescription());
                    cell21.setCellValue(export.getWorkAreaRemarks());
                }
            }

            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 exportAss(HttpServletResponse response, XSSFWorkbook workbook, List<Project> list){
        try {
            int rowNum = 4;
            Sheet sheet = workbook.getSheetAt(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); //施工单位物资专职
                Cell cell11 = crateCell(workbook,row,11); //施工单位技经专职
                Cell cell12 = crateCell(workbook,row,12); //施工项目经理
                Cell cell13 = crateCell(workbook,row,13); //施工单位领导
                Cell cell14 = crateCell(workbook,row,14); //分包联系人
                Cell cell15 = crateCell(workbook,row,15); //设计人员
                Cell cell16 = crateCell(workbook,row,16); //设计单位领导
                Cell cell17 = crateCell(workbook,row,17); //业主项目经理（配网项目经理）
                Cell cell18 = crateCell(workbook,row,18); //配网工程专职
                Cell cell19 = crateCell(workbook,row,19); //配网物资专职
                Cell cell20 = crateCell(workbook,row,20); //配网系统维护专职
                Cell cell21 = crateCell(workbook,row,21); //配网政策处理
                Cell cell22 = crateCell(workbook,row,22); //项目管理中心领导
                Cell cell23 = crateCell(workbook,row,23); //项目管理中心技经专职
                Cell cell24 = crateCell(workbook,row,24); //经研所角色
                Cell cell25 = crateCell(workbook,row,25); //运行单位业主代表
                Cell cell26 = crateCell(workbook,row,26); //运行单位运检专职

                Project project = list.get(i);
                Assignment ass = project.getAssignment();

                cell0.setCellValue(project.getReserveProjectName());
                if(ass!=null){
                    cell1.setCellValue(ass.getDisclosureType());
                    cell2.setCellValue(ass.getProjectReleaseBatch());
                    cell3.setCellValue(ass.getDeliveryDate());
                    cell4.setCellValue(ass.getPackageNo());
                    cell5.setCellValue(ass.getRate());
                    cell6.setCellValue(ass.getAssignedUnit());
                    cell7.setCellValue(ass.getConstructionUnit());
                    cell8.setCellValue(ass.getConstructionSubcontractor());
                    cell9.setCellValue(ass.getBehalf());
                    cell10.setCellValue(ass.getConstructionFullTime());
                    cell11.setCellValue(ass.getConstructionTechnical());
                    cell12.setCellValue(ass.getConstructionManage());
                    cell13.setCellValue(ass.getConstructionLeader());
                    cell14.setCellValue(ass.getSubcontractingContact());
                    cell15.setCellValue(ass.getDesigner());
                    cell16.setCellValue(ass.getDesignLeader());
                    cell17.setCellValue(ass.getOwnerManager());
                    cell18.setCellValue(ass.getDistribution());
                    cell19.setCellValue(ass.getDistributionFullTime());
                    cell20.setCellValue(ass.getDistributionSystem());
                    cell21.setCellValue(ass.getDistributionPolicy());
                    cell22.setCellValue(ass.getCenterLeader());
                    cell23.setCellValue(ass.getCenterTechnical());
                    cell24.setCellValue(ass.getInstituteRole());
                    cell25.setCellValue(ass.getRepUnit());
                    cell26.setCellValue(ass.getInspectionUnit());
                }
            }

            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 exportPlan(HttpServletResponse response, XSSFWorkbook workbook, List<Project> list){
        try {
            int rowNum = 5;
            Sheet sheet = workbook.getSheetAt(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); //带电查勘联系人
                Cell cell11 = crateCell(workbook,row,11); //供电所查勘人
                Cell cell12 = crateCell(workbook,row,12); //交底计划日期

                Project project = list.get(i);
                DisclosurePlan plan = project.getDisclosurePlan();

                cell0.setCellValue(project.getReserveProjectName());
                if(plan!=null){
                    cell1.setCellValue(plan.getDisclosureType());
                    cell2.setCellValue(plan.getProjectType());
                    cell3.setCellValue(plan.getDisclosureMethod());
                    cell4.setCellValue(plan.getOperationUnit());
                    cell5.setCellValue(plan.getConstructionUnit());
                    cell6.setCellValue(plan.getPowerSupplyStation());
                    cell7.setCellValue(plan.getConstructionManage());
                    cell8.setCellValue(plan.getMaintenance());
                    cell9.setCellValue(plan.getAutomationContacts());
                    cell10.setCellValue(plan.getLiveSurveyContact());
                    cell11.setCellValue(plan.getInspector());
                    cell12.setCellValue(plan.getDisclosurePlanDate());
                }
            }

            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;
    }
}
