package com.eegrid.webapi;

import cn.com.countrygarden.bi.main.util.NumberUtil;
import cn.com.countrygarden.bi.main.util.RedisCacheUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eegrid.webapi.util.HttpClientUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class Device {

    /**
     * 获取项目所有设备
     *
     * @param projectCode
     * @return
     */
    public List<Map<String, String>> getProjectDevices(String projectCode) {
        return getProjectDevices(projectCode, 3600);
    }

    /**
     * 获取项目所有设备
     *
     * @param projectCode
     * @param ttl
     * @return
     */
    public List<Map<String, String>> getProjectDevices(String projectCode, int ttl) {
        Map<String, String> param = new HashMap<String, String>() {{
            put("ProjectCode", projectCode);
        }};
        Function<Map<String, String>, List<Map<String, String>>> function = p -> {
            List<Map<String, String>> items = new ArrayList<>();
            String response = HttpClientUtil.post("/device/get", JSONObject.toJSONString(param));
            JSONArray jsonArray = JSON.parseArray(response);
            for (int i = 0; i < jsonArray.size(); i++) {
                Map<String, String> item = new HashMap<>();
                JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                item.put("DeviceID", jsonObject.getString("DeviceID"));
                item.put("DeviceName", jsonObject.getString("DeviceName"));
                item.put("NavName", jsonObject.getString("NavName"));
                item.put("TypeName", jsonObject.getString("TypeName"));
                item.put("TypeID", jsonObject.getString("TypeID"));
                items.add(item);
            }

            return items;
        };

        List<Map<String, String>> items = RedisCacheUtils.rememberList(
                "datav:eegrid:webapi:project:device:" + projectCode,
                ttl /* 2 * 3600 */,
                param,
                function
        );
        return items;
    }

    /**
     * 计算项目设备数
     *
     * @param projectCode
     * @return
     */
    public float getProjectDeviceQuantity(String projectCode) {
        int quantity = 0;
        List<Map<String, String>> projectDevices = getProjectDevices(projectCode);
        quantity = Math.max(0, projectDevices.size());
        return quantity;
    }

    /**
     * 获取设备总数
     *
     * @return
     */
    public int getDeviceQuantity() {
        int quantity = 0;

        // 遍历项目
        Project project = new Project();
        List<Map<String, String>> projects = project.getProjects();

        // 计算每个项目设备数
        for (Map<String, String> item : projects) {
            quantity += getProjectDeviceQuantity(item.getOrDefault("projectCode", ""));
        }

        return quantity;
    }

    /**
     * 按设备类型汇总设备总数
     *
     * @return
     */
    public Map<String, Integer> getDeviceQuantityByDeviceType() {
        // 遍历项目
        Project project = new Project();
        List<Map<String, String>> projects = project.getProjects();
        Map<String, Integer> deviceTypes = new HashMap<>();

        // 计算设备类型数目
        for (Map<String, String> item : projects) {
            String projectCode = item.getOrDefault("projectCode", "");
            List<Map<String, String>> projectDevices = getProjectDevices(projectCode);
            for (Map<String, String> projectDevice : projectDevices) {
                String type = projectDevice.getOrDefault("TypeName", "");
                deviceTypes.putIfAbsent(type, 0);
                deviceTypes.replace(type, deviceTypes.getOrDefault(type, 0) + 1);
            }
        }

        return deviceTypes;
    }

    public static void main(String args[]) {
        Device device = new Device();
        Map<String, Integer> deviceTypes = device.getDeviceQuantityByDeviceType();
        System.out.println("################### getDeviceQuantity: " + deviceTypes);
    }
}
