package com.zcxk.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.dto.*;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.Pagination;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.*;
import com.zcxk.util.DatesUtil;
import com.zcxk.vo.AlarmDetailsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 在线监测控制层
 *
 * @author yjy
 * @since 2020-12-2
 */
@RestController
@RequestMapping("/onlineMonitor")
@Api(tags = "在线监测")
public class OnlineMonitorController {
    /**
     * 服务对象
     */
    @Autowired
    private OnlineMonitorService onlineMonitorService;
    @Autowired
    private SceneService sceneService;
    @Autowired
    private RevenueService revenueService;

    @Autowired
    private ReportWaterPumpStateService reportWaterPumpStateService;

    @RequestMapping(value="selectAlarmDetails" , method = RequestMethod.GET)
    @ApiOperation(value = "查询实时报警信息")
    public AjaxMessage<List<AlarmDetailsDto>> selectAlarmDetails(
            @ApiParam(value = "一级场景类型名称", required = false) @RequestParam(required = false) String sceneTypeName,
            @ApiParam(value = "场景名称", required = false) @RequestParam(required = false) String sceneName,
            @ApiParam(value = "公司机构id", required = false)@RequestParam(required = false) Integer companyOrgId){
        AlarmDetailsDto alarmDetailsDto=new AlarmDetailsDto();
        alarmDetailsDto.setSceneTypeName(sceneTypeName);
        alarmDetailsDto.setSceneName(sceneName);
        alarmDetailsDto.setCompanyOrgId(companyOrgId);
        alarmDetailsDto.setSceneIds(sceneService.findByParentIdsLike(null));
        return new AjaxMessage<>(ResultStatus.OK, onlineMonitorService.selectAlarmDetails(alarmDetailsDto));
    }

    @RequestMapping(value="selectAlarmCount" , method = RequestMethod.GET)
    @ApiOperation(value = "查询实时报警数量")
    public AjaxMessage<List<Map<String, Object>>> selectAlarmCount(
            @ApiParam(value = "一级场景类型名称", required = true) @RequestParam String sceneTypeName,
            @ApiParam(value = "场景名称", required = false) @RequestParam(required = false) String sceneName){
        AlarmDetailsDto alarmDetailsDto=new AlarmDetailsDto();
        alarmDetailsDto.setSceneTypeName(sceneTypeName);
        alarmDetailsDto.setSceneName(sceneName);
        alarmDetailsDto.setSceneIds(sceneService.findByParentIdsLike(null));
        return new AjaxMessage<>(ResultStatus.OK, onlineMonitorService.selectAlarmCount(alarmDetailsDto));
    }

