package com.hw.ems.report.controller;

import com.hw.common.core.utils.SpringUtils;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.core.web.page.TableDataInfo;
import com.hw.ems.base.domain.BaseLineLoss;
import com.hw.ems.base.utils.ExportExcelUtil;
import com.hw.ems.report.domain.ExportReport;
import com.hw.ems.report.service.IDataAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.hw.common.core.utils.poi.ExcelUtil.findConsecutiveIndices;

/**
 * 数据分析Controller
 *
 * @author YinQ
 * @date 2023-04-05
 */
@Controller
@RequestMapping("/report/analysis")
public class dataAnalysisController extends BaseController
{
    private String prefix = "report/dataAnalysis";

    @Autowired
    private IDataAnalysisService dataAnalysisService;

    /**
     * 环比分析
     * @return
     */
    @GetMapping("/momAnalysis/{energyType}")
    public String momAnalysis(@PathVariable("energyType") Long energyType, ModelMap mmap)
    {
        mmap.put("energyType", energyType);
        return prefix + "/momAnalysis";
    }


    /**
     * 环比分析返回数据
     * @return
     */
    @ResponseBody
    @GetMapping("/energyMomAnalysisData")
    public AjaxResult energyMomAnalysisData(@RequestParam(required = false) Map paramMap){
        Map<String, Object> result = dataAnalysisService.energyMomAnalysisData(paramMap);
        return AjaxResult.success(result);
    }

    /**
     * 同比分析
     * @return
     */
    @GetMapping("/yoyAnalysis/{energyType}")
    public String yoyAnalysis(@PathVariable("energyType") Long energyType, ModelMap mmap)
    {
        mmap.put("energyType", energyType);
        return prefix + "/yoyAnalysis";
    }

    /**
     * 同比分析返回数据
     * @return
     */
    @ResponseBody
    @GetMapping("/energyYoyAnalysisData")
    public AjaxResult energyYoyAnalysisData(@RequestParam(required = false) Map paramMap){
        Map<String, Object> result = dataAnalysisService.energyYoyAnalysisData(paramMap);
        return AjaxResult.success(result);
    }

    /**
     * 线损分析
     * @return
     */
    @GetMapping("/lineLossAnalysis/{energyType}")
    public String lineLossAnalysis(@PathVariable("energyType") Long energyType, ModelMap mmap)
    {
        mmap.put("energyType", energyType);
        return prefix + "/lineLossAnalysis";
    }

    /**
     * 线损分析返回数据
     * @return
     */
    //todo 原方法无法接收数据
//    @ResponseBody
//    @PostMapping("/lineLossAnalysisData")
//    public TableDataInfo lineLossAnalysisData(@RequestBody BaseLineLoss baseLineLoss){
//        System.out.println(baseLineLoss.getParams());
//        List<Map<String, String>> result = dataAnalysisService.lineLossAnalysisData(baseLineLoss);
//        return getDataTable(result);
//    }
    @ResponseBody
    @PostMapping("/lineLossAnalysisData")
    public TableDataInfo lineLossAnalysisData(@RequestBody Map<String, String> map){
        BaseLineLoss baseLineLoss = new BaseLineLoss();
        String beginTime = map.get("beginCollectTime");
        String endTime = map.get("endCollectTime");
        baseLineLoss.getParams().put("beginCollectTime", beginTime);
        baseLineLoss.getParams().put("endCollectTime", endTime);
        List<Map<String, String>> result = dataAnalysisService.lineLossAnalysisData(baseLineLoss);
        return getDataTable(result);
    }

    /**
     * 线损分析详情页面
     * @return
     */
    @GetMapping("/lineLossAnalysisDetails")
    public String lineLossAnalysisDetails(ModelMap mmap)
    {
//        mmap.put("energyType", energyType);
        return prefix + "/lineLossAnalysisDetails";
    }

    /**
     * 能耗统计
     * @return
     */
    @GetMapping("/energyStatistics/{energyType}")
    public String energyStatistics(@PathVariable("energyType") Long energyType, ModelMap mmap)
    {
        mmap.put("energyType", energyType);
        return prefix + "/energyStatistics";
    }

    /**
     * 能耗统计返回数据
     * @param paramMap
     * @return
     */
    @PostMapping("/energyStatisticsData")
    @ResponseBody
//    public TableDataInfo energyStatisticsData(@RequestParam(required = false) Map paramMap)
    public TableDataInfo energyStatisticsData(@RequestBody Map paramMap)
    {
        List<Map<String, String>> list = dataAnalysisService.energyStatisticsData(paramMap);
        return getDataTable(list);
    }

