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

import com.jeeplus.modules.sg.managementcenter.constructionProject.entity.ConstructionProjectTemporary;
import com.jeeplus.modules.sg.managementcenter.project.entity.Project1;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/*8
    递归类
 */
public class RecursiveUtil {

    //获取级联的工程------------增加入库操作
    public static List<Project1> getAllProjectRecursive(List<Project1> projects, List<String> projectNumbers){
        List<Project1> sureProject = getDuplicateRemoval(projects);//确定的project集合
        List<Project1> duplicateRemoval = getDuplicateRemoval(projects);//备用集合
        List<Project1> areProject = getThereAreProject(sureProject, projectNumbers);//查找项目定义号的工程
        HashSet<String> hashSetAll = getProjectScopeList(areProject);//获取去重的停电线路
        getRecursive(duplicateRemoval,hashSetAll);//递归
        List<Project1> returnProject = getReturnProject(sureProject, hashSetAll);
        return returnProject;
    }
    //其他
    public static List<Project1> getAllProjectNo(List<Project1> projects, List<String> projectNumbers){
        List<Project1> projectList = getDuplicateRemoval(projects);//确定集合
        List<Project1> duplicateRemoval = getDuplicateRemoval(projects);
        List<Project1> noNoProject = getNoNoProject(projectList, projectNumbers);//主用
        List<Project1> noNoProject11 = getNoNoProject(duplicateRemoval, projectNumbers);//备用
        HashSet<String> projectScopeList = getProjectScopeList(noNoProject11);
        getRecursive(noNoProject11,projectScopeList);
        List<Project1> returnProject = getReturnProject(noNoProject, projectScopeList);
        return returnProject;
    }

    public static List<Project1> getNoNoProject(List<Project1> projects, List<String> projectNumbers){
        for (int i = 0; i < projects.size(); i++) {
            for (String string:projectNumbers){
                if (projects.get(i).getProjectNumber().equals(string)){
                    projects.remove(i);
                    i--;
                    break;
                }
            }
        }
        return projects;
    }
    //获取确定的list<project>集合
    public static List<Project1> getReturnProject(List<Project1> projects, HashSet<String> hashSetAll){
        List<Project1> projectList = new ArrayList<Project1>();
        for (Project1 project : projects){
            for (String string:hashSetAll){
                if (project.getPowerScope().contains(string)){
                    projectList.add(project);
                    break;
                }
            }
        }
        return projectList;
    }

    //递归
    public static void getRecursive(List<Project1> projects, HashSet<String> hashSetAll){
        HashSet<String> hashSet = new HashSet<String>();
        for (String string:hashSetAll){
            for (int i = 0; i < projects.size(); i++) {
                if (projects.get(i).getPowerScope().contains(string)){
                    getSplice(projects.get(i).getPowerScope(),hashSet);
                    projects.remove(i);
                    i--;
                }
            }
        }
        if (!hashSet.isEmpty()){
            for (String has:hashSet){
                hashSetAll.add(has);
            }
            getRecursive(projects,hashSetAll);
        }
    }
    public static void getSplice(String powerScope,HashSet<String> strings){
        String[] splice = null;
        if (powerScope.contains("；")){
            splice = powerScope.split("；");
            for (String str:splice){
                strings.add(str);
            }
        }else if(powerScope.contains(";")){
            splice = powerScope.split(";");
            for (String str:splice){
                strings.add(str);
            }
        }else if (powerScope.contains(",")){
            splice = powerScope.split(",");
            for (String str:splice){
                strings.add(str);
            }
        }else if (powerScope.contains("，")){
            splice = powerScope.split("，");
            for (String str:splice){
                strings.add(str);
            }
        }else {
            strings.add(powerScope);
        }
    }