    @RequestMapping(value="selectMapSuspension" , method = RequestMethod.GET)
    @ApiOperation(value = "查询地图悬浮数据及用水量排行")
    public AjaxMessage<List<MonitorDataCollectDto>> selectMapSuspension(
            @ApiParam(value = "一级场景类型名称", required = false) @RequestParam(required = false) String sceneTypeName){
        MonitorDataCollectDto monitorDataCollectDto=new MonitorDataCollectDto();
        monitorDataCollectDto.setSceneTypeName(sceneTypeName);
        monitorDataCollectDto.setFlag(1);
        monitorDataCollectDto.setSceneIds(sceneService.findByParentIdsLike(null));
        List<MonitorDataCollectDto> result=onlineMonitorService.selectMapSuspension(monitorDataCollectDto);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    @RequestMapping(value="selectMapParam" , method = RequestMethod.GET)
    @ApiOperation(value = "查询地图参数数据")
    public AjaxMessage<List<MonitorDataCollectDto>> selectMapParam(
            @ApiParam(value = "一级场景id", required = true) @RequestParam Long sceneId){
        MonitorDataCollectDto monitorDataCollectDto=new MonitorDataCollectDto();
        monitorDataCollectDto.setSceneId(sceneId);
        monitorDataCollectDto.setFlag(1);
        monitorDataCollectDto.setSceneIds(sceneService.findByParentIdsLike(null));
        List<MonitorDataCollectDto> result=onlineMonitorService.selectMapParam(monitorDataCollectDto);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    @RequestMapping(value="selectPage" , method = RequestMethod.GET)
    @ApiOperation(value = "查询实时数据列表")
    public AjaxMessage<Pagination<OnlineDataDto>> selectPage(
            @ApiParam(value = "页数", required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "条数", required = true)@RequestParam Integer pageSize,
            @ApiParam(value = "一级场景类型名称", required = true) @RequestParam String sceneTypeName,
            @ApiParam(value = "场景名称", required = false) @RequestParam(required = false) String sceneName){
        OnlineDataDto onlineDataDto=new OnlineDataDto();
        onlineDataDto.setSceneTypeName(sceneTypeName);
        onlineDataDto.setSceneName(sceneName);
        onlineDataDto.setSceneIds(sceneService.findByParentIdsLike(null));
        IPage<OnlineDataDto> iPage = new Page<>(pageNum, pageSize);
        iPage = onlineMonitorService.selectPage(iPage, onlineDataDto);
        Pagination<OnlineDataDto> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK, pages);
    }

    @RequestMapping(value="selectAlarmStats" , method = RequestMethod.GET)
    @ApiOperation(value = "查询设备报警分析")
    public AjaxMessage<List<AlarmDetailsVo>> selectAlarmStats(
            @ApiParam(value = "一级场景id", required = true) @RequestParam Long sceneId,
            @ApiParam(value = "查询天数 1、3、7", required = true) @RequestParam Integer days){
        Date nowDate = new Date();
        AlarmDetailsQueryDto queryDto = new AlarmDetailsQueryDto();
        queryDto.setId(sceneId);
        queryDto.setSceneIds(sceneService.findByParentIdsLike(null));
        queryDto.setStartTime(DatesUtil.formatDate(DatesUtil.addDayOfDate(nowDate, -days), "yyyy-MM-dd HH:mm:ss"));
        queryDto.setEndTime(DatesUtil.formatDate(nowDate, "yyyy-MM-dd HH:mm:ss"));
        return new AjaxMessage<>(ResultStatus.OK, onlineMonitorService.selectAlarmStatsInfo(queryDto));
    }

    @RequestMapping(value="selectDeviceCount" , method = RequestMethod.GET)
    @ApiOperation(value = "按管网名称分类查询设备数量",notes = "sceneTypeName 为压力、水质、流量")
    public AjaxMessage<List<Map<String, Object>>> selectDeviceCount(
            @ApiParam(value = "场景类型名称", required = true) @RequestParam String sceneTypeName,
            @ApiParam(value = "场景id", required = false) @RequestParam(required = false) Long sceneId,
            @ApiParam(value = "设备名称", required = false) @RequestParam(required = false) String deviceName){
        OnlineDataDto onlineDataDto=new OnlineDataDto();
        onlineDataDto.setSceneTypeName(sceneTypeName);
        onlineDataDto.setSceneId(sceneId);
        onlineDataDto.setDeviceName(deviceName);
        onlineDataDto.setSceneIds(sceneService.findByParentIdsLike(null));
        return new AjaxMessage<>(ResultStatus.OK, onlineMonitorService.selectDeviceCount(onlineDataDto));
    }

    @RequestMapping(value="selectDeviceMapParam" , method = RequestMethod.GET)
    @ApiOperation(value = "查询设备地图参数数据")
    public AjaxMessage<List<MonitorDataCollectDto>> selectDeviceMapParam(
            @ApiParam(value = "一级场景id", required = true) @RequestParam Long deviceId){
        MonitorDataCollectDto monitorDataCollectDto=new MonitorDataCollectDto();
        monitorDataCollectDto.setDeviceId(deviceId);
        monitorDataCollectDto.setFlag(1);
        monitorDataCollectDto.setSceneIds(sceneService.findByParentIdsLike(null));
        List<MonitorDataCollectDto> result=onlineMonitorService.selectDeviceMapParam(monitorDataCollectDto);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    @RequestMapping(value="statsDeviceQualified" , method = RequestMethod.GET)
    @ApiOperation(value = "统计设备合格数据",notes = "sceneTypeName 为压力、水质")
    public AjaxMessage<List<DeviceCountStatsDto>> statsDeviceQualified(
            @ApiParam(value = "场景类型名称", required = false) @RequestParam(required = false) String sceneTypeName,
            @ApiParam(value = "场景id", required = false) @RequestParam(required = false) Long sceneId,
            @ApiParam(value = "设备名称", required = false) @RequestParam(required = false) String deviceName){
        OnlineDataDto onlineDataDto=new OnlineDataDto();
        onlineDataDto.setSceneTypeName(sceneTypeName);
        onlineDataDto.setSceneId(sceneId);
        onlineDataDto.setDeviceName(deviceName);
        onlineDataDto.setSceneIds(sceneService.findByParentIdsLike(null));
        List<DeviceCountStatsDto> result=onlineMonitorService.statsDeviceQualified(onlineDataDto);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    @RequestMapping(value="statsDeviceFlow" , method = RequestMethod.GET)
    @ApiOperation(value = "统计今日累计流量")
    public AjaxMessage<List<MonitorDataCollectDto>> statsDeviceFlow(
            @ApiParam(value = "场景id", required = false) @RequestParam(required = false) Long sceneId,
            @ApiParam(value = "设备名称", required = false) @RequestParam(required = false) String deviceName){
        OnlineDataDto onlineDataDto=new OnlineDataDto();
        onlineDataDto.setSceneTypeName("流量");
        onlineDataDto.setType(4);
        onlineDataDto.setSceneId(sceneId);
        onlineDataDto.setDeviceName(deviceName);
        onlineDataDto.setSceneIds(sceneService.findByParentIdsLike(null));
        List<MonitorDataCollectDto> result=onlineMonitorService.statsDeviceFlow(onlineDataDto);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    @RequestMapping(value="statsDeviceParmCount" , method = RequestMethod.GET)
    @ApiOperation(value = "统计压力、瞬时流量的分布区间")
    public AjaxMessage<DistributeIntervalDto> statsDeviceParmCount(
            @ApiParam(value = "场景类型名称", required = true) @RequestParam String sceneTypeName,
            @ApiParam(value = "场景id", required = false) @RequestParam(required = false) Long sceneId,
            @ApiParam(value = "设备名称", required = false) @RequestParam(required = false) String deviceName){
        OnlineDataDto onlineDataDto=new OnlineDataDto();
        onlineDataDto.setSceneTypeName(sceneTypeName);
        if(sceneTypeName.equals("压力")){
            onlineDataDto.setType(1);
        }else if(sceneTypeName.equals("流量")){
            onlineDataDto.setType(5);
        }
        onlineDataDto.setSceneId(sceneId);
        onlineDataDto.setDeviceName(deviceName);
        onlineDataDto.setSceneIds(sceneService.findByParentIdsLike(null));
        DistributeIntervalDto result=onlineMonitorService.statsDeviceParmCount(onlineDataDto);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    @RequestMapping(value="selectPipeNetPage" , method = RequestMethod.GET)
    @ApiOperation(value = "查询管网实时数据列表")
    public AjaxMessage<Pagination<OnlineDataDto>> selectPipeNetPage(
            @ApiParam(value = "页数", required = true)@RequestParam Integer pageNum,
            @ApiParam(value = "条数", required = true)@RequestParam Integer pageSize,
            @ApiParam(value = "场景类型名称(压力、流量、水质)", required = true) @RequestParam String sceneTypeName,
            @ApiParam(value = "场景id", required = false) @RequestParam(required = false) Long sceneId,
            @ApiParam(value = "设备名称", required = false) @RequestParam(required = false) String deviceName){
        OnlineDataDto onlineDataDto=new OnlineDataDto();
        onlineDataDto.setSceneTypeName(sceneTypeName);
        if(sceneTypeName.equals("压力")){
            onlineDataDto.setType(1);
        }else if(sceneTypeName.equals("流量")){
            onlineDataDto.setType(2);
        }else if(sceneTypeName.equals("水质")){
            onlineDataDto.setType(3);
        }
        onlineDataDto.setSceneId(sceneId);
        onlineDataDto.setDeviceName(deviceName);
        onlineDataDto.setSceneIds(sceneService.findByParentIdsLike(null));
        IPage<OnlineDataDto> iPage = new Page<>(pageNum, pageSize);
        iPage = onlineMonitorService.selectPipeNetPage(iPage, onlineDataDto);
        Pagination<OnlineDataDto> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK, pages);
    }

    @RequestMapping(value="selectPipeNetLayer" , method = RequestMethod.GET)
    @ApiOperation(value = "查询管网地图图层及设备")
    public AjaxMessage<List<PipeNetLayerDto>> selectPipeNetLayer(
            @ApiParam(value = "场景类型名称", required = false) @RequestParam(required = false) String sceneTypeName){
        OnlineDataDto onlineDataDto=new OnlineDataDto();
        onlineDataDto.setSceneTypeName(sceneTypeName);
        onlineDataDto.setFlag(1);
        onlineDataDto.setSceneIds(sceneService.findByParentIdsLike(null));
        List<PipeNetLayerDto> result=onlineMonitorService.selectPipeNetLayer(onlineDataDto);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    @RequestMapping(value="statsDispatchSummary" , method = RequestMethod.GET)
    @ApiOperation(value = "统计从水源地到水龙头汇总数据")
    public AjaxMessage<DispatchSummaryDto> statsDispatchSummary(){
        MonitorDataCollectDto monitorDataCollectDto=new MonitorDataCollectDto();
        monitorDataCollectDto.setSceneIds(sceneService.findByParentIdsLike(null));
        DispatchSummaryDto result=onlineMonitorService.statsDispatchSummary(monitorDataCollectDto);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    @RequestMapping(value="statsPipeNetQualified" , method = RequestMethod.GET)
    @ApiOperation(value = "App-统计管网设备合格数据",notes = "sceneTypeName 为压力、水质、流量")
    public AjaxMessage<List<DeviceCountStatsDto>> statsPipeNetQualified(
            @ApiParam(value = "场景类型名称", required = false) @RequestParam(required = false) String sceneTypeName){
        OnlineDataDto onlineDataDto=new OnlineDataDto();
        onlineDataDto.setSceneTypeName(sceneTypeName);
        onlineDataDto.setFlag(1);
        onlineDataDto.setSceneIds(sceneService.findByParentIdsLike(null));
        List<DeviceCountStatsDto> result=onlineMonitorService.statsDeviceQualified(onlineDataDto);
        for(DeviceCountStatsDto item : result){
            if(item.getType().equals("流量"))
                continue;
            onlineDataDto.setFlag(null);
            onlineDataDto.setSceneTypeName(item.getType());
            List<DeviceCountStatsDto> itemResult=onlineMonitorService.statsDeviceQualified(onlineDataDto);
            if(itemResult.size()>0){
                if(item.getType().equals("压力")){
                    DeviceCountStatsDto pressQualified=itemResult.get(0).getParmQualified().size()>0?itemResult.get(0).getParmQualified().get(0):null;
                    item.setQualifiedRate(pressQualified==null?100:(double)pressQualified.getNormalCount()/itemResult.get(0).getTotalCount()*100);
                }else if(item.getType().equals("水质")){
                    item.setQualifiedRate((double)itemResult.get(0).getNormalCount()/itemResult.get(0).getTotalCount()*100);
                }
            }
        }
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    @RequestMapping(value="selectList" , method = RequestMethod.GET)
    @ApiOperation(value = "App-查询场景实时数据列表")
    public AjaxMessage<List<OnlineDataDto>> selectList(
            @ApiParam(value = "一级场景类型名称", required = true) @RequestParam String sceneTypeName,
            @ApiParam(value = "场景id", required = false) @RequestParam(required = false)Long sceneId,
            @ApiParam(value = "公司id", required = false) @RequestParam(required = false)Integer companyOrgId){
        OnlineDataDto onlineDataDto=new OnlineDataDto();
        onlineDataDto.setSceneTypeName(sceneTypeName);
        onlineDataDto.setSceneId(sceneId);
        onlineDataDto.setCompanyOrgId(companyOrgId);
        onlineDataDto.setSceneIds(sceneService.findByParentIdsLike(null));
        List<OnlineDataDto> result = onlineMonitorService.selectList(onlineDataDto);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    @RequestMapping(value="selectPipeNetList" , method = RequestMethod.GET)
    @ApiOperation(value = "App-查询管网实时数据列表")
    public AjaxMessage<List<OnlineDataDto>> selectPipeNetList(
            @ApiParam(value = "场景类型名称(压力、流量、水质)", required = true) @RequestParam String sceneTypeName,
            @ApiParam(value = "设备id", required = false) @RequestParam(required = false)Long deviceId,
            @ApiParam(value = "公司id", required = false) @RequestParam(required = false)Integer companyOrgId){
        OnlineDataDto onlineDataDto=new OnlineDataDto();
        onlineDataDto.setSceneTypeName(sceneTypeName);
        if(sceneTypeName.equals("压力")){
            onlineDataDto.setType(1);
        }else if(sceneTypeName.equals("流量")){
            onlineDataDto.setType(2);
        }else if(sceneTypeName.equals("水质")){
            onlineDataDto.setType(3);
        }
        onlineDataDto.setDeviceId(deviceId);
        onlineDataDto.setCompanyOrgId(companyOrgId);
        onlineDataDto.setSceneIds(sceneService.findByParentIdsLike(null));
        List<OnlineDataDto> result = onlineMonitorService.selectPipeNetList(onlineDataDto);
        return new AjaxMessage<>(ResultStatus.OK, result);
    }
    @RequestMapping(value="selectStateCount" , method = RequestMethod.GET)
    @ApiOperation(value = "查询场景实时状态数量")
    public AjaxMessage<List<Map<String, Object>>> selectStatusCount(
            @ApiParam(value = "一级场景类型名称", required = true) @RequestParam String sceneTypeName){
        OnlineDataDto onlineDataDto=new OnlineDataDto();
        onlineDataDto.setSceneTypeName(sceneTypeName);
        onlineDataDto.setFlag(1);
        onlineDataDto.setSceneIds(sceneService.findByParentIdsLike(null));
        return new AjaxMessage<>(ResultStatus.OK, onlineMonitorService.selectStateCount(onlineDataDto));
    }

    @RequestMapping(value="selectWaterPumpSate" , method = RequestMethod.GET)
    @ApiOperation(value = "查询泵站状态数据")
    public AjaxMessage selectWaterPumpSate(
            @ApiParam(value = "一级场景id", required = true) @RequestParam Long sceneId,
            @ApiParam(value = "查询天数 1、3、7", required = true) @RequestParam Integer days){
        return new AjaxMessage<>(ResultStatus.OK, reportWaterPumpStateService.findReportWaterPumpStateList(sceneId, sceneService.findByParentIdsLike(sceneId),days));
    }
}