package com.ruoyi.report.service.impl;

import java.io.OutputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

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.RzlClassStatisticsInfo;
import com.ruoyi.report.service.IRzlClassStatisticsInfoExcel;
import com.ruoyi.report.vo.ClassStatisticsInfoVo;
import com.ruoyi.report.vo.ViewStatisticsInfoVo;

@Service
public class RzlClassStatisticsInfoExcelImpl extends AbstractExcelExp implements IRzlClassStatisticsInfoExcel {

	@Override
	public void doExport(OutputStream out, RzlClassStatisticsInfo req, List<ClassStatisticsInfoVo> voList) {
		// 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 = 3;
			if (req.getApplicantTypesList() != null) {
				int size = req.getApplicantTypesList().size();
				titleSize = titleSize +size;
				if (size > 1) {
					titleSize++;
				}
			}

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

			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() + 2, 0, 0);
			sheet.addMergedRegion(areaRegion);


//			autoColumnWidthForChineseChar(sheet, 0, 2, minWidth);
			//
			String title1 = "统计时期";
			String year = req.getYear();
			title1 = title1 + year;
			if (StringUtils.isNotBlank(req.getMonth())) {
				title1 = title1 + "-" + req.getMonth();
			}
			Cell titleCell1 = headRow1.createCell(1);
			titleCell1.setCellStyle(styles.get("header"));
			titleCell1.setCellValue(title1);
			CellRangeAddress titleRange1 = new CellRangeAddress(headRow1.getRowNum(), headRow1.getRowNum(), 1,
					titleSize - 2);
			sheet.addMergedRegion(titleRange1);
			setRegionStyle(sheet, titleRange1, styles.get("header"));

			Row headRow2 = sheet.createRow(2);
			headRow2.setHeight(headHeight);
			String title2 = "专例类型";
			if (req.getPatentTypesList() != null) {
				title2=title2+":";
				if (req.getPatentTypesList().size() >= 3) {
					title2 = title2 + "全部";
				} else {
					for (int i = 0; i < req.getPatentTypesList().size(); i++) {
						if (i > 0) {
							title2 = title2 + ",";
						}
						title2 = title2 + req.getPatentTypesList().get(i);
					}
				}

			}
			Cell titleCell2 = headRow2.createCell(1);
			titleCell2.setCellStyle(styles.get("header"));
			titleCell2.setCellValue(title2);
			CellRangeAddress titleRange2 = new CellRangeAddress(headRow2.getRowNum(), headRow2.getRowNum(), 1,
					titleSize - 2);
			sheet.addMergedRegion(titleRange2);

			Row headRow3 = sheet.createRow(3);
			headRow3.setHeight(headHeight);
			Cell patentTypeCell = headRow3.createCell(1);
			patentTypeCell.setCellStyle(styles.get("header"));
			patentTypeCell.setCellValue("专利类型");

			Set<String> pts = new HashSet();
			
			if (req.getApplicantTypesList() != null) {

				int column = 2;

				if (req.getApplicantTypesList().size() > 1) {
					Cell totalCell = headRow3.createCell(column);
					totalCell.setCellStyle(styles.get("header"));
					totalCell.setCellValue("合计");
					column++;
				}
				
				pts.addAll(req.getApplicantTypesList());
				if (pts.contains("大专院校")) {
					Cell totalCell = headRow3.createCell(column);
					totalCell.setCellStyle(styles.get("header"));
					totalCell.setCellValue("大专院校");
					column++;
				}
				if (pts.contains("科研机构")) {
					Cell totalCell = headRow3.createCell(column);
					totalCell.setCellStyle(styles.get("header"));
					totalCell.setCellValue("科研机构");
					column++;
				}
				if (pts.contains("企业")) {
					Cell totalCell = headRow3.createCell(column);
					totalCell.setCellStyle(styles.get("header"));
					totalCell.setCellValue("企业");
					column++;
				}
				if (pts.contains("机关团体")) {
					Cell totalCell = headRow3.createCell(column);
					totalCell.setCellStyle(styles.get("header"));
					totalCell.setCellValue("机关团体");
					column++;
				}
				if (pts.contains("个人")) {
					Cell totalCell = headRow3.createCell(column);
					totalCell.setCellStyle(styles.get("header"));
					totalCell.setCellValue("个人");
					column++;
				}

			}

			setRegionStyle(sheet, areaRegion, styles.get("header"));
			setRegionStyle(sheet, titleRange2, styles.get("header"));
			
			autoColumnWidthForChineseChar(sheet, 0,3, minWidth);
			// 冻结
			sheet.createFreezePane(0, 4, 0, 4);
			for(int i=0;i<voList.size();i++) {
				ClassStatisticsInfoVo vo=voList.get(i);
				Row dataRow = sheet.createRow(4+i);
				dataRow.setHeightInPoints(dataHeight);
				
				int column=0;
				Cell areaCell1 = dataRow.createCell(column);
				areaCell1.setCellStyle(styles.get("data"));
				if(StringUtils.isNotBlank(vo.getAreaName())) {
					areaCell1.setCellValue(vo.getAreaName());
				}else {
					areaCell1.setCellValue("");
				}
				
				
				column++;
				Cell patentTypeCell1 = dataRow.createCell(column);
				patentTypeCell1.setCellStyle(styles.get("data"));
				if(StringUtils.isNotBlank(vo.getPatentType())) {
					patentTypeCell1.setCellValue(vo.getPatentType());
				}else {
					patentTypeCell1.setCellValue("");
				}
				
				
				
				if (req.getApplicantTypesList().size() > 1) {
					column++;
					Cell totalCell = dataRow.createCell(column);
					totalCell.setCellStyle(styles.get("data"));
					totalCell.setCellValue(vo.getTotalCnt());
				}
				
				if (pts.contains("大专院校")) {
					column++;
					Cell totalCell = dataRow.createCell(column);
					totalCell.setCellStyle(styles.get("data"));
					totalCell.setCellValue(vo.getHigherSchool());
					
				}
				if (pts.contains("科研机构")) {
					column++;
					Cell totalCell = dataRow.createCell(column);
					totalCell.setCellStyle(styles.get("data"));
					totalCell.setCellValue(vo.getResearchInstitute());
				}
				if (pts.contains("企业")) {
					column++;
					Cell totalCell = dataRow.createCell(column);
					totalCell.setCellStyle(styles.get("data"));
					totalCell.setCellValue(vo.getEnterprise());
				}
				if (pts.contains("机关团体")) {
					column++;
					Cell totalCell = dataRow.createCell(column);
					totalCell.setCellStyle(styles.get("data"));
					totalCell.setCellValue(vo.getInstitutions());
				}
				if (pts.contains("个人")) {
					column++;
					Cell totalCell = dataRow.createCell(column);
					totalCell.setCellStyle(styles.get("data"));
					totalCell.setCellValue(vo.getIndividual());
				}
			}

		} finally {
			writeToOut(wb, out);
		}

		

	}

}