    //获取当前搜索项目定义号的停电范围
    public static HashSet<String> getProjectScopeList(List<Project1> areProject){
        HashSet<String> setProject = new HashSet<String>();
        String[] arrayProject = null;
        String projectScope = "";
        for (Project1 project : areProject){
            if (null!= project.getPowerScope()){
                projectScope = project.getPowerScope();
                if (projectScope.contains("；")){
                    arrayProject = projectScope.split("；");
                    for (String str:arrayProject){
                        setProject.add(str);
                    }
                }else if (projectScope.contains(";")){
                    arrayProject = projectScope.split(";");
                    for (String str:arrayProject){
                        setProject.add(str);
                    }
                }else if (projectScope.contains("，")){
                    arrayProject = projectScope.split("，");
                    for (String str:arrayProject){
                        setProject.add(str);
                    }
                }else if (projectScope.contains(",")){
                    arrayProject = projectScope.split(",");
                    for (String str:arrayProject){
                        setProject.add(str);
                    }
                }else {
                    setProject.add(projectScope);
                }
            }
        }
        return setProject;
    }
    //获取查找的集合
    public static List<Project1> getThereAreProject(List<Project1> projects, List<String> projectNumbers){
        List<Project1> projectList = new ArrayList<Project1>();
        for (String string:projectNumbers){
            for (Project1 project : projects){
                if (string.equals(project.getProjectNumber())){
                    projectList.add(project);
                    break;
                }
            }
        }
        return projectList;
    }
    //list project去重
    public static List<Project1> getDuplicateRemoval(List<Project1> projects){
        List<Project1> projectList = new ArrayList<Project1>();
        boolean flag = false;
        String projectNumber = "";
        String newProjectNumber = "";
        for(Project1 project : projects){
            if (null!= project.getProjectNumber()&&!"".equals(project.getProjectNumber())){
                flag = true;
                projectNumber = project.getProjectNumber();
                for (Project1 newProject : projectList){
                    if (null!= newProject.getProjectNumber()&&!"".equals(newProject.getProjectNumber())){
                        newProjectNumber = newProject.getProjectNumber();
                        if (projectNumber.equals(newProjectNumber)){
                            flag = false;
                            break;
                        }
                    }
                }
                if (flag){
                    projectList.add(project);
                }
            }
        }
        return projectList;
    }
    //获取级联的工程------------
    //添加操作项目--------！
        public static List<ConstructionProjectTemporary> getAddPlanList(List<ConstructionProjectTemporary> constructionProjectTemporaries,String ids[]){
            //获取移动操作的数据--父级
            List<ConstructionProjectTemporary> constructionProjectParent = new ArrayList<ConstructionProjectTemporary>();
            //获取移动操作的数据--子集
            List<ConstructionProjectTemporary> constructionProjectChild = new ArrayList<ConstructionProjectTemporary>();
            for (String id:ids){
                for (ConstructionProjectTemporary constructionProjectTemporary:constructionProjectTemporaries){
                    if (id.equals(constructionProjectTemporary.getId())){
                        if ("0".equals(constructionProjectTemporary.getParentId())){
                            constructionProjectParent.add(constructionProjectTemporary);
                        }else {
                            constructionProjectChild.add(constructionProjectTemporary);
                        }
                        break;
                    }
                }
            }
            if (null!=constructionProjectParent && constructionProjectParent.size()>0){
                for (ConstructionProjectTemporary projectTemporary:constructionProjectParent){
                    for (ConstructionProjectTemporary temporary:constructionProjectTemporaries){
                        if (temporary.getParentId().contains(projectTemporary.getId())){
                            constructionProjectChild.add(temporary);
                        }
                    }
                }
            }
            //去重
            List<ConstructionProjectTemporary> qcConstruction = getQcConstruction(constructionProjectChild);//主
            List<ConstructionProjectTemporary> qcConstructionCopy = getQcConstruction(constructionProjectChild);//备用
            HashSet<String> hashSet = getHashSet(qcConstruction);//获取奴前存在的开关
            getConstructionRecurse(qcConstructionCopy,hashSet);
            //获取最终的集合
            List<ConstructionProjectTemporary> returnConstructionProjectTemporary = getReturnConstructionProjectTemporary(constructionProjectTemporaries, hashSet);
            //父类添加进入
            for (ConstructionProjectTemporary constructionProjectTemporary:constructionProjectParent){
                returnConstructionProjectTemporary.add(constructionProjectTemporary);
            }
            return returnConstructionProjectTemporary;
        }
        //获取确定的list<project>集合
        public static List<ConstructionProjectTemporary> getReturnConstructionProjectTemporary(List<ConstructionProjectTemporary> constructionProjectTemporaries,HashSet<String> hashSetAll){
            List<ConstructionProjectTemporary> constructionProjectTemporaryList = new ArrayList<ConstructionProjectTemporary>();
            for (ConstructionProjectTemporary constructionProjectTemporary:constructionProjectTemporaries){
               if (!"0".equals(constructionProjectTemporary.getParentId())){
                   for (String string:hashSetAll){
                       if (constructionProjectTemporary.getPowerScope().contains(string)){
                           constructionProjectTemporaryList.add(constructionProjectTemporary);
                           break;
                       }
                   }
               }
            }
            return constructionProjectTemporaryList;
        }
        //递归
        public static void getConstructionRecurse(List<ConstructionProjectTemporary> constructionProjectTemporaries,HashSet<String> hashSet){
            HashSet<String> stringHashSet = new HashSet<String>();
            for (String s:stringHashSet){
                for (int i = 0; i < constructionProjectTemporaries.size(); i++) {
                    if (constructionProjectTemporaries.get(i).getPowerScope().contains(s)){
                        getSplice(constructionProjectTemporaries.get(i).getPowerScope(),stringHashSet);
                        constructionProjectTemporaries.remove(i);
                        i--;
                    }
                }
            }
            if (!stringHashSet.isEmpty()){
                for (String string:stringHashSet){
                    hashSet.add(string);
                }
                getConstructionRecurse(constructionProjectTemporaries,hashSet);
            }
        }

