package com.ruoyi.reportpub.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.service.impl.AbstractExcelExp;
import com.ruoyi.reportpub.domain.RzgApplicantRankingInfo;
import com.ruoyi.reportpub.service.IRzgApplicantRankingInfoExcel;
import com.ruoyi.reportrzs.domain.RzsApplicantRankingInfo;


@Service
public class RzgApplicantRankingInfoExcelImpl extends AbstractExcelExp implements IRzgApplicantRankingInfoExcel {

	@Override
	public void doExport(OutputStream out, RzgApplicantRankingInfo req, List<RzgApplicantRankingInfo> 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);

			int titleSize = 2;
			int patentTypesLen = req.getPatentTypes().split(",").length;
			if (req.getPatentTypes() != null) {
				int size = patentTypesLen;
				titleSize = titleSize + size;
				if (size > 1) {
					titleSize++;
				}
			}

			// 合并单元格语法： 开始行、结束行、开始列、结束列
			// 对应代码：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 titleCell1 = titleRow1.createCell(0);
			titleCell1.setCellStyle(styles.get("header"));
			title = "统计时期";
			if (StringUtils.isNotBlank(req.getCountTimeStr())) {
				title = title +  req.getCountTimeStr();
			}
			titleCell1.setCellValue(title);
			CellRangeAddress areaRegion = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum(), 0,
					titleSize);
			sheet.addMergedRegion(areaRegion);
			setRegionStyle(sheet, areaRegion, styles.get("header"));

			int column = 0;
			Row titleRow2 = sheet.createRow(2);
			titleRow2.setHeight(headHeight);
			Cell rankCell1 = titleRow2.createCell(column);
			rankCell1.setCellStyle(styles.get("header"));
			rankCell1.setCellValue("排名");

			column++;
			Cell patyee1 = titleRow2.createCell(column);
			patyee1.setCellStyle(styles.get("header"));
			patyee1.setCellValue("专利权人名称");

			if (patentTypesLen > 1) {
				column++;
				Cell total = titleRow2.createCell(column);
				total.setCellStyle(styles.get("header"));
				total.setCellValue("公开专利量");
			}

			column++;
			Cell appliType = titleRow2.createCell(column);
			appliType.setCellStyle(styles.get("header"));
			appliType.setCellValue("申请人类型");

			if (req.getPatentTypes().contains("1")) {
				column++;
				Cell cell = titleRow2.createCell(column);
				cell.setCellStyle(styles.get("header"));
				cell.setCellValue("发明");
			}
			if (req.getPatentTypes().contains("2")) {
				column++;
				Cell cell = titleRow2.createCell(column);
				cell.setCellStyle(styles.get("header"));
				cell.setCellValue("实用新型");
			}
			if (req.getPatentTypes().contains("3")) {
				column++;
				Cell cell = titleRow2.createCell(column);
				cell.setCellStyle(styles.get("header"));
				cell.setCellValue("外观设计");
			}
			
			// 冻结
			sheet.createFreezePane(0, 3, 0, 3);
			
			for(int i=0;i<list.size();i++) {
				RzgApplicantRankingInfo vo=list.get(i);
				
				Row dataRow = sheet.createRow(3+i);
				dataRow.setHeightInPoints(dataHeight);
				
				
				column = 0;
				Cell ranking = dataRow.createCell(column);
				ranking.setCellStyle(styles.get("data"));
				ranking.setCellValue(vo.getRanking());
				
				column++;
				Cell patentee = dataRow.createCell(column);
				patentee.setCellStyle(styles.get("data"));
				if(StringUtils.isNotBlank(vo.getPatenteeName())) {
					patentee.setCellValue(vo.getPatenteeName());
				}else {
					patentee.setCellValue("");
				}
				
				
				if (patentTypesLen > 1) {
					column++;
					Cell total = dataRow.createCell(column);
					total.setCellStyle(styles.get("data"));
					if(vo.getPatentsTotalNumber()!=null) {
						total.setCellValue(vo.getPatentsTotalNumber());
					}else {
						total.setCellValue(0L);
					}
				}
				
				column++;
				Cell applicantType = dataRow.createCell(column);
				applicantType.setCellStyle(styles.get("data"));
				if(StringUtils.isNotBlank(vo.getApplicantType())) {
					applicantType.setCellValue(vo.getApplicantType());
				}else {
					applicantType.setCellValue("");
				}
				
				if (req.getPatentTypes().contains("1")) {
					column++;
					Cell cell = dataRow.createCell(column);
					cell.setCellStyle(styles.get("data"));
					if(vo.getInvent()!=null) {
						cell.setCellValue(vo.getInvent());
					}else {
						cell.setCellValue(0L);
					}
					
				}
				if (req.getPatentTypes().contains("2")) {
					column++;
					Cell cell = dataRow.createCell(column);
					cell.setCellStyle(styles.get("data"));
					if(vo.getUtilityModel()!=null) {
						cell.setCellValue(vo.getUtilityModel());
					}else {
						cell.setCellValue(0L);
					}
				}
				if (req.getPatentTypes().contains("3")) {
					column++;
					Cell cell = dataRow.createCell(column);
					cell.setCellStyle(styles.get("data"));
					if(vo.getAppearanceDesign()!=null) {
						cell.setCellValue(vo.getAppearanceDesign());
					}else {
						cell.setCellValue(0L);
					}
				}
				
				
			}
			
			
			autoColumnWidthForChineseChar(sheet, 0, titleSize, minWidth);

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

}
