package com.mandi.common;

import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

public class ListSortUtil<T> {
	
	 @SuppressWarnings({ "unchecked", "rawtypes" })  
	    public void sort(List<T> targetList, final String sortField, final String sortMode) {  
	      
	        Collections.sort(targetList, new Comparator() {  
	            public int compare(Object obj1, Object obj2) {   
	                int retVal = 0;  
	                try {  
	                    //首字母转大写  
	                    String newStr=sortField.substring(0, 1).toUpperCase()+sortField.replaceFirst("\\w","");   
	                    String methodStr="get"+newStr;  
	                      
	                    Method method1 = ((T)obj1).getClass().getMethod(methodStr, null);  
	                    Method method2 = ((T)obj2).getClass().getMethod(methodStr, null);  
	                    if (sortMode != null && "desc".equals(sortMode)) {  
	                    	if(method1.invoke(((T) obj2), null)==null||method2.invoke(((T) obj1), null)==null){
                				retVal=1;
                			}else {
                				retVal = method1.invoke(((T) obj2), null).toString().compareTo(method2.invoke(((T) obj1), null).toString()); // 倒序  
                			}
	                    } else {  
	                    	if(method2.invoke(((T) obj2), null)==null||method1.invoke(((T) obj1), null)==null){
                				retVal=-1;
                			}else {
                				retVal = method2.invoke(((T) obj1), null).toString().compareTo(method1.invoke(((T) obj2), null).toString()); // 正序  
                			}
	                    }  
	                } catch (Exception e) {  
	                    throw new RuntimeException();  
	                }  
	                return retVal;  
	            }  
	        });  
	    } 
	 
	 
	 @SuppressWarnings({ "unchecked", "rawtypes" })  
	    public void sortFields(List<T> targetList, final List<String> sortFields) {  
	      
	        Collections.sort(targetList, new Comparator() {  
	            public int compare(Object obj1, Object obj2) {   
	                int retVal = 0;  
	                try {  
	                	for (int i=0;i<sortFields.size();i++) {
	                		String sortField=sortFields.get(i).split("-")[0];
	                		String sortMode=sortFields.get(i).split("-")[1];
	                		//首字母转大写  
	                		String newStr=sortField.substring(0, 1).toUpperCase()+sortField.replaceFirst("\\w","");   
	                		String methodStr="get"+newStr;  
	                		
	                		Method method1 = ((T)obj1).getClass().getMethod(methodStr, null);  
	                		Method method2 = ((T)obj2).getClass().getMethod(methodStr, null); 
	                		if(method1==null|| method2==null){
	                			continue;
	                		}
	                	    Type tp =method1.getGenericReturnType();
	                	    String tt="";
	                	    boolean sz=false;
	                	    if(tp!=null){
	                	    	tt=tp.toString();
	                	    	if(!StringUtils.isEmpty(tt)){
	                	    		if(tt.contains("int")||tt.contains("double")){
	                	    			sz=true;
	                	    		}
	                	    	}
	                	    }
	                		if (sortMode != null && "desc".equals(sortMode)) {  
	                			if(method2.invoke(((T) obj2), null)==null){
	                				retVal=0;
	                				continue;
	                			}else if(method1.invoke(((T) obj1), null)==null){
	                				retVal=0;
	                				continue;
	                			}else {
	                				if(sz){
	                					if(!method1.invoke(((T) obj2), null).toString().equals(method2.invoke(((T) obj1), null).toString())){
	                						if(tt.contains("int")){
	                							if(Integer.parseInt(method1.invoke(((T) obj2), null).toString())>Integer.parseInt(method2.invoke(((T) obj1), null).toString())){
	                								retVal = 1; // 倒序  
	                							}else if(Integer.parseInt(method1.invoke(((T) obj2), null).toString())<Integer.parseInt(method2.invoke(((T) obj1), null).toString())){
	                								retVal = -1;
	                							}else{
	                								retVal = 0; // 倒序  
	                								continue;
	                							}
	                							break;
	                						}else if(tt.contains("double")){
	                							if(Double.parseDouble((method1.invoke(((T) obj2), null).toString()))>Double.parseDouble(method2.invoke(((T) obj1), null).toString())){
	                								retVal = 1; // 倒序  
	                							}else if(Double.parseDouble(method1.invoke(((T) obj2), null).toString())<Double.parseDouble(method2.invoke(((T) obj1), null).toString())){
	                								retVal = -1;
	                							}else{
	                								retVal = 0; // 倒序  
	                								continue;
	                							}
	                							break;
	                						}
	                					}else{
	                						retVal =0;
	                						continue;
	                					}
	                				}else{
	                					if(!method1.invoke(((T) obj2), null).toString().equals(method2.invoke(((T) obj1), null).toString())){
	                						retVal = method1.invoke(((T) obj2), null).toString().compareTo(method2.invoke(((T) obj1), null).toString()); // 倒序  
	                						break;
	                					}else{
	                						retVal =0;
	                						continue;
	                					}
	                				}
	                				
	                			}
	                		} else {  
	                			if(method1.invoke(((T) obj1), null)==null){
	                				retVal=0;
	                				continue;
	                			}else if(method2.invoke(((T) obj2), null)==null){
	                				retVal=0;
	                				continue;
	                			}else {
	                				if(sz){
	                					if(!method2.invoke(((T) obj1), null).toString().equals(method1.invoke(((T) obj2), null).toString())){
	                						if(tt.contains("int")){
	                							if(Integer.parseInt(method1.invoke(((T) obj1), null).toString())>Integer.parseInt(method2.invoke(((T) obj2), null).toString())){
	                								retVal =1;
	                								break;
	                							}else if(Integer.parseInt(method1.invoke(((T) obj1), null).toString())<Integer.parseInt(method2.invoke(((T) obj2), null).toString())){
	                								retVal =-1;
	                								break;
	                							}else{
	                								retVal=0;
	    	                						continue;
	                							}
	                						}else if(tt.contains("double")){
	                							if(Double.parseDouble(method1.invoke(((T) obj1), null).toString())>Double.parseDouble(method2.invoke(((T) obj2), null).toString())){
	                								retVal =1;
	                								break;
	                							}else if(Double.parseDouble(method1.invoke(((T) obj1), null).toString())<Double.parseDouble(method2.invoke(((T) obj2), null).toString())){
	                								retVal =-1;
	                								break;
	                							}else{
	                								retVal=0;
	    	                						continue;
	                							}
	                						}
	                					}else{
	                						retVal=0;
	                						continue;
	                					}
	                				}else{
	                					if(!method2.invoke(((T) obj1), null).toString().equals(method1.invoke(((T) obj2), null).toString())){
	                						retVal = method2.invoke(((T) obj1), null).toString().compareTo(method1.invoke(((T) obj2), null).toString()); // 正序  
	                						break;
	                					}else{
	                						retVal=0;
	                						continue;
	                					}
	                				}
	                			}
	                		}  
						}
	                } catch (Exception e) {  
	                    throw new RuntimeException();  
	                }  
	                return retVal;  
	            }  
	        });  
//	        System.out.println("targetList:::::"+Jacksonmethod.tojson(targetList, false));
	    } 

}