package com.hssx.pcbms.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * Author: 吴涛涛 cuiyi@itany.com
 * Date : 2019 - 07 - 25 16:56
 * Description:<描述>处理字符串转成集合的
 * Version: 1.0
 */

public class ListUtil {
	/**
	 * 
	 * @param idStr 1,2,3,4,5字符串
	 * @return List<Long>
	 */
	public static List<Long> convertIdsArrayToList(String idStr) {
		String[] array = idStr.split(",");
		List<Long> ids = new ArrayList<Long>();
		for (String a : array) {
			if (a != null && a.length() > 0) {
				ids.add(Long.valueOf(a));
			}
		}
		return ids;
	}

	/**
	 * Long
	 * @param idStr  1,2,3,4,5字符串
	 * @return
	 */
	public static List<String> convertLongIdsArrayToList(String idStr) {
		String[] array = idStr.split(",");
		List<String> ids = new ArrayList<String>();
		for (String a : array) {
			if (a != null && a.length() > 0) {
				ids.add(a);
			}
		}
		return ids;
	}
	/**
	 * Long
	 * @param idStr  1,2,3,4,5字符串
	 * @return
	 */
	public static List<Integer> convertIntegerIdsArrayToList(String idStr) {
		String[] array = idStr.split(",");
		List<Integer> ids = new ArrayList<Integer>();
		for (String a : array) {
			if (a != null && a.length() > 0) {
				ids.add(Integer.parseInt(a));
			}
		}
		return ids;
	}

	public static List<Integer> extractIdFromList(List object, String key) {
		List<Integer> list = new ArrayList<Integer>();
        for (Object obj : object) {
            // 得到类对象
            Class userCla = (Class) obj.getClass();
            /* 得到类中的所有属性集合 */
            Field[] fs = userCla.getDeclaredFields();
            for (int i = 0; i < fs.length; i++) {
                Field f = fs[i];
                f.setAccessible(true); // 设置些属性是可以访问的
                try {
                    if (f.getName().equals(key)) {
                        list.add((Integer)f.get(obj));
                    }
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
	}
	
	
	public static List<String> extractNameFromList(List object, String key) {
		List<String> list = new ArrayList<String>();
		for (Object obj : object) {
			// 得到类对象
			Class userCla = (Class) obj.getClass();
			/* 得到类中的所有属性集合 */
			Field[] fs = userCla.getDeclaredFields();
			for (int i = 0; i < fs.length; i++) {
				Field f = fs[i];
				f.setAccessible(true); // 设置些属性是可以访问的
				try {
					if (f.getName().equals(key)) {
						list.add((String)f.get(obj));
					}
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		return list;
	}
	
	public static List<Integer> addList(String ids,List<Integer> idList){
		String[] joinStr = ids.split(",");
		boolean isCf = false;
		for(String id : joinStr){
			if (id != null && id.length() > 0) {
				for(int i = 0;i<idList.size();i++){
					if(Integer.valueOf(id).intValue() != idList.get(i).intValue()){
						isCf = false;
					}else{
						isCf = true;
						break;
					}
				}
				if(!isCf){
					idList.add(Integer.valueOf(id));
				}
			}
		}
		
		return idList;
	}
	
	//去重
	public static List removeDuplicateData(List list) {
		HashSet set = new HashSet();
		set.addAll(list);
		list.clear();
		list.addAll(set);
		return list;
	}
	
	public static void main(String[] args) {
		String str = "123|456";
		System.out.println(str.contains("|"));
	}
}
