package com.ruoyi.business.controller;

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

import com.ruoyi.business.domain.HwScene;
import com.ruoyi.business.domain.VO.*;
import com.ruoyi.business.mapper.HwSceneMapper;
import com.ruoyi.business.service.IHwDeviceService;
import com.ruoyi.business.service.IHwElectronicFenceService;
import com.ruoyi.business.service.IHwSceneService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.business.domain.HwMonitorUnit;
import com.ruoyi.business.service.IHwMonitorUnitService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

import static com.ruoyi.common.core.utils.PageUtils.startPage;

@RestController
@RequestMapping("/monitorPlatform")
public class HwMonitorPlatformController extends BaseController {

    @Autowired
    private IHwMonitorUnitService hwMonitorUnitService;
    @Autowired
    private IHwDeviceService hwDeviceService;
    @Autowired
    private IHwSceneService hwSceneService;
    @Autowired
    private IHwElectronicFenceService hwElectronicFenceService;


    /**
     * 树状获取监控单元列表
     * @param hwMonitorUnits 监控单元对象
     * @return list
     * @throws
     * */
    @GetMapping("/treeList")
    public TableDataInfo treeList(List<HwMonitorUnit> hwMonitorUnits){
        List<HwMonitorUnit> list = hwMonitorUnitService.selectTreeList(hwMonitorUnits);
        return getDataTable(list);

    }
//    /**
//     *
//     * 选择场景
//     * @param sceneModeId 场景id
//     * @return
//     */
//
//    @PostMapping("/selectHwSceneBySceneModeId")
//    public TableDataInfo selectHwSceneBySceneModeId(Long sceneModeId){
//        List<HwScene> hwScenes = hwSceneService.selectHwSceneBySceneModeId(sceneModeId);
//        return getDataTable(hwScenes);
//    }
    /**
     * 查询3.	设备各类型占所有设备数量百分比饼状图（左下角）
     * */
    @GetMapping("/monitorUnitPercentage")
    public TableDataInfo monitorUnitPercentage(){
        List<DeviceModeVo> maps = hwDeviceService.selectDeviceModeNameVo();
        return getDataTable(maps);
    }
    //
    /**
     * 获取有开关属性的设备
     * @param  monitorUnitId 监控单元id
     * */
    @PostMapping("/selectDeviceByDeviceModeByMonitorUnitId")
    public Map<String, Map<String, Object>> selectDeviceByDeviceModeByMonitorUnitId(Long monitorUnitId){
        startPage();

        Map<String, Map<String, Object>> maps = hwDeviceService.selectDeviceByDeviceModeByMonitorUnitId(monitorUnitId);
        return maps;
    }

    /**
     * 获取直采变量的设备
     * @param monitorUnitId 监控单元id
     */
    @PostMapping("/selectDeviceModeFunctionByFunctionType")
    public Map<String, Map<String, Object>> selectDeviceModeFunctionByFunctionType(Long monitorUnitId){
        startPage();

        Map<String, Map<String, Object>> maps = hwDeviceService.selectDeviceModeFunctionByFunctionType(monitorUnitId);
        return maps;
    }
    //
    /**
     * 设备历史数据
     * @param  deviceId 设备id
     * */
    @PostMapping("/getHistoryData")
    public TableDataInfo getHistoryData(Long deviceId){
        startPage();
        List<Map<String, Object>> historyData = hwDeviceService.getHistoryData(deviceId);
        return getDataTable(historyData);
    }
    /**
     * 查询区域数量站场景区总数百分比
     * */
    @GetMapping("/monitorPercentage")
    public TableDataInfo monitorPercentage(){
        List<HwMonitorUnitVo> maps = hwMonitorUnitService.selectMonitorPercentage();
        return getDataTable(maps);
    }
    /**
     * 	展示总的设备数量和总的监控单元数量
     * */
    @GetMapping("/allNums")
    public AllNumsVo allNums(){
        AllNumsVo allNumsVos = hwMonitorUnitService.selectAllNums();

        return allNumsVos;
    }
    /**
     * 	按场景展示总的设备数量和总的监控单元数量
     * */
    @GetMapping("/sceneAllNums")
    public TableDataInfo sceneAllNums(){
        List<AllNumsVo> allNumsVos = hwMonitorUnitService.selectSenceAllNums();

        return getDataTable(allNumsVos);
    }
    /**
     * 分页获取最子集监控单元列表
     * */
    @PostMapping("/limitSubMonitorUnit")
    public TableDataInfo limitSubMonitorUnit(){
        startPage();
        List<HwMonitorUnit> hwMonitorUnits = hwMonitorUnitService.selectLimitSubMonitorUnit();
        return getDataTable(hwMonitorUnits);
    }
    /**
     * 监控单元报警统计
     */
    @GetMapping("/alarmStats")
    public TableDataInfo alarmStats(){
        List<AlarmStatsVo> alarmStatsVos = hwMonitorUnitService.selectAlarmStats();
        return getDataTable(alarmStatsVos);
    }
    /**
     * 获取最子级监控单元数量、设备数量和在线设备数量
     * @param sceneId 场景id
     * */
    @PostMapping("/SubDevice")
    public SubDeviceSumVo subDevice(Long sceneId){

        SubDeviceSumVo subDeviceSumVos = hwMonitorUnitService.selectSubDeviceSum(sceneId);

        return subDeviceSumVos;
    }
    /**
     * 按电子围栏定位显示其下设备数量，以及报警的设备数量。
     * @param  sceneId
     * */
    @PostMapping("/ElectronicNumVo")
    public TableDataInfo ElectronicNumVo(Long sceneId){

        List<EleMonitorDeviceSumVo> eleMonitorDeviceSumVos = hwElectronicFenceService.selectEleTypeNums(sceneId);
        return getDataTable(eleMonitorDeviceSumVos);
    }
    /**
     * 获取监控单元详细信息
     * @param monitorUnitId
     * */
    @PostMapping("/selectMonitorUnitSub")
    //ok
    public  HwMonitorUnit selectMonitorUnitSub(Long monitorUnitId){

        HwMonitorUnit hwMonitorUnits = hwMonitorUnitService.selectHwMonitorUnitByMonitorUnitId(monitorUnitId);

        return hwMonitorUnits;
    }
    /**
     * 2.	设备实时在运数量曲线图
     * @param beginTime 开始时间
     * @param endTime 结束时间
     * @param sceneId 场景id
     * */
    @PostMapping("/getDeviceOperations")
    public  int[] selectTdengine(String beginTime,String endTime,Long sceneId){
        int ints[] = hwMonitorUnitService.selectTdengine(beginTime,endTime,sceneId);
        return  ints;
    }
    /**
     * 设备汇总
     * */
    @PostMapping("/selectLatestDataByTags")
    public R<Map<String, Map<String, Object>>>selectLatestDataByTags(Long monitorUnitId){

        R<Map<String, Map<String, Object>>> mapR = hwMonitorUnitService.selectLatestDataByTags(monitorUnitId);
        return mapR;
    }
    /**
     * 场景选择
     * ，id=0 可以返回所有，其他根据id过滤，默认标识
     *
     * */
    @PostMapping("/SelectSecne")
    public TableDataInfo SelectSecneByUser(){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        Long tenantId = sysUser.getTenantId();
        List<HwScene> hwScenes = hwSceneService.selectHwSceneByTenantId(1l);
        if (hwScenes.size()!=0){
        hwScenes.get(0).setDefaultFlag("1");}
        return getDataTable(hwScenes);
    }
}
