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.common.constant.ApplicantType;
import com.ruoyi.report.service.impl.AbstractExcelExp;
import com.ruoyi.report.vo.IpcStatisticsInfo;
import com.ruoyi.report.vo.IpcStatisticsInfoVo;
import com.ruoyi.reportpub.domain.RzgIpcStatisticsInfo;
import com.ruoyi.reportpub.service.IRzgIpcStatisticsInfoExcel;

@Service
public class RzgIpcStatisticsInfoExcelImpl extends AbstractExcelExp implements IRzgIpcStatisticsInfoExcel {

	@Override
	public void doExport(OutputStream out, RzgIpcStatisticsInfo req, List<IpcStatisticsInfoVo> voList) {


		// TODO Auto-generated method stub
		SXSSFWorkbook wb = new SXSSFWorkbook(500);
		
		try {
			short headHeight = 30 * 20;
			int minWidth = "四个字宽".getBytes().length * 256;
			int maxWidth = "二十个字宽二十个字宽二十个字宽二十个字宽".getBytes().length * 256;
			String title = "专利公开-主IPC统计";
			String queryData=req.getQueryData();
			
			if("locarno".equals(queryData)) {
				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);
			if(req.getPatentTypes()==null) {
				req.setPatentTypes("3"); 
			}
			int ptSize=req.getPatentTypes().split(",").length;
			if(req.getApplicantTypes()==null) {
				req.setApplicantTypes("1,2,3,4,5");
			}
			int atSize=req.getApplicantTypes().split(",").length+2;
			int titleSize = ptSize*atSize+1;

			// 合并单元格语法： 开始行、结束行、开始列、结束列
			// 对应代码：new CellRangeAddress(startRowIndex, rowIndex - 1, i, i);
			sheet.addMergedRegion(new CellRangeAddress(titleRow0.getRowNum(), titleRow0.getRowNum(),titleRow0.getRowNum(), titleSize));
			
			
			String title1=null;
			String title2=null;
			String queryIpc=req.getQueryIpc();
			
			if("ipc".equals(queryData)) {
				if("ipc".equals(queryIpc)) {
					title1="部类";
					title2="部类名";
				}else {
					title1="大类号";
					title2="大类名";
				}
			}else {
				
//				title1="分类号";
//				title2="大类名";
				title1="部类";
				title2="部类名";
			}
			
			
			
			Row headRow1 = sheet.createRow(1);
			headRow1.setHeight(headHeight);
			Cell areaCell = headRow1.createCell(0);
			areaCell.setCellStyle(styles.get("header"));
			areaCell.setCellValue(title1);
			CellRangeAddress areaRegion = new CellRangeAddress(headRow1.getRowNum(), headRow1.getRowNum() + 2, 0, 0);
			sheet.addMergedRegion(areaRegion);
			
			
			Cell areaCel2 = headRow1.createCell(1);
			areaCel2.setCellStyle(styles.get("header"));
			areaCel2.setCellValue(title2);
			CellRangeAddress areaRegion2 = new CellRangeAddress(headRow1.getRowNum(), headRow1.getRowNum() + 2, 1, 1);
			sheet.addMergedRegion(areaRegion2);
			
			
			
			autoColumnWidthForChineseChar(sheet, 0, 2, minWidth);
			//
			String topTitle1 = "统计时期";
			if (StringUtils.isNotBlank(req.getCountTimeStr())) {
				topTitle1 = topTitle1 + req.getCountTimeStr();
			}
			Cell titleCell1 = headRow1.createCell(2);
			titleCell1.setCellStyle(styles.get("header"));
			titleCell1.setCellValue(topTitle1);
			CellRangeAddress titleRange1 = new CellRangeAddress(headRow1.getRowNum(), headRow1.getRowNum(), 2,titleSize );
			sheet.addMergedRegion(titleRange1);
			setRegionStyle(sheet, titleRange1, styles.get("header"));

			Row headRow2 = sheet.createRow(2);
			headRow2.setHeight(headHeight);
			int column=2;
			if(req.getPatentTypes().contains("1")) {
				String topTitle2 = "发明";
				Cell titleCell2 = headRow2.createCell(column);
				titleCell2.setCellStyle(styles.get("header"));
				titleCell2.setCellValue(topTitle2);
				CellRangeAddress titleRange2 = new CellRangeAddress(headRow2.getRowNum(), headRow2.getRowNum(), 2,atSize+1);
				sheet.addMergedRegion(titleRange2);
				setRegionStyle(sheet, titleRange2, styles.get("header"));
				column=column+atSize;
			}
			if(req.getPatentTypes().contains("2")) {
				String topTitle2 = "实用新型";
				Cell titleCell2 = headRow2.createCell(column);
				titleCell2.setCellStyle(styles.get("header"));
				titleCell2.setCellValue(topTitle2);
				CellRangeAddress titleRange2 = new CellRangeAddress(headRow2.getRowNum(), headRow2.getRowNum(), column,atSize+column-1);
				sheet.addMergedRegion(titleRange2);
				setRegionStyle(sheet, titleRange2, styles.get("header"));
				column=column+atSize;
			}
			if(req.getPatentTypes().contains("3")) {
				String topTitle2 = "外观设计";
				Cell titleCell2 = headRow2.createCell(column);
				titleCell2.setCellStyle(styles.get("header"));
				titleCell2.setCellValue(topTitle2);
				CellRangeAddress titleRange2 = new CellRangeAddress(headRow2.getRowNum(), headRow2.getRowNum(), column,atSize+column-1);
				sheet.addMergedRegion(titleRange2);
				setRegionStyle(sheet, titleRange2, styles.get("header"));
				column=column+atSize;
			}
			
			column=1;
			Row headRow3 = sheet.createRow(3);
			headRow3.setHeight(headHeight);
			
			for(int t=1;t<=3;t++) {
				if(req.getPatentTypes().contains(t+"")) {
					column++;
					String totalTitle = "数量";
					Cell totalCell2 = headRow3.createCell(column);
					totalCell2.setCellStyle(styles.get("header"));
					totalCell2.setCellValue(totalTitle);
					
					column++;
					String preTitle = "占比";
					Cell preCell2 = headRow3.createCell(column);
					preCell2.setCellStyle(styles.get("header"));
					preCell2.setCellValue(preTitle);
					for(int i=1;i<=5;i++) {
						if(req.getApplicantTypes().contains(i+"")) {
							ApplicantType at=ApplicantType.valueOf(i+"");
							column++;
							String preTitleuc = at.getTypeName();
							Cell preCelluc = headRow3.createCell(column);
							preCelluc.setCellStyle(styles.get("header"));
							preCelluc.setCellValue(preTitleuc);
						}
					}
					
					
				}
			}
			
			
			setRegionStyle(sheet, areaRegion, styles.get("header"));
			setRegionStyle(sheet, areaRegion2, styles.get("header"));
			
			
			// 冻结
			sheet.createFreezePane(0, 4, 0, 4);
			for(int i=0;i<voList.size();i++) {
				IpcStatisticsInfoVo vo=voList.get(i);
				Row dataRow = sheet.createRow(4+i);
				dataRow.setHeightInPoints(dataHeight);
				
				column=0;
				Cell typeCell = dataRow.createCell(column);
				typeCell.setCellStyle(styles.get("data"));
				if(StringUtils.isNotBlank(vo.getType())) {
					typeCell.setCellValue(vo.getType());
				}else {
					typeCell.setCellValue("");
				}
				
				column++;
				Cell typeCellName = dataRow.createCell(column);
				typeCellName.setCellStyle(styles.get("data"));
				if(StringUtils.isNotBlank(vo.getTypeName())) {
					typeCellName.setCellValue(vo.getTypeName());
				}else {
					typeCellName.setCellValue("");
				}
				
				//发明
				if(req.getPatentTypes().contains("1")) {
					IpcStatisticsInfo invention=vo.getDatas().get("invention");
					if(invention==null) {
						invention=new IpcStatisticsInfo();
						invention.setNumber(0L);
						invention.setProportion("0%");
						invention.setHigherSchool(0L);
						invention.setResearchInstitute(0L);
						invention.setEnterprise(0L);
						invention.setInstitutions(0L);
						invention.setIndividual(0L);
					}
					
					column++;
					Cell numberCell = dataRow.createCell(column);
					numberCell.setCellStyle(styles.get("data"));
					if(invention.getNumber()!=null) {
						numberCell.setCellValue(invention.getNumber());
					}else {
						numberCell.setCellValue(0);
					}
					
					column++;
					Cell proportionCell = dataRow.createCell(column);
					proportionCell.setCellStyle(styles.get("data"));
					if(invention.getProportion()!=null) {
						proportionCell.setCellValue(invention.getProportion());
					}else {
						proportionCell.setCellValue(0);
					}
					
					if(req.getApplicantTypes().contains("1")) {
						column++;
						Cell cell = dataRow.createCell(column);
						cell.setCellStyle(styles.get("data"));
						if(invention.getHigherSchool()!=null) {
							cell.setCellValue(invention.getHigherSchool());
						}else {
							cell.setCellValue(0);
						}
					}
					if(req.getApplicantTypes().contains("2")) {
						column++;
						Cell cell = dataRow.createCell(column);
						cell.setCellStyle(styles.get("data"));
						if(invention.getResearchInstitute()!=null) {
							cell.setCellValue(invention.getResearchInstitute());
						}else {
							cell.setCellValue(0);
						}
					}
					if(req.getApplicantTypes().contains("3")) {
						column++;
						Cell cell = dataRow.createCell(column);
						cell.setCellStyle(styles.get("data"));
						if(invention.getEnterprise()!=null) {
							cell.setCellValue(invention.getEnterprise());
						}else {
							cell.setCellValue(0);
						}
					}
					if(req.getApplicantTypes().contains("4")) {
						column++;
						Cell cell = dataRow.createCell(column);
						cell.setCellStyle(styles.get("data"));
						if(invention.getInstitutions()!=null) {
							cell.setCellValue(invention.getInstitutions());
						}else {
							cell.setCellValue(0);
						}
					}
					if(req.getApplicantTypes().contains("5")) {
						column++;
						Cell cell = dataRow.createCell(column);
						cell.setCellStyle(styles.get("data"));
						if(invention.getIndividual()!=null) {
							cell.setCellValue(invention.getIndividual());
						}else {
							cell.setCellValue(0);
						}
					}
					
					
				}
				//实用新型
				if(req.getPatentTypes().contains("2")) {
					IpcStatisticsInfo invention=vo.getDatas().get("modelUtility");
					if(invention==null) {
						invention=new IpcStatisticsInfo();
						invention.setNumber(0L);
						invention.setProportion("0%");
						invention.setHigherSchool(0L);
						invention.setResearchInstitute(0L);
						invention.setEnterprise(0L);
						invention.setInstitutions(0L);
						invention.setIndividual(0L);
					}
					
					column++;
					Cell numberCell = dataRow.createCell(column);
					numberCell.setCellStyle(styles.get("data"));
					if(invention.getNumber()!=null) {
						numberCell.setCellValue(invention.getNumber());
					}else {
						numberCell.setCellValue(0);
					}
					
					column++;
					Cell proportionCell = dataRow.createCell(column);
					proportionCell.setCellStyle(styles.get("data"));
					if(invention.getProportion()!=null) {
						proportionCell.setCellValue(invention.getProportion());
					}else {
						proportionCell.setCellValue(0);
					}
					
					if(req.getApplicantTypes().contains("1")) {
						column++;
						Cell cell = dataRow.createCell(column);
						cell.setCellStyle(styles.get("data"));
						if(invention.getHigherSchool()!=null) {
							cell.setCellValue(invention.getHigherSchool());
						}else {
							cell.setCellValue(0);
						}
					}
					if(req.getApplicantTypes().contains("2")) {
						column++;
						Cell cell = dataRow.createCell(column);
						cell.setCellStyle(styles.get("data"));
						if(invention.getResearchInstitute()!=null) {
							cell.setCellValue(invention.getResearchInstitute());
						}else {
							cell.setCellValue(0);
						}
					}
					if(req.getApplicantTypes().contains("3")) {
						column++;
						Cell cell = dataRow.createCell(column);
						cell.setCellStyle(styles.get("data"));
						if(invention.getEnterprise()!=null) {
							cell.setCellValue(invention.getEnterprise());
						}else {
							cell.setCellValue(0);
						}
					}
					if(req.getApplicantTypes().contains("4")) {
						column++;
						Cell cell = dataRow.createCell(column);
						cell.setCellStyle(styles.get("data"));
						if(invention.getInstitutions()!=null) {
							cell.setCellValue(invention.getInstitutions());
						}else {
							cell.setCellValue(0);
						}
					}
					if(req.getApplicantTypes().contains("5")) {
						column++;
						Cell cell = dataRow.createCell(column);
						cell.setCellStyle(styles.get("data"));
						if(invention.getIndividual()!=null) {
							cell.setCellValue(invention.getIndividual());
						}else {
							cell.setCellValue(0);
						}
					}
					
					
				}
				//外观设计
				if(req.getPatentTypes().contains("3")) {
					IpcStatisticsInfo invention=vo.getDatas().get("appearance");
					if(invention==null) {
						invention=new IpcStatisticsInfo();
						invention.setNumber(0L);
						invention.setProportion("0%");
						invention.setHigherSchool(0L);
						invention.setResearchInstitute(0L);
						invention.setEnterprise(0L);
						invention.setInstitutions(0L);
						invention.setIndividual(0L);
					}
					
					column++;
					Cell numberCell = dataRow.createCell(column);
					numberCell.setCellStyle(styles.get("data"));
					if(invention.getNumber()!=null) {
						numberCell.setCellValue(invention.getNumber());
					}else {
						numberCell.setCellValue(0);
					}
					
					column++;
					Cell proportionCell = dataRow.createCell(column);
					proportionCell.setCellStyle(styles.get("data"));
					if(invention.getProportion()!=null) {
						proportionCell.setCellValue(invention.getProportion());
					}else {
						proportionCell.setCellValue(0);
					}
					
					if(req.getApplicantTypes().contains("1")) {
						column++;
						Cell cell = dataRow.createCell(column);
						cell.setCellStyle(styles.get("data"));
						if(invention.getHigherSchool()!=null) {
							cell.setCellValue(invention.getHigherSchool());
						}else {
							cell.setCellValue(0);
						}
					}
					if(req.getApplicantTypes().contains("2")) {
						column++;
						Cell cell = dataRow.createCell(column);
						cell.setCellStyle(styles.get("data"));
						if(invention.getResearchInstitute()!=null) {
							cell.setCellValue(invention.getResearchInstitute());
						}else {
							cell.setCellValue(0);
						}
					}
					if(req.getApplicantTypes().contains("3")) {
						column++;
						Cell cell = dataRow.createCell(column);
						cell.setCellStyle(styles.get("data"));
						if(invention.getEnterprise()!=null) {
							cell.setCellValue(invention.getEnterprise());
						}else {
							cell.setCellValue(0);
						}
					}
					if(req.getApplicantTypes().contains("4")) {
						column++;
						Cell cell = dataRow.createCell(column);
						cell.setCellStyle(styles.get("data"));
						if(invention.getInstitutions()!=null) {
							cell.setCellValue(invention.getInstitutions());
						}else {
							cell.setCellValue(0);
						}
					}
					if(req.getApplicantTypes().contains("5")) {
						column++;
						Cell cell = dataRow.createCell(column);
						cell.setCellStyle(styles.get("data"));
						if(invention.getIndividual()!=null) {
							cell.setCellValue(invention.getIndividual());
						}else {
							cell.setCellValue(0);
						}
					}
					
					
				}
				
				autoColumnWidthForChineseChar(sheet, 0, 2, minWidth,maxWidth);
			}
			
			
			
		} finally {
			writeToOut(wb, out);
			System.out.println("ok");
		}
		
		
		
	
	
	}

}
