package com.ruoyi.reportpub.controller;

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

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.report.vo.ViewStatisticsInfoRep;
import com.ruoyi.reportpub.domain.RzgViewStatisticsInfo;
import com.ruoyi.reportpub.service.IRzgViewStatisticsInfoExcel;
import com.ruoyi.reportpub.service.IRzgViewStatisticsInfoService;

/**
 * 专利公开-概况统计Controller
 *
 * @author ruoyi
 * @date 2023-03-27
 */
@RestController
@RequestMapping("/reportpub/rzgViewStatisticsInfo")
public class RzgViewStatisticsInfoController extends BaseController {
    private final IRzgViewStatisticsInfoService rzgViewStatisticsInfoService;
    
    @Autowired
    private IRzgViewStatisticsInfoExcel rzgViewStatisticsInfoExcel;

    public RzgViewStatisticsInfoController(IRzgViewStatisticsInfoService rzgViewStatisticsInfoService) {
        this.rzgViewStatisticsInfoService = rzgViewStatisticsInfoService;
    }


    /**
     * 查询专利公开-概况统计列表
     *
     * @param rzgViewStatisticsInfo 专利公开-概况统计
     * @return 专利公开-概况统计集合
     * @date 2023-03-27
     */
    @PreAuthorize("@ss.hasPermi('reportpub:rzgViewStatisticsInfo:list')")
    @GetMapping("/list")
    public AjaxResult list(RzgViewStatisticsInfo rzgViewStatisticsInfo) {
        ViewStatisticsInfoRep voList = rzgViewStatisticsInfoService.listByReport(rzgViewStatisticsInfo);
        
//		OutputStream out = null;
//		try {
//			out = new FileOutputStream(new File("D:\\temp\\exp.xlsx"));
//			rzgViewStatisticsInfoExcel.doExport(out, rzgViewStatisticsInfo, voList);
//
//		} 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(voList);
    }
    @GetMapping("/getYearList")
    public AjaxResult getYearList() {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        year = year - 9;
        List<String> list = rzgViewStatisticsInfoService.selectYearList(year);
        return AjaxResult.success(list);
    }

    /**
     * 导出专利公开-概况统计列表
     *
     * @param rzgViewStatisticsInfo 专利公开-概况统计
     * @date 2023-03-27
     */
    @PreAuthorize("@ss.hasPermi('reportpub:rzgViewStatisticsInfo:export')")
    @Log(title = "专利公开-概况统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RzgViewStatisticsInfo rzgViewStatisticsInfo) {
//        List<RzgViewStatisticsInfo> list = rzgViewStatisticsInfoService.list(new QueryWrapper<>());
//        ExcelUtil<RzgViewStatisticsInfo> util = new ExcelUtil<>(RzgViewStatisticsInfo. class);
//        util.exportExcel(response, list, "专利公开-概况统计数据");
        
        

        ViewStatisticsInfoRep voList = rzgViewStatisticsInfoService.listByReport(rzgViewStatisticsInfo);
//        return AjaxResult.success(voList);
    
        
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setCharacterEncoding("utf-8");
    	try {
    		rzgViewStatisticsInfoExcel.doExport(response.getOutputStream(), rzgViewStatisticsInfo,voList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("导出失败," + e.getMessage(), e);
			throw new RuntimeException("导出失败，请联系管理员", e);
		}
    	
    }

    /**
     * 获取专利公开-概况统计详细信息
     *
     * @param id 主键id
     * @return 结果
     * @date 2023-03-27
     */
    @PreAuthorize("@ss.hasPermi('reportpub:rzgViewStatisticsInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(rzgViewStatisticsInfoService.getById(id));
    }


    /**
     * 新增专利公开-概况统计信息
     *
     * @param rzgViewStatisticsInfo 专利公开-概况统计
     * @return 结果
     * @date 2023-03-27
     */
    @PreAuthorize("@ss.hasPermi('reportpub:rzgViewStatisticsInfo:add')")
    @Log(title = "专利公开-概况统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RzgViewStatisticsInfo rzgViewStatisticsInfo) {
        return toAjax(rzgViewStatisticsInfoService.save(rzgViewStatisticsInfo));
    }


    /**
     * 修改专利公开-概况统计信息
     *
     * @param rzgViewStatisticsInfo 专利公开-概况统计
     * @return 结果
     * @date 2023-03-27
     */
    @PreAuthorize("@ss.hasPermi('reportpub:rzgViewStatisticsInfo:edit')")
    @Log(title = "专利公开-概况统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RzgViewStatisticsInfo rzgViewStatisticsInfo) {
        return toAjax(rzgViewStatisticsInfoService.updateById(rzgViewStatisticsInfo));
    }


    /**
     * 删除专利公开-概况统计信息
     *
     * @param ids 主键ids
     * @return 结果
     * @date 2023-03-27
     */
    @PreAuthorize("@ss.hasPermi('reportpub:rzgViewStatisticsInfo:remove')")
    @Log(title = "专利公开-概况统计", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(rzgViewStatisticsInfoService.removeByIds(Arrays.asList(ids)));
    }


}