    /**
     * 线损分析详情页面返回数据
     * @return
     */
    @ResponseBody
    @PostMapping("/lineLossAnalysisDetailsData")
    public TableDataInfo lineLossAnalysisDetailsData(@RequestBody BaseLineLoss baseLineLoss){
        List<Map<String, String>> result = dataAnalysisService.lineLossAnalysisDetailsData(baseLineLoss);
        return getDataTable(result);
    }

    /**
     * 线损分析详情页面图表
     * @param baseLineLoss
     * @return
     */
    @ResponseBody
    @PostMapping("/lineLossAnalysisDetailsCharts")
    public AjaxResult lineLossAnalysisDetailsCharts(BaseLineLoss baseLineLoss){
        Map<String, Object> result = dataAnalysisService.lineLossAnalysisDetailsCharts(baseLineLoss);
        return AjaxResult.success(result);
    }


    /**
     * 层级导出报表
     * @param paramMap
     * @return
     */
    @PostMapping("/classificationExportReport")
    @ResponseBody
    public TableDataInfo classificationExportReport(@RequestParam(required = false) Map paramMap)
    {
        List<ExportReport> list = dataAnalysisService.classificationExportReport(paramMap);
        return getDataTable(list);
    }

    @PostMapping("/classificationExport")
    @ResponseBody
    public void classificationExport(HttpServletResponse response, @RequestParam(required = false) Map paramMap)
    {
        List<ExportReport> list = dataAnalysisService.classificationExportReport(paramMap);

        List<ExportReport> originalList = SpringUtils.deepCopy(list);

        try {
            ArrayList<String> oneList = new ArrayList<>();
            ArrayList<String> twoList = new ArrayList<>();
            ArrayList<String> threeList = new ArrayList<>();
            ArrayList<String> fourList = new ArrayList<>();
            ArrayList<String> fiveList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                oneList.add(list.get(i).getOneExpend() == null ? "" : list.get(i).getOneExpend().toPlainString());
                twoList.add(list.get(i).getTwoExpend() == null ? "" : list.get(i).getTwoExpend().toPlainString());
                threeList.add(list.get(i).getThreeExpend() == null ? "" : list.get(i).getThreeExpend().toPlainString());
                fourList.add(list.get(i).getFourExpend() == null ? "" : list.get(i).getFourExpend().toPlainString());
                fiveList.add(list.get(i).getFiveExpend() == null ? "" : list.get(i).getFiveExpend().toPlainString());
            }
            List<int[]> oneListIndices = findConsecutiveIndices(oneList);
            for (int[] consecutiveIndex : oneListIndices) {
                for (int i = consecutiveIndex[0] + 1; i < consecutiveIndex[1] + 1; i++) {
                    list.get(i).setOneExpend(null);
                }
            }
            List<int[]> twoListIndices = findConsecutiveIndices(twoList);
            for (int[] consecutiveIndex : twoListIndices) {
                for (int i = consecutiveIndex[0] + 1; i < consecutiveIndex[1] + 1; i++) {
                    list.get(i).setTwoExpend(null);
                }
            }
            List<int[]> threeListIndices = findConsecutiveIndices(threeList);
            for (int[] consecutiveIndex : threeListIndices) {
                for (int i = consecutiveIndex[0] + 1; i < consecutiveIndex[1] + 1; i++) {
                    list.get(i).setThreeExpend(null);
                }
            }
            List<int[]> fourListIndices = findConsecutiveIndices(fourList);
            for (int[] consecutiveIndex : fourListIndices) {
                if(consecutiveIndex[1] - consecutiveIndex[0] < 2){
                    continue;
                }
                for (int i = consecutiveIndex[0] + 1; i < consecutiveIndex[1] + 1; i++) {
                    list.get(i).setFourExpend(null);
                }
            }
            List<int[]> fiveListIndices = findConsecutiveIndices(fiveList);
            for (int[] consecutiveIndex : fiveListIndices) {
                if(consecutiveIndex[1] - consecutiveIndex[0] < 2){
                    continue;
                }
                for (int i = consecutiveIndex[0] + 1; i < consecutiveIndex[1] + 1; i++) {
                    list.get(i).setFiveExpend(null);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ExportExcelUtil<ExportReport> util = new ExportExcelUtil<ExportReport>(ExportReport.class);
        util.exportExcel(response, list, "层级导出报表", originalList);
    }

}
