package com.ruoyi.reportrzs.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.common.utils.DateUtils;
import com.ruoyi.report.vo.ClassStatisticsInfoVo;
import com.ruoyi.reportrzs.domain.RzsClassStatisticsInfo;
import com.ruoyi.reportrzs.service.IRzsClassStatisticsInfoExcel;
import com.ruoyi.reportrzs.service.IRzsClassStatisticsInfoService;

/**
 * 专利授权-分类统计Controller
 *
 * @author ruoyi
 * @date 2023-03-27
 */
@RestController
@RequestMapping("/reportrzs/rzsClassStatisticsInfo")
public class RzsClassStatisticsInfoController extends BaseController {
	private final IRzsClassStatisticsInfoService rzsClassStatisticsInfoService;

	@Autowired
	private  IRzsClassStatisticsInfoExcel rzsClassStatisticsInfoExcel;
	
	public RzsClassStatisticsInfoController(IRzsClassStatisticsInfoService rzsClassStatisticsInfoService) {
		this.rzsClassStatisticsInfoService = rzsClassStatisticsInfoService;
	}

	/**
	 * 查询专利授权-分类统计列表
	 *
	 * @param rzsClassStatisticsInfo 专利授权-分类统计
	 * @return 专利授权-分类统计集合
	 * @date 2023-03-27
	 */
	@PreAuthorize("@ss.hasPermi('reportrzs:rzsClassStatisticsInfo:list')")
	@GetMapping("/list")
	public TableDataInfo list(RzsClassStatisticsInfo rzsClassStatisticsInfo) {
//        startPage();
//        List<RzsClassStatisticsInfo> list = rzsClassStatisticsInfoService.queryList(rzsClassStatisticsInfo);
		if (rzsClassStatisticsInfo != null) {
			String createTimeBegin = rzsClassStatisticsInfo.getCreateTimeBegin();
			String createTimeEnd = rzsClassStatisticsInfo.getCreateTimeEnd();
			if (StringUtils.isNotBlank(createTimeBegin) && StringUtils.isNotBlank(createTimeEnd)
					&& createTimeBegin.trim().length() >= 6 && createTimeEnd.trim().length() >= 6
//    			&& createTimeBegin.trim().substring(0, 6).equals(createTimeEnd.trim().substring(0, 6))
			) {// 单选一个月
				rzsClassStatisticsInfo.setCountTimeStr(createTimeBegin+"至"+createTimeEnd);
				createTimeBegin=createTimeBegin.trim().replace("-", "");
				createTimeEnd=createTimeEnd.trim().replace("-", "");
//    		createTimeBegin=createTimeBegin.trim().substring(0, 6)+"00";
//    		createTimeEnd=createTimeEnd.trim().substring(0, 6)+"00";
				rzsClassStatisticsInfo.setCreateTimeBegin(createTimeBegin.trim().substring(0, 6));
				rzsClassStatisticsInfo.setCreateTimeEnd(createTimeEnd.trim().substring(0, 6));
				if(rzsClassStatisticsInfo.getCreateTimeBegin().equals(rzsClassStatisticsInfo.getCreateTimeEnd())) {
					rzsClassStatisticsInfo.setAuthInboundDate(DateUtils.getMaxdayInMonth(rzsClassStatisticsInfo.getCreateTimeEnd()));
					rzsClassStatisticsInfo.setCreateTimeBegin(null);
					rzsClassStatisticsInfo.setCreateTimeEnd(null);
				}else {
//					rzsClassStatisticsInfo.setCreateTimeBegin(rzsClassStatisticsInfo.getCreateTimeBegin()+"01");
					rzsClassStatisticsInfo.setCreateTimeBegin(rzsClassStatisticsInfo.getCreateTimeBegin()+"00");//20231007 要用 00 号
					rzsClassStatisticsInfo.setCreateTimeEnd(DateUtils.getMaxdayInMonth(rzsClassStatisticsInfo.getCreateTimeEnd()));
				}
			}
			

			if (StringUtils.isNotBlank(rzsClassStatisticsInfo.getPatentTypes())) {
				List<String> patentTypesList = new ArrayList<String>();
				String[] patentTypes = rzsClassStatisticsInfo.getPatentTypes().split(",");
				for (String pt : patentTypes) {
					if (StringUtils.isNotBlank(pt)) {
						PatentType patentType = PatentType.valueOf(pt.trim());
						if (patentType != null) {
							patentTypesList.add(patentType.getTypeName());
						}
					}
				}
				rzsClassStatisticsInfo.setPatentTypesList(patentTypesList);
			}

			if (StringUtils.isNotBlank(rzsClassStatisticsInfo.getApplicantTypes())) {
				List<String> patentTypesList = new ArrayList<String>();
				String[] patentTypes = rzsClassStatisticsInfo.getApplicantTypes().split(",");
				for (String pt : patentTypes) {
					if (StringUtils.isNotBlank(pt)) {
						ApplicantType at = ApplicantType.valueOf(pt);
						if (at != null) {
							patentTypesList.add(at.getTypeName());
						}

					}
				}
				rzsClassStatisticsInfo.setApplicantTypesList(patentTypesList);
			}
		}
		List<ClassStatisticsInfoVo> list = rzsClassStatisticsInfoService.listByReport(rzsClassStatisticsInfo);
		
		
//		OutputStream out=null;
//	      try {
//				out=new FileOutputStream(new File("D:\\temp\\exp.xlsx"));
//				rzsClassStatisticsInfoExcel.doExport(out, rzsClassStatisticsInfo, 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 getDataTable(list);
	}

