#
Junjie
1 天以前 536e17a446c170a8b214aaf188b98817ee6258c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.core.controller;
 
import com.core.common.BaseRes;
import com.core.common.Cools;
import com.core.exception.CoolException;
 
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public abstract class AbstractBaseController {
 
    public <T> List exportSupport(List<T> list, List<String> fields) {
        if (Cools.isEmpty(list)) {
            throw new CoolException(BaseRes.EMPTY);
        }
        try {
            List<Object> result = new ArrayList<>();
            Method[] methods = list.get(0).getClass().getMethods();
            for (T item : list) {
                List<Object> row = new ArrayList<>();
                for (String field : fields) {
                    for (Method method : methods) {
                        if (("get" + field).toLowerCase().equals(method.getName().toLowerCase())) {
                            Object value = method.invoke(item);
                            row.add(value);
                            break;
                        }
                    }
                }
                result.add(row);
            }
            return result;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
    public static Map<String, Object> excludeTrash(Map<String, Object> map) {
        if (Cools.isEmpty(map)) {
            return new HashMap<>();
        }
        Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            String key = entry.getKey();
            if ("curr".equals(key)
                    || "limit".equals(key)
                    || "orderByField".equals(key)
                    || "orderByType".equals(key)
                    || "condition".equals(key)
                    || Cools.isEmpty(entry.getValue())) {
                iterator.remove();
            }
        }
        return map;
    }
 
    public static String humpToLine(String str) {
        Pattern pattern = Pattern.compile("[A-Z]");
        Matcher matcher = pattern.matcher(str);
        StringBuffer buffer = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(buffer, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(buffer);
        return buffer.toString();
    }
}