package com.ruoyi.hv.service.impl;

import java.io.OutputStream;
import java.util.HashMap;
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.common.constant.HighValue;
import com.ruoyi.hv.domain.RzgClassifiedStatisticsInfo;
import com.ruoyi.hv.service.IRzgClassifiedStatisticsInfoExcel;
import com.ruoyi.hv.vo.RzgClassifiedStatisticsInfoVo;
import com.ruoyi.report.service.impl.AbstractExcelExp;


@Service
public class RzgClassifiedStatisticsInfoExcelImpl extends AbstractExcelExp
		implements IRzgClassifiedStatisticsInfoExcel {

	@Override
	public void doExport(OutputStream out, RzgClassifiedStatisticsInfo req, List<RzgClassifiedStatisticsInfoVo> 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 maxWidth =  "六个字的宽度".getBytes().length * 256;
			int maxWidth = minWidth;
			int dataHeight=30;

			SXSSFSheet sheet = wb.createSheet();
			wb.setSheetName(0, title);
			sheet.trackAllColumnsForAutoSizing();
			
			String[]atTypeArr=req.getApplicantTypes().split(",");
			String[]hvTypeArr=req.getHighValueTypes().split(",");
			int atTypeSize=atTypeArr.length;
			int hvTypeSize=hvTypeArr.length;
			if(hvTypeSize>1) {
				hvTypeSize++;
			}
			
			if(atTypeSize>=3) {
				maxWidth = minWidth;
			}else {
				maxWidth = maxWidth/2*3;
			}

			Map<String, XSSFCellStyle> styles = createStyles(wb);
			int row=0;
			Row titleRow0 = sheet.createRow(row);
			titleRow0.setHeightInPoints(dataHeight);
			Cell titleCell = titleRow0.createCell(0);
			titleCell.setCellStyle(styles.get("title"));
			titleCell.setCellValue(title);

			int titleSize = 0;
			titleSize=titleSize+atTypeSize*(hvTypeSize+1);

			// 合并单元格语法： 开始行、结束行、开始列、结束列
			// 对应代码：new CellRangeAddress(startRowIndex, rowIndex - 1, i, i);
			sheet.addMergedRegion(new CellRangeAddress(row, row, 0, titleSize));
			row=1;
			Row headRow1 = sheet.createRow(row);
			headRow1.setHeight(headHeight);
			Cell areaCell = headRow1.createCell(0);
			areaCell.setCellStyle(styles.get("header"));
			areaCell.setCellValue("地区");
			CellRangeAddress areaRegion = new CellRangeAddress(row, row + 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(row, row, 1, titleSize);
			sheet.addMergedRegion(titleRange1);
			setRegionStyle(sheet, titleRange1, styles.get("header"));

			row=2;
			int column=0;
			Row headRow2 = sheet.createRow(row);
			headRow2.setHeight(headHeight);
			if (req.getApplicantTypes() != null) {
				String[]atArr=req.getApplicantTypes().split(",");
				for(int i=0;i<atArr.length;i++) {
					
					ApplicantType at=ApplicantType.valueOf(atArr[i]);
					if(at==null) {
						continue;
					}
					
					column++;
					Cell titleCell2 = headRow2.createCell(column);
					titleCell2.setCellStyle(styles.get("header"));
					titleCell2.setCellValue(at.getTypeName());
					CellRangeAddress titleRange2 = new CellRangeAddress(row, row, column,column+hvTypeSize);
					sheet.addMergedRegion(titleRange2);
					setRegionStyle(sheet, titleRange2, styles.get("header"));
					column=column+hvTypeSize;
					
				}
				
				
			}
			

			Row headRow3 = sheet.createRow(3);
			headRow3.setHeight((short)(headHeight /2*3));

			
			if(req.getApplicantTypes()!=null) {
				column = 0;
				for(int a=0;a<atTypeArr.length;a++) {
					if (req.getHighValueTypes() != null) {
						
						for(int i=0;i<hvTypeArr.length;i++) {
							HighValue hv=HighValue.valueOf(hvTypeArr[i]);
							if( hv==null) {
								continue;
							}
							column++;
							Cell totalCell = headRow3.createCell(column);
							totalCell.setCellStyle(styles.get("header2"));
							totalCell.setCellValue(hv.getTypeName().replace("的有效发明专利", ""));
							
						}


					}
					
					if(hvTypeArr.length>1) {
						column++;
						Cell patentTypeCell = headRow3.createCell(column);
						patentTypeCell.setCellStyle(styles.get("header2"));
						patentTypeCell.setCellValue("高价值发明拥有量合计");
						
					}
					column++;
					Cell patentTypeCell = headRow3.createCell(column);
					patentTypeCell.setCellStyle(styles.get("header"));
					patentTypeCell.setCellValue("同比增长");

				}
			}
			

			setRegionStyle(sheet, areaRegion, styles.get("header"));
			
			autoColumnWidthForChineseChar(sheet, 0,titleSize, minWidth,maxWidth);
			// 冻结
			sheet.createFreezePane(0, 4, 0, 4);
			
			for(int k=0;k<voList.size();k++) {
				
				RzgClassifiedStatisticsInfoVo vo=voList.get(k);
				Row dataRow = sheet.createRow(4+k);
				dataRow.setHeightInPoints(dataHeight);
				
				column=0;
				Cell areaCell1 = dataRow.createCell(column);
				areaCell1.setCellStyle(styles.get("data"));
				if(StringUtils.isNotBlank(vo.getAreaName())) {
					areaCell1.setCellValue(vo.getAreaName());
				}else {
					areaCell1.setCellValue("");
				}
				
				if(req.getApplicantTypes()!=null) {
					
					
					column = 0;
					for(int a=0;a<atTypeArr.length;a++) {
						ApplicantType at=ApplicantType.valueOf(atTypeArr[a]);
						if(at==null) {
							continue;
						}
						vo.getItems();
						if(vo.getItems()==null) {
							vo.setItems(new HashMap<String,RzgClassifiedStatisticsInfoVo.Item>());
						}
						if(!vo.getItems().containsKey(at.getTypeCode())) {
							vo.getItems().put(at.getTypeCode(), new RzgClassifiedStatisticsInfoVo.Item());
						}
						
						RzgClassifiedStatisticsInfoVo.Item item=vo.getItems().get(at.getTypeCode());
						if (req.getHighValueTypes() != null) {
							for(int i=0;i<hvTypeArr.length;i++) {
								HighValue hv=HighValue.valueOf(hvTypeArr[i]);
								if( hv==null) {
									continue;
								}
								Long sum=0L;
								if("1".equals(hv.getTypeId())) {
									sum=item.getHvSeisPress();
								}else if("2".equals(hv.getTypeId())) {
									sum=item.getHvKeep10Press();
								}else if("3".equals(hv.getTypeId())) {
									sum=item.getHvHaveOverseasPress();
								}else if("4".equals(hv.getTypeId())) {
									sum=item.getHvHighPledgePress();
								}else if("5".equals(hv.getTypeId())) {
									sum=item.getHvTechnologyAwardPress();
								}
								column++;
								Cell totalCell = dataRow.createCell(column);
								totalCell.setCellStyle(styles.get("data"));
								totalCell.setCellValue(sum);
								
							}
						}
						
						if(hvTypeArr.length>1) {
							column++;
							Cell patentTypeCell = dataRow.createCell(column);
							patentTypeCell.setCellStyle(styles.get("data"));
							patentTypeCell.setCellValue(item.getTotalCnt());
							
						}
						column++;
						Cell totalCell = dataRow.createCell(column);
						totalCell.setCellStyle(styles.get("data"));
						totalCell.setCellValue(item.getGrowth());
						
					}
				}
				
			}

		} finally {
			writeToOut(wb, out);
		}

		

	
	}

}
