package cn.com.countrygarden.bi.main.util;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class ExcelCommon {
	public static void downloadExcel(XSSFWorkbook wb, String name, HttpServletResponse response) throws IOException {
		DateFormat dFormat = new SimpleDateFormat("yyyy.MM.dd"); // HH表示24小时制；
		// dFormat.format(dto.getFollowTime())
		response.setContentType("application/vnd.ms-excel; charset=utf-8");
		String fileName = URLEncoder.encode(dFormat.format(new Date()) + name + ".xlsx", "UTF-8");
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
		OutputStream outputStream = null;
		outputStream = response.getOutputStream();
		wb.write(outputStream);
		outputStream.close();

	}

	public static void downloadInsuranceOrderExcel(XSSFWorkbook wb, String name, HttpServletResponse response) throws IOException {
		DateFormat dFormat = new SimpleDateFormat("yyyy.MM.dd"); // HH表示24小时制；
		// dFormat.format(dto.getFollowTime())
		response.setContentType("application/vnd.ms-excel; charset=utf-8");
		String fileName = URLEncoder.encode(name + ".xlsx", "UTF-8");
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
		OutputStream outputStream = null;
		outputStream = response.getOutputStream();
		wb.write(outputStream);
		outputStream.close();

	}

	/**
	 * 复制行
	 * 
	 * @param sheet
	 *            要复制的sheet页
	 * @param startRow
	 *            复制的开始行
	 * @param endRow
	 *            复制的结束行
	 * @param pPosition
	 *            复制后要放的目标位置
	 */
	public static void copyRows(XSSFSheet sheet, int startRow, int endRow, int pPosition) {
		int pStartRow = startRow - 1;

		int pEndRow = endRow - 1;

		int columnCount;
		int i;

		int j;

		if (pStartRow == -1 || pEndRow == -1) {

			return;

		}
		for (i = pStartRow; i <= pEndRow; i++) {

			XSSFRow sourceRow = sheet.getRow(i);

			columnCount = sourceRow.getLastCellNum();

			if (sourceRow != null) {

				XSSFRow newRow = sheet.createRow(pPosition

						- pStartRow + i);

				newRow.setHeight(sourceRow.getHeight());

				for (j = 0; j < columnCount; j++) {

					XSSFCell templateCell = sourceRow.getCell(j);

					if (templateCell != null) {

						XSSFCell newCell = newRow.createCell(j);

						copyCell(templateCell, newCell);

					}

				}

			}

		}

	}

	/**
	 * 复制单元格
	 * 
	 * @param srcCell
	 * @param distCell
	 */
	public static void copyCell(XSSFCell srcCell, XSSFCell distCell) {

		distCell.setCellStyle(srcCell.getCellStyle());

		if (srcCell.getCellComment() != null) {

			distCell.setCellComment(srcCell.getCellComment());

		}
	}

	public static boolean isRowNotEmpty(Row row) {
		for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
			XSSFCell cell = (XSSFCell) row.getCell(c);
			if (cell != null && cell.getCellType() != XSSFCell.CELL_TYPE_BLANK) {
				return true;
			}

		}
		return false;
	}

	public static String getDate(XSSFCell cell, String dataFormat) throws ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat(dataFormat);
		DecimalFormat df = new DecimalFormat("#");
		if (cell == null) {
			return "";
		}
		switch (cell.getCellType()) {
		case HSSFCell.CELL_TYPE_NUMERIC:
			if (HSSFDateUtil.isCellDateFormatted(cell)) {

				return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
			}
			return df.format(cell.getNumericCellValue());
		case HSSFCell.CELL_TYPE_STRING:
			String str = cell.getStringCellValue().replace("-", "/");
			return str;
		case HSSFCell.CELL_TYPE_FORMULA:
			return cell.getCellFormula();
		case HSSFCell.CELL_TYPE_BLANK:
			return "";

		}
		return "";

	}

	public static void main(String[] args) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String value = "2018-11-11";
		String str = null;
		try {
			str = sdf.format(sdf.parse(value));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(str);
	}
}
