package com.ruoyi.report.controller;

import java.io.File;
import java.io.FileNotFoundException;
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.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.common.utils.poi.ExcelUtil;
import com.ruoyi.report.domain.RzlViewStatisticsInfo;
import com.ruoyi.report.service.IRzlViewStatisticsInfoExcel;
import com.ruoyi.report.service.IRzlViewStatisticsInfoService;
import com.ruoyi.report.vo.ViewStatisticsInfoRep;

/**
 * 专利有效-概况统计Controller
 *
 * @author ruoyi
 * @date 2023-03-26
 */
@RestController
@RequestMapping("/report/rzlViewStatisticsInfo")
public class RzlViewStatisticsInfoController extends BaseController {
    private final IRzlViewStatisticsInfoService rzlViewStatisticsInfoService;
    
    @Autowired
    private IRzlViewStatisticsInfoExcel rzlViewStatisticsInfoExcel;

    public RzlViewStatisticsInfoController(IRzlViewStatisticsInfoService rzlViewStatisticsInfoService) {
        this.rzlViewStatisticsInfoService = rzlViewStatisticsInfoService;
    }


    /**
     * 查询专利有效-概况统计列表
     *
     * @param rzlViewStatisticsInfo 专利有效-概况统计
     * @return 专利有效-概况统计集合
     * @date 2023-03-26
     */
//    @PreAuthorize("@ss.hasPermi('report:rzlViewStatisticsInfo:list')")
//    @GetMapping("/list_bak")
//    public TableDataInfo list(RzlViewStatisticsInfo rzlViewStatisticsInfo) {
//        startPage();
//        List<RzlViewStatisticsInfo> list = rzlViewStatisticsInfoService.queryList(rzlViewStatisticsInfo);
//        return getDataTable(list);
//    }

    @PreAuthorize("@ss.hasPermi('report:rzlViewStatisticsInfo:list')")
    @GetMapping("/list")
    public AjaxResult listByReport(HttpServletResponse response,RzlViewStatisticsInfo rzlViewStatisticsInfo) {
        ViewStatisticsInfoRep voList = rzlViewStatisticsInfoService.listByReport(rzlViewStatisticsInfo);
        return AjaxResult.success(voList);
    }

    
    
    
    
    
    /**
     * 导出专利有效-概况统计列表
     *
     * @param rzlViewStatisticsInfo 专利有效-概况统计
     * @date 2023-03-26
     */
    @PreAuthorize("@ss.hasPermi('report:rzlViewStatisticsInfo:export')")
    @Log(title = "专利有效-概况统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RzlViewStatisticsInfo rzlViewStatisticsInfo) {
    	
//      List<RzlViewStatisticsInfo> list = rzlViewStatisticsInfoService.queryList(rzlViewStatisticsInfo);
      ViewStatisticsInfoRep voList = rzlViewStatisticsInfoService.listByReport(rzlViewStatisticsInfo);
      
      response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setCharacterEncoding("utf-8");
      try {
		rzlViewStatisticsInfoExcel.doExport(response.getOutputStream(), rzlViewStatisticsInfo, voList);
	} catch (IOException e) {
		e.printStackTrace();
		logger.error("导出失败,"+e.getMessage(),e);
		throw new RuntimeException("导出失败，请联系管理员",e);
	}
      
    }

//    /**
//     * 获取动态表头
//     */
//    @GetMapping("/getHeader")
//    public AjaxResult getHeader(String year) {
//        UrPatentValidVo vo = new UrPatentValidVo();
//        if(StringUtils.isNotBlank(year)) {
//            vo.setEndDate(year);
//            int intYear = Integer.parseInt(year);
//            vo.setStartDate(String.valueOf(intYear - 9));
//        }
//        List<UrPatentValid> list = urPatentValidService.selectHeaderList(vo);
//        return AjaxResult.success(list);
//    }

    /**
     * 获取查询年列表
     */
    @GetMapping("/getYearList")
    public AjaxResult getYearList() {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        year = year - 10;
        List<String> list = rzlViewStatisticsInfoService.selectYearList(year);
        return AjaxResult.success(list);
    }




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


    /**
     * 新增专利有效-概况统计信息
     *
     * @param rzlViewStatisticsInfo 专利有效-概况统计
     * @return 结果
     * @date 2023-03-26
     */
    @PreAuthorize("@ss.hasPermi('report:rzlViewStatisticsInfo:add')")
    @Log(title = "专利有效-概况统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RzlViewStatisticsInfo rzlViewStatisticsInfo) {
        return toAjax(rzlViewStatisticsInfoService.save(rzlViewStatisticsInfo));
    }


    /**
     * 修改专利有效-概况统计信息
     *
     * @param rzlViewStatisticsInfo 专利有效-概况统计
     * @return 结果
     * @date 2023-03-26
     */
    @PreAuthorize("@ss.hasPermi('report:rzlViewStatisticsInfo:edit')")
    @Log(title = "专利有效-概况统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RzlViewStatisticsInfo rzlViewStatisticsInfo) {
        return toAjax(rzlViewStatisticsInfoService.updateById(rzlViewStatisticsInfo));
    }


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


}
