package com.fastbee.data.controller.deviceData;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.dtflys.forest.annotation.Get;
import com.fastbee.common.core.controller.BaseController;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.deviceData.api.devlink.service.DevLinkAuthorizationService;
import com.fastbee.deviceData.api.devlink.service.DevLinkBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 设备告警
 */
@RestController
@RequestMapping("/device/statistics")
public class DeviceAlarmController extends BaseController {

    @Autowired
    private DevLinkBaseService devLinkBaseService;

    /**
     * 获取设备告警数量
     */
    @GetMapping("/alarm/count")
    public AjaxResult getDeviceAlarmCount() {

        HashMap<String, Object> body = new HashMap<>();
        body.put("method","get");
        body.put("path","api/v1/product/device/statistics/months");
        JSONObject data = devLinkBaseService.baseRequest(body);
        return AjaxResult.success(data);
    }

    /**
     * 获取设备相关统计
     */
    @GetMapping("/count")
    public AjaxResult getDeviceStatusCount() {
        HashMap<String, Object> body = new HashMap<>();
        body.put("method","get");
        body.put("path","api/v1/product/device/statistics");
        JSONObject data = devLinkBaseService.baseRequest(body);
        //计算安全状态等级
        String securityLevel="?";
        JSONObject total = JSONUtil.parseObj(data.get("deviceTotal"));

        int alarmTotal = Integer.parseInt(total.get("alarmTotal").toString());
        if(alarmTotal < 2){
            securityLevel= "A";
        }
        else if(alarmTotal<4){
            securityLevel = "B";
        }else if(alarmTotal<6){
            securityLevel = "C";
        }else if(alarmTotal<8){
            securityLevel = "D";
        }
        total.put("securityLevel",securityLevel);
        return AjaxResult.success(total);
    }

    /**
     * 统计不同设备数量
     */
    @GetMapping("/countByType")
    public AjaxResult getDeviceCountByType() {
        HashMap<String, Object> body = new HashMap<>();
        body.put("method","get");
        body.put("path","api/v1/product/device/list");
        JSONObject data = devLinkBaseService.baseRequest(body);
        //设备列表
        JSONArray devArray = JSONUtil.parseArray(data);
        //分组统计
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < devArray.size(); i++) {
            JSONObject dev = devArray.getJSONObject(i);
            String type = dev.get("type").toString();
            if(map.containsKey(type)){
                map.put(type,map.get(type)+1);
            }else
                map.put(type,1);
        }
        return AjaxResult.success();
    }

    /**
     * 统计设备在线率
     */
    @GetMapping("/onlineRate")
    public AjaxResult getDeviceOnlineRate() {
        HashMap<String, Object> body = new HashMap<>();
        body.put("method","get");
        body.put("path","api/v1/product/device/list");
        JSONObject data = devLinkBaseService.baseRequest(body);
        //设备列表
        JSONArray devArray = JSONUtil.parseArray(data);
        //分组统计

        return AjaxResult.success();
    }





}
