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

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.modules.sg.managementcenter.constructionProject.entity.ConstructionProject;
import org.hibernate.validator.constraints.EAN;

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

public class TreeTableUtil {
    //树生成
    public static List<Map<String,Object>> getBootstrapTreeTable(List<Map<String,Object>> objects,List<ConstructionProject> constructionProjects){
        List<ConstructionProject> lineNames = new ArrayList<ConstructionProject>();
        Map<String,Object> maps = Maps.newHashMap();
        boolean flag = false;
        for (ConstructionProject constructionProject:constructionProjects){
            flag = true;
            for (ConstructionProject project:lineNames){
                if (project.getLineName().equals(constructionProject.getLineName())){
                    flag = false;
                    break;
                }
            }
            if (flag){
                lineNames.add(constructionProject);
                maps = Maps.newHashMap();
                maps.put("id",constructionProject.getId()+"line");
                maps.put("name",constructionProject.getLineName());
                getTreeChild(maps,constructionProjects,constructionProject.getLineName());
                objects.add(maps);
            }
        }
        return objects;
    }

    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;
    }
    //右侧树


//    //右边树形表格
//    public static List<Map<String,Object>> getParentConstructionMap(List<Map<String, Object>> mapList,List<ConstructionProject> constructionProjects){
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        Map<String,Object> map = new HashMap<String, Object>();
//        List<ConstructionProject> constructionProjectList = new ArrayList<ConstructionProject>();
//        boolean flag = false;
//        for (ConstructionProject constructionProject:constructionProjects){
//            flag = true;
//            for (ConstructionProject constructionProject1:constructionProjectList){
//                int i = constructionProject1.getArrangeTime().compareTo(constructionProject.getArrangeTime());
//                if (i==0){
//                    flag = false;
//                    break;
//                }
//            }
//            if (flag){
//                constructionProjectList.add(constructionProject);
//                map = Maps.newHashMap();
//                String format = sdf.format(constructionProject.getArrangeTime());
//                map.put("id",constructionProject.getId()+"time");
//                map.put("name",format);
//                getRightChildTable(map,constructionProjects,constructionProject.getArrangeTime());
//                mapList.add(map);
//            }
//        }
//        return mapList;
//    }
//    public static void getRightChildTable(Map<String,Object> mapList, List<ConstructionProject> constructionProjects, Date date){
//        List<Map<String,Object>> lists = Lists.newArrayList();
//        Map<String,Object> maps =Maps.newHashMap();
//        List<ConstructionProject> constructionProjects1 = new ArrayList<ConstructionProject>();
//        boolean flag = false;
//        for (ConstructionProject constructionProject:constructionProjects){
//            int i = date.compareTo(constructionProject.getArrangeTime());
//            if (i==0){
//                flag = true;
//                for (ConstructionProject constructionProject1:constructionProjects1){
//                    if (constructionProject.getLineName().equals(constructionProject1.getLineName())){
//                        flag = false;
//                        break;
//                    }
//                }
//                if (flag){
//                    constructionProjects1.add(constructionProject);
//                    maps = Maps.newHashMap();
//                    maps.put("id",constructionProject.getId()+"line");
//                    maps.put("name",constructionProject.getLineName());
//                    getRightChildTable2(maps,constructionProjects,constructionProject.getLineName(),date);
//                    lists.add(maps);
//                }
//            }
//        }
//        if (lists.size()>0){
//            mapList.put("children",lists);
//        }
//    }
//    //获取项目名称
//    public static void getRightChildTable2(Map<String,Object> mapList, List<ConstructionProject> constructionProjects, String lineName,Date date){
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
//        List<Map<String,Object>> lists = Lists.newArrayList();
//        Map<String,Object> maps =Maps.newHashMap();
//        String str = "";
//        for (ConstructionProject constructionProject:constructionProjects){
//            if (lineName.equals(constructionProject.getLineName())){
//                int i = date.compareTo(constructionProject.getArrangeTime());
//                if (i==0){
//                    maps = Maps.newHashMap();
//                    maps.put("id",constructionProject.getId());
//                    if (null!=constructionProject.getSpecificTime()){
//                       str =  constructionProject.getProjectName()+"(具体时间安排为："+sdf.format(constructionProject.getSpecificTime())+")";
//                    }else {
//                        str =  constructionProject.getProjectName()+"(暂无时间安排)";
//                    }
//                    maps.put("name",str);
//                    lists.add(maps);
//                }
//            }
//        }
//        if (lists.size()>0){
//            mapList.put("children",lists);
//        }
//    }

    //获取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;
    }
}
