package com.ruoyi.hv.service.impl;

import java.io.OutputStream;
import java.util.List;
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.RzgHighValuePatentInfo;
import com.ruoyi.hv.service.IRzgHighValuePatentInfoExcel;
import com.ruoyi.hv.vo.RzgClassifiedStatisticsInfoVo;
import com.ruoyi.report.service.impl.AbstractExcelExp;

@Service
public class RzgHighValuePatentInfoExcelImpl extends AbstractExcelExp implements IRzgHighValuePatentInfoExcel {

	@Override
	public void doExport(OutputStream out, RzgHighValuePatentInfo req, List<RzgHighValuePatentInfo> list) {

		// TODO Auto-generated method stub

		// 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;
			String title = "高价值统计-万人高价值专利统计";
			int maxWidth = minWidth;

			int dataHeight = 30;

			String year = req.getYear();

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

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

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

			int titleSize = 10;
//			if (req.getApplicantTypesList() != null) {
//				int size = req.getApplicantTypesList().size();
//				titleSize = titleSize + size;
//				if (size > 1) {
//					titleSize++;
//				}
//			}

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

			row++;
			Row titleRow1 = sheet.createRow(row);
			titleRow1.setHeightInPoints(dataHeight);
			Cell rank = titleRow1.createCell(column);
			rank.setCellStyle(styles.get("header"));
			rank.setCellValue("排序");
			CellRangeAddress rankRange = new CellRangeAddress(row, row + 1, column, column);
			sheet.addMergedRegion(rankRange);

			column++;
			Cell area = titleRow1.createCell(column);
			area.setCellStyle(styles.get("header"));
			area.setCellValue("地区");
			CellRangeAddress areaRange = new CellRangeAddress(row, row + 1, column, column);
			sheet.addMergedRegion(areaRange);

			column++;
			Cell title1 = titleRow1.createCell(column);
			title1.setCellStyle(styles.get("header"));
			title1.setCellValue("统计时期-" + year);
			CellRangeAddress titleRange = new CellRangeAddress(row, row, column, titleSize);
			sheet.addMergedRegion(titleRange);
			setRegionStyle(sheet, titleRange, styles.get("header"));

			row++;
			Row titleRow2 = sheet.createRow(row);
			titleRow2.setHeightInPoints(dataHeight / 2 * 3);
			column = 1;
			for (HighValue hv : HighValue.ALL.values()) {
				column++;
				Cell hvCell = titleRow2.createCell(column);
				hvCell.setCellStyle(styles.get("header2"));
				hvCell.setCellValue(hv.getTypeName().replace("的有效发明专利", ""));
			}

			column++;
			Cell totalCell = titleRow2.createCell(column);
			totalCell.setCellStyle(styles.get("header2"));
			totalCell.setCellValue("高价值发明专利有效量合计");

			column++;
			Cell popuCell = titleRow2.createCell(column);
			popuCell.setCellStyle(styles.get("header2"));
			popuCell.setCellValue(year + "年人口(万人)");

			column++;
			Cell preCell = titleRow2.createCell(column);
			preCell.setCellStyle(styles.get("header2"));
			preCell.setCellValue("每万人高价值发明专利拥有量(件/万人)");

			column++;
			Cell groupCell = titleRow2.createCell(column);
			groupCell.setCellStyle(styles.get("header2"));
			groupCell.setCellValue("同比增长");

			setRegionStyle(sheet, rankRange, styles.get("header"));
			setRegionStyle(sheet, areaRange, styles.get("header"));

			autoColumnWidthForChineseChar(sheet, 0, titleSize, minWidth, maxWidth);
			// 冻结
			sheet.createFreezePane(0, 3, 0, 3);
			for(int i=0;i<list.size();i++) {
				
				RzgHighValuePatentInfo vo=list.get(i);
				Row dataRow = sheet.createRow(3+i);
				dataRow.setHeightInPoints(dataHeight);
				
				column=0;
				Cell areaCell0 = dataRow.createCell(column);
				areaCell0.setCellStyle(styles.get("data"));
				if(vo.getRanking()!=null) {
					areaCell0.setCellValue(vo.getRanking());
				}else {
					areaCell0.setCellValue(0);
				}
				
				column++;
				Cell areaCell1 = dataRow.createCell(column);
				areaCell1.setCellStyle(styles.get("data"));
				if(StringUtils.isNotBlank(vo.getCityName())) {
					areaCell1.setCellValue(vo.getCityName());
				}else {
					areaCell1.setCellValue("");
				}
				
				column++;
				Cell cell1 = dataRow.createCell(column);
				cell1.setCellStyle(styles.get("data"));
				if(vo.getIndustryEmergIndustry()!=null) {
					cell1.setCellValue(vo.getIndustryEmergIndustry());
				}else {
					cell1.setCellValue(0);
				}
				
				
				column++;
				Cell cell2 = dataRow.createCell(column);
				cell2.setCellStyle(styles.get("data"));
				if(vo.getMaintenanceTenYear()!=null) {
					cell2.setCellValue(vo.getMaintenanceTenYear());
				}else {
					cell2.setCellValue(0);
				}
				
				column++;
				Cell cell3 = dataRow.createCell(column);
				cell3.setCellStyle(styles.get("data"));
				if(vo.getOverseasCongenerPatent()!=null) {
					cell3.setCellValue(vo.getOverseasCongenerPatent());
				}else {
					cell3.setCellValue(0);
				}
				
				column++;
				Cell cell4= dataRow.createCell(column);
				cell4.setCellStyle(styles.get("data"));
				if(vo.getPledgeRecord()!=null) {
					cell4.setCellValue(vo.getPledgeRecord());
				}else {
					cell4.setCellValue(0);
				}
				
				column++;
				Cell cell5= dataRow.createCell(column);
				cell5.setCellStyle(styles.get("data"));
				if(vo.getNationalScienceAward()!=null) {
					cell5.setCellValue(vo.getNationalScienceAward());
				}else {
					cell5.setCellValue(0);
				}
				
				column++;
				Cell cell6= dataRow.createCell(column);
				cell6.setCellStyle(styles.get("data"));
				if(vo.getEffectiveTotal()!=null) {
					cell6.setCellValue(vo.getEffectiveTotal());
				}else {
					cell6.setCellValue(0);
				}
				
				column++;
				Cell cell7= dataRow.createCell(column);
				cell7.setCellStyle(styles.get("data"));
				if(StringUtils.isNotBlank(vo.getYearPopulation())) {
					cell7.setCellValue(vo.getYearPopulation());
				}else {
					cell7.setCellValue("--");
				}
				column++;
				Cell cell8= dataRow.createCell(column);
				cell8.setCellStyle(styles.get("data"));
				if(StringUtils.isNotBlank(vo.getHvPatentThousands())) {
					cell8.setCellValue(vo.getHvPatentThousands());
				}else {
					cell8.setCellValue("--");
				}
				column++;
				Cell cell9= dataRow.createCell(column);
				cell9.setCellStyle(styles.get("data"));
				if(StringUtils.isNotBlank(vo.getGrowth())) {
					cell9.setCellValue(vo.getGrowth());
				}else {
					cell9.setCellValue("--");
				}
				
			}

		} finally {
			writeToOut(wb, out);
		}

	}

}