	/**
	 * 导出专利授权-分类统计列表
	 *
	 * @param rzsClassStatisticsInfo 专利授权-分类统计
	 * @date 2023-03-27
	 */
	@PreAuthorize("@ss.hasPermi('reportrzs:rzsClassStatisticsInfo:export')")
	@Log(title = "专利授权-分类统计", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, RzsClassStatisticsInfo rzsClassStatisticsInfo) {
		

//      startPage();
//      List<RzsClassStatisticsInfo> list = rzsClassStatisticsInfoService.queryList(rzsClassStatisticsInfo);
		if (rzsClassStatisticsInfo != null) {
			String createTimeBegin = rzsClassStatisticsInfo.getCreateTimeBegin();
			String createTimeEnd = rzsClassStatisticsInfo.getCreateTimeEnd();
			if (StringUtils.isNotBlank(createTimeBegin) && StringUtils.isNotBlank(createTimeEnd)
					&& createTimeBegin.trim().length() >= 6 && createTimeEnd.trim().length() >= 6
//  			&& createTimeBegin.trim().substring(0, 6).equals(createTimeEnd.trim().substring(0, 6))
			) {// 单选一个月
				rzsClassStatisticsInfo.setCountTimeStr(createTimeBegin+"至"+createTimeEnd);
				createTimeBegin=createTimeBegin.trim().replace("-", "");
				createTimeEnd=createTimeEnd.trim().replace("-", "");
//  		createTimeBegin=createTimeBegin.trim().substring(0, 6)+"00";
//  		createTimeEnd=createTimeEnd.trim().substring(0, 6)+"00";
				rzsClassStatisticsInfo.setCreateTimeBegin(createTimeBegin.trim().substring(0, 6));
				rzsClassStatisticsInfo.setCreateTimeEnd(createTimeEnd.trim().substring(0, 6));
				if(rzsClassStatisticsInfo.getCreateTimeBegin().equals(rzsClassStatisticsInfo.getCreateTimeEnd())) {
					rzsClassStatisticsInfo.setAuthInboundDate(DateUtils.getMaxdayInMonth(rzsClassStatisticsInfo.getCreateTimeEnd()));
					rzsClassStatisticsInfo.setCreateTimeBegin(null);
					rzsClassStatisticsInfo.setCreateTimeEnd(null);
				}else {
					rzsClassStatisticsInfo.setCreateTimeBegin(rzsClassStatisticsInfo.getCreateTimeBegin()+"01");
					rzsClassStatisticsInfo.setCreateTimeEnd(DateUtils.getMaxdayInMonth(rzsClassStatisticsInfo.getCreateTimeEnd()));
				}
			}
			

			if (StringUtils.isNotBlank(rzsClassStatisticsInfo.getPatentTypes())) {
				List<String> patentTypesList = new ArrayList<String>();
				String[] patentTypes = rzsClassStatisticsInfo.getPatentTypes().split(",");
				for (String pt : patentTypes) {
					if (StringUtils.isNotBlank(pt)) {
						PatentType patentType = PatentType.valueOf(pt.trim());
						if (patentType != null) {
							patentTypesList.add(patentType.getTypeName());
						}
					}
				}
				rzsClassStatisticsInfo.setPatentTypesList(patentTypesList);
			}

			if (StringUtils.isNotBlank(rzsClassStatisticsInfo.getApplicantTypes())) {
				List<String> patentTypesList = new ArrayList<String>();
				String[] patentTypes = rzsClassStatisticsInfo.getApplicantTypes().split(",");
				for (String pt : patentTypes) {
					if (StringUtils.isNotBlank(pt)) {
						ApplicantType at = ApplicantType.valueOf(pt);
						if (at != null) {
							patentTypesList.add(at.getTypeName());
						}

					}
				}
				rzsClassStatisticsInfo.setApplicantTypesList(patentTypesList);
			}
		}
		List<ClassStatisticsInfoVo> list = rzsClassStatisticsInfoService.listByReport(rzsClassStatisticsInfo);
		
		
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setCharacterEncoding("utf-8");
		
		try {
			rzsClassStatisticsInfoExcel.doExport(response.getOutputStream(), rzsClassStatisticsInfo, list);
		} 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('reportrzs:rzsClassStatisticsInfo:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(rzsClassStatisticsInfoService.getById(id));
	}

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

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

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

}
