package com.ruoyi.madridmark.service.impl;

import java.io.OutputStream;
import java.util.Map;

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.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.springframework.stereotype.Service;

import com.ruoyi.madridmark.domain.CMadridmark;
import com.ruoyi.madridmark.service.ICMadridmarkExcel;
import com.ruoyi.madridmark.vo.CMadridmarkDTO;
import com.ruoyi.madridmark.vo.CMadridmarkVO;
import com.ruoyi.report.service.impl.AbstractExcelExp;


@Service
public class CMadridmarkExcelImpl extends AbstractExcelExp implements ICMadridmarkExcel {

	@Override
	public void exportReport(OutputStream out, CMadridmark req, CMadridmarkDTO dto) {

		SXSSFWorkbook wb = new SXSSFWorkbook(500);

		try {
			// response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
			// response.setCharacterEncoding("utf-8");
			short headHeight = 30 * 20;
			int minWidth = "四个字宽".getBytes().length * 256;
			String title = "马德里商标注册统计报表";

			int dataHeight = 30;

			SXSSFSheet sheet = wb.createSheet();
			wb.setSheetName(0, title);
			sheet.trackAllColumnsForAutoSizing();

			Map<String, XSSFCellStyle> styles = createStyles(wb);

			int column = 0;
			int row = 0;
			Row titleRow0 = sheet.createRow(row);
			titleRow0.setHeightInPoints(dataHeight);
			Cell titleCell = titleRow0.createCell(column);
			titleCell.setCellStyle(styles.get("title"));
			titleCell.setCellValue(title);

			int titleSize = 0;
			if (dto.getYears() != null && dto.getYears().size() > 0) {
				int size = dto.getYears().size();
				titleSize = titleSize + size;
			}

			// 合并单元格语法： 开始行、结束行、开始列、结束列
			// 对应代码：new CellRangeAddress(startRowIndex, rowIndex - 1, i, i);
			sheet.addMergedRegion(
					new CellRangeAddress(titleRow0.getRowNum(), titleRow0.getRowNum(), column, titleSize));

			row++;
			Row titleRow1 = sheet.createRow(row);
			titleRow1.setHeight(headHeight);
			String val = "地区";
			XSSFCellStyle header = styles.get("header");
			createCell(titleRow1, column, val, header);
			CellRangeAddress titleRowRange = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum() + 1, 0,
					0);
			sheet.addMergedRegion(titleRowRange);

			val = "统计时期";
			if (StringUtils.isNotBlank(req.getCountTimeStr())) {
				val = req.getCountTimeStr();
			}
			column++;
			createCell(titleRow1, column, val, header);
			CellRangeAddress titleRange1 = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum(), column,
					titleSize);
			sheet.addMergedRegion(titleRange1);

			row++;
			Row titleRow2 = sheet.createRow(row);
			titleRow2.setHeight(headHeight);
			column = 0;
			for (int i = 0; i < dto.getYears().size(); i++) {
				column++;
				createCell(titleRow2, column, dto.getYears().get(i), header);
			}

			setRegionStyle(sheet, titleRange1, styles.get("header"));
			setRegionStyle(sheet, titleRowRange, styles.get("header"));

			// 冻结
			sheet.createFreezePane(0, 3, 0, 3);

			XSSFCellStyle data = styles.get("data");

			for (int i = 0; i < dto.getList().size(); i++) {
				CMadridmarkVO vo = dto.getList().get(i);

				Row dataRow = sheet.createRow(3 + i);
				dataRow.setHeightInPoints(dataHeight);

				column = 0;
				createCell(dataRow, column, vo.getAreaName(), data);
				for (int y = 0; y < dto.getYears().size(); y++) {
					String year = dto.getYears().get(y);
					if (vo.getTotals() == null || !vo.getTotals().containsKey(year)) {
						vo.getTotals().put(year, 0L);
					}
					column++;
					createCell(dataRow, column, vo.getTotals().get(year), data);
				}

			}

			autoColumnWidthForChineseChar(sheet, 0, titleSize + 1, (minWidth / 2 * 3), (minWidth / 2 * 3));

		} finally {
			writeToOut(wb, out);
		}

	}

}
