package com.imdroid.beidou.controller;

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.Iterator;
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);
        int deviceDeployedNum;
        int deviceOfflineNum=0;
        int warning1Num=0;//一般告警
        int warning2Num=0;//严重告警
        int noGGA = 0;

        List<GnssStatusJoin> deviceList;
        if(tenantId == Tenant.SAAS_PROVIDER_ID){
            deviceList = statusMapper.queryDeployed();
        }
        else{
            deviceList = statusMapper.queryDeployedByTenant(tenantId);
        }
        deviceDeployedNum = deviceList.size();

        //for(GnssStatusJoin status:deviceList){遍历删除异常
        Iterator<GnssStatusJoin> iterator = deviceList.iterator();
        while(iterator.hasNext()){
            GnssStatusJoin status=iterator.next();
            if(status.getState() == GnssStatus.STATE_OFFLINE) deviceOfflineNum++;
            if(status.getWarning() == WarningCfg.LEVEL_1) warning1Num++;
            else if(status.getWarning() == WarningCfg.LEVEL_2) warning2Num++;

            // 剔除无经纬度的对象，不在地图上显示
            if(status.getLatitude()==null || status.getLongitude()==null){
                noGGA++;
                iterator.remove();
            }
            else{
                // 地图上显示工点名称
                if(status.getName()!=null){
                    status.setDeviceid(status.getDeviceid() +" "+status.getName());
                }
                // 地图上显示告警原因
                if(status.getWarning()!=WarningCfg.LEVEL_0){
                    List<String> warningInfo= WarningCfg.parseCode(status.getWarningcode());
                    status.setDeviceid(status.getDeviceid() +" "+warningInfo);
                }
            }
        }

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

        return "/page/device_overview";
    }

}
