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

import com.jeeplus.modules.sg.managementcenter.project.entity.Project;
import com.jeeplus.modules.sg.managementcenter.projectPlanArrage.entity.ProjectActProclnsAss;
import com.jeeplus.modules.sg.managementcenter.projectPlanArrage.entity.ProjectPlanArrage;
import com.jeeplus.modules.sys.utils.UserUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 打包工具类
 */
public class PackagingUtil {
    //不是初始节点的展示 -- 施工单位项目经理 配网项目经理2展示列表
    public static List<ProjectPlanArrage> getProjectPlanArrageList(ProjectPlanArrage projectPlanArrage,List<ProjectPlanArrage> projectPlanArrages){
        //获取已经安排的id
        List<String> ids = getSplitProjectNumber(projectPlanArrage);
        for (ProjectPlanArrage planArrage:projectPlanArrages){
            if (!planArrage.getWhetherStatus().equals("1")){
                for (String s:ids){
                    if (planArrage.getProjectNumber().equals(s)){
                        planArrage.setArrageFlag("1");
                        break;
                    }
                }
            }
        }

        return projectPlanArrages;
    }



    //列表展示数据 --
    public static List<ProjectPlanArrage> getProjectPlanList(ProjectPlanArrage projectPlanArrage,List<ProjectPlanArrage> projectPlanArrages){
        //关联停电包信息
        HashSet<String> currentSwitch = getCurrentSwitch(projectPlanArrage, projectPlanArrages);
        //获取list
        List<ProjectPlanArrage> planArrages = new ArrayList<ProjectPlanArrage>();
        //循环
        for (ProjectPlanArrage pa:projectPlanArrages){
           if (!pa.getWhetherStatus().equals("1")){
               for (String s:currentSwitch){
                   if (pa.getProjectPowerRange().contains(s)){
                       pa.setArrageFlag("1");
                       break;
                   }
               }
           }
        }
        return projectPlanArrages;
    }

    //获取当前用户输入项目定义号的所有开关
    public static HashSet<String> getCurrentSwitch(ProjectPlanArrage projectPlanArrage,List<ProjectPlanArrage> projectPlanArrageList){
        //复制所有数据集合,以供使用
        List<ProjectPlanArrage> copyProjectPlanArrage = getCopyProjectPlanArrage(projectPlanArrageList);
        //获取项目定义号
        List<String> splitProjectNumber = getSplitProjectNumber(projectPlanArrage);
        //获取当前用户根据项目定义号获得的开关
        HashSet<String> hashSetRange = getHashSetRange(splitProjectNumber, copyProjectPlanArrage);
        //获取项目名称
        if (null!=projectPlanArrage.getProjectName()&&!"".equals(projectPlanArrage.getProjectName())){
            List<String> splitProjectName = getSplitProjectName(projectPlanArrage);
            getHashSetRangeName(splitProjectName,copyProjectPlanArrage,hashSetRange);
        }
        //方法执行完会获取hashSetRange会获取所有开关信息
        getRecursive(copyProjectPlanArrage,hashSetRange);
        return hashSetRange;
    }

