package com.casic.missiles.modular.system.controller;

import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.core.base.response.ResponseData;
import com.casic.missiles.core.common.service.ICommonPermissionService;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.modular.system.dto.AlarmNowView;
import com.casic.missiles.modular.system.service.IAlarmNowViewService;
import com.casic.missiles.modular.system.service.IStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@RequestMapping(value = "/statics")
public class StaticsController extends BaseController {
    @Autowired
    private IStatisticsService statisticsService;
    @Autowired
    private IAlarmNowViewService alarmNowViewService;
    @Autowired
    private ICommonPermissionService permissionService;

    /**
     * 根据权属单位统计闸井数量
     * @return
     */
    @RequestMapping(value = "/wellStaticsByDept")
    @ResponseBody
    public Object wellStaticsByDept(){
        DataScope dataScope = permissionService.getCurrUserDataScope();
        try{
            List<Map<String,Object>> res = statisticsService.wellStaticsByDept(dataScope);
            return ResponseData.success(res);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.error("faliure");
        }
    }

    /**
     * 根据井类型统计闸井数量
     */
    @RequestMapping(value = "/wellStaticsByType")
    @ResponseBody
    public Object wellStaticsByType(){
        DataScope dataScope = permissionService.getCurrUserDataScope();
        try{
            List<Map<String,Object>> res = statisticsService.wellStaticsByType(dataScope);
            return ResponseData.success(res);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.error("faliure");
        }
    }

    /**
     * 根据设备类型统计设备数量
     */
    @RequestMapping(value = "/deviceStaticsByType")
    @ResponseBody
    public Object deviceStaticsByType(){
        DataScope dataScope = permissionService.getCurrUserDataScope();
        try{
            List<Map<String,Object>> res = statisticsService.deviceStaticsByType(dataScope);
            return ResponseData.success(res);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.error("faliure");
        }
    }

    /**
     * 根据权属单位统计设备数量
     */
    @RequestMapping(value = "/deviceStaticsByDept")
    @ResponseBody
    public Object deviceStaticsByDept(@RequestParam(value = "deviceType",required = false)String deviceType){
        DataScope dataScope = permissionService.getCurrUserDataScope();
        try{
            List<Map<String,Object>> res = statisticsService.deviceStaticsByDept(dataScope,deviceType);
            return ResponseData.success(res);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.error("faliure");
        }
    }

    /**
     * 根据在线状态统计设备数量
     */
    @RequestMapping(value = "/deviceStaticsByStatus")
    @ResponseBody
    public Object deviceStaticsByStatus(){
        DataScope dataScope = permissionService.getCurrUserDataScope();
        try{
            Map<String,Object> res = statisticsService.deviceStaticsByStatus(dataScope);
            return ResponseData.success(res);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.error("faliure");
        }
    }

    /**
     * 根据权属单位统计工单数
     */
    @RequestMapping(value = "/jobsByDept")
    @ResponseBody
    public Object jobsByDept(String beginTime, String endTime, String deviceType){
        DataScope dataScope = permissionService.getCurrUserDataScope();
        try{
            List<Map<String,Object>> res = statisticsService.jobsByDept(dataScope,beginTime,endTime,deviceType);
            return ResponseData.success(res);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.error("faliure");
        }
    }

    /**
     * 当前报警统计
     */
    @RequestMapping(value = "/alarmsNow")
    @ResponseBody
    public Object alarmsNow(){
        DataScope dataScope = permissionService.getCurrUserDataScope();
        try{
            List<AlarmNowView> alarmNowViews = alarmNowViewService.getNowAlarmRecords(dataScope);
            Map<String, Object> res = new HashMap<>();
            res.put("total",alarmNowViews.size());
            return ResponseData.success(res);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.error("faliure");
        }
    }

    /**
     * 统计每日报警总数
     */
    @RequestMapping("/alarmsByDay")
    @ResponseBody
    public Object alarmsByDay(String beginTime, String endTime){
        DataScope dataScope = permissionService.getCurrUserDataScope();
        try{
            List<Map<String,Object>> res = statisticsService.alarmsByDay(beginTime,endTime,dataScope);
            return ResponseData.success(res);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.error("faliure");
        }
    }



}
