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();
|
}
|
}
|