package com.xzzn.web.controller.ems;

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.utils.StringUtils;
import com.xzzn.ems.domain.vo.BMSBatteryDataList;
import com.xzzn.ems.domain.vo.BatteryDataStatsListVo;
import com.xzzn.ems.domain.vo.DateSearchRequest;
import com.xzzn.ems.domain.vo.RunningGraphRequest;
import com.xzzn.ems.domain.vo.SiteBatteryDataList;
import com.xzzn.ems.domain.vo.SiteMonitorDataSaveRequest;
import com.xzzn.ems.domain.vo.SiteMonitorRuningInfoVo;
import com.xzzn.ems.service.IEmsDeviceSettingService;
import com.xzzn.ems.service.IEmsSiteService;
import com.xzzn.ems.service.IEmsStatsReportService;
import com.xzzn.ems.service.ISingleSiteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 *
 *  单站监控
 *
 */
@RestController
@RequestMapping("/ems/siteMonitor")
public class EmsSiteMonitorController extends BaseController{
    private static final Logger log = LoggerFactory.getLogger(EmsSiteMonitorController.class);
    private static final String RUNNING_GRAPH_CTRL_DEBUG = "RunningGraphCtrlDebug";

    @Autowired
    private ISingleSiteService iSingleSiteService;
    @Autowired
    private IEmsSiteService iEmsSiteService;
    @Autowired
    private IEmsStatsReportService iemsStatsReportService;
    @Autowired
    private IEmsDeviceSettingService iEmsDeviceSettingService;

    /**
     * 获取单站首页数据
     */
    @GetMapping("/homeView")
    public AjaxResult getSingleSiteViewInfo(@RequestParam String siteId)
    {
        return success(iSingleSiteService.getSiteMonitorDataVo(siteId));
    }

    /**
     * 获取单站首页总累计运行数据（基于日表）
     */
    @GetMapping("/homeTotalView")
    public AjaxResult getSingleSiteHomeTotalView(@RequestParam String siteId)
    {
        return success(iSingleSiteService.getSiteMonitorTotalDataVo(siteId));
    }

    /**
     * 单站监控-设备监控-实时运行头部数据
     */
    @GetMapping("/runningHeadInfo")
    public AjaxResult getRunningHeadInfo(@RequestParam String siteId)
    {
        return success(iSingleSiteService.getSiteRunningHeadInfo(siteId));
    }

    /**
     * 单站监控-设备监控-实时运行曲线图数据
     */
    @GetMapping("/runningGraph/storagePower")
    public AjaxResult getRunningGraphStorage(RunningGraphRequest request,
                                             @RequestParam(required = false) String startDate,
                                             @RequestParam(required = false) String endDate)
    {
        SiteMonitorRuningInfoVo data = iSingleSiteService.getRunningGraphStorage(request);
        int deviceCount = data == null || data.getPcsPowerList() == null ? 0 : data.getPcsPowerList().size();
        log.info("{} storage, siteId={}, rawStartDate={}, rawEndDate={}, bindStartDate={}, bindEndDate={}, deviceCount={}",
                RUNNING_GRAPH_CTRL_DEBUG,
                request == null ? null : request.getSiteId(),
                startDate,
                endDate,
                request == null ? null : request.getStartDate(),
                request == null ? null : request.getEndDate(),
                deviceCount);
        return success(data);
    }

    @GetMapping("/runningGraph/pcsMaxTemp")
    public AjaxResult getRunningGraphPcsMaxTemp(RunningGraphRequest request,
                                                @RequestParam(required = false) String startDate,
                                                @RequestParam(required = false) String endDate)
    {
        SiteMonitorRuningInfoVo data = iSingleSiteService.getRunningGraphPcsMaxTemp(request);
        int deviceCount = data == null || data.getPcsMaxTempList() == null ? 0 : data.getPcsMaxTempList().size();
        log.info("{} pcsMaxTemp, siteId={}, rawStartDate={}, rawEndDate={}, bindStartDate={}, bindEndDate={}, deviceCount={}",
                RUNNING_GRAPH_CTRL_DEBUG,
                request == null ? null : request.getSiteId(),
                startDate,
                endDate,
                request == null ? null : request.getStartDate(),
                request == null ? null : request.getEndDate(),
                deviceCount);
        return success(data);
    }