    //获取所有相关联的开关
    public static void getRecursive(List<ProjectPlanArrage> 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).getProjectPowerRange().contains(string)){
                    getSplice(projects.get(i).getProjectPowerRange(),hashSet);
                    projects.remove(i);
                    i--;
                }
            }
        }
        if (!hashSet.isEmpty()){
            for (String has:hashSet){
                hashSetAll.add(has);
            }
            getRecursive(projects,hashSetAll);
        }
    }

    //获取当前存在的开关
    public static HashSet<String> getHashSetRange(List<String> splitProjectNumber,List<ProjectPlanArrage> projectPlanArrageList){
        HashSet<String> hashSet = new HashSet<String>();
        for (String s:splitProjectNumber){
            for (ProjectPlanArrage projectPlanArrage:projectPlanArrageList){
                if (s.equals(projectPlanArrage.getProjectNumber())){
                    //获取当前的开关
                    getSplice(projectPlanArrage.getProjectPowerRange(),hashSet);
                }
            }
        }
        return hashSet;
    }

    //获取当前存在的开关---项目名称
    public static void getHashSetRangeName(List<String> splitProjectNumber,List<ProjectPlanArrage> projectPlanArrageList,HashSet<String> hashSet){
        for (String s:splitProjectNumber){
            for (ProjectPlanArrage projectPlanArrage:projectPlanArrageList){
                if (s.equals(projectPlanArrage.getProjectName())){
                    //获取当前的开关
                    getSplice(projectPlanArrage.getProjectPowerRange(),hashSet);
                }
            }
        }
    }
    //截取开关
    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 List<String> getSplitProjectNumber(ProjectPlanArrage projectPlanArrage){
        String[] split = null;
        List<String> list = new ArrayList<String>();
        if (null!=projectPlanArrage.getProjectNumber()&&!"".equals(projectPlanArrage.getProjectNumber())){
            split = projectPlanArrage.getProjectNumber().split(",");
            for (String s:split){
                list.add(s);
            }
        }
        return list;
    }
    //截取名称
    public static List<String> getSplitProjectName(ProjectPlanArrage projectPlanArrage){
        String[] split = null;
        List<String> list = new ArrayList<String>();
        if (null!=projectPlanArrage.getProjectName()&&!"".equals(projectPlanArrage.getProjectName())){
            split = projectPlanArrage.getProjectName().split(",");
            for (String s:split){
                list.add(s);
            }
        }
        return list;
    }

    //去重--并得到复制的集合
    public static List<ProjectPlanArrage> getCopyProjectPlanArrage(List<ProjectPlanArrage> projectPlanArrages){
        List<ProjectPlanArrage> planArrages = new ArrayList<ProjectPlanArrage>();
        boolean flag = false;
        for (ProjectPlanArrage p1:projectPlanArrages){
            flag = true;
            if (!p1.getWhetherStatus().equals("1")){
                for (ProjectPlanArrage p2:planArrages){
                    if (p1.getProjectNumber().equals(p2.getProjectNumber())&&p1.getProjectName().equals(p2.getProjectName())){
                        flag = false;
                        break;
                    }
                }
                if (flag){
                    planArrages.add(p1);
                }
            }
        }
        return planArrages;
    }

    //获取id
    public static List<String> getString(String ids){
        String[] strings = null;
        List<String> idList = new ArrayList<String>();
        if (null!=ids&&!"".equals(ids)){
            if (ids.contains(",")){
                strings = ids.split(",");
                for (String str:strings){
                    idList.add(str);
                }
            }else {
                idList.add(ids);
            }
        }
        return idList;
    }

    //替换集合--施工单位项目经理
    public static List<ProjectActProclnsAss> getProjectActProcInsAssList(List<ProjectPlanArrage> projectPlans,String procIns){
        List<ProjectActProclnsAss> projectActProclnsAsses = new ArrayList<ProjectActProclnsAss>();
        ProjectActProclnsAss proclnsAss = null;
        String name = UserUtils.getUser().getName();
        for (ProjectPlanArrage planArrage:projectPlans){
            if (!planArrage.getWhetherStatus().equals("1")&&planArrage.getArrageFlag().equals("1")){
                if (planArrage.getConstructionUnit().equals(name)){
                    proclnsAss = new ProjectActProclnsAss();
                    proclnsAss.preInsert();
                    proclnsAss.setProcInsId(procIns);//流程定义号
                    proclnsAss.setCustomId("0");
                    proclnsAss.setProjectName(planArrage.getProjectName());//项目名称
                    proclnsAss.setProjectNumber(planArrage.getProjectNumber());//项目定义号
                    proclnsAss.setProjectLine(planArrage.getProjectLine());
                    proclnsAss.setProjectPowerRange(planArrage.getProjectPowerRange());//停电范围
                    proclnsAss.setProjectSwitch(planArrage.getProjectSwitch());//停电开关
                    proclnsAss.setProjectMonthDate(planArrage.getProjectMonthDate());//月份
                    proclnsAss.setProjectDate(planArrage.getProjectDate());//停电日
                    proclnsAss.setDistributionNetwork(planArrage.getDistributionNetwork());//配网项目经理
                    proclnsAss.setConstructionUnit(planArrage.getConstructionUnit());//施工单位项目经理
                    proclnsAss.setOperationUnit(planArrage.getOperationUnit());//运行单位项目经理
                    proclnsAss.setBackToStauts(planArrage.getBackToStauts());//状态
                    proclnsAss.setBackToText(planArrage.getBackToText());//备注
                    proclnsAss.setProjectPowerOut(planArrage.getProjectPowerOut());
                    proclnsAss.setAssociatedBag(planArrage.getAssociatedBag());
                    proclnsAss.setAssociatedSort(planArrage.getAssociatedSort());
                    proclnsAss.setPowerPackage(planArrage.getPowerPackage());
                    proclnsAss.setPowerPackageSort(planArrage.getPowerPackageSort());
                    proclnsAss.setStatus("0");
                    projectActProclnsAsses.add(proclnsAss);
                }
            }
        }
        return projectActProclnsAsses;
    }

    //替换集合--配网项目经理
    public static List<ProjectActProclnsAss> getNetList(List<ProjectPlanArrage> projectPlans,String procIns){
        List<ProjectActProclnsAss> projectActProclnsAsses = new ArrayList<ProjectActProclnsAss>();
        ProjectActProclnsAss proclnsAss = null;
        for (ProjectPlanArrage planArrage:projectPlans){
            if (!planArrage.getWhetherStatus().equals("1")&&planArrage.getArrageFlag().equals("1")){
                proclnsAss = new ProjectActProclnsAss();
                proclnsAss.preInsert();
                proclnsAss.setProcInsId(procIns);//流程定义号
                proclnsAss.setCustomId("0");
                proclnsAss.setProjectName(planArrage.getProjectName());//项目名称
                proclnsAss.setProjectNumber(planArrage.getProjectNumber());//项目定义号
                proclnsAss.setProjectLine(planArrage.getProjectLine());
                proclnsAss.setProjectPowerRange(planArrage.getProjectPowerRange());//停电范围
                proclnsAss.setProjectSwitch(planArrage.getProjectSwitch());//停电开关
                proclnsAss.setProjectMonthDate(planArrage.getProjectMonthDate());//月份
                proclnsAss.setProjectDate(planArrage.getProjectDate());//停电日
                proclnsAss.setDistributionNetwork(planArrage.getDistributionNetwork());//配网项目经理
                proclnsAss.setConstructionUnit(planArrage.getConstructionUnit());//施工单位项目经理
                proclnsAss.setOperationUnit(planArrage.getOperationUnit());//运行单位项目经理
                proclnsAss.setBackToStauts(planArrage.getBackToStauts());//状态
                proclnsAss.setBackToText(planArrage.getBackToText());//备注
                proclnsAss.setProjectPowerOut(planArrage.getProjectPowerOut());
                proclnsAss.setStatus("0");
                proclnsAss.setAssociatedBag(planArrage.getAssociatedBag());
                proclnsAss.setAssociatedSort(planArrage.getAssociatedSort());
                proclnsAss.setPowerPackage(planArrage.getPowerPackage());
                proclnsAss.setPowerPackageSort(planArrage.getPowerPackageSort());
                projectActProclnsAsses.add(proclnsAss);
//                if (planArrage.getConstructionUnit().equals(name)){
//
//                }
            }
        }
        return projectActProclnsAsses;
    }

    //得到自己项目
    public static List<ProjectActProclnsAss> getUtitName(List<ProjectActProclnsAss> projectActProclnsAsses,String name){
        List<ProjectActProclnsAss> proclnsAssList = new ArrayList<ProjectActProclnsAss>();
        for (ProjectActProclnsAss proclnsAss:projectActProclnsAsses){
            if (name.equals(proclnsAss.getConstructionUnit())){
                proclnsAssList.add(proclnsAss);
            }
        }
        return proclnsAssList;
    }
}
