package com.jeeplus.modules.sg.managementcenter.projectPlanArrage.util;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.modules.sg.managementcenter.projectPlanArrage.entity.ProjectDailyPlan;
import com.jeeplus.modules.sg.managementcenter.projectPlanArrage.entity.ProjectPlanArrage;
import com.jeeplus.modules.sys.utils.UserUtils;

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

public class BookStrapTreeUtil {
    //月计划安排树
    public static List<Map<String,Object>> getAllBookStrapTree(List<Map<String,Object>> objects, List<ProjectPlanArrage> projectPlanArrages,String bs){
        List<Map<String, Object>> bookStrapTree = getBookStrapTree(objects, projectPlanArrages,bs);
        List<Map<String,Object>> allBookStrapTree = Lists.newArrayList();
        Map<String,Object> allMap = Maps.newHashMap();
        allMap.put("id","-1");
        allMap.put("name","未安排的项目计划");
        allMap.put("children",bookStrapTree);
        allBookStrapTree.add(allMap);
        return allBookStrapTree;
    }

    //获取树
    public static List<Map<String,Object>> getBookStrapTree(List<Map<String,Object>> objects, List<ProjectPlanArrage> projectPlanArrages,String bs){
        Map<String, Object> maps = Maps.newHashMap();
        String outRange = "";
        for (ProjectPlanArrage projectPlanArrage:projectPlanArrages){
            //判断是否为父类
            if ("1".equals(projectPlanArrage.getWhetherStatus())){
                maps = Maps.newHashMap();//获取一级父类maps的集合
                outRange = projectPlanArrage.getProjectName();//获取停电开关
                Boolean childTree = getChildTree(maps, projectPlanArrages, outRange,bs);
                if (childTree){
                    maps.put("id",projectPlanArrage.getId());
                    maps.put("name",projectPlanArrage.getProjectName());
                    objects.add(maps);
                }
            }
        }
        return objects;
    }
    //获取子类树
    public static Boolean getChildTree(Map<String,Object> maps,List<ProjectPlanArrage> projectPlanArrages,String outRange,String bs){
        boolean flag = false;
        List<Map<String,Object>> lists = Lists.newArrayList();
        Map<String,Object> ms = Maps.newHashMap();//获取集合
        String name = UserUtils.getUser().getName();
        for (ProjectPlanArrage projectPlanArrage:projectPlanArrages){
            if (!"1".equals(projectPlanArrage.getWhetherStatus())) {
                if (bs.equals("2")){
                    if (name.equals(projectPlanArrage.getConstructionUnit())){
                       if (!"1".equals(projectPlanArrage.getArrageFlag())){
                           if (projectPlanArrage.getProjectPowerRange().contains(outRange)) {
                               ms = Maps.newHashMap();
                               ms.put("id", projectPlanArrage.getId());//获取id
                               ms.put("name", projectPlanArrage.getProjectName());//获取名字
                               lists.add(ms);
                           }
                       }
                    }
                }else {
                    if (projectPlanArrage.getProjectPowerRange().contains(outRange)) {
                        ms = Maps.newHashMap();
                        ms.put("id", projectPlanArrage.getId());//获取id
                        ms.put("name", projectPlanArrage.getProjectName());//获取名字
                        lists.add(ms);
                    }
                }
            }
        }
        if (!lists.isEmpty()){
            flag = true;
            maps.put("children",lists);
        }
        return flag;
    }

    //日计划安排树
    //获取树
    public static List<Map<String,Object>> getDailyBookStrapTree(List<Map<String,Object>> objects, List<ProjectDailyPlan> projectDailyPlans){
        List<Map<String, Object>> dailyChildTree = getDailyChildTree(projectDailyPlans);
        Map<String,Object> map = Maps.newHashMap();
        map.put("id","-1");
        map.put("name","未安排的日计划项目");
        map.put("children",dailyChildTree);
        objects.add(map);
        return objects;
    }
    //获取子类树
    public static List<Map<String,Object>>  getDailyChildTree(List<ProjectDailyPlan> projectDailyPlans){
        List<Map<String,Object>> objects = Lists.newArrayList();
        Map<String,Object> maps = null;
        for (ProjectDailyPlan plan:projectDailyPlans){
            if (!plan.getArrangeDailyPlan().equals("1")){
                maps = Maps.newHashMap();
                maps.put("id",plan.getId());
                maps.put("name",plan.getProjectName());
                objects.add(maps);
            }
        }
        return objects;
    }
}
