package com.ruoyi.report.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.RzlInventStatisticsInfo;
import com.ruoyi.report.service.IRzlInventStatisticsInfoExcel;
import com.ruoyi.report.vo.ViewStatisticsInfoVo;

@Service
public class RzlInventStatisticsInfoExcelImpl extends AbstractExcelExp implements IRzlInventStatisticsInfoExcel {

	@Override
	public void doExport(OutputStream out, RzlInventStatisticsInfo req, List<RzlInventStatisticsInfo> list) {
		// TODO Auto-generated method stub
		SXSSFWorkbook wb = new SXSSFWorkbook(500);

		try {
			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(), titleRow0.getRowNum(), 4));

			Row titleRow2 = sheet.createRow(1);
			titleRow2.setHeightInPoints(dataHeight);
			Cell titleCel2 = titleRow2.createCell(0);
			titleCel2.setCellStyle(styles.get("header"));
			String title2 = "统计时期";
			title2 = title2 + req.getYear();
			if (StringUtils.isNotBlank(req.getMonth())) {
				title2 = title2 + "-" + req.getMonth();
			}
			titleCel2.setCellValue(title2);
			CellRangeAddress titleRange2 = new CellRangeAddress(titleRow2.getRowNum(), titleRow2.getRowNum(), 0, 4);
			sheet.addMergedRegion(titleRange2);
			setRegionStyle(sheet, titleRange2, styles.get("header"));

			String year = req.getYear();
			Row headRow1 = sheet.createRow(2);
			headRow1.setHeight(headHeight);
			Cell areaCell = headRow1.createCell(0);
			areaCell.setCellStyle(styles.get("header"));
			areaCell.setCellValue("排名");

			Cell areaCel2 = headRow1.createCell(1);
			areaCel2.setCellStyle(styles.get("header"));
			areaCel2.setCellValue("地区");

			Cell areaCel3 = headRow1.createCell(2);
			areaCel3.setCellStyle(styles.get("header"));
			areaCel3.setCellValue(year + "年");

			Cell areaCel4 = headRow1.createCell(3);
			areaCel4.setCellStyle(styles.get("header"));
			areaCel4.setCellValue(year + "年发明专利有效量(件)");

			Cell areaCel5 = headRow1.createCell(4);
			areaCel5.setCellStyle(styles.get("header"));
			areaCel5.setCellValue(year + "年人口(万人)");

			autoColumnWidthForChineseChar(sheet, 0, 5, minWidth);

			// 冻结
			sheet.createFreezePane(0, 3, 0, 3);
			
			
			for(int i=0;i<list.size();i++) {
				RzlInventStatisticsInfo vo=list.get(i);
				Row dataRow = sheet.createRow(3+i);
				
				dataRow.setHeightInPoints(dataHeight);
				
				Cell areaCell1 = dataRow.createCell(0);
				areaCell1.setCellStyle(styles.get("data"));
				if(StringUtils.isNotBlank(vo.getRanking())) {
					areaCell1.setCellValue(vo.getRanking());
				}else {
					areaCell1.setCellValue("");
				}
				
				
				Cell areaCell2 = dataRow.createCell(1);
				areaCell2.setCellStyle(styles.get("data"));
				if(StringUtils.isNotBlank(vo.getCityName())) {
					areaCell2.setCellValue(vo.getCityName());
				}else {
					areaCell2.setCellValue("");
				}
				
				Cell areaCell3 = dataRow.createCell(2);
				areaCell3.setCellStyle(styles.get("data"));
				if(StringUtils.isNotBlank(vo.getYearCnt())) {
					areaCell3.setCellValue(vo.getYearCnt());
				}else {
					areaCell3.setCellValue("");
				}
				
				
				Cell areaCell4 = dataRow.createCell(3);
				areaCell4.setCellStyle(styles.get("data"));
				if(vo.getYearValid()!=null) {
					areaCell4.setCellValue(vo.getYearValid());
				}else {
					areaCell4.setCellValue(0);
				}
				
				Cell areaCell5 = dataRow.createCell(4);
				areaCell5.setCellStyle(styles.get("data"));
				if(StringUtils.isNotBlank(vo.getYearPopulation())) {
					areaCell5.setCellValue(vo.getYearPopulation());
				}else {
					areaCell5.setCellValue("");
				}
				
			}

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

}
