package com.ruoyi.report.controller;

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.NationalRankingUtils;
import com.ruoyi.NationalRankingUtils.Item;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.report.domain.RzlClassStatisticsInfo;
import com.ruoyi.report.domain.RzlNationalRankingInfo;
import com.ruoyi.report.service.IRzlClassStatisticsInfoService;
import com.ruoyi.report.service.IRzlNationalRankingInfoExcel;
import com.ruoyi.report.service.IRzlNationalRankingInfoService;

/**
 * 专利有效-全国排名Controller
 *
 * @author ruoyi
 * @date 2023-03-26
 */
@RestController
@RequestMapping("/report/rzlNationalRankingInfo")
public class RzlNationalRankingInfoController extends BaseController {

	@Autowired
	private RedisCache redisCache;
	private final IRzlNationalRankingInfoService rzlNationalRankingInfoService;

	@Autowired
	private IRzlNationalRankingInfoExcel rzlNationalRankingInfoExcel;
	@Autowired
	private RuoYiConfig ruoYiConfig;

	@Autowired
	private IRzlClassStatisticsInfoService rzlClassStatisticsInfoService;
	
	
	public RzlNationalRankingInfoController(IRzlNationalRankingInfoService rzlNationalRankingInfoService) {
		this.rzlNationalRankingInfoService = rzlNationalRankingInfoService;
	}

	/**
	 * 查询专利有效-全国排名列表
	 *
	 * @param rzlNationalRankingInfo 专利有效-全国排名
	 * @return 专利有效-全国排名集合
	 * @date 2023-03-26
	 */
	@PreAuthorize("@ss.hasPermi('report:rzlNationalRankingInfo:list')")
	@GetMapping("/list")
	public AjaxResult list(HttpServletResponse response,RzlNationalRankingInfo rzlNationalRankingInfo) {
//        startPage();
		if (StringUtils.isBlank(rzlNationalRankingInfo.getYear())
				|| StringUtils.isBlank(rzlNationalRankingInfo.getMonth())) {
			return AjaxResult.error("请指定数据年月");
		}
//        List<RzlNationalRankingInfo> list = rzlNationalRankingInfoService.queryList(new RzlNationalRankingInfo());
//        return getDataTable(list);
		String timeUpdate = rzlNationalRankingInfo.getYear() + rzlNationalRankingInfo.getMonth();
		String year=rzlNationalRankingInfo.getYear();
		String month=rzlNationalRankingInfo.getMonth();
		timeUpdate = NationalRankingUtils.getLastDayOfMonth(timeUpdate);
		String type = "act";
		
		

		List<Item> items = NationalRankingUtils.getRankingList(type, timeUpdate, null, redisCache, ruoYiConfig,year,month);
		List<RzlNationalRankingInfo> list = new ArrayList<RzlNationalRankingInfo>();
		for (Item item : items) {

			RzlNationalRankingInfo info = new RzlNationalRankingInfo();
			info.setRanking(item.ranking);
			info.setArea(item.province);
			
			info.setInventionPatentsUmber(item.number);
			list.add(info);

		}
		
		
//		OutputStream out=null;
//      try {
//			out=new FileOutputStream(new File("D:\\temp\\exp.xlsx"));
//			rzlNationalRankingInfoExcel.doExport(out, rzlNationalRankingInfo, 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);
	}

	/**
	 * 导出专利有效-全国排名列表
	 *
	 * @param rzlNationalRankingInfo 专利有效-全国排名
	 * @date 2023-03-26
	 */
	@PreAuthorize("@ss.hasPermi('report:rzlNationalRankingInfo:export')")
	@Log(title = "专利有效-全国排名", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, RzlNationalRankingInfo rzlNationalRankingInfo) {

//      startPage();
		if (StringUtils.isBlank(rzlNationalRankingInfo.getYear())
				|| StringUtils.isBlank(rzlNationalRankingInfo.getMonth())) {
//			return AjaxResult.error("请指定数据年月");
			throw new RuntimeException("请指定数据年月");
		}
//      List<RzlNationalRankingInfo> list = rzlNationalRankingInfoService.queryList(new RzlNationalRankingInfo());
//      return getDataTable(list);
		String timeUpdate = rzlNationalRankingInfo.getYear() + rzlNationalRankingInfo.getMonth();

		timeUpdate = NationalRankingUtils.getLastDayOfMonth(timeUpdate);
		String type = "act";
		String year=rzlNationalRankingInfo.getYear();
		String month=rzlNationalRankingInfo.getMonth();
		

		List<Item> items = NationalRankingUtils.getRankingList(type, timeUpdate, null, redisCache, ruoYiConfig,year,month);
		List<RzlNationalRankingInfo> list = new ArrayList<RzlNationalRankingInfo>();
		for (Item item : items) {

			RzlNationalRankingInfo info = new RzlNationalRankingInfo();
			info.setRanking(item.ranking);
			info.setArea(item.province);
			info.setInventionPatentsUmber(item.number);
			list.add(info);

		}
		try {
			response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
			response.setCharacterEncoding("utf-8");
			rzlNationalRankingInfoExcel.doExport(response.getOutputStream(), rzlNationalRankingInfo, 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-26
	 */
	@PreAuthorize("@ss.hasPermi('report:rzlNationalRankingInfo:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(rzlNationalRankingInfoService.getById(id));
	}

	/**
	 * 新增专利有效-全国排名信息
	 *
	 * @param rzlNationalRankingInfo 专利有效-全国排名
	 * @return 结果
	 * @date 2023-03-26
	 */
	@PreAuthorize("@ss.hasPermi('report:rzlNationalRankingInfo:add')")
	@Log(title = "专利有效-全国排名", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody RzlNationalRankingInfo rzlNationalRankingInfo) {
		return toAjax(rzlNationalRankingInfoService.save(rzlNationalRankingInfo));
	}

	/**
	 * 修改专利有效-全国排名信息
	 *
	 * @param rzlNationalRankingInfo 专利有效-全国排名
	 * @return 结果
	 * @date 2023-03-26
	 */
	@PreAuthorize("@ss.hasPermi('report:rzlNationalRankingInfo:edit')")
	@Log(title = "专利有效-全国排名", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody RzlNationalRankingInfo rzlNationalRankingInfo) {
		return toAjax(rzlNationalRankingInfoService.updateById(rzlNationalRankingInfo));
	}

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

}
