package com.zc.business.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.zc.business.domain.DcWarning;
import com.zc.business.service.IDCPerceivedEventsWarningService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王思祥
 * @ClassName DCPerceivedEventsWarningController
 */
@RestController
@RequestMapping("/perceivedEvents/warning")

public class DCPerceivedEventsWarningController extends BaseController {
    @Autowired
    private IDCPerceivedEventsWarningService perceivedEventsWarningService;
    //查询预警表与历史表所有感知事件的数量
    @PostMapping("/warningTotal")
    public AjaxResult getPerceivedEventsWarning(){
        return AjaxResult.success(perceivedEventsWarningService.perceivedEventsWarningNum());
    }

    //感知事件类型
    @PostMapping("/evenType")
    public AjaxResult getEvenTypeList(){
        return AjaxResult.success(perceivedEventsWarningService.selectEventTypeList());
    }
    //感知事件详情
    @PostMapping("/getWarningById")
    public AjaxResult getWarningById(@RequestBody DcWarning dcWarning){
        String id = dcWarning.getId();
        if (id==null){
            return AjaxResult.error("参数错误");
        }
        return AjaxResult.success(perceivedEventsWarningService.selectWarningById(id));
    }
    //告警事件列表
    @PostMapping("/perceivedEventsList")
    public TableDataInfo getPerceivedEventsList(@RequestBody DcWarning dcWarning){
        startPage();
        List<DcWarning> list = perceivedEventsWarningService.selectPerceivedEventsList(dcWarning);
        return getDataTable(list);
    }
    //非机预警列表
    @PostMapping("/nonAutomaticWarningList")
    public TableDataInfo nonAutomaticWarningList(@RequestBody DcWarning dcWarning){
        startPage();
        List<DcWarning> list = perceivedEventsWarningService.selectNonAutomaticWarningList(dcWarning);
        return getDataTable(list);
    }
    //首页感知事件列表
    @PostMapping("/warningList")
    public AjaxResult getWarningList(@RequestBody DcWarning dcWarning){
        return  AjaxResult.success(perceivedEventsWarningService.selectWarningList(dcWarning));
    }
    //查询感知数量按照路段数量进行排名
    @PostMapping("/sectionPerceivedList")
    public AjaxResult getSectionPerceivedEventsList(){
        return AjaxResult.success(perceivedEventsWarningService.selectSectionPerceivedEventsList());
    }
    //查询感知数量按照路段进行区分
    @PostMapping("/sectionPerceivedNumber")
    public AjaxResult getSectionPerceivedNumber(){
        return AjaxResult.success(perceivedEventsWarningService.selectSectionPerceivedNumber());
    }
    //查询当前日,日累计感知事件,按照小时进行分组
    @PostMapping("/dailyCumulative")
    public AjaxResult getDailyCumulative(){
        return AjaxResult.success(perceivedEventsWarningService.selectDailyCumulative());
    }
    //查询当前月,月累计感知事件,按照小时段进行分组
    @PostMapping("/dailyCumulativeMonth")
    public AjaxResult getDailyCumulativeMonth(){
        return AjaxResult.success(perceivedEventsWarningService.selectDailyCumulativeMonth());
    }
    //按事件源统计感知事件统计信息
    @PostMapping("/warningSourceGroup")
    public AjaxResult getWarningSourceGroup(){
        HashMap<Object, Object> map = new HashMap<>();
        List<HashMap<String, Object>> mapList = perceivedEventsWarningService.selectWarningSourceGroup();
        String total = perceivedEventsWarningService.selectWarningSourceGroupCount();
        map.put("warningSourceList", mapList);
        map.put("total", total);
        return AjaxResult.success(map);
    }
    //当日感知事件的处置情况占比
    @PostMapping("/warningStateDay")
    public AjaxResult getWarningStateDay(){
        return AjaxResult.success(perceivedEventsWarningService.selectWarningStateDay());
    }
    //感知事件类型占比
    @PostMapping("/warningTypeDay")
    public AjaxResult getWarningTypeDay(){
        return AjaxResult.success(perceivedEventsWarningService.selectWarningTypeDay());
    }

