package com.xzzn.web.controller.ems;

import com.xzzn.common.annotation.Log;
import com.xzzn.common.core.controller.BaseController;
import com.xzzn.common.core.domain.AjaxResult;
import com.xzzn.common.core.page.TableDataInfo;
import com.xzzn.common.enums.BusinessType;
import com.xzzn.common.utils.StringUtils;
import com.xzzn.ems.domain.vo.AmmeterRevenueStatisListVo;
import com.xzzn.ems.domain.vo.AmmeterStatisListVo;
import com.xzzn.ems.domain.vo.ClusterStatisListVo;
import com.xzzn.ems.domain.vo.DateSearchRequest;
import com.xzzn.ems.domain.vo.StatisAmmeterDateRequest;
import com.xzzn.ems.domain.vo.StatisClusterDateRequest;
import com.xzzn.ems.domain.vo.WeatherSyncResultVo;
import com.xzzn.ems.service.IEmsStatsReportService;
import com.xzzn.ems.service.IEmsWeatherSyncService;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 单站监控-统计报表
 * 
 * @author xzzn
 */
@RestController
@RequestMapping("/ems/statsReport")
public class EmsStatisticalReportController extends BaseController
{

    @Autowired
    private IEmsStatsReportService ieEmsStatsReportService;
    @Autowired
    private IEmsWeatherSyncService iEmsWeatherSyncService;

    /**
     * 概率统计-收益指标查询
     */
    @GetMapping("/getRevenueData")
    public AjaxResult getRevenueData(DateSearchRequest requestVo)
    {
        return success(null);
    }

    /**
     * 概率统计-电量指标查询
     */
    @GetMapping("/getElectricData")
    public AjaxResult getElectricData(DateSearchRequest requestVo)
    {
        if (!StringUtils.isEmpty(requestVo.getSiteId())) {
            return success(ieEmsStatsReportService.getElectricDataResult(requestVo));
        } else {
            return error("站点id必传");
        }
    }

    /**
     * 概率统计-PCS曲线
     */
    @GetMapping("/getPCSData")
    public AjaxResult getPCSData(DateSearchRequest requestVo)
    {
        if (!StringUtils.isEmpty(requestVo.getSiteId())&&
                !StringUtils.isEmpty(requestVo.getDataType())) {
            return success(ieEmsStatsReportService.getPCSDataResult(requestVo));
        } else {
            return error("缺少必传项");
        }
    }

    /**
     * 概率统计-电池堆曲线
     */
    @GetMapping("/getStackData")
    public AjaxResult getStackData(DateSearchRequest requestVo)
    {
        if (!StringUtils.isEmpty(requestVo.getSiteId()) &&
                !StringUtils.isEmpty(requestVo.getDataType())) {
            return success(ieEmsStatsReportService.getStackDataResult(requestVo));
        } else {
            return error("缺少必传项");
        }
    }

    /**
     * 概率统计-电池温度等数据
     */
    @GetMapping("/getClusterData")
    public TableDataInfo getClusterData(StatisClusterDateRequest requestVo)
    {
        startPage();
        List<ClusterStatisListVo> dataList =  new ArrayList<>();
        if (!StringUtils.isEmpty(requestVo.getStackId()) &&
                !StringUtils.isEmpty(requestVo.getClusterId())) {
            dataList = ieEmsStatsReportService.getClusterDataResult(requestVo);
            return getDataTable(dataList);
        } else {
            return getDataTable(dataList);
        }
    }

    /**
     * 概率统计-获取站点下所有电表
     */
    @GetMapping("/getLoadNameList")
    public AjaxResult getLoadNameList(String siteId)
    {
        if (!StringUtils.isEmpty(siteId)) {
            return success(ieEmsStatsReportService.getLoadNameList(siteId));
        } else {
            return error("缺少必传项");
        }
    }

    /**
     * 概率统计-电表报表
     */
    @GetMapping("/getAmmeterData")
    public TableDataInfo getAmmeterData(StatisAmmeterDateRequest requestVo)
    {
        startPage();
        List<AmmeterStatisListVo> dataList = ieEmsStatsReportService.getAmmeterDataResult(requestVo);
        return getDataTable(dataList);
    }

    /**
     * 统计报表-电表报表（直接基于 ems_daily_energy_data）
     */
    @GetMapping("/getAmmeterDataFromDaily")
    public TableDataInfo getAmmeterDataFromDaily(StatisAmmeterDateRequest requestVo)
    {
        startPage();
        List<AmmeterStatisListVo> dataList = ieEmsStatsReportService.getAmmeterDataResult(requestVo);
        return getDataTable(dataList);
    }

    /**
     * 导出电表报表
     */
    @PreAuthorize("@ss.hasPermi('system:ammeterData:export')")
    @Log(title = "电表报表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAmmeterData")
    public void exportAmmeterData(HttpServletResponse response, StatisAmmeterDateRequest requestVo)
    {
        ieEmsStatsReportService.exportAmmeterData(response, requestVo);
    }

    /**
     * 导出电表报表（直接基于 ems_daily_energy_data）
     */
    @PreAuthorize("@ss.hasPermi('system:ammeterData:export')")
    @Log(title = "电表报表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAmmeterDataFromDaily")
    public void exportAmmeterDataFromDaily(HttpServletResponse response, StatisAmmeterDateRequest requestVo)
    {
        ieEmsStatsReportService.exportAmmeterData(response, requestVo);
    }

    /**
     * 概率统计-电表收益报表
     */
    @GetMapping("/getAmmeterRevenueData")
    public TableDataInfo getAmmeterRevenueData(StatisAmmeterDateRequest requestVo)
    {
        startPage();
        List<AmmeterRevenueStatisListVo> dataList = ieEmsStatsReportService.getAmmeterRevenueDataResult(requestVo);
        return getDataTable(dataList);
    }

    /**
     * 导出收益报表
     */
    @PreAuthorize("@ss.hasPermi('system:ammeterRevenueData:export')")
    @Log(title = "收益报表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportAmmeterRevenueData")
    public void exportAmmeterRevenueData(HttpServletResponse response, StatisAmmeterDateRequest requestVo)
    {
        ieEmsStatsReportService.exportAmmeterRevenueData(response, requestVo);
    }

    /**
     * 概率统计-功率曲线
     */
    @GetMapping("/getPowerData")
    public AjaxResult getPowerData(DateSearchRequest requestVo)
    {
        if (!StringUtils.isEmpty(requestVo.getSiteId())) {
            return success(ieEmsStatsReportService.getPowerDataList(requestVo));
        } else {
            return error("缺少必传项");
        }
    }

    /**
     * 手动触发天气同步
     */
    @PostMapping("/syncWeatherByDateRange")
    public AjaxResult syncWeatherByDateRange(StatisAmmeterDateRequest requestVo)
    {
        if (StringUtils.isEmpty(requestVo.getSiteId())
                || StringUtils.isEmpty(requestVo.getStartTime())
                || StringUtils.isEmpty(requestVo.getEndTime())) {
            return error("缺少必传项: siteId/startTime/endTime");
        }
        WeatherSyncResultVo resultVo = iEmsWeatherSyncService.syncWeatherByDateRange(requestVo);
        return success(resultVo);
    }

}
