package com.eegrid.webapi;

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 Project {

    /**
     * 获取项目列表接口对接
     *
     * @return
     */
    public List<Map<String, String>> getProjects() {
        Map<String, String> param = new HashMap<>();
        Function<Map<String, String>, List<Map<String, String>>> function = p -> {
            List<Map<String, String>> projects = new ArrayList<>();
            String response = HttpClientUtil.post("/project/get", JSONObject.toJSONString(param));
            JSONArray projectArray = JSON.parseArray(response);
            for (int i = 0; i < projectArray.size(); i++) {
                Map<String, String> item = new HashMap<>();
                JSONObject jsonObject = (JSONObject) projectArray.get(i);

                // 排除测试项目
                // 71328 测试项目1
                // 71329 测试项目2
                String projectCode = jsonObject.getString("projectCode");
                if (projectCode.equals("71328") || projectCode.equals("71329")) {
                    continue;
                }

                item.put("projectCode", projectCode);
                item.put("projectName", jsonObject.getString("projectName"));
                item.put("ccProjectCode", jsonObject.getString("ccProjectCode"));
                projects.add(item);
            }

            return projects;
        };

        List<Map<String, String>> projects = RedisCacheUtils.rememberList(
                "datav:eegrid:webapi:project",
                7200 /* 2 * 3600 */,
                param,
                function
        );
        return projects;
    }

    /**
     * 获取项目数量
     *
     * @return
     */
    public int getProjectQuantity() {
        List<Map<String, String>> projects = getProjects();
        return projects.size();
    }

    public static void main(String args[]) {
        Project project = new Project();
        System.out.println("Project.GetProjectQuantity(): " + project.getProjectQuantity());
    }
}
