package com.zcxk.charge.web.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.charge.api.dto.*;
import com.zcxk.charge.api.dto.query.OnlineMonitorDto;
import com.zcxk.charge.api.dto.query.OnlineMonitorPageDto;
import com.zcxk.charge.api.dto.query.PipeNetPageDto;
import com.zcxk.charge.api.enums.PipeNetEnum;
import com.zcxk.charge.api.vo.DeviceCountStatsVo;
import com.zcxk.charge.api.vo.OnlineDataVo;
import com.zcxk.charge.api.vo.PipeNetLayerVo;
import com.zcxk.charge.web.common.Pagination;
import com.zcxk.charge.web.service.OnlineMonitorService;
import com.zcxk.charge.web.service.ReportWaterPumpStateService;
import com.zcxk.charge.web.service.RevenueService;
import com.zcxk.charge.web.service.SceneService;
import com.zcxk.charge.api.vo.AlarmDetailsVo;
import com.zcxk.core.common.pojo.AjaxMessage;
import com.zcxk.core.common.util.BeanCopyUtils;
import com.zcxk.core.utils.DateUtil;
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 javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private SceneService sceneService;

    @Autowired
    private ReportWaterPumpStateService reportWaterPumpStateService;

    @RequestMapping(value="selectAlarmDetails" , method = RequestMethod.POST)
    @ApiOperation(value = "查询实时报警信息")
    public AjaxMessage selectAlarmDetails(@RequestBody @Valid OnlineMonitorDto dto){
        AlarmDetailsDto alarmDetailsDto=new AlarmDetailsDto();
        BeanCopyUtils.copyProperties(dto, alarmDetailsDto, AlarmDetailsDto.class);
        alarmDetailsDto.setSceneIds(sceneService.findByParentIdsLike(null));
        return AjaxMessage.success(onlineMonitorService.selectAlarmDetails(alarmDetailsDto));
    }

    @RequestMapping(value="selectAlarmCount" , method = RequestMethod.POST)
    @ApiOperation(value = "查询实时报警数量")
    public AjaxMessage selectAlarmCount(@RequestBody @Valid OnlineMonitorDto dto){
        AlarmDetailsDto alarmDetailsDto=new AlarmDetailsDto();
        alarmDetailsDto.setSceneTypeName(dto.getSceneTypeName());
        alarmDetailsDto.setSceneName(dto.getSceneName());
        alarmDetailsDto.setSceneIds(sceneService.findByParentIdsLike(null));
        return AjaxMessage.success( onlineMonitorService.selectAlarmCount(alarmDetailsDto));
    }

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

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

    @RequestMapping(value="selectPage" , method = RequestMethod.POST)
    @ApiOperation(value = "查询实时数据列表")
    public AjaxMessage selectPage(@RequestBody @Valid OnlineMonitorPageDto dto){
        OnlineDataDto onlineDataDto=new OnlineDataDto();
        onlineDataDto.setSceneTypeName(dto.getSceneTypeName());
        onlineDataDto.setSceneName(dto.getSceneName());
        onlineDataDto.setSceneIds(sceneService.findByParentIdsLike(null));
        IPage<OnlineDataDto> iPage = new Page<>(dto.getPageNum(), dto.getPageSize());
        return AjaxMessage.success(new Pagination<>(onlineMonitorService.selectPage(iPage, onlineDataDto)));
    }

    @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(DateUtil.formatDate(DateUtil.addDayOfDate(nowDate, -days), "yyyy-MM-dd HH:mm:ss"));
        queryDto.setEndTime(DateUtil.formatDate(nowDate, "yyyy-MM-dd HH:mm:ss"));
        return AjaxMessage.success(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") @RequestParam(required = false) Long sceneId,
            @ApiParam(value = "设备名称") @RequestParam(required = false) String deviceName){
        OnlineDataDto onlineDataDto=new OnlineDataDto();
        onlineDataDto.setSceneTypeName(sceneTypeName);
        onlineDataDto.setSceneId(sceneId);
        onlineDataDto.setDeviceName(deviceName);
        onlineDataDto.setSceneIds(sceneService.findByParentIdsLike(null));
        return AjaxMessage.success(onlineMonitorService.selectDeviceCount(onlineDataDto));
    }

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

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

    @RequestMapping(value="statsDeviceFlow" , method = RequestMethod.GET)
    @ApiOperation(value = "统计今日累计流量")
    public AjaxMessage statsDeviceFlow(
            @ApiParam(value = "场景id") @RequestParam(required = false) Long sceneId,
            @ApiParam(value = "设备名称") @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));
        return AjaxMessage.success(onlineMonitorService.statsDeviceFlow(onlineDataDto));
    }

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

    @RequestMapping(value="selectPipeNetPage" , method = RequestMethod.POST)
    @ApiOperation(value = "查询管网实时数据列表")
    public AjaxMessage selectPipeNetPage(@RequestBody @Valid PipeNetPageDto dto){
        OnlineDataDto onlineDataDto=new OnlineDataDto();
        onlineDataDto.setSceneTypeName(dto.getSceneTypeName());
        if(dto.getSceneTypeName().equals(PipeNetEnum.PRESSURE.getMessage())){
            onlineDataDto.setType(1);
        }else if(dto.getSceneTypeName().equals(PipeNetEnum.FLOW.getMessage())){
            onlineDataDto.setType(2);
        }else if(dto.getSceneTypeName().equals(PipeNetEnum.WATER_QUALITY.getMessage())){
            onlineDataDto.setType(3);
        }
        onlineDataDto.setSceneId(dto.getSceneId());
        onlineDataDto.setDeviceName(dto.getDeviceName());
        onlineDataDto.setSceneIds(sceneService.findByParentIdsLike(null));
        IPage<OnlineDataDto> iPage = new Page<>(dto.getPageNum(), dto.getPageSize());
        Pagination<OnlineDataVo> pages = new Pagination<>(onlineMonitorService.selectPipeNetPage(iPage, onlineDataDto));
        return AjaxMessage.success( pages);
    }

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

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

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

    @RequestMapping(value="selectPipeNetList" , method = RequestMethod.GET)
    @ApiOperation(value = "App-查询管网实时数据列表")
    public AjaxMessage selectPipeNetList(
            @ApiParam(value = "场景类型名称(压力、流量、水质)", required = true) @RequestParam String sceneTypeName,
            @ApiParam(value = "设备id") @RequestParam(required = false)Long deviceId,
            @ApiParam(value = "公司id") @RequestParam(required = false)Integer companyOrgId){
        OnlineDataDto onlineDataDto=new OnlineDataDto();
        onlineDataDto.setSceneTypeName(sceneTypeName);
        if(sceneTypeName.equals(PipeNetEnum.PRESSURE.getMessage())){
            onlineDataDto.setType(1);
        }else if(sceneTypeName.equals(PipeNetEnum.FLOW.getMessage())){
            onlineDataDto.setType(2);
        }else if(sceneTypeName.equals(PipeNetEnum.WATER_QUALITY.getMessage())){
            onlineDataDto.setType(3);
        }
        onlineDataDto.setDeviceId(deviceId);
        onlineDataDto.setCompanyOrgId(companyOrgId);
        onlineDataDto.setSceneIds(sceneService.findByParentIdsLike(null));
        return AjaxMessage.success(onlineMonitorService.selectPipeNetList(onlineDataDto));
    }
    @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 AjaxMessage.success( 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 AjaxMessage.success(reportWaterPumpStateService.findReportWaterPumpStateList(sceneId, sceneService.findByParentIdsLike(sceneId),days));
    }
}