package com.imdroid.beidou.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imdroid.beidou.entity.Tenant;
import com.imdroid.secapi.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class IndexController extends BasicController{
    @Autowired
    GnssDeviceMapper gnssDeviceMapper;
    @Autowired
    GnssStatusMapper statusMapper;

    @RequestMapping("/")
    public String index0(Model m, HttpSession session) {
        initModel(m, session);
        return "/index";
    }

    @RequestMapping("/index")
    public String index(Model m, HttpSession session) {
        initModel(m, session);
        return "/index";
    }

    @RequestMapping("/page/device_overview")
    public String deviceOverview(Model m, HttpSession session) {
        initModel(m, session);
        Long deviceDeployedNum;
        Long deviceOnlineNum;
        Long warning1Num;//一般告警
        Long warning2Num;//严重告警

        QueryWrapper<GnssDevice> deviceQueryWrapper = new QueryWrapper<>();
        QueryWrapper<GnssStatus> statusQueryWrapper;
        List<GnssStatus> deviceList;
        if(tenantId == Tenant.SAAS_PROVIDER_ID){
            // 统计所有企业设备
            deviceQueryWrapper.ne("tenantid", tenantId);
            deviceQueryWrapper.ne("opmode", GnssDevice.OP_MODE_UNUSE);
            // 设备数量
            deviceDeployedNum = gnssDeviceMapper.selectCount(deviceQueryWrapper);

            statusQueryWrapper = new QueryWrapper<>();
            statusQueryWrapper.ne("tenantid", tenantId);
            statusQueryWrapper.isNotNull("latitude");
            statusQueryWrapper.isNotNull("longitude");
            statusQueryWrapper.orderByAsc("warning");
            deviceList = statusMapper.selectList(statusQueryWrapper);

            statusQueryWrapper = new QueryWrapper<>();
            statusQueryWrapper.ne("tenantid", tenantId);
            statusQueryWrapper.ne("state", GnssStatus.STATE_OFFLINE);
            deviceOnlineNum = statusMapper.selectCount(statusQueryWrapper);

            // 告警统计
            statusQueryWrapper = new QueryWrapper<>();
            statusQueryWrapper.ne("tenantid", tenantId);
            statusQueryWrapper.eq("warning", WarningCfg.LEVEL_1);
            warning1Num = statusMapper.selectCount(statusQueryWrapper);

            statusQueryWrapper = new QueryWrapper<>();
            statusQueryWrapper.ne("tenantid", tenantId);
            statusQueryWrapper.eq("warning", WarningCfg.LEVEL_2);
            warning2Num = statusMapper.selectCount(statusQueryWrapper);
        }
        else{
            // 统计本企业设备
            // 统计所有企业设备
            deviceQueryWrapper.eq("tenantid", tenantId);
            deviceQueryWrapper.ne("opmode", GnssDevice.OP_MODE_UNUSE);
            // 设备数量
            deviceDeployedNum = gnssDeviceMapper.selectCount(deviceQueryWrapper);

            statusQueryWrapper = new QueryWrapper<>();
            statusQueryWrapper.ne("tenantid", tenantId);
            statusQueryWrapper.isNotNull("latitude");
            statusQueryWrapper.isNotNull("longitude");
            deviceList = statusMapper.selectList(statusQueryWrapper);

            statusQueryWrapper = new QueryWrapper<>();
            statusQueryWrapper.eq("tenantid", tenantId);
            statusQueryWrapper.ne("state", GnssStatus.STATE_OFFLINE);
            deviceOnlineNum = statusMapper.selectCount(statusQueryWrapper);

            // 告警统计
            statusQueryWrapper = new QueryWrapper<>();
            statusQueryWrapper.eq("tenantid", tenantId);
            statusQueryWrapper.eq("warning", WarningCfg.LEVEL_1);
            warning1Num = statusMapper.selectCount(statusQueryWrapper);

            statusQueryWrapper = new QueryWrapper<>();
            statusQueryWrapper.eq("tenantid", tenantId);
            statusQueryWrapper.eq("warning", WarningCfg.LEVEL_2);
            warning2Num = statusMapper.selectCount(statusQueryWrapper);
        }

        m.addAttribute("deviceDeployedNum", deviceDeployedNum);
        m.addAttribute("deviceOnlineNum", deviceOnlineNum);
        m.addAttribute("deviceOfflineNum", deviceDeployedNum - deviceOnlineNum);
        m.addAttribute("warning1Num", warning1Num);
        m.addAttribute("warning2Num", warning2Num);
        m.addAttribute("warningTotalNum", warning1Num+warning2Num);
        m.addAttribute("deviceList", deviceList);

        return "/page/device_overview";
    }

}
