package com.ruoyi.hv.service.impl;

import java.io.OutputStream;
import java.util.HashMap;
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.common.constant.HighValue;
import com.ruoyi.hv.domain.RzgHighValueIewStatisticsInfo;
import com.ruoyi.hv.service.IRzgHighValueIewStatisticsInfoExcel;
import com.ruoyi.hv.vo.RzgHighValueIewStatisticsInfoRep;
import com.ruoyi.hv.vo.RzgHighValueIewStatisticsInfoVo;
import com.ruoyi.report.service.impl.AbstractExcelExp;

@Service
public class RzgHighValueIewStatisticsInfoExcelImpl extends AbstractExcelExp
		implements IRzgHighValueIewStatisticsInfoExcel {

	@Override
	public void doExport(OutputStream out, RzgHighValueIewStatisticsInfo req, RzgHighValueIewStatisticsInfoRep rep) {

		// TODO Auto-generated method stub
		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;
//			int maxWidth = "获得国家科技奖或中国专利奖".getBytes().length * 256;
			int maxWidth =  "六个字的宽度".getBytes().length * 256;
			String title = "高价值统计-概况统计";

			int dataHeight = 30;

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

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

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

			int titleSize = 0;
//			if (req.getApplicantTypesList() != null) {
//				int size = req.getApplicantTypesList().size();
//				titleSize = titleSize + size;
//				if (size > 1) {
//					titleSize++;
//				}
//			}
			int yearSize = 0;
			if (rep.getYearList() != null) {
				yearSize = rep.getYearList().size();
			}
			int hvTypeSize = req.getHighValueTypes().split(",").length;

			titleSize = titleSize + (hvTypeSize + 1) * yearSize;

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

			Row titleRow1 = sheet.createRow(1);
			titleRow1.setHeight(headHeight);
			Cell areaCell1 = titleRow1.createCell(0);
			areaCell1.setCellStyle(styles.get("header"));
			areaCell1.setCellValue("地区");

			CellRangeAddress areaRegion = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum() + 2, 0, 0);
			sheet.addMergedRegion(areaRegion);
			setRegionStyle(sheet, areaRegion, styles.get("header"));

			Cell titleCell1 = titleRow1.createCell(1);
			titleCell1.setCellStyle(styles.get("header"));
			titleCell1.setCellValue("历年高价值发明专利数据");

			CellRangeAddress titleRegion = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum(), 1,
					titleSize);
			sheet.addMergedRegion(titleRegion);
			setRegionStyle(sheet, titleRegion, styles.get("header"));

			int column = 1;
			Row yearRow = sheet.createRow(2);
			yearRow.setHeight(headHeight);
			for (int i = 0; i < rep.getYearList().size(); i++) {
				String year = rep.getYearList().get(i);
				Cell yearCell = yearRow.createCell(column);
				yearCell.setCellStyle(styles.get("header"));
				yearCell.setCellValue(year);

				CellRangeAddress yearRegion = new CellRangeAddress(yearRow.getRowNum(), yearRow.getRowNum(), column,
						column + (hvTypeSize));
				sheet.addMergedRegion(yearRegion);
				setRegionStyle(sheet, yearRegion, styles.get("header"));
				column = column + (hvTypeSize + 1);
			}

			Row headerRow = sheet.createRow(3);
			headerRow.setHeight(headHeight);
			column = 0;
			String[] hvTypes = req.getHighValueTypes().split(",");
			for (int i = 0; i < rep.getYearList().size(); i++) {
				column++;
				Cell totalCell = headerRow.createCell(column);
				totalCell.setCellStyle(styles.get("header"));
				totalCell.setCellValue("合计");

				for (int j = 0; j < hvTypes.length; j++) {
					String hvid = hvTypes[j];
					HighValue hv = HighValue.valueOf(hvid);
					if (hv == null) {
						continue;
					}
					column++;
					Cell typeCell = headerRow.createCell(column);
					typeCell.setCellStyle(styles.get("header"));
					typeCell.setCellValue(hv.getTypeName().replace("的有效发明专利", ""));
				}
			}
			autoColumnWidthForChineseChar(sheet, 0, titleSize + 1, minWidth, maxWidth);

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

			if (rep.getDataList() != null && rep.getDataList().size() > 0) {
				for (int i = 0; i < rep.getDataList().size(); i++) {
					RzgHighValueIewStatisticsInfoVo vo = rep.getDataList().get(i);
					if (vo.getYearData() == null) {
						vo.setYearData(new HashMap<String, RzgHighValueIewStatisticsInfoVo.Item>());
					}

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

					column = 0;
					Cell area = dataRow.createCell(column);
					area.setCellStyle(styles.get("data"));
					if (StringUtils.isNotBlank(vo.getAreaName())) {
						area.setCellValue(vo.getAreaName());
					} else {
						area.setCellValue("");
					}
					for (int y = 0; y < rep.getYearList().size(); y++) {
						String year = rep.getYearList().get(y);
						if (!vo.getYearData().containsKey(year)) {
							vo.getYearData().put(year, new RzgHighValueIewStatisticsInfoVo.Item());
						}
						RzgHighValueIewStatisticsInfoVo.Item item = vo.getYearData().get(year);

						column++;
						Cell totalCell = dataRow.createCell(column);
						totalCell.setCellStyle(styles.get("data"));
						totalCell.setCellValue(item.getTotalCnt());

						for (int j = 0; j < hvTypes.length; j++) {
							String hvid = hvTypes[j];
							HighValue hv = HighValue.valueOf(hvid);
							if (hv == null) {
								continue;
							}
							long num = 0;
							if ("1".equals(hv.getTypeId())) {
								num = item.getSeis();
							} else if ("2".equals(hv.getTypeId())) {
								num = item.getKeep10Years();
							} else if ("3".equals(hv.getTypeId())) {
								num = item.getHaveOverseas();
							} else if ("4".equals(hv.getTypeId())) {
								num = item.getHighPledge();
							} else if ("5".equals(hv.getTypeId())) {
								num = item.getTechnologyAward();
							}

							column++;
							Cell typeCell = dataRow.createCell(column);
							typeCell.setCellStyle(styles.get("data"));
							typeCell.setCellValue(num);

						}

					}

				}
			}

		} finally {
			writeToOut(wb, out);
		}

	}

}
