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.constructionProject.entity.ConstructionProject;
import com.jeeplus.modules.sg.managementcenter.constructionProject.entity.ConstructionProjectTemporary;

import java.text.SimpleDateFormat;
import java.util.*;

public class TreeTableUtil {
    //树生成
    public static List<Map<String,Object>> getBootstrapTreeTable(List<Map<String,Object>> objects,List<ConstructionProjectTemporary> listTemporary){
        Map<String,Object> maps = Maps.newHashMap();
        boolean treeChild = false;
        for (ConstructionProjectTemporary constructionProjectTemporary:listTemporary){
            if ("0".equals(constructionProjectTemporary.getParentId())){
                maps = Maps.newHashMap();
                treeChild = getTreeChild(maps, listTemporary, constructionProjectTemporary.getId());
                if (treeChild){
                    maps.put("id",constructionProjectTemporary.getId());
                    maps.put("name",constructionProjectTemporary.getProjectName());
                    objects.add(maps);
                }
            }
        }
        return objects;
    }
    //子集
    public static boolean  getTreeChild(Map<String,Object> maps,List<ConstructionProjectTemporary> constructionProjectTemporaries,String id){
        boolean flag = false;
        List<Map<String,Object>> lists = Lists.newArrayList();
        Map<String,Object> mapList = null;
        String[] split = null;//截取多个父类的集合
        for (ConstructionProjectTemporary constructionProjectTemporary:constructionProjectTemporaries){
            if (constructionProjectTemporary.getParentId().contains(",")){
                split = constructionProjectTemporary.getParentId().split(",");
                for (String string:split){
                    if (string.equals(id)){
                        mapList = Maps.newHashMap();
                        mapList.put("id",constructionProjectTemporary.getId());
                        mapList.put("name",constructionProjectTemporary.getProjectName());
                        lists.add(mapList);
                    }
                }
            }else {
                if (constructionProjectTemporary.getParentId().equals(id)){
                    mapList = Maps.newHashMap();
                    mapList.put("id",constructionProjectTemporary.getId());
                    mapList.put("name",constructionProjectTemporary.getProjectName());
                    lists.add(mapList);
                }
            }
        }
        if (lists.size()>0){
            flag = true;
            maps.put("children",lists);
        }
        return flag;
    }
//    public static void getTreeChild(Map<String,Object> mapList,List<ConstructionProject> constructionProjects,String lineName){
//        List<Map<String,Object>> lists = Lists.newArrayList();
//        Map<String,Object> maps =Maps.newHashMap();
//        for (ConstructionProject constructionProject:constructionProjects){
//            if (lineName.equals(constructionProject.getLineName())){
//                maps = Maps.newHashMap();
//                maps.put("id",constructionProject.getId());
//                maps.put("name",constructionProject.getProjectName());
//                lists.add(maps);
//            }
//        }
//        if (lists.size()>0){
//            mapList.put("children",lists);
//        }
//    }
    //获取月份
    public static List<ConstructionProject> getMonths(List<ConstructionProject> constructionProjects,String time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String arrangeTime = "";//安排计划
        List<ConstructionProject> constructionProjectList = new ArrayList<ConstructionProject>();
        for (ConstructionProject constructionProject:constructionProjects){
            if (null!=constructionProject.getArrangeTime()){
                arrangeTime = sdf.format(constructionProject.getArrangeTime());
                if (arrangeTime.equals(time)){
                    constructionProjectList.add(constructionProject);
                }
            }
        }
        return constructionProjectList;
    }

    //获取id类型
    public static String getConstructionId(ConstructionProject constructionProject){
        String projectId = "";
        String cid= "";
        String status = "";//状态默认为0
        if (null!=constructionProject.getId()&&!"".equals(constructionProject.getId())){
            cid = constructionProject.getId();
            projectId = cid.substring(0,cid.length()-4);
            if (cid.substring(cid.length()-4).equals("time")){
                status = "time";
            }
            if (cid.substring(cid.length()-4).equals("line")){
                status = "line";
            }
        }
        if ("".equals(status)){
            constructionProject.setId(constructionProject.getId());
        }else {
            constructionProject.setId(projectId);
        }
        return status;
    }


    //获取修改的值：
    public static List<ConstructionProjectTemporary> getUpdateConstruction(List<ConstructionProjectTemporary> constructionProjectTemporaries){
        for (ConstructionProjectTemporary constructionProjectTemporary:constructionProjectTemporaries){
            //线路修改的状态
            if ("0".equals(constructionProjectTemporary.getParentId())){
                    constructionProjectTemporary.setStatus("2");
//                getUpdateConstructionChild1(constructionProjectTemporaries,constructionProjectTemporary.getId());
            }else {
                if ("0".equals(constructionProjectTemporary.getStatus())){
                    constructionProjectTemporary.setStatus("1");
//                    getUpdateConstructionChild2(constructionProjectTemporaries,constructionProjectTemporary);
                }
            }
        }
        return constructionProjectTemporaries;
    }
    //线路修改的状态-----01
    public static void getUpdateConstructionChild1(List<ConstructionProjectTemporary> constructionProjectTemporaries,String id){
        for (ConstructionProjectTemporary constructionProjectTemporary:constructionProjectTemporaries){
            if (!"0".equals(constructionProjectTemporary.getParentId())){
                if (constructionProjectTemporary.getParentId().equals(id)){
                    constructionProjectTemporary.setStatus("1");
                }
            }
        }
    }
    //项目修改的状态----02
    public static void getUpdateConstructionChild2(List<ConstructionProjectTemporary> constructionProjectTemporaries,ConstructionProjectTemporary constructionProjectTemporary){
        String parentId = constructionProjectTemporary.getParentId();
        for (ConstructionProjectTemporary project : constructionProjectTemporaries){
            if (parentId.equals(project.getId())){
                project.setStatus("2");
                break;
            }
        }
    }
}
