package com.ruoyi.agentrep.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.agentrep.domain.DAgentAuthInfo;
import com.ruoyi.agentrep.service.IDAgentAuthInfoExcel;
import com.ruoyi.report.service.impl.AbstractExcelExp;
import com.ruoyi.reportpub.domain.RzgApplicantRankingInfo;


@Service
public class DAgentAuthInfoExcelImpl extends AbstractExcelExp implements IDAgentAuthInfoExcel {

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


		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 = 6;

			// 合并单元格语法： 开始行、结束行、开始列、结束列
			// 对应代码：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 title1Cell = titleRow1.createCell(0);
			title1Cell.setCellStyle(styles.get("header"));
			title = "统计时期";
			if (StringUtils.isNotBlank(req.getCountTimeStr())) {
				title = title +  req.getCountTimeStr();
			}
			title1Cell.setCellValue(title);
			CellRangeAddress areaRegion = new CellRangeAddress(titleRow1.getRowNum(), titleRow1.getRowNum(), 0, titleSize);
			sheet.addMergedRegion(areaRegion);
			setRegionStyle(sheet, areaRegion, styles.get("header"));
			
			
			Row titleRow2 = sheet.createRow(2);
			titleRow2.setHeight(headHeight);
			
			XSSFCellStyle header = styles.get("header");
			createCell(titleRow2, 0, "排名",header );
			createCell(titleRow2, 1, "代理机构",header );
			createCell(titleRow2, 2, "案件代理总量(公开总量)",header );
			createCell(titleRow2, 3, "授权量",header );
			createCell(titleRow2, 4, "发明授权",header );
			createCell(titleRow2, 5, "实用新型",header );
			createCell(titleRow2, 6, "外观设计",header );
			
			// 冻结
			sheet.createFreezePane(0, 3, 0, 3);
			
			for(int i=0;i<list.size();i++) {
				DAgentAuthInfo vo=list.get(i);
				Row dataRow = sheet.createRow(3+i);
				dataRow.setHeightInPoints(dataHeight);
				
				XSSFCellStyle data = styles.get("data");
				int column = 0;
				createCell(dataRow, column++, vo.getRanking(),data );
				createCell(dataRow, column++, vo.getAgencyName(),data );

				createCell(dataRow, column++, vo.getPubTotal(),data );

				createCell(dataRow, column++, vo.getAuthTotal(),data );
				createCell(dataRow, column++, vo.getInventionCnt(),data );
				createCell(dataRow, column++, vo.getModelUtilityCnt(),data );
				createCell(dataRow, column++, vo.getAppearanceCnt(),data );
			}
			
			autoColumnWidthForChineseChar(sheet, 0, titleSize, minWidth);
		} finally {
			writeToOut(wb, out);
		}
	

	
	
		
	
	
	
	
	}

}
