package com.ruoyi.reportrzs.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.report.domain.RzlNationalRankingInfo;
import com.ruoyi.report.service.impl.AbstractExcelExp;
import com.ruoyi.reportrzs.domain.RzsNationalRankingInfo;
import com.ruoyi.reportrzs.service.IRzsNationalRankingInfoExcel;


@Service
public class RzsNationalRankingInfoExcelImpl extends AbstractExcelExp implements IRzsNationalRankingInfoExcel {

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

		// 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 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);

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

			Row headRow1 = sheet.createRow(1);
			headRow1.setHeight(headHeight);
			Cell titleCell1 = headRow1.createCell(0);
			titleCell1.setCellStyle(styles.get("header"));
			String headTitle = "统计时期";
			if (StringUtils.isNotBlank(req.getCountTimeStr())) {
				headTitle = headTitle + req.getCountTimeStr();
			}
			titleCell1.setCellValue(headTitle);

			// 合并单元格语法： 开始行、结束行、开始列、结束列
			// 对应代码：new CellRangeAddress(startRowIndex, rowIndex - 1, i, i);
			CellRangeAddress titleRange1 = new CellRangeAddress(headRow1.getRowNum(), headRow1.getRowNum(), 0, 2);
			sheet.addMergedRegion(titleRange1);
			setRegionStyle(sheet, titleRange1, styles.get("header"));

			Row headRow2 = sheet.createRow(2);
			headRow2.setHeight(headHeight);

			int column = 0;
			Cell cell0 = headRow2.createCell(column);
			cell0.setCellStyle(styles.get("header"));
			cell0.setCellValue("排名");

			column++;
			Cell cell1 = headRow2.createCell(column);
			cell1.setCellStyle(styles.get("header"));
			cell1.setCellValue("地区");

			column++;
			Cell cell2 = headRow2.createCell(column);
			cell2.setCellStyle(styles.get("header"));
			cell2.setCellValue("发明专利授权量");//20230712改为授权量
			
			
			
			// 冻结
			sheet.createFreezePane(0, 3, 0, 3);

			for (int i = 0; i < list.size(); i++) {
				RzlNationalRankingInfo vo = list.get(i);
				
				Row dataRow = sheet.createRow(3+i);
				dataRow.setHeightInPoints(dataHeight);
				
				column=0;
				Cell typeCell = dataRow.createCell(column);
				typeCell.setCellStyle(styles.get("data"));
				if(vo.getRanking()!=null) {
					typeCell.setCellValue(vo.getRanking());
				}else {
					typeCell.setCellValue(0);
				}
				
				
				column++;
				Cell area = dataRow.createCell(column);
				area.setCellStyle(styles.get("data"));
				if(StringUtils.isNotBlank(vo.getArea())) {
					area.setCellValue(vo.getArea());
				}else {
					area.setCellValue("");
				}

				column++;
				Cell inventionPatentsUmber = dataRow.createCell(column);
				inventionPatentsUmber.setCellStyle(styles.get("data"));
				if(vo.getInventionPatentsUmber()!=null) {
					inventionPatentsUmber.setCellValue(vo.getInventionPatentsUmber());
				}else {
					inventionPatentsUmber.setCellValue(0L);
				}
				
			}

			autoColumnWidthForChineseChar(sheet, 0, 3, minWidth);

		} finally {
			writeToOut(wb, out);
		}
	
	}

}