    @GetMapping("/runningGraph/batteryAveSoc")
    public AjaxResult getRunningGraphBatterySoc(RunningGraphRequest request,
                                                @RequestParam(required = false) String startDate,
                                                @RequestParam(required = false) String endDate)
    {
        SiteMonitorRuningInfoVo data = iSingleSiteService.getRunningGraphBatterySoc(request);
        int pointCount = data == null || data.getBatteryAveSOCList() == null ? 0 : data.getBatteryAveSOCList().size();
        log.info("{} batteryAveSoc, siteId={}, rawStartDate={}, rawEndDate={}, bindStartDate={}, bindEndDate={}, pointCount={}",
                RUNNING_GRAPH_CTRL_DEBUG,
                request == null ? null : request.getSiteId(),
                startDate,
                endDate,
                request == null ? null : request.getStartDate(),
                request == null ? null : request.getEndDate(),
                pointCount);
        return success(data);
    }

    @GetMapping("/runningGraph/batteryAveTemp")
    public AjaxResult getRunningGraphBatteryTemp(RunningGraphRequest request,
                                                 @RequestParam(required = false) String startDate,
                                                 @RequestParam(required = false) String endDate)
    {
        SiteMonitorRuningInfoVo data = iSingleSiteService.getRunningGraphBatteryTemp(request);
        int pointCount = data == null || data.getBatteryAveTempList() == null ? 0 : data.getBatteryAveTempList().size();
        log.info("{} batteryAveTemp, siteId={}, rawStartDate={}, rawEndDate={}, bindStartDate={}, bindEndDate={}, pointCount={}",
                RUNNING_GRAPH_CTRL_DEBUG,
                request == null ? null : request.getSiteId(),
                startDate,
                endDate,
                request == null ? null : request.getStartDate(),
                request == null ? null : request.getEndDate(),
                pointCount);
        return success(data);
    }

    /**
     * 单站监控-设备监控-PCS
     */
    @GetMapping("/getPcsDetailInfo")
    public AjaxResult getPcsDetailInfo(@RequestParam String siteId)
    {
        return success(iSingleSiteService.getPcsDetailInfo(siteId));
    }

    /**
     * 单站监控-设备监控-BMS总览
     */
    @GetMapping("/getBMSOverView")
    public AjaxResult getBMSOverView(@RequestParam String siteId)
    {
        return success(iSingleSiteService.getBMSOverView(siteId));
    }

    /**
     * 单站监控-设备监控-BMS电池簇
     */
    @GetMapping("/getBMSBatteryCluster")
    public AjaxResult getBMSBatteryCluster(@RequestParam String siteId)
    {
        return success(iSingleSiteService.getBMSBatteryCluster(siteId));
    }

    /**
     * 获取所有电池堆
     */
    @GetMapping("/getStackNameList")
    public AjaxResult getStackNameList(@RequestParam String siteId)
    {
        return success(iEmsSiteService.getAllStackInfo(siteId));
    }

    /**
     * 获取所有pcs
     */
    @GetMapping("/getPcsNameList")
    public AjaxResult getPcsNameList(@RequestParam String siteId)
    {
        return success(iEmsSiteService.getAllPcsInfo(siteId));
    }

    /**
     * 获取所有电池簇
     */
    @GetMapping("/getClusterNameList")
    public AjaxResult getClusterNameList(@RequestParam String siteId, @RequestParam String stackDeviceId)
    {
        return success(iEmsSiteService.getAllClusterInfo(siteId, stackDeviceId));
    }

