package com.ruoyi.growthrate.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.growthrate.service.PatentGrowthrateExcel;
import com.ruoyi.growthrate.service.PatentGrowthrateService;
import com.ruoyi.growthrate.vo.PatentGrowthrateDTO;
import com.ruoyi.growthrate.vo.PatentGrowthrateQO;

@RestController
@RequestMapping("/growthrate/patentGrowthrate")
public class PatentGrowthrateController {

    protected final Logger logger = LoggerFactory.getLogger(PatentGrowthrateController.class);
	@Autowired
	private PatentGrowthrateExcel patentGrowthrateExcel;
	@Autowired
	private PatentGrowthrateService patentGrowthrateService;

	@PreAuthorize("@ss.hasPermi('growthrate:patentgrowthrate:list')")
	@GetMapping("/list")
	public AjaxResult list(PatentGrowthrateQO patentGrowthrateQO) {
		List<PatentGrowthrateDTO> list = patentGrowthrateService.countGrowth(patentGrowthrateQO);
		
//		OutputStream out = null;
//		try {
//			out = new FileOutputStream(new File("D:\\temp\\exp.xlsx"));
//			patentGrowthrateExcel.doExport(out, patentGrowthrateQO, list);
//
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} finally {
//			try {
//				out.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
		
		return AjaxResult.success(list);
	}
	
	
	@PreAuthorize("@ss.hasPermi('growthrate:patentgrowthrate:list')")
	@PostMapping("/export")
	public void export(HttpServletResponse response, PatentGrowthrateQO patentGrowthrateQO) {
		List<PatentGrowthrateDTO> list = patentGrowthrateService.countGrowth(patentGrowthrateQO);
		
		
		
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setCharacterEncoding("utf-8");
		try {
//  		rzgApplicantRankingInfoExcel.doExport(response.getOutputStream(), rzgApplicantRankingInfo,list);
			patentGrowthrateExcel.doExport(response.getOutputStream(), patentGrowthrateQO, list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("导出失败," + e.getMessage(), e);
			throw new RuntimeException("导出失败，请联系管理员", e);
		}
		
		
		
	}

}
