package com.jeeplus.modules.sg.managementcenter.reserveManagement.utils;

import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.ExportScheme;
import com.jeeplus.modules.sg.managementcenter.reserveManagement.entity.ReserveManagement;
import org.apache.commons.lang3.StringUtils;
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 javax.xml.crypto.Data;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ReserveExportUtil {

    private  XSSFCellStyle style = null;

    public static ReserveExportUtil getOne(){

        return new ReserveExportUtil();
    }

    /**
     * 传入excel模板名称 获取workbook对象
     * @param fileName excel模板名称
     * @return
     */
    public  XSSFWorkbook getWorkbook(String fileName){
        InputStream inputStream = null;
        XSSFWorkbook workbook = null;
        try {
            File file = new File(this.getClass().getResource("/").getPath() + "/freemarker/excelmodel/"+fileName+".xlsx");
            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<ReserveManagement> list){
        try {
            int index = 1;//第一列序号
            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); //是否完成物资清册编制
                Cell cell27 = crateCell(workbook,row,27); //是否完成图纸移交
                Cell cell28 = crateCell(workbook,row,28); //交底牵头部门
                Cell cell29 = crateCell(workbook,row,29); //交底参与部门
                Cell cell30 = crateCell(workbook,row,30); //是否完成交底
                Cell cell31 = crateCell(workbook,row,31); //项目管理中心人员
                Cell cell32 = crateCell(workbook,row,32); //设计院人员
                Cell cell33 = crateCell(workbook,row,33); //经研所人员
                Cell cell34 = crateCell(workbook,row,34); //区域中心人员
                Cell cell35 = crateCell(workbook,row,35); //不停电作业中心人员
                Cell cell36 = crateCell(workbook,row,36); //作业方式
                Cell cell37 = crateCell(workbook,row,37); //交底完成日期
                Cell cell38 = crateCell(workbook,row,38); //备注
                Cell cell39 = crateCell(workbook,row,39); //立项说明
                Cell cell40 = crateCell(workbook,row,40); //不具备立项条件说明
                Cell cell41 = crateCell(workbook,row,41); //方案变更说明
                Cell cell42 = crateCell(workbook,row,42); //政处难度初判
                Cell cell43 = crateCell(workbook,row,43); //是否有资本设备
                Cell cell44 = crateCell(workbook,row,44); //是否有变电站新出线
                Cell cell45 = crateCell(workbook,row,45); //是否已实施
                Cell cell46 = crateCell(workbook,row,46); //现场情况描述
                Cell cell47 = crateCell(workbook,row,47); //是否有实施必要性
                Cell cell48 = crateCell(workbook,row,48); //实施无必要描述
                Cell cell49 = crateCell(workbook,row,49); //是否存在前置条件
                Cell cell50 = crateCell(workbook,row,50); //前置条件描述
                Cell cell51= crateCell(workbook,row,51); //是否存在施工方案问题
                Cell cell52 = crateCell(workbook,row,52); //施工方案问题描述
                Cell cell53 = crateCell(workbook,row,53); //现场是否存在安全隐患
                Cell cell54 = crateCell(workbook,row,54); //安全隐患描述
                Cell cell55 = crateCell(workbook,row,55); //是否需要定制物资
                Cell cell56 = crateCell(workbook,row,56); //定制物资描述
                Cell cell57 = crateCell(workbook,row,57); //是否有拆旧物资
                Cell cell58 = crateCell(workbook,row,58); //作业方式
                Cell cell59 = crateCell(workbook,row,59); //停电问题描述
                Cell cell60 = crateCell(workbook,row,60); //是否有土建工作量
                Cell cell61 = crateCell(workbook,row,61); //是否有立杆工作量
                Cell cell62 = crateCell(workbook,row,62); //总工作量描述
                Cell cell63 = crateCell(workbook,row,63); //区域中心
                Cell cell64 = crateCell(workbook,row,64); //供电所辖区
                Cell cell65 = crateCell(workbook,row,65); //政处地点
                Cell cell66 = crateCell(workbook,row,66); //政处类型
                Cell cell67 = crateCell(workbook,row,67); //政处对象
                Cell cell68 = crateCell(workbook,row,68); //政处描述
                Cell cell69 = crateCell(workbook,row,69); //是否需规划报批
                Cell cell70 = crateCell(workbook,row,70); //是否需办理安评航评手续
                Cell cell71 = crateCell(workbook,row,71); //政处难点提醒

                ReserveManagement reserveManagement = list.get(i);

                cell0.setCellValue(String.valueOf(index++));
                cell1.setCellValue(reserveManagement.getProjectNature());
                cell2.setCellValue(reserveManagement.getDemandUnit());
                cell3.setCellValue(reserveManagement.getDistrict());
                cell4.setCellValue(reserveManagement.getProjectGroup());
                cell5.setCellValue(reserveManagement.getReserveBatches());
                cell6.setCellValue(reserveManagement.getBlockSupply());
                cell7.setCellValue(reserveManagement.getGridNumber());
                cell8.setCellValue(reserveManagement.getCellNumber());
                cell9.setCellValue(reserveManagement.getProjectReserveId());
                cell10.setCellValue(reserveManagement.getPowerGridProjectId());
                cell11.setCellValue(reserveManagement.getProjectName());
                cell12.setCellValue(reserveManagement.getConstructionNecessityAndContent());
                cell13.setCellValue(reserveManagement.getSubstationName());
                cell14.setCellValue(reserveManagement.getCircuitName());
                cell15.setCellValue(reserveManagement.getNetRackProjectDivide());
                cell16.setCellValue(reserveManagement.getProjectType());

                String costEstimateStr= reserveManagement.getCostEstimate().toString();
                if(StringUtils.isNotBlank(costEstimateStr)){
                    cell17.setCellValue(Double.parseDouble(costEstimateStr));
                }else{
                    cell37.setCellValue("");
                }
                cell18.setCellValue(reserveManagement.getWhetherWindwardProject());
                cell19.setCellValue(reserveManagement.getProjectUrgencyDegree());
                cell20.setCellValue(reserveManagement.getImplementIssuedYear());
                cell21.setCellValue(reserveManagement.getImplementBatch());
                cell22.setCellValue(reserveManagement.getBasicRemarks());
                cell23.setCellValue(reserveManagement.getDesignCondition());
                cell24.setCellValue(reserveManagement.getWhetherPassReview());
                cell25.setCellValue(reserveManagement.getWhetherNeedWiringDiagram());
                cell26.setCellValue(reserveManagement.getWhetherFinishMaterials());
                cell27.setCellValue(reserveManagement.getWhetherFinishDrawing());
                cell28.setCellValue(reserveManagement.getDisclosureToCoordinate());
                cell29.setCellValue(reserveManagement.getDisclosureParticipate());
                cell30.setCellValue(reserveManagement.getWhetherFinishDisclosure());
                cell31.setCellValue(reserveManagement.getProjectManagementPeople());
                cell32.setCellValue(reserveManagement.getDesigningInstitutePeople());
                cell33.setCellValue(reserveManagement.getIfoPeople());
                cell34.setCellValue(reserveManagement.getRegionalCenterPeople());
                cell35.setCellValue(reserveManagement.getUninterruptedOperationPeople());
                cell36.setCellValue(reserveManagement.getWorkMode());

                String dateStr = reserveManagement.getDisclosureFinishDateStr();
                if(StringUtils.isNotBlank(dateStr)){
                    cell37.setCellValue(dateStr);
                }else{
                    cell37.setCellValue("");
                }

                cell38.setCellValue(reserveManagement.getDisclosureRemarks());
                cell39.setCellValue(reserveManagement.getProjectDescription());
                cell40.setCellValue(reserveManagement.getDoNotPossessProjectDescription());
                cell41.setCellValue(reserveManagement.getChangedProgramsExplain());
                cell42.setCellValue(reserveManagement.getInitialDifficulty());
                cell43.setCellValue(reserveManagement.getCapitalEquipment());
                cell44.setCellValue(reserveManagement.getSubstationNewLine());
                cell45.setCellValue(reserveManagement.getWhetherImplement());
                cell46.setCellValue(reserveManagement.getFieldDescription());
                cell47.setCellValue(reserveManagement.getNecessaryOfImplementation());
                cell48.setCellValue(reserveManagement.getUnnecessaryDescription());
                cell49.setCellValue(reserveManagement.getPrecondition());
                cell50.setCellValue(reserveManagement.getConditionsDescribed());
                cell51.setCellValue(reserveManagement.getConstructionSchemeProblem());
                cell52.setCellValue(reserveManagement.getProblemDescriptionOfConstructionScheme());
                cell53.setCellValue(reserveManagement.getExistingSecurityThreat());
                cell54.setCellValue(reserveManagement.getSafetyHazardDescription());
                cell55.setCellValue(reserveManagement.getCustomizedMaterials());
                cell56.setCellValue(reserveManagement.getMaterialDescription());
                cell57.setCellValue(reserveManagement.getDismantlingTheOldMaterial());
                cell58.setCellValue(reserveManagement.getOperatingType());
                cell59.setCellValue(reserveManagement.getProblemDescription());
                cell60.setCellValue(reserveManagement.getCivilWork());
                cell61.setCellValue(reserveManagement.getVerticalRodWorkload());
                cell62.setCellValue(reserveManagement.getTotalWorkloadDescription());
                cell63.setCellValue(reserveManagement.getRegionalCenter());
                cell64.setCellValue(reserveManagement.getPowerSupplyArea());
                cell65.setCellValue(reserveManagement.getZhengLocation());
                cell66.setCellValue(reserveManagement.getZhengType());
                cell67.setCellValue(reserveManagement.getZhengObject());
                cell68.setCellValue(reserveManagement.getZhengDescribe());
                cell69.setCellValue(reserveManagement.getProjectApproval());
                cell70.setCellValue(reserveManagement.getCourseEvaluationProcedures());
                cell71.setCellValue(reserveManagement.getZhengDifficultyWarn());
            }

            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 exportJlModule(HttpServletResponse response, XSSFWorkbook workbook, List<ExportScheme> list){
        try {
            int index = 1;//第一列序号
            int rowNum = 4;//数据起始列
            Sheet sheet = workbook.getSheetAt(0);
            for(int i=0;i<list.size();i++){
                Row row = sheet.createRow(rowNum+i);
                Cell cell1 = crateCell(workbook,row,0); //项目储备编号
                Cell cell2 = crateCell(workbook,row,1); //项目名称
                Cell cell28 = crateCell(workbook,row,2); //交底牵头部门
                Cell cell29 = crateCell(workbook,row,3); //交底参与部门
                Cell cell30 = crateCell(workbook,row,4); //是否完成交底
                Cell cell31 = crateCell(workbook,row,5); //项目管理中心人员
                Cell cell32 = crateCell(workbook,row,6); //设计院人员
                Cell cell33 = crateCell(workbook,row,7); //经研所人员
                Cell cell34 = crateCell(workbook,row,8); //区域中心人员
                Cell cell35 = crateCell(workbook,row,9); //不停电作业中心人员
                Cell cell36 = crateCell(workbook,row,10); //作业方式
                Cell cell37 = crateCell(workbook,row,11); //交底完成日期
                Cell cell38 = crateCell(workbook,row,12); //备注
                Cell cell39 = crateCell(workbook,row,13); //立项说明
                Cell cell40 = crateCell(workbook,row,14); //不具备立项条件说明
                Cell cell41 = crateCell(workbook,row,15); //方案变更说明
                Cell cell42 = crateCell(workbook,row,16); //政处难度初判
                Cell cell43 = crateCell(workbook,row,17); //是否有资本设备
                Cell cell44 = crateCell(workbook,row,18); //是否有变电站新出线
                Cell cell45 = crateCell(workbook,row,19); //是否已实施
                Cell cell46 = crateCell(workbook,row,20); //现场情况描述
                Cell cell47 = crateCell(workbook,row,21); //是否有实施必要性
                Cell cell48 = crateCell(workbook,row,22); //实施无必要描述
                Cell cell49 = crateCell(workbook,row,23); //是否存在前置条件
                Cell cell50 = crateCell(workbook,row,24); //前置条件描述
                Cell cell51= crateCell(workbook,row,25); //是否存在施工方案问题
                Cell cell52 = crateCell(workbook,row,26); //施工方案问题描述
                Cell cell53 = crateCell(workbook,row,27); //现场是否存在安全隐患
                Cell cell54 = crateCell(workbook,row,28); //安全隐患描述
                Cell cell55 = crateCell(workbook,row,29); //是否需要定制物资
                Cell cell56 = crateCell(workbook,row,30); //定制物资描述
                Cell cell57 = crateCell(workbook,row,31); //是否有拆旧物资
                Cell cell58 = crateCell(workbook,row,32); //作业方式
                Cell cell59 = crateCell(workbook,row,33); //停电问题描述
                Cell cell60 = crateCell(workbook,row,34); //是否有土建工作量
                Cell cell61 = crateCell(workbook,row,35); //是否有立杆工作量
                Cell cell62 = crateCell(workbook,row,36); //总工作量描述
                Cell cell63 = crateCell(workbook,row,37); //区域中心
                Cell cell64 = crateCell(workbook,row,38); //供电所辖区
                Cell cell65 = crateCell(workbook,row,39); //政处地点
                Cell cell66 = crateCell(workbook,row,40); //政处类型
                Cell cell67 = crateCell(workbook,row,41); //政处对象
                Cell cell68 = crateCell(workbook,row,42); //政处描述
                Cell cell69 = crateCell(workbook,row,43); //是否需规划报批
                Cell cell70 = crateCell(workbook,row,44); //是否需办理安评航评手续
                Cell cell71 = crateCell(workbook,row,45); //政处难点提醒

                ExportScheme exportScheme = list.get(i);

                cell1.setCellValue(exportScheme.getProjectReserveId());
                cell2.setCellValue(exportScheme.getProjectName());
                cell28.setCellValue(exportScheme.getDisclosureToCoordinate());
                cell29.setCellValue(exportScheme.getDisclosureParticipate());
                cell30.setCellValue(exportScheme.getWhetherFinishDisclosure());
                cell31.setCellValue(exportScheme.getProjectManagementPeople());
                cell32.setCellValue(exportScheme.getDesigningInstitutePeople());
                cell33.setCellValue(exportScheme.getIfoPeople());
                cell34.setCellValue(exportScheme.getRegionalCenterPeople());
                cell35.setCellValue(exportScheme.getUninterruptedOperationPeople());
                cell36.setCellValue(exportScheme.getWorkMode());

                Date dateStr = exportScheme.getDisclosureFinishDate();
                if (null != dateStr) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    String format = simpleDateFormat.format(dateStr);
                    cell37.setCellValue(format);
                }else {
                    cell37.setCellValue("");
                }
                cell38.setCellValue(exportScheme.getDisclosureRemarks());
                cell39.setCellValue(exportScheme.getProjectDescription());
                cell40.setCellValue(exportScheme.getDoNotPossessProjectDescription());
                cell41.setCellValue(exportScheme.getChangedProgramsExplain());
                cell42.setCellValue(exportScheme.getInitialDifficulty());
                cell43.setCellValue(exportScheme.getCapitalEquipment());
                cell44.setCellValue(exportScheme.getSubstationNewLine());
                cell45.setCellValue(exportScheme.getWhetherImplement());
                cell46.setCellValue(exportScheme.getFieldDescription());
                cell47.setCellValue(exportScheme.getNecessaryOfImplementation());
                cell48.setCellValue(exportScheme.getUnnecessaryDescription());
                cell49.setCellValue(exportScheme.getPrecondition());
                cell50.setCellValue(exportScheme.getConditionsDescribed());
                cell51.setCellValue(exportScheme.getConstructionSchemeProblem());
                cell52.setCellValue(exportScheme.getProblemDescriptionOfConstructionScheme());
                cell53.setCellValue(exportScheme.getExistingSecurityThreat());
                cell54.setCellValue(exportScheme.getSafetyHazardDescription());
                cell55.setCellValue(exportScheme.getCustomizedMaterials());
                cell56.setCellValue(exportScheme.getMaterialDescription());
                cell57.setCellValue(exportScheme.getDismantlingTheOldMaterial());
                cell58.setCellValue(exportScheme.getOperatingType());
                cell59.setCellValue(exportScheme.getProblemDescription());
                cell60.setCellValue(exportScheme.getCivilWork());
                cell61.setCellValue(exportScheme.getVerticalRodWorkload());
                cell62.setCellValue(exportScheme.getTotalWorkloadDescription());
                cell63.setCellValue(exportScheme.getRegionalCenter());
                cell64.setCellValue(exportScheme.getPowerSupplyArea());
                cell65.setCellValue(exportScheme.getZhengLocation());
                cell66.setCellValue(exportScheme.getZhengType());
                cell67.setCellValue(exportScheme.getZhengObject());
                cell68.setCellValue(exportScheme.getZhengDescribe());
                cell69.setCellValue(exportScheme.getProjectApproval());
                cell70.setCellValue(exportScheme.getCourseEvaluationProcedures());
                cell71.setCellValue(exportScheme.getZhengDifficultyWarn());
            }

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