    /**
     * 液冷设备参数
     */
    @GetMapping("/getCoolingDataList")
    public AjaxResult getCoolingDataList(@RequestParam String siteId)
    {
        return success(iSingleSiteService.getCoolingDataList(siteId));
    }

    /**
     * 获取电池簇下面的单体电池数据
     */
    @GetMapping("/getClusterDataInfoList")
    public TableDataInfo getClusterDataInfoList(@RequestParam String clusterDeviceId,@RequestParam String siteId,
                                                @RequestParam String stackDeviceId,@RequestParam String batteryId)
    {
        startPage();
        SiteBatteryDataList siteBatteryDataList = new SiteBatteryDataList();

        // 单体电池数据
        List<BatteryDataStatsListVo> List = iSingleSiteService.getClusterDataInfoList(clusterDeviceId,siteId,stackDeviceId,batteryId);
        // 对batteryList进行分页处理
        List<BatteryDataStatsListVo> batteryList = paginateList(List);
        siteBatteryDataList.setBatteryList(batteryList);

        // 封装分页信息
        TableDataInfo pageInfo = new TableDataInfo();
        pageInfo.setTotal(List.size());
        pageInfo.setRows(Arrays.asList(siteBatteryDataList));
        pageInfo.setCode(0);
        pageInfo.setMsg("查询成功");
        return pageInfo;
    }

    /**
     * 单个单体电池曲线图
     */
    @GetMapping("/getSingleBatteryData")
    public AjaxResult getSingleBatteryData(DateSearchRequest requestVo)
    {
        return success(iemsStatsReportService.getSingleBatteryData(requestVo));
    }

    /**
     * 电表数据
     */
    @GetMapping("/getAmmeterDataList")
    public AjaxResult getAmmeterDataList(@RequestParam String siteId)
    {
        return success(iSingleSiteService.getAmmeterDataList(siteId));
    }

    /**
     * 动环数据
     */
    @GetMapping("/getDhDataList")
    public AjaxResult getDhDataList(@RequestParam String siteId)
    {
        return success(iSingleSiteService.getDhDataList(siteId));
    }

    /**
     * 消防数据
     */
    @GetMapping("/getXfDataList")
    public AjaxResult getXfDataList(@RequestParam String siteId)
    {
        return success(iSingleSiteService.getXfDataList(siteId));
    }

    /**
     * EMS数据
     */
    @GetMapping("/getEmsDataList")
    public AjaxResult getEmsDataList(@RequestParam String siteId)
    {
        return success(iSingleSiteService.getEmsDataList(siteId));
    }

    /**
     * 单站监控-首页-点位展示
     * 储能功率、电网功率、负荷功率、光伏功率
     * SOC、SOH、电池平均温度
     */
    @GetMapping("/getPointData")
    public AjaxResult getPointData(DateSearchRequest requestVo)
    {
        if (!StringUtils.isEmpty(requestVo.getSiteId())) {
            return success(iSingleSiteService.getPointData(requestVo));
        } else {
            return error("缺少必传项");
        }
    }

    /**
     * 单站监控项目点位配置查询
     */
    @GetMapping("/getProjectPointMapping")
    public AjaxResult getProjectPointMapping(@RequestParam String siteId)
    {
        return success(iEmsDeviceSettingService.getSiteMonitorProjectPointMapping(siteId));
    }

    /**
     * 单站监控项目展示数据查询（配置字段 + 字段值）
     */
    @GetMapping("/getProjectDisplayData")
    public AjaxResult getProjectDisplayData(@RequestParam String siteId)
    {
        return success(iEmsDeviceSettingService.getSiteMonitorProjectDisplay(siteId));
    }

    /**
     * 单站监控项目展示数据写入
     */
    @PostMapping("/saveProjectDisplayData")
    public AjaxResult saveProjectDisplayData(@RequestBody SiteMonitorDataSaveRequest request)
    {
        return AjaxResult.success(iEmsDeviceSettingService.saveSiteMonitorProjectData(request, getUsername()));
    }
}
