package com.ruoyi.reportpub.controller;

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

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
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.constant.ApplicantType;
import com.ruoyi.common.constant.PatentType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.report.vo.ClassStatisticsInfoVo;
import com.ruoyi.reportpub.domain.RzgClassStatisticsInfo;
import com.ruoyi.reportpub.service.IRzgClassStatisticsInfoExcel;
import com.ruoyi.reportpub.service.IRzgClassStatisticsInfoService;

/**
 * 专利公开-分类统计Controller
 *
 * @author ruoyi
 * @date 2023-03-27
 */
@RestController
@RequestMapping("/reportpub/rzgClassStatisticsInfo")
public class RzgClassStatisticsInfoController extends BaseController {
    private final IRzgClassStatisticsInfoService rzgClassStatisticsInfoService;

    
    @Autowired
    private IRzgClassStatisticsInfoExcel rzgClassStatisticsInfoExcel;
    
    public RzgClassStatisticsInfoController(IRzgClassStatisticsInfoService rzgClassStatisticsInfoService) {
        this.rzgClassStatisticsInfoService = rzgClassStatisticsInfoService;
    }


    /**
     * 查询专利公开-分类统计列表
     *
     * @param rzgClassStatisticsInfo 专利公开-分类统计
     * @return 专利公开-分类统计集合
     * @date 2023-03-27
     */
    @PreAuthorize("@ss.hasPermi('reportpub:rzgClassStatisticsInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(RzgClassStatisticsInfo rzgClassStatisticsInfo) {

		   String createTimeBegin=rzgClassStatisticsInfo.getCreateTimeBegin();
		   String createTimeEnd=rzgClassStatisticsInfo.getCreateTimeEnd();
		   
		   if(StringUtils.isNotBlank(createTimeBegin) && StringUtils.isNotBlank(createTimeEnd)) {
			   rzgClassStatisticsInfo.setCountTimeStr(createTimeBegin+"至"+createTimeEnd);
		   }
		   if(StringUtils.isNotBlank(createTimeBegin)) {
			   rzgClassStatisticsInfo.setCreateTimeBegin(createTimeBegin.replaceAll("-", ""));
		   }
		   if(StringUtils.isNotBlank(createTimeEnd)) {
			   rzgClassStatisticsInfo.setCreateTimeEnd(createTimeEnd.replaceAll("-", ""));
		   }
		   if(rzgClassStatisticsInfo!=null) {
			   if(StringUtils.isNotBlank(rzgClassStatisticsInfo.getPatentTypes())) {
				   List<String>patentTypesList=new ArrayList<String>();
				   String[]patentTypes=rzgClassStatisticsInfo.getPatentTypes().split(",");
				   for(String pt:patentTypes) {
					   if(StringUtils.isNotBlank(pt)) {
						   PatentType patentType =PatentType.valueOf(pt.trim());
						   if(patentType!=null) {
//							   patentTypesList.add(patentType.getTypeId());
							   patentTypesList.add(patentType.getTypeName());
						   }
					   }
				   }
				   rzgClassStatisticsInfo.setPatentTypesList(patentTypesList);
			   }
			   
			   if(StringUtils.isNotBlank(rzgClassStatisticsInfo.getApplicantTypes())) {
				   List<String>patentTypesList=new ArrayList<String>();
				   String[]patentTypes=rzgClassStatisticsInfo.getApplicantTypes().split(",");
				   for(String pt:patentTypes) {
					   if(StringUtils.isNotBlank(pt)) {
						   ApplicantType at=ApplicantType.valueOf(pt);
						   if(at!=null) {
							   patentTypesList.add(at.getTypeCode());
//							   patentTypesList.add(at.getTypeName());
						   }
						  
					   }
				   }
				   rzgClassStatisticsInfo.setApplicantTypesList(patentTypesList);
			   }
		   }
		   
	        List<ClassStatisticsInfoVo>voList=rzgClassStatisticsInfoService.listByReport(rzgClassStatisticsInfo);
	        
	        
//			OutputStream out = null;
//			try {
//				out = new FileOutputStream(new File("D:\\temp\\exp.xlsx"));
//				rzgClassStatisticsInfoExcel.doExport(out, rzgClassStatisticsInfo, 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 getDataTable(voList);
	    
    }


    /**
     * 导出专利公开-分类统计列表
     *
     * @param rzgClassStatisticsInfo 专利公开-分类统计
     * @date 2023-03-27
     */
    @PreAuthorize("@ss.hasPermi('reportpub:rzgClassStatisticsInfo:export')")
    @Log(title = "专利公开-分类统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RzgClassStatisticsInfo rzgClassStatisticsInfo) {
    	
    	String createTimeBegin=rzgClassStatisticsInfo.getCreateTimeBegin();
		   String createTimeEnd=rzgClassStatisticsInfo.getCreateTimeEnd();
		   
		   if(StringUtils.isNotBlank(createTimeBegin) && StringUtils.isNotBlank(createTimeEnd)) {
			   rzgClassStatisticsInfo.setCountTimeStr(createTimeBegin+"至"+createTimeEnd);
		   }
		   if(StringUtils.isNotBlank(createTimeBegin)) {
			   rzgClassStatisticsInfo.setCreateTimeBegin(createTimeBegin.replaceAll("-", ""));
		   }
		   if(StringUtils.isNotBlank(createTimeEnd)) {
			   rzgClassStatisticsInfo.setCreateTimeEnd(createTimeEnd.replaceAll("-", ""));
		   }

		   
		   if(rzgClassStatisticsInfo!=null) {
			   if(StringUtils.isNotBlank(rzgClassStatisticsInfo.getPatentTypes())) {
				   List<String>patentTypesList=new ArrayList<String>();
				   String[]patentTypes=rzgClassStatisticsInfo.getPatentTypes().split(",");
				   for(String pt:patentTypes) {
					   if(StringUtils.isNotBlank(pt)) {
						   PatentType patentType =PatentType.valueOf(pt.trim());
						   if(patentType!=null) {
							   patentTypesList.add(patentType.getTypeId());
						   }
					   }
				   }
				   rzgClassStatisticsInfo.setPatentTypesList(patentTypesList);
			   }
			   
			   if(StringUtils.isNotBlank(rzgClassStatisticsInfo.getApplicantTypes())) {
				   List<String>patentTypesList=new ArrayList<String>();
				   String[]patentTypes=rzgClassStatisticsInfo.getApplicantTypes().split(",");
				   for(String pt:patentTypes) {
					   if(StringUtils.isNotBlank(pt)) {
						   ApplicantType at=ApplicantType.valueOf(pt);
						   if(at!=null) {
							   patentTypesList.add(at.getTypeCode());
						   }
						  
					   }
				   }
				   rzgClassStatisticsInfo.setApplicantTypesList(patentTypesList);
			   }
		   }
		   
	        List<ClassStatisticsInfoVo>voList=rzgClassStatisticsInfoService.listByReport(rzgClassStatisticsInfo);

	        
	        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
			response.setCharacterEncoding("utf-8");
	    	try {
	    		rzgClassStatisticsInfoExcel.doExport(response.getOutputStream(), rzgClassStatisticsInfo,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:rzgClassStatisticsInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(rzgClassStatisticsInfoService.getById(id));
    }


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


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


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


}
