package com.sysu.admin.utils;

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


/**
 * 泛型工具类（数据的copy 查找 复制等等）
 * @author 86137
 * @param <T>
 */
public class GenericsUtil<T> {
	
	Class<T> clazz;
	
	public GenericsUtil(Class<T> clazz) {
		this.clazz=clazz;
	}
	
	/**
	 * 在集合 中找到与values 每个值相等的元素 
	 * @param dataSource  （数据源）
	 * @param fieldString （要匹配的字段名）
	 * @param values  格式例子 111,222,333
	 * @return 返回找到的元素集合
	 */
	public List<T> findListItems(List<T> dataSource,String fieldString,String values) {
		String[] valueArray= values.split(",");
		List<T> resultList=new ArrayList<T>(valueArray.length);
		for(T t:dataSource) {
			try {
				Field field= clazz.getDeclaredField(fieldString);
				field.setAccessible(true);
				for(int i=0;i<valueArray.length;i++) {
					if(valueArray[i]!=null && valueArray[i].equals(field.get(t))){
						resultList.add(t);
						valueArray[i]=null;
						break;
					}
				}
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return resultList;
	}
	
	/**
	   *   找到集合中的某个字段的拼接字符串
	 * @param dataSource
	 * @param fieldString
	 * @return 格式例子 111,222,333
	 */
	public String findListIds(List<T> dataSource,String fieldString) {
		StringBuffer ids=new StringBuffer();
		for(T t:dataSource) {
			try {
				Field field= clazz.getDeclaredField(fieldString);
				field.setAccessible(true);
				ids.append(",").append(field.get(t));
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		if(ids.length()>0) {
			ids.delete(0,1);
		}
		return ids.toString();
	}
	
	/**
	 * 找到集合中的某个字段的数组
	 * @param dataSource
	 * @param fieldString
	 * @return
	 */
	public Long[] findNumberArray(List<T> dataSource,String fieldString) {
		Long[] nunmberArray = new Long[dataSource.size()];
		for(Integer i = 0 ; i < dataSource.size() ;i++) {
			T t = dataSource.get(i);
			try {
				Field field = clazz.getDeclaredField(fieldString);
				field.setAccessible(true);
				nunmberArray[i] = (Long)field.get(t);
				
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return nunmberArray;
	}
	
	/**
	 * 找到集合中的某个字段的数组
	 * @param dataSource
	 * @param fieldString
	 * @return
	 */
	public List<Integer> findNumberList(List<T> dataSource,String fieldString) {
		List<Integer> nunmberArray = new ArrayList(dataSource.size());
		for(Integer i = 0 ; i < dataSource.size() ;i++) {
			T t = dataSource.get(i);
			try {
				Field field = clazz.getDeclaredField(fieldString);
				field.setAccessible(true);
				nunmberArray.add((Integer)field.get(t));
				
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return nunmberArray;
	}
	
	/**
	   *   找到集合中的某个字段的拼接字符串
	 * @param dataSource
	 * @param fieldString
	 * @param clearRepeat 是否去除重复
	 * @return 格式例子 111,222,333
	 */
	public String findListIds(T[] dataSource,String fieldString,boolean clearRepeat) {
		StringBuffer ids=new StringBuffer();
		Set set=null;
		if(clearRepeat) {
			set=new HashSet();
		}
		for(T t:dataSource) {
			try {
				Field field= clazz.getDeclaredField(fieldString);
				field.setAccessible(true);
				if(clearRepeat) {
					if(!set.contains(field.get(t))) {
						ids.append(",").append(field.get(t));
						set.add(field.get(t));
					}
				}else {
					ids.append(",").append(field.get(t));
				}
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		if(ids.length()>0) {
			ids.delete(0,1);
		}
		return ids.toString();
	}


	/**
	 * 分隔数组 根据段数分段 <多出部分在最后一个数组>
	 *
	 * @param data     被分隔的数组
	 * @param segments 需要分隔的段数
	 * @return
	 */
	public List<List<T>> subListBySegment(List<T> data, int segments) {
		List<List<T>> result = new ArrayList<>();

		int size = data.size();// 数据长度

		if (size > 0 && segments > 0) {// segments == 0 ，不需要分隔

			int count = size / segments;// 每段数量

			List<T> cutList = null;// 每段List

			for (int i = 0; i < segments; i++) {
				if (i == segments - 1) {
					cutList = data.subList(count * i, size);
				} else {
					cutList = data.subList(count * i, count * (i + 1));
				}
				result.add(cutList);
			}
		} else {
			result.add(data);
		}
		return result;
	}




}
