package com.jiayue.ipfcst.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
import com.jiayue.ipfcst.service.PrValueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * pr值
 * @author fxy
 * @version 1.0
 * @since 2022/10/26 11:23
 */
@RestController
@Slf4j
@RequestMapping("prValue")
public class PrValueController {

  private final PrValueService prValueService;

  @Autowired
  public PrValueController(PrValueService prValueService) {
    this.prValueService = prValueService;
  }

  @GetMapping("/findPrValueData/{startTime}/{endTime}")
  public ResponseVO findPrValueData(@PathVariable Long startTime, @PathVariable Long endTime) {
    Map<String, Object> byRealTime = prValueService.getData(startTime, endTime);
    return ResponseVO.success(byRealTime);
  }

  /**
   * Pr值数据导出
   *
   * @param response
   */
  @RequestMapping(value = "/export")
  public void export(HttpServletResponse response, Long startTime, Long endTime) {
    try {
      Map<String, Object> maps = prValueService.getData(startTime, endTime);

      List<Map<String, BigDecimal>> dayLists = (List<Map<String, BigDecimal>>) maps.get("dayList");

      List<Map<String, BigDecimal>> monthLists = (List<Map<String, BigDecimal>>) maps.get("monthList");

      List<String> header = new ArrayList();
      header.add("时间");
      header.add("容量(MW)");
      header.add("日照小时数(h)");
      header.add("系统效率");
      header.add("理论发电量(MW)");
      header.add("实际发电量(MW)");
      header.add("pr值(%)");
      List<String> headerDay = new ArrayList();
      headerDay.add("时间");
      headerDay.add("容量(MW)");
      headerDay.add("日照小时数(h)");
      headerDay.add("系统效率");
      headerDay.add("理论发电量(MW)");
      headerDay.add("实际发电量(MW)");
      headerDay.add("pr值(%)");

      ExcelWriter writer = ExcelUtil.getWriter(true);
      writer.renameSheet(0, "日");

      List<String> rowHeather = CollUtil.newArrayList(header);
      List<String> rowHeatherDay = CollUtil.newArrayList(headerDay);
      List<List<String>> rowsDay = CollUtil.newArrayList();
      rowsDay.add(rowHeatherDay);
      for (Map<String, BigDecimal> dayList : dayLists) {
        List<String> rowContent = CollUtil.newArrayList();
        rowContent.add(dayList.get("time") + "");
        rowContent.add(dayList.get("capacity") + "");
        rowContent.add(dayList.get("hourDA") + "");
        rowContent.add(dayList.get("ultraShortTermAccuracy") + "");
        rowContent.add(dayList.get("theoreticalPowerGeneration") + "");
        rowContent.add(dayList.get("actualPowerGeneration") + "");
        rowContent.add(dayList.get("prValue") + "");
        rowsDay.add(rowContent);
      }

      writer.write(rowsDay, true);

      ExcelWriter monthSheet = writer.setSheet("月");

      List<List<String>> rowsMonth = CollUtil.newArrayList();
      rowsMonth.add(rowHeather);
      for (Map<String, BigDecimal> monthList : monthLists) {
        List<String> rowContent = CollUtil.newArrayList();
        rowContent.add(monthList.get("time") + "");
        rowContent.add(monthList.get("capacity") + "");
        rowContent.add(monthList.get("hourDA") + "");
        rowContent.add(monthList.get("ultraShortTermAccuracy") + "");
        rowContent.add(monthList.get("theoreticalPowerGeneration") + "");
        rowContent.add(monthList.get("actualPowerGeneration") + "");
        rowContent.add(monthList.get("prValue") + "");
        rowsMonth.add(rowContent);
      }

      monthSheet.write(rowsMonth, true);
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

      String fileName = "pr值信息" + simpleDateFormat.format(startTime) + "至" + simpleDateFormat.format(endTime) + ".xlsx";
      response.setContentType("application/x-msdownload;charset=utf-8");
      response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
      ServletOutputStream out = null;
      out = response.getOutputStream();
      writer.flush(out, true);
      // 关闭writer，释放内存
      writer.close();
      //此处记得关闭输出Servlet流
      IoUtil.close(out);
    } catch (IOException e) {
      e.printStackTrace();
    }

  }
}