    //某一路段某一时间段的感知事件趋势
    @PostMapping("/warningTrend")
    public AjaxResult getWarningTrend(@RequestBody DcWarning dcWarning){
        String type = dcWarning.getType();//类型
        String sectionId = dcWarning.getSectionId();//路段辖区id
        String direction = dcWarning.getDirection();//方向
        if (StringUtils.isBlank(type)||StringUtils.isBlank(sectionId)||StringUtils.isBlank(direction)){
            return AjaxResult.error("参数数据异常");
        }
        if (!"day".equals(type) && !"month".equals(type) && !"year".equals(type) && !"quarter".equals(type)) {
            return AjaxResult.error("参数错误");
        }
        if (type.equals("quarter")&&dcWarning.getQuarter()==null){
            return AjaxResult.error("参数错误");
        }
        if (!"quarter".equals(type)&&dcWarning.getCreateTime()==null){
            return AjaxResult.error("参数错误");
        }
        if (dcWarning.getCreateTime()!=null){
            dcWarning.setWarningTime(dcWarning.getCreateTime());
        }
        return AjaxResult.success(perceivedEventsWarningService.selectWarningTrend(dcWarning));
    }
    //某一路段某一时间段的感知事件类型占比
    @PostMapping("/warningSectionType")
    public AjaxResult getWarningSectionType(@RequestBody DcWarning dcWarning){
        HashMap<String, Object> map = new HashMap<>();
        String type = dcWarning.getType();//类型
        String sectionId = dcWarning.getSectionId();//路段辖区id
        String direction = dcWarning.getDirection();//方向
        if (StringUtils.isBlank(type)||StringUtils.isBlank(sectionId)||StringUtils.isBlank(direction)){
            return AjaxResult.error("参数数据异常");
        }
        if (!"day".equals(type) && !"month".equals(type) && !"year".equals(type) && !"quarter".equals(type)) {
            return AjaxResult.error("参数错误");
        }
        if (type.equals("quarter")&&dcWarning.getQuarter()==null){
            return AjaxResult.error("参数错误");
        }
        if (!"quarter".equals(type)&&dcWarning.getCreateTime()==null){
            return AjaxResult.error("参数错误");
        }
        if (dcWarning.getCreateTime()!=null){
            dcWarning.setWarningTime(dcWarning.getCreateTime());
        }
        if (type.equals("quarter")) {
            String total = perceivedEventsWarningService.selectWarningSectionTypeCountQuarter(dcWarning);
            map.put("total",total);
        } else {
            String total = perceivedEventsWarningService.selectWarningSectionTypeCount(dcWarning);
            map.put("total",total);
        }
        List<HashMap<String, Object>> mapList = perceivedEventsWarningService.selectWarningSectionType(dcWarning);
        map.put("warningTypeList",mapList);
        return AjaxResult.success(map);
    }
    //查询某个路段下的全部桩号
    @PostMapping("/sectionMarkNumber")
    public AjaxResult getSectionMark(@RequestBody DcWarning dcWarning){
        String sectionId = dcWarning.getSectionId();
        if (StringUtils.isBlank(sectionId)){
            return AjaxResult.error("参数异常");
        }
        String direction = dcWarning.getDirection();
        if (StringUtils.isBlank(direction)){
            return AjaxResult.error("参数异常");
        }
        if (dcWarning.getCreateTime()!=null){
            dcWarning.setWarningTime(dcWarning.getCreateTime());
        }
        String type = dcWarning.getType();
        if (!"day".equals(type) && !"month".equals(type) && !"year".equals(type) && !"quarter".equals(type)) {
            return AjaxResult.error("参数错误");
        }
        if (!"quarter".equals(type)&&dcWarning.getCreateTime()==null){
            return AjaxResult.error("参数错误");
        }
        if (type.equals("quarter")&&dcWarning.getQuarter()==null){
            return AjaxResult.error("参数错误");
        }
        return AjaxResult.success(perceivedEventsWarningService.selectSectionMarkOptimize(dcWarning));
    }
    //查询某个路段下的全部桩号
//    @PostMapping("/sectionMarkNumberOptimize")
//    public AjaxResult getSectionMarkOptimize(@RequestBody DcWarning dcWarning){
//        String sectionId = dcWarning.getSectionId();
//        if (StringUtils.isBlank(sectionId)){
//            return AjaxResult.error("参数异常");
//        }
//        String direction = dcWarning.getDirection();
//        if (StringUtils.isBlank(direction)){
//            return AjaxResult.error("参数异常");
//        }
//        return AjaxResult.success(perceivedEventsWarningService.selectSectionMarkOptimize(dcWarning));
//    }

    //预警事件,事件列表 查询状态为上报的感知事件
    @PostMapping("/warningEscalation")
    public TableDataInfo getWarningEscalation(@RequestBody DcWarning dcWarning){
        startPage();
        List<HashMap<String, Object>> list = perceivedEventsWarningService.selectWarningEscalation(dcWarning);
        return getDataTable(list);
    }
    //修改感知事件信息
    @PostMapping("/updateWarning")
    public AjaxResult updateWarning(@RequestBody DcWarning dcWarning){
        return toAjax(perceivedEventsWarningService.updateWarning(dcWarning));
    }

    //查询非机预警类型,按照时间与类型查询(传入时间类型type与时间warningTime)
    @PostMapping("/nonAutomaticWarningType")
    public AjaxResult nonAutomaticWarningType(@RequestBody DcWarning dcWarning){
        if (StringUtils.isBlank(dcWarning.getType())||dcWarning.getWarningTime()==null){
            return AjaxResult.error("参数错误");
        }
        return AjaxResult.success(perceivedEventsWarningService.selectTypeNonAutomaticWarning(dcWarning));
    }
    //查询非机预警,按照时间查询(传入时间类型type与时间warningTime)
    @PostMapping("/nonAutomaticWarningTimeType")
    public AjaxResult nonAutomaticWarningTimeType(@RequestBody DcWarning dcWarning){
        if (StringUtils.isBlank(dcWarning.getType())||dcWarning.getWarningTime()==null){
            return AjaxResult.error("参数错误");
        }
        return AjaxResult.success(perceivedEventsWarningService.selectNonAutomaticWarningTimeType(dcWarning));
    }
    //查询非机预警,按照时间查询收费站分组(传入时间类型type与时间warningTime)
    @PostMapping("/nonAutomaticWarningFacility")
    public AjaxResult nonAutomaticWarningFacility(@RequestBody DcWarning dcWarning){
        if (StringUtils.isBlank(dcWarning.getType())||dcWarning.getWarningTime()==null){
            return AjaxResult.error("参数错误");
        }
        return AjaxResult.success(perceivedEventsWarningService.selectNonAutomaticWarningFacility(dcWarning));
    }
}