package com.jeeplus.modules.sg.financial.settlement.util;

import com.jeeplus.modules.sg.financial.settlement.entity.CostCheck;
import com.jeeplus.modules.sg.financial.settlement.entity.ProjectBudget;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class MyExportUtil {

    private  XSSFCellStyle style = null;

    public static MyExportUtil getOne(){

        return new MyExportUtil();
    }

    /**
     * 传入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/"+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 exportExcelOne(HttpServletResponse response, XSSFWorkbook workbook, List<CostCheck> list){
        OutputStream outputStream = null;
        try {
            int rowNum = 2;
            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 = crateCellLeft(workbook,row,2); //施工费应付金额
                    Cell cell3 = crateCellLeft(workbook,row,3); //施工费实付金额
                    Cell cell4 = crateCellLeft(workbook,row,4); //施工费差额
                    Cell cell5 = crateCell(workbook,row,5); //施工费允许少付
                    Cell cell6 = crateCellLeft(workbook,row,6); //设计费应付金额
                    Cell cell7 = crateCellLeft(workbook,row,7); //设计费实付金额
                    Cell cell8 = crateCellLeft(workbook,row,8); //设计费差额
                    Cell cell9 = crateCell(workbook,row,9); //设计费允许少付
                    Cell cell10 = crateCellLeft(workbook,row,10); //监理费应付金额
                    Cell cell11 = crateCellLeft(workbook,row,11); //监理费实付金额
                    Cell cell12 = crateCellLeft(workbook,row,12); //监理费差额
                    Cell cell13 = crateCell(workbook,row,13); //监理费允许少付
                    CostCheck view = list.get(i);
                    cell0.setCellValue(getString(view.getProjectId()));
                    cell1.setCellValue(getString(view.getProjectName()));
                    cell2.setCellValue(getDouble(view.getConstructionAmountDue()));
                    cell3.setCellValue(getDouble(view.getConstructionAmountPaid()));
                    cell4.setCellValue(getDouble(view.getConstructionCostDifference()));
                    cell5.setCellValue(getString(view.getConstructionUnderPayment()));
                    cell6.setCellValue(getDouble(view.getDesignAmountDue()));
                    cell7.setCellValue(getDouble(view.getDesignAmountPaid()));
                    cell8.setCellValue(getDouble(view.getDesignCostDifference()));
                    cell9.setCellValue(getString(view.getDesignUnderPayment()));
                    cell10.setCellValue(getDouble(view.getSupervisorAmountDue()));
                    cell11.setCellValue(getDouble(view.getSupervisorAmountPaid()));
                    cell12.setCellValue(getDouble(view.getSupervisorDifference()));
                    cell13.setCellValue(getString(view.getSupervisorUnderPayment()));
                }
            }
            String str = URLEncoder.encode("费用检查.xlsx", "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();
        }
    }

    /**
     * 导出财务项目概预算信息管理
     * @param response
     * @param workbook
     * @param list
     */
    public void exportExcelTwo(HttpServletResponse response, XSSFWorkbook workbook, List<ProjectBudget> list){
        OutputStream outputStream = null;
        try {
            int rowNum = 3;
            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 = crateCellLeft(workbook,row,10); //建筑费
                    Cell cell11 = crateCellLeft(workbook,row,11); //安装费
                    Cell cell12 = crateCellLeft(workbook,row,12); //设备购置费
                    Cell cell13 = crateCellLeft(workbook,row,13); //主材费
                    Cell cell14 = crateCellLeft(workbook,row,14); //设计费
                    Cell cell15 = crateCellLeft(workbook,row,15); //监理费
                    Cell cell16 = crateCellLeft(workbook,row,16); //项目前期工作费
                    Cell cell17 = crateCellLeft(workbook,row,17); //线路施工赔偿费
                    Cell cell18 = crateCellLeft(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); //监理含税税率
                    ProjectBudget view = list.get(i);
                    cell0.setCellValue(getString(view.getProjectName()));
                    cell1.setCellValue(getString(view.getProjectId()));
                    cell2.setCellValue(getString(view.getDesignUnit()));
                    cell3.setCellValue(getString(view.getConstructionUnit()));
                    cell4.setCellValue(getString(view.getBuildingAddress()));
                    cell5.setCellValue(getString(view.getBuildingProperty()));
                    cell6.setCellValue(getString(view.getTotalInvestment()));
                    cell7.setCellValue(getDate(view.getStartTime()));
                    cell8.setCellValue(getDate(view.getStopTime()));
                    cell9.setCellValue(view.getApprovalNumber());
                    cell10.setCellValue(getDouble(view.getConstructionCost()));
                    cell11.setCellValue(getDouble(view.getInstallationCost()));
                    cell12.setCellValue(getDouble(view.getEquipmentCost()));
                    cell13.setCellValue(getDouble(view.getMaterialCost()));
                    cell14.setCellValue(getDouble(view.getDesignCost()));
                    cell15.setCellValue(getDouble(view.getSupervisorCost()));
                    cell16.setCellValue(getDouble(view.getPreliminaryWorkCost()));
                    cell17.setCellValue(getDouble(view.getCompensationCost()));
                    cell18.setCellValue(getDouble(view.getAdministrationCost()));
                    cell19.setCellValue(getDouble(view.getDesignContractDiscount()));
                    cell20.setCellValue(getDouble(view.getDesignTypicalDiscount()));
                    cell21.setCellValue(getDouble(view.getDesignTaxRate()));
                    cell22.setCellValue(getDouble(view.getSupervisorContractDiscount()));
                    cell23.setCellValue(getDouble(view.getSupervisorTaxRate()));
                }
            }
            String str = URLEncoder.encode("导出财务项目概预算信息.xlsx", "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();
        }
    }

    /**
     * 导出财务检查表
     * @param response
     * @param workbook
     * @param list
     */
    public void exportExcelThree(HttpServletResponse response, XSSFWorkbook workbook, List<ProjectBudget> list){
        OutputStream outputStream = null;
        try {
            int rowNum = 2;
            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 = crateCellLeft(workbook,row,6); //发文总投资（必填）
                    Cell cell7 = crateCell(workbook,row,7); //开工时间
                    ProjectBudget view = list.get(i);
                    cell0.setCellValue(getString(view.getProjectId()));
                    cell1.setCellValue(getString(view.getProjectName()));
                    cell2.setCellValue(getString(view.getProjectType()));
                    cell3.setCellValue(getString(view.getTotalInvestment()));
                    cell4.setCellValue(getString(view.getConstructionCostCheck()));
                    cell5.setCellValue(getString(view.getDesignCostCheck()));
                    cell6.setCellValue(getString(view.getDonorMaterialCheck()));
                    cell7.setCellValue(getString(view.getSupervisorCostCheck()));
                }
            }
            String str = URLEncoder.encode("财务检查管理.xlsx", "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
     * @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);
            style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        }
        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;
    }

    String getDate(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String returnDate = "";
        if (null != date) {
            returnDate = dateFormat.format(date);
        }
        return returnDate;
    }
}
