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.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class Energy {

    private final LocalDate localDate = LocalDate.now();
    private final int year = localDate.getYear();
    private final int month = localDate.getMonthValue();
    private final int dayOfMonth = localDate.getDayOfMonth();
    private final int previousDayOfMonth = Math.max(1, dayOfMonth - 1);


    /**
     * 计算项目某天能耗
     *
     * @param projectCode
     * @param date
     * @return
     */
    public List<Map<String, String>> getProjectEnergys(String projectCode, String date) {
        return getProjectEnergys(projectCode, date, 3600);
    }

    /**
     * 计算项目某天能耗并缓存
     *
     * @param projectCode
     * @param date
     * @param ttl
     * @return
     */
    public List<Map<String, String>> getProjectEnergys(String projectCode, String date, int ttl) {
        Map<String, String> param = new HashMap<String, String>() {{
            put("Date", date);
            put("ProjectCode", projectCode);
        }};
        Function<Map<String, String>, List<Map<String, String>>> function = p -> {
            List<Map<String, String>> items = new ArrayList<>();
            String response = HttpClientUtil.post("/energy/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("CollectTime", jsonObject.getString("CollectTime"));
                item.put("CollectData", jsonObject.getString("CollectData"));
                item.put("DataCode", jsonObject.getString("DataCode"));
                items.add(item);
            }

            return items;
        };

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

    /**
     * 计算项目某天能耗
     *
     * @param projectCode
     * @param date
     * @return
     */
    public float getProjectDailyEnergyQuantity(String projectCode, String date) {
        return getProjectDailyEnergyQuantity(projectCode, date, 3600);
    }

    /**
     * 计算项目某天能耗
     *
     * @param projectCode
     * @param date
     * @param ttl
     * @return
     */
    public float getProjectDailyEnergyQuantity(String projectCode, String date, int ttl) {
        float quantity = 0f;
        List<Map<String, String>> energys = getProjectEnergys(projectCode, date, ttl);
        for (Map<String, String> energy : energys) {
            float collectData = Float.parseFloat(energy.getOrDefault("CollectData", "0.0"));
            quantity += collectData;
        }

        return quantity;
    }

    /**
     * 计算项目能耗
     *
     * @param projectCode
     * @return
     */
    public float getProjectEnergyQuantity(String projectCode) {
        float quantity = 0f;

        if (dayOfMonth <= 1) {
            quantity += getProjectDailyEnergyQuantity(projectCode, "" + year + "-" + month + "-" + dayOfMonth);
        } else {
            for (int i = 1; i <= previousDayOfMonth; i++) {
                quantity += getProjectDailyEnergyQuantity(
                        projectCode,
                        "" + year + "-" + month + "-" + i,
                        2678400 /* 31 * 86400*/
                );
            }

            quantity += getProjectDailyEnergyQuantity(projectCode, "" + year + "-" + month + "-" + dayOfMonth);
        }

        return quantity;
    }

    /**
     * 获取总能耗
     *
     * @return
     */
    public float getEnergyQuantity() {
        float quantity = 0f;

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

        // 计算每个项目本月能耗
        for (Map<String, String> item : projects) {
            quantity += getProjectEnergyQuantity(item.getOrDefault("projectCode", ""));
        }

        return quantity;
    }

    public static void main(String args[]) {
        Energy energy = new Energy();
        float energyQuantity = energy.getEnergyQuantity();
        System.out.println("################### energyQuantity: " + NumberUtil.decimalFormat(energyQuantity));
    }
}
