package com.ruoyi.common.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

public class ListPageUtil {

    /**
     * 对List进行排序操作
     *
     * @param list    需要排序的列表
     * @param orderBy 需要进行排序的对象字段（需要含有相应的get方法）
     * @param sort    asc 正序，desc 反序
     * @param <E>     泛型
     */
    public static <E> void sort(List<E> list, String orderBy, String sort) {
        //1.获取指定字段的get方法名
        String methodName = "get" + orderBy.toUpperCase().substring(0, 1).toUpperCase() + orderBy.substring(1);

        list.sort((a, b) -> {
            int ret = 0;
            try {
                //2.获取方法
                Method m1 = a.getClass().getMethod(methodName, null);
                Method m2 = b.getClass().getMethod(methodName, null);

                Object rs1 = null;
                Object rs2 = null;
                //3.获取方法返回值
                if (m1.invoke(a, null) != null) {
                    rs1 = m1.invoke(a, null);
                }
                if (m2.invoke(a, null) != null) {
                    rs2 = m2.invoke(b, null);
                }

                //4.判断字符串是否为数字，若为数字则直接加减
                if (rs1 != null && rs2 != null &&  rs1 instanceof Integer) {
                    if ("desc".equals(sort)) {
                        ret = Integer.valueOf(rs2.toString()).compareTo(Integer.valueOf(rs1.toString()));
                    } else {
                        ret = Integer.valueOf(rs1.toString()).compareTo(Integer.valueOf(rs2.toString()));
                    }
                } else if (rs1 != null && rs2 != null && rs1 instanceof Long) {
                    if ("desc".equals(sort))//倒序
                        ret = ((Long)rs2).compareTo((Long)rs1);
                    else//正序
                        ret = ((Long)rs1).compareTo((Long)rs2);
                } else if (rs1 != null && rs2 != null && rs1 instanceof Float) {
                    if ("desc".equals(sort))//倒序
                        ret = ((Float)rs2).compareTo((Float)rs1);
                    else//正序
                        ret = ((Float)rs1).compareTo((Float)rs2);
                } else if (rs1 != null && rs2 != null && rs1 instanceof Double) {
                    if ("desc".equals(sort))//倒序
                        ret = ((Double)rs2).compareTo((Double)rs1);
                    else//正序
                        ret = ((Double)rs1).compareTo((Double)rs2);
                } else if (rs1 != null && rs2 != null && rs1 instanceof String) {
                    if ("desc".equals(sort))//倒序
                        ret = ((String)rs2).compareTo((String)rs1);
                    else//正序
                        ret = ((String)rs1).compareTo((String)rs2);
                } else if (rs1 != null && rs2 != null && rs1 instanceof Date) {
                    if ("desc".equals(sort))//倒序
                        ret = ((Date)rs2).compareTo((Date)rs1);
                    else//正序
                        ret = ((Date)rs1).compareTo((Date)rs2);
                }
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ne) {
                ne.printStackTrace();
            }
            return ret;
        });
    }

    /**
     * 分页排序
     *
     * @param list     待处理列表
     * @param pageNum  页数
     * @param pageSize 分页大小
     * @param orderBy  根据什么排序
     * @param sortBy   正序反序（asc、desc）
     * @param <E>      任意对象
     * @return 处理好的数据
     */
    public static <E> List<E> divisionList(List<E> list, int pageNum, int pageSize, String orderBy, String sortBy) {
        int total = list.size();
        int startIndex = pageNum * pageSize - pageSize;
        int endIndex = pageNum * pageSize;
        if (total < startIndex) {
            startIndex = total;
        }
        if (total < endIndex) {
            endIndex = total;
        }
        if (orderBy != null && !"".equals(orderBy)) {
            sort(list, orderBy, sortBy);
        }
        return list.subList(startIndex, endIndex);
    }

}