        //获取目前的开关
        public static HashSet<String> getHashSet(List<ConstructionProjectTemporary> constructionProjectTemporaries){
            HashSet<String> stringHashSet = new HashSet<String>();
            String[] powerScopes = null;
            for (ConstructionProjectTemporary constructionProjectTemporary:constructionProjectTemporaries){
                if (null!=constructionProjectTemporary.getPowerScope()){
                    if (constructionProjectTemporary.getPowerScope().equals("；")){
                        powerScopes = constructionProjectTemporary.getPowerScope().split("；");
                        for (String power:powerScopes){
                            stringHashSet.add(power);
                        }
                    }else if (constructionProjectTemporary.getPowerScope().equals(";")){
                        powerScopes = constructionProjectTemporary.getPowerScope().split(";");
                        for (String power:powerScopes){
                            stringHashSet.add(power);
                        }
                    }else if (constructionProjectTemporary.getPowerScope().equals("，")){
                        powerScopes = constructionProjectTemporary.getPowerScope().split("，");
                        for (String power:powerScopes){
                            stringHashSet.add(power);
                        }
                    }else if (constructionProjectTemporary.getPowerScope().equals(",")){
                        powerScopes = constructionProjectTemporary.getPowerScope().split(",");
                        for (String power:powerScopes){
                            stringHashSet.add(power);
                        }
                    }else {
                        stringHashSet.add(constructionProjectTemporary.getPowerScope());
                    }
                }
            }
            return stringHashSet;
        }
        //去重
        public static List<ConstructionProjectTemporary> getQcConstruction(List<ConstructionProjectTemporary> constructionProjectTemporaries){
            List<ConstructionProjectTemporary> constructionProjectTemporaryList = new ArrayList<ConstructionProjectTemporary>();
            boolean flag = false;
            for (ConstructionProjectTemporary constructionProjectTemporary:constructionProjectTemporaries){
                flag = true;
                for (ConstructionProjectTemporary projectTemporary:constructionProjectTemporaryList){
                    if (constructionProjectTemporary.getId().equals(projectTemporary.getId())){
                        flag = false;
                    }
                }
                if (flag){
                    constructionProjectTemporaryList.add(constructionProjectTemporary);
                }
            }
            return constructionProjectTemporaryList;
        }
    //添加操作项目--------！
}
