package com.ruoyi.reportrzs.service.impl;

import java.io.OutputStream;
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.report.vo.ViewStatisticsInfoRep;
import com.ruoyi.report.vo.ViewStatisticsInfoVo;
import com.ruoyi.report.vo.ViewStatisticsInfoVo.Item;
import com.ruoyi.reportrzs.domain.RzsViewStatisticsInfo;
import com.ruoyi.reportrzs.service.IRzsViewStatisticsInfoExcel;

@Service
public class RzsViewStatisticsInfoExcelImpl extends AbstractExcelExp implements IRzsViewStatisticsInfoExcel{

	@Override
	public void doExport(OutputStream out, RzsViewStatisticsInfo req, ViewStatisticsInfoRep voList) {
		

		// TODO Auto-generated method stub
//		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//		response.setCharacterEncoding("utf-8");
		
		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);

			int titleSize = voList.getYearList().size() * 7 + 2;

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

			Row headRow1 = sheet.createRow(1);
			headRow1.setHeight(headHeight);
			Cell areaCell = headRow1.createCell(0);
			areaCell.setCellStyle(styles.get("header"));
			areaCell.setCellValue("地区");
			
			
			CellRangeAddress areaRegion=new CellRangeAddress(headRow1.getRowNum(), headRow1.getRowNum() + 1, 0, 0);
			sheet.addMergedRegion(areaRegion);
			
			Cell patentTypeCell = headRow1.createCell(1);
			patentTypeCell.setCellStyle(styles.get("header"));
			patentTypeCell.setCellValue("专例类型");
			CellRangeAddress patentTypeRange=new CellRangeAddress(headRow1.getRowNum(), headRow1.getRowNum() + 1, 1, 1);
			sheet.addMergedRegion(patentTypeRange);
			
			autoColumnWidthForChineseChar(sheet, 0, 2,minWidth);
			
			for (int i = 0; i < voList.getYearList().size(); i++) {
				String year = voList.getYearList().get(i);
				int column = 2 + 7 * i;
				Cell titleCell1 = headRow1.createCell(column);
				titleCell1.setCellStyle(styles.get("header"));
//				titleCell1.setCellValue("统计时期" + year + "-" + req.getMonth());
				String sumTime="12";
				if(Integer.parseInt(year)==Integer.parseInt(req.getYear())) {
					sumTime=req.getMonth();
				}
				titleCell1.setCellValue("统计时期" + year + "-" + sumTime);
				
				CellRangeAddress titleRange1=new CellRangeAddress(headRow1.getRowNum(), headRow1.getRowNum(), column, column + 6);
				sheet.addMergedRegion(titleRange1);
				setRegionStyle(sheet, titleRange1, styles.get("header"));
			}

			Row headRow2 = sheet.createRow(2);
			headRow2.setHeight(headHeight);
			for (int i = 0; i < voList.getYearList().size(); i++) {
				String year = voList.getYearList().get(i);
				int column = 1 + 7 * i;
				Cell titleCell2 = headRow2.createCell(column + 1);
				titleCell2.setCellStyle(styles.get("header"));
				titleCell2.setCellValue(year);
				
				Cell titleCell0 = headRow2.createCell(column + 2);
				titleCell0.setCellStyle(styles.get("header"));
				titleCell0.setCellValue("小计");

				Cell titleCell3 = headRow2.createCell(column + 3);
				titleCell3.setCellStyle(styles.get("header"));
				titleCell3.setCellValue("大专院校");
				
				Cell titleCell4 = headRow2.createCell(column + 4);
				titleCell4.setCellStyle(styles.get("header"));
				titleCell4.setCellValue("科研机构");

				Cell titleCell5 = headRow2.createCell(column + 5);
				titleCell5.setCellStyle(styles.get("header"));
				titleCell5.setCellValue("企业");

				Cell titleCell6 = headRow2.createCell(column + 6);
				titleCell6.setCellStyle(styles.get("header"));
				titleCell6.setCellValue("机关团体");

				Cell titleCell7 = headRow2.createCell(column + 7);
				titleCell7.setCellStyle(styles.get("header"));
				titleCell7.setCellValue("个人");
			}
			
			autoColumnWidthForChineseChar(sheet, 2, voList.getYearList().size()*7,minWidth);
			
			setRegionStyle(sheet, areaRegion, styles.get("header"));
			setRegionStyle(sheet, patentTypeRange, styles.get("header"));

			// 冻结
			sheet.createFreezePane(0, 3, 0, 3);
			
			
			for(int i=0;i<voList.getDataList().size();i++) {
				ViewStatisticsInfoVo vo=voList.getDataList().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.getAreaName())) {
					areaCell1.setCellValue(vo.getAreaName());
				}else {
					areaCell1.setCellValue("");
				}
				
				Cell patentTypeCell1 = dataRow.createCell(1);
				patentTypeCell1.setCellStyle(styles.get("data"));
				if(StringUtils.isNotBlank(vo.getPatentType())) {
					patentTypeCell1.setCellValue(vo.getPatentType());
				}else {
					patentTypeCell1.setCellValue("");
				}
				
				
				int column=2;
				for (int y = 0; y < voList.getYearList().size(); y++) {
					String year = voList.getYearList().get(y);
					Map<String,Item>map = vo.getYearData();
					Item item=null;
					if(map.containsKey(year)) {
						item=map.get(year);
					}else {
						item=new Item();
					}
					
					int c=column+7*y;
					Cell dataCell0 = dataRow.createCell(c);
					dataCell0.setCellStyle(styles.get("data"));
					dataCell0.setCellValue(year);
					
					Cell dataCell00 = dataRow.createCell(c+1);
					dataCell00.setCellStyle(styles.get("data"));
					dataCell00.setCellValue(item.getTotalCnt());
					
					Cell dataCell1 = dataRow.createCell(c+2);
					dataCell1.setCellStyle(styles.get("data"));
					dataCell1.setCellValue(item.getHigherSchool());
					
					Cell dataCell2 = dataRow.createCell(c+3);
					dataCell2.setCellStyle(styles.get("data"));
					dataCell2.setCellValue(item.getResearchInstitute());
					
					Cell dataCell3 = dataRow.createCell(c+4);
					dataCell3.setCellStyle(styles.get("data"));
					dataCell3.setCellValue(item.getEnterprise());
					
					Cell dataCell4 = dataRow.createCell(c+5);
					dataCell4.setCellStyle(styles.get("data"));
					dataCell4.setCellValue(item.getInstitutions());
					
					Cell dataCell5 = dataRow.createCell(c+6);
					dataCell5.setCellStyle(styles.get("data"));
					dataCell5.setCellValue(item.getIndividual());
				}
			}
		} finally {
			writeToOut(wb,out);
		}
		
		
	
		
	}
}
