/** 
 * @Title: ffds.java 
 * @Package com.zhiweidata.bjfzxWebapp.util 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author xuyimeng 
 * @date 2018年2月6日 上午11:57:05 
 * @version V1.0 
 */ /**
 * 
 */
package com.zhiweidata.bjfzxWebapp.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.mongodb.DBObject;


/** 
 * @ClassName: ffds 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author xuyimeng 
 * @date 2018年2月6日 上午11:57:05  
 */
public class TreatOrder
{
	 /**
     * @deprecated:对集合数据降序
     * @param List<HashMap> list 需要排序的集合
     * @param String field 需要排序的字段
     * @reutrn List<HashMap>
     **/
    public static List<DBObject> treatOrderDESC(List<DBObject> list, final String field)
    {
        Collections.sort(list, new Comparator<DBObject>()
        {
            public int compare(DBObject o1, DBObject o2)
            {
                int fensi1 = Integer.parseInt(o1.get(field).toString());
                int fensi2 = Integer.parseInt(o2.get(field).toString());
                return fensi2 - fensi1;
            }
        });
        return list;
    }
	
    /**
     * @deprecated:对集合数据按时间降序
     * @param List<HashMap> list 需要排序的集合
     * @param String field 需要排序的字段
     * @reutrn List<HashMap>
     **/
    public static List<DBObject> treatOrderFollowTime(List<DBObject> list, final String field)
    {
    	
        Collections.sort(list, new Comparator<DBObject>()
        {
            public int compare(DBObject o1, DBObject o2)
            {
//                int fensi1 = Integer.parseInt(o1.get(field).toString());
//                int fensi2 = Integer.parseInt(o2.get(field).toString());
            	Date fensi1 = (Date) o1.get(field);
            	Date fensi2 = (Date) o2.get(field);
                return (int) (fensi2.getTime() - fensi1.getTime());
            }
        });
        return list;
    }
    
    /**
     * @deprecated:对集合数据升序
     * @param List<HashMap> list 需要排序的集合
     * @param String field 需要排序的字段
     * @reutrn List<HashMap>
     **/
    @SuppressWarnings({"rawtypes"})
    public static List treatOrderASC(ArrayList<Map<String, Object>> lists1, final String field)
    {
        Collections.sort(lists1, new Comparator<Map>()
        {
            public int compare(Map o1, Map o2)
            {
                int fensi1 = Integer.parseInt(o1.get(field).toString());
                int fensi2 = Integer.parseInt(o2.get(field).toString());
                return fensi1 - fensi2;
            }
        });
        return lists1;
    }
    
    /**
     * @deprecated:按照时间升序排列 时间格式为yyyy-MM-dd HH:mm
     * @param List<HashMap> list 要排序的事件
     * @return list
     *         
     **/
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static List treatOrderByDateAsc(List list)
    {
        Collections.sort(list, new Comparator<HashMap>()
        {
            public int compare(HashMap o1, HashMap o2)
            {
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                int time1 = 0;
                int time2 = 0;
                try
                {
                    time1 = Long.valueOf(sf.parse(o1.get("time").toString()).getTime()).intValue();
                    time2 = Long.valueOf(sf.parse(o2.get("time").toString()).getTime()).intValue();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    return 0;
                }
                return time1 - time2;
            }
        });
        return list;
    }
    
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static List treatOrderByDateDesc(List list)
    {
        Collections.sort(list, new Comparator<HashMap>()
        {
            public int compare(HashMap o1, HashMap o2)
            {
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                int time1 = 0;
                int time2 = 0;
                try
                {
                    time1 = Long.valueOf(sf.parse(o1.get("time").toString()).getTime()).intValue();
                    time2 = Long.valueOf(sf.parse(o2.get("time").toString()).getTime()).intValue();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    return 0;
                }
                return time2 - time1;
            }
        });
        return list;
    }
    
    /**
     * @deprecated:按照时间升序排列 时间格式为yyyy-MM-dd HH
     * @param HashMap<String, Integer> timeLine 要排序的事件
     * @return list
     *         
     **/
    @SuppressWarnings("unchecked")
    public static List<Map.Entry<String, Integer>> treatOrderByDateAsc(HashMap<String, Integer> timeLine)
    {
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(timeLine.entrySet());
        
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>()
        {
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2)
            {
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH");
                int time1 = 0;
                int time2 = 0;
                try
                {
                    time1 = Long.valueOf(sf.parse(o1.getKey()).getTime()).intValue();
                    time2 = Long.valueOf(sf.parse(o2.getKey()).getTime()).intValue();
                }
                catch (ParseException e)
                {
                    e.printStackTrace();
                }
                return time1 - time2;
            }
        });
        return list;
    }
    
    /**
     * @deprecated:按照时间降序排列 时间格式为yyyy-MM-dd
     * @param HashMap<String, Integer> timeLine 要排序的事件
     * @return list
     *         
     **/
    @SuppressWarnings("unchecked")
    public static List<Map.Entry<String, Integer>> treatOrderByDateDesc(HashMap<String, Integer> timeLine)
    {
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(timeLine.entrySet());
        
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>()
        {
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2)
            {
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
                int time1 = 0;
                int time2 = 0;
                try
                {
                    time1 = Long.valueOf(sf.parse((String)o1.getKey()).getTime()).intValue();
                    time2 = Long.valueOf(sf.parse((String)o2.getKey()).getTime()).intValue();
                }
                catch (ParseException e)
                {
                    e.printStackTrace();
                }
                return time2 - time1;
            }
        });
        return list;
    }
    
    /**
     * @deprecated:按照数量降序排序
     * @param HashMap<String,Integer> dataMap 数据集合
     * @return List
     ***/
    @SuppressWarnings({"rawtypes"})
    public static List<Entry<String, Integer>> treatOrderByCountDesc(HashMap<String, Integer> dataMap)
    {
        Map<String, Integer> map = dataMap;
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>();
        list.addAll(map.entrySet());
        
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>()
        {
            public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2)
            {
                return o2.getValue() - o1.getValue();
            }
        });
        return list;
    }
    
    /**
     * 根据字段降序对mapList进行降序
     * 
     * @param @param list 需要降序的List
     * @param @param field 字段
     * @param @return 设定文件
     * @return List 返回类型
     */
    @SuppressWarnings({"rawtypes"})
    public static ArrayList treatOrderDESCByMap(ArrayList<Map<String, Object>> top, final String field)
    {
        Collections.sort(top, new Comparator<Map>()
        {
            public int compare(Map o1, Map o2)
            {
                int fensi1 = Integer.parseInt(o1.get("" + field + "").toString());
                int fensi2 = Integer.parseInt(o2.get("" + field + "").toString());
                return fensi2 - fensi1;
            }
        });
        return top;
    }
}
    