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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.core.application.context.AbstractPermissionContext;
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.dao.DataAccessException;
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 javax.annotation.Resource;
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;
    @Resource
    private AbstractPermissionContext permissionService;

    /**
     * 根据权属单位统计闸井数量
     *
     * @return
     */
    @RequestMapping(value = "/wellStaticsByDept")
    @ResponseBody
    public Object wellStaticsByDept() {
        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        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.getAuthService().getLoginUserDataScope();
        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.getAuthService().getLoginUserDataScope();
        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.getAuthService().getLoginUserDataScope();
        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() {
        ResponseData responseData = new ResponseData();
        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        try {
            Map<String, Object> res = statisticsService.deviceStaticsByStatus(dataScope);
            responseData.setData(res);
            responseData.setCode(200);
            responseData.setMessage("查询成功");
        } catch (DataAccessException e) {
            e.printStackTrace();
            responseData.setCode(500);
            responseData.setMessage("查询异常");
        }
        return responseData;
    }

    /**
     * 根据在线状态统计设备数量
     */
    @RequestMapping(value = "/deviceStaticsByStatusAndType")
    @ResponseBody
    public Object deviceStaticsByStatusAndType() {
        ResponseData responseData = new ResponseData();
        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        try {
            List<Map<String, Object>> res = statisticsService.deviceStaticsByStatusAndType(dataScope);
            responseData.setData(res);
            responseData.setCode(200);
            responseData.setMessage("查询成功");
        } catch (DataAccessException e) {
            e.printStackTrace();
            responseData.setCode(500);
            responseData.setMessage("查询异常");
        }
        return responseData;
    }

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

    /**
     * 报警井排名
     */
    @RequestMapping("/alarmWellListPage")
    @ResponseBody
    public Object alarmWellListPage(String beginTime, String endTime, String alarmContent) {
        Page<Map<String, Object>> page = PageFactory.defaultPage();
        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        ResponseData responseData=statisticsService.alarmWellListPage(dataScope, page,beginTime, endTime, alarmContent);
        responseData.setData(super.packForBT(page));
        return responseData;
    }

    /**
     * 报警井排名
     */
    @RequestMapping("/alarmStaticsByContent")
    @ResponseBody
    public Object getAlarmCountByType(String beginTime, String endTime, String alarmType) {
        return statisticsService.getAlarmCountByType( beginTime, endTime, alarmType);
    }

    /**
     * 报警情况统计
     */
    @RequestMapping("/alarmStaticsByStatus")
    @ResponseBody
    public Object getAlarmByStatus(String beginTime, String endTime, String alarmType) {
        return statisticsService.getAlarmByStatus(beginTime, endTime, alarmType);
    }


}
