package com.ruoyi.dimp.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.dimp.domain.DHighValueInfo;
import com.ruoyi.dimp.service.IDHighValueInfoExcel;
import com.ruoyi.hv.domain.DHeightValuePatentDetail;
import com.ruoyi.report.service.impl.AbstractExcelExp;

@Service
public class DHighValueInfoExcelImpl extends AbstractExcelExp implements IDHighValueInfoExcel {

	@Override
	public void doExport(OutputStream out, DHighValueInfo req, List<DHighValueInfo> list) {
		// TODO Auto-generated method stub

		// 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;
			int maxWidth = minWidth;
			String title = "高价值专利对照";
			
			int width8 = "四个字宽四个字宽".getBytes().length * 256;

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

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

			Row titleRow1 = sheet.createRow(1);
			titleRow1.setHeightInPoints(dataHeight/2*3);

			XSSFCellStyle header2 = styles.get("header2");
			int column = 0;
			createCell(titleRow1, column++, "ID", header2);
			createCell(titleRow1, column++, "省份", header2);
			
			int addrColumn=column++;
			createCell(titleRow1,addrColumn , "地址", header2);
			
			
			createCell(titleRow1, column++, "申请人", header2);
			createCell(titleRow1, column++, "战略性新兴产业", header2);
			createCell(titleRow1, column++, "在海外有同族专利", header2);
			createCell(titleRow1, column++, "维护年限超过十年", header2);
			createCell(titleRow1, column++, "存在质押记录", header2);
			createCell(titleRow1, column++, "获得国家科技奖或者中心专利奖", header2);
			createCell(titleRow1, column++, "高价值专利拥有量", header2);

			autoColumnWidthForChineseChar(sheet, 0, titleSize, minWidth, maxWidth);
			sheet.setColumnWidth(addrColumn, width8);
			
			
			// 冻结
			sheet.createFreezePane(0, 2, 0, 2);

			for (int i = 0; i < list.size(); i++) {

				DHighValueInfo vo = list.get(i);
				Row dataRow = sheet.createRow(2 + i);
				dataRow.setHeightInPoints(dataHeight);

				column = 0;
				XSSFCellStyle dataStyle = styles.get("data");
				createCell(dataRow, column++, vo.getId(), dataStyle);
				createCell(dataRow, column++, vo.getProvinceName(), dataStyle);
				createCell(dataRow, column++, vo.getAddress(), dataStyle);
				
				StringBuffer sb=new StringBuffer();
				if(vo.getAppliPubList()!=null && !vo.getAppliPubList().isEmpty()) {
					int c=0;
					for(int j=0;j<vo.getAppliPubList().size();j++) {
						if(StringUtils.isNotBlank(vo.getAppliPubList().get(j))) {
							if(c>0) {
								sb.append(",");
							}
							sb.append(vo.getAppliPubList().get(j));
							c++;
						}
						
					}
				}
				createCell(dataRow, column++, sb.toString(), dataStyle);
				
				
				createCell(dataRow, column++, vo.getIndustryEmergIndustry(), dataStyle);
				createCell(dataRow, column++, vo.getOverseasCongenerPatent(), dataStyle);
				createCell(dataRow, column++, vo.getMaintenanceTenYear(), dataStyle);
				createCell(dataRow, column++, vo.getPledgeRecord(), dataStyle);
				createCell(dataRow, column++, vo.getNationalScienceAward(), dataStyle);
				createCell(dataRow, column++, vo.getInventionPatentNumber(), dataStyle);

			}

		} finally {
			writeToOut(wb, out);
		}

	}

}
