ecology_maven/aiyh/utils/Util.java

1394 lines
43 KiB
Java
Raw Normal View History

2021-11-14 15:29:16 +08:00
package aiyh.utils;
import aiyh.utils.annotation.DateFormatAn;
import aiyh.utils.entity.AInputStream;
import aiyh.utils.entity.AZipOutputStream;
import aiyh.utils.entity.ApiConfigMainDTO;
import aiyh.utils.entity.ListZipEntity;
import aiyh.utils.mapUtil.UtilHashMap;
import aiyh.utils.mapUtil.UtilLinkedHashMap;
import aiyh.utils.service.UtilService;
import aiyh.utils.sqlUtil.builderSql.impl.BuilderSqlImpl;
import aiyh.utils.sqlUtil.whereUtil.Where;
import aiyh.utils.sqlUtil.whereUtil.impl.PrepWhereImpl;
import aiyh.utils.sqlUtil.whereUtil.impl.WhereImpl;
import aiyh.utils.zwl.common.ToolUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ibm.icu.text.SimpleDateFormat;
2021-11-16 09:32:09 +08:00
import org.h2.util.StringUtils;
2021-11-14 15:29:16 +08:00
import weaver.common.util.string.StringUtil;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;
import weaver.workflow.workflow.WorkflowVersion;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.text.ParseException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
/**
* @author EBU7-dev1-ayh
* @date 2021/8/23 0023 11:42
* dao
*/
public class Util extends weaver.general.Util {
static ToolUtil toolUtil = new ToolUtil();
private static final UtilService utilService = new UtilService();
private static final RecordSet rs = new RecordSet();
/**
*
*
* @param format
* @return
*/
public static String getTime(String format) {
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(format);
return formatter.format(date);
}
/**
* SQL where使
*
* @return Where
*/
@Deprecated
public static Where createWhereImpl() {
return new WhereImpl();
}
/**
* SQL where
*
* @return Where
*/
public static Where createPrepWhereImpl() {
return new PrepWhereImpl();
}
/**
* Map
*
* @return UtilHashMap
*/
public static UtilHashMap<String, Object> createUtilHashMap() {
return new UtilHashMap<>();
}
/**
* SQLSQL
*
* @return BuilderSqlImpl
*/
public static BuilderSqlImpl createSqlBuilder() {
return new BuilderSqlImpl();
}
/**
* Map
*
* @return UtilLinkedHashMap
*/
public static UtilLinkedHashMap<String, Object> createUtilLinkedHashMap() {
return new UtilLinkedHashMap<>();
}
/**
* map
*
* @param map map
* @return 0
*/
public static boolean mapIsNullOrEmpty(Map map) {
return Objects.isNull(map) || map.size() == 0;
}
/**
*
*
* @param sqlBuilder SQL StringBuilder
* @return SQL
*/
public static String removeSeparator(StringBuilder sqlBuilder) {
String str = sqlBuilder.toString().trim();
String removeSeparator = ",";
if (str.endsWith(removeSeparator)) {
// 如果以分割号结尾,则去除分割号
str = str.substring(0, str.length() - 1);
}
if (str.trim().startsWith(removeSeparator)) {
str = str.substring(1);
}
return str;
}
public static String removeSeparator(StringBuilder sqlBuilder, String removeSeparator) {
String str = sqlBuilder.toString().trim();
if (str.endsWith(removeSeparator)) {
// 如果以分割号结尾,则去除分割号
str = str.substring(0, str.length() - 1);
}
if (str.trim().startsWith(removeSeparator)) {
str = str.substring(1);
}
return str;
}
/**
* RecordSetmap
*
* @param rs RecordSet
* @param conversion
* @return Map
*/
private static Map<String, Object> getMapMapping(RecordSet rs, boolean conversion) {
Map<String, Object> map = new HashMap<>();
String[] columnType = rs.getColumnTypeName();
int colCounts = 0;
colCounts = rs.getColCounts() == 0 ? columnType.length : rs.getColCounts();
toolUtil.writeErrorLog(Arrays.toString(columnType));
toolUtil.writeErrorLog("字段数:" + colCounts);
for (int i = 1; i <= colCounts; i++) {
String key = null;
String type = "varchar";
if (columnType != null) {
if (columnType.length != colCounts) {
type = "varchar";
} else {
type = columnType[i - 1];
}
}
if (conversion) {
key = toCamelCase(rs.getColumnName(i));
} else {
key = rs.getColumnName(i);
}
rs.getColumnName(i);
if ("int".equalsIgnoreCase(type) || "Integer".equalsIgnoreCase(type)
|| "Long".equalsIgnoreCase(type) || "BIGINT".equalsIgnoreCase(type)
|| "NUMBER".equalsIgnoreCase(type) || "INTEGER".equalsIgnoreCase(type)
|| "TINYINT".equalsIgnoreCase(type) || "SMALLINT".equalsIgnoreCase(type)) {
map.put(key, rs.getInt(i) == -1 ? rs.getString(i) : rs.getInt(i));
continue;
}
if ("FLOAT".equalsIgnoreCase(type)) {
map.put(key, rs.getFloat(i));
continue;
}
if ("DATE".equalsIgnoreCase(type) || "TIMESTAMP".equalsIgnoreCase(type)
|| "DATETIME".equalsIgnoreCase(type)) {
map.put(key, rs.getDouble(i));
continue;
}
if ("DOUBLE".equalsIgnoreCase(type)) {
map.put(key, rs.getDouble(i));
continue;
}
if ("DECIMAL".equalsIgnoreCase(type) || "NUMERIC".equalsIgnoreCase(type)) {
map.put(key, rs.getDouble(i));
continue;
}
map.put(key, rs.getString(i));
}
return map;
}
/**
* RecordSetmap
*
* @param rs RecordSet
* @return map
*/
public static Map<String, Object> recordSet2Map(RecordSet rs) {
if (rs.next()) {
return getMapMapping(rs, false);
}
return null;
}
/***
* RecordSetmap
* @param rs RecordSet
* @param conversion 线
* @return map
*/
public static Map<String, Object> recordSet2Map(RecordSet rs, boolean conversion) {
if (rs.next()) {
return getMapMapping(rs, conversion);
}
return null;
}
/**
* RecordSetmap
*
* @param rs RecordSet
* @return map
*/
public static List<Map<String, Object>> recordSet2MapList(RecordSet rs) {
List<Map<String, Object>> list = new ArrayList<>();
while (rs.next()) {
list.add(getMapMapping(rs, false));
}
return list;
}
/***
* RecordSetmap
* @param rs RecordSet
* @param conversion 线
* @return map
*/
public static List<Map<String, Object>> recordSet2MapList(RecordSet rs, boolean conversion) {
List<Map<String, Object>> list = new ArrayList<>();
while (rs.next()) {
list.add(getMapMapping(rs, conversion));
}
return list;
}
/**
* SQL
*
* @param rs RecordSet
* @param t
* @param emptyFields
* @param <T>
* @return
* @throws IllegalAccessException IllegalAccessException
*/
private static <T> T getMappingEntity(RecordSet rs, T t, List<Field> emptyFields) throws IllegalAccessException {
for (Field field : emptyFields) {
String fieldName = field.getName();
field.setAccessible(true);
if (field.getType().equals(String.class)) {
field.set(t, rs.getString(fieldName));
continue;
}
if (field.getType().equals(Integer.class) || field.getType().equals(int.class)
|| field.getType().equals(short.class) || field.getType().equals(long.class)
|| field.getType().equals(Long.class)) {
field.set(t, rs.getInt(fieldName));
continue;
}
if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class)) {
field.set(t, rs.getBoolean(fieldName));
continue;
}
if (field.getType().equals(Float.class) || field.getType().equals(float.class)) {
field.set(t, rs.getFloat(fieldName));
continue;
}
if (field.getType().equals(Double.class) || field.getType().equals(double.class)) {
field.set(t, rs.getDouble(fieldName));
continue;
}
if (field.getType().equals(Date.class)) {
if (field.isAnnotationPresent(DateFormatAn.class)) {
DateFormatAn annotation = field.getAnnotation(DateFormatAn.class);
String value = annotation.value();
try {
field.set(t, new SimpleDateFormat(value).parse(rs.getString(fieldName)));
} catch (ParseException e) {
e.printStackTrace();
field.set(t, null);
}
} else {
try {
field.set(t, rs.getDate(fieldName));
} catch (Exception e) {
field.set(t, null);
}
}
continue;
}
if (field.getType().equals(BigDecimal.class)) {
field.set(t, rs.getDouble(fieldName));
}
}
return t;
}
/**
* SQL
*
* @param rs RecordSet
* @param t
* @param emptyFields
* @param conversion 线
* @param <T>
* @return
* @throws IllegalAccessException IllegalAccessException
*/
private static <T> T getMappingEntity(RecordSet rs, T t, List<Field> emptyFields, boolean conversion) throws IllegalAccessException {
if (!conversion) {
return getMappingEntity(rs, t, emptyFields);
}
for (Field field : emptyFields) {
String fieldName = toUnderlineCase(field.getName());
field.setAccessible(true);
if (field.getType().equals(String.class)) {
field.set(t, rs.getString(fieldName));
continue;
}
if (field.getType().equals(Integer.class) || field.getType().equals(int.class)
|| field.getType().equals(short.class) || field.getType().equals(long.class)
|| field.getType().equals(Long.class)) {
field.set(t, rs.getInt(fieldName));
continue;
}
if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class)) {
field.set(t, rs.getBoolean(fieldName));
continue;
}
if (field.getType().equals(Float.class) || field.getType().equals(float.class)) {
field.set(t, rs.getFloat(fieldName));
continue;
}
if (field.getType().equals(Double.class) || field.getType().equals(double.class)) {
field.set(t, rs.getDouble(fieldName));
continue;
}
if (field.getType().equals(Date.class)) {
if (field.isAnnotationPresent(DateFormatAn.class)) {
DateFormatAn annotation = field.getAnnotation(DateFormatAn.class);
String value = annotation.value();
try {
field.set(t, new SimpleDateFormat(value).parse(rs.getString(fieldName)));
} catch (ParseException e) {
e.printStackTrace();
field.set(t, null);
}
} else {
try {
field.set(t, rs.getDate(fieldName));
} catch (Exception e) {
field.set(t, null);
}
}
continue;
}
if (field.getType().equals(BigDecimal.class)) {
field.set(t, rs.getDouble(fieldName));
}
}
return t;
}
@Deprecated
public static <T> T recordeSet2Empty(RecordSet rs, Class<T> clazz, boolean conversion) {
return recordeSet2Entity(rs, clazz, conversion);
}
/**
*
*
* @param rs
* @param clazz
* @param conversion 线
* @param <T>
* @return
*/
public static <T> T recordeSet2Entity(RecordSet rs, Class<T> clazz, boolean conversion) {
if (clazz == null) {
return null;
}
try {
if (rs.next()) {
T t = clazz.newInstance();
List<Field> entityFields = getEmptyFields(clazz);
if (entityFields == null) {
toolUtil.writeErrorLog("emptyFields is empty!");
return null;
}
return getMappingEntity(rs, t, entityFields, conversion);
}
} catch (InstantiationException | IllegalAccessException e) {
toolUtil.writeErrorLog("err" + "\n" + e);
return null;
}
return null;
}
@Deprecated
public static <T> T recordeSet2Empty(RecordSet rs, Class<T> clazz) {
return recordeSet2Entity(rs, clazz);
}
/**
*
*
* @param rs
* @param clazz
* @param <T>
* @return
*/
public static <T> T recordeSet2Entity(RecordSet rs, Class<T> clazz) {
if (clazz == null) {
return null;
}
if (clazz.equals(String.class)) {
if (rs.next()) {
return (T) rs.getString(1);
}
return null;
} else if (clazz.equals(Integer.class)) {
if (rs.next()) {
return (T) Integer.valueOf(rs.getInt(1));
}
return null;
} else if (clazz.equals(Double.class)) {
if (rs.next()) {
return (T) Double.valueOf(rs.getDouble(1));
}
return null;
} else if (clazz.equals(Float.class)) {
if (rs.next()) {
return (T) Float.valueOf(rs.getFloat(1));
}
return null;
} else if (clazz.equals(Boolean.class)) {
if (rs.next()) {
return (T) Boolean.valueOf(rs.getBoolean(1));
}
return null;
}
try {
if (rs.next()) {
T t = clazz.newInstance();
List<Field> emptyFields = getEmptyFields(clazz);
if (emptyFields == null) {
return null;
}
return getMappingEntity(rs, t, emptyFields);
}
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
return null;
}
/**
*
*
* @param rs
* @param clazz
* @param conversion 线
* @param <T>
* @return
*/
public static <T> List<T> recordeSet2Array(RecordSet rs, Class<T> clazz, boolean conversion) {
if (clazz == null) {
return null;
}
List<T> list = new ArrayList<>();
List<Field> entityFields = getEmptyFields(clazz);
if (entityFields == null) {
return null;
}
while (rs.next()) {
try {
T t = clazz.newInstance();
list.add(getMappingEntity(rs, t, entityFields, conversion));
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
return list;
}
/**
*
*
* @param rs
* @param clazz
* @param <T>
* @return
*/
public static <T> List<T> recordeSet2Array(RecordSet rs, Class<T> clazz) {
if (clazz == null) {
return null;
}
List<T> list = new ArrayList<>();
List<Field> emptyFields = getEmptyFields(clazz);
if (emptyFields == null) {
return null;
}
while (rs.next()) {
try {
if (clazz.equals(String.class)) {
list.add((T) rs.getString(1));
} else if (clazz.equals(Integer.class)) {
list.add((T) Integer.valueOf(rs.getInt(1)));
} else if (clazz.equals(Double.class)) {
list.add((T) Double.valueOf(rs.getDouble(1)));
} else if (clazz.equals(Float.class)) {
list.add((T) Float.valueOf(rs.getFloat(1)));
} else if (clazz.equals(Boolean.class)) {
list.add((T) Boolean.valueOf(rs.getBoolean(1)));
} else {
T t = clazz.newInstance();
list.add(getMappingEntity(rs, t, emptyFields));
}
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
return list;
}
/**
* 线
*
* @param underlineStr 线
* @return
*/
public static String toCamelCase(String underlineStr) {
if (underlineStr == null) {
return null;
}
// 分成数组
char[] charArray = underlineStr.toCharArray();
// 判断上次循环的字符是否是"_"
boolean underlineBefore = false;
StringBuilder buffer = new StringBuilder();
for (int i = 0, l = charArray.length; i < l; i++) {
// 判断当前字符是否是"_",如果跳出本次循环
if (charArray[i] == 95) {
underlineBefore = true;
} else if (underlineBefore) {
// 如果为true代表上次的字符是"_",当前字符需要转成大写
buffer.append(charArray[i] -= 32);
underlineBefore = false;
} else {
// 不是"_"后的字符就直接追加
buffer.append(charArray[i]);
}
}
return buffer.toString();
}
/**
* 线
*
* @param camelCaseStr
* @return 线String
*/
public static String toUnderlineCase(String camelCaseStr) {
if (camelCaseStr == null) {
return null;
}
// 将驼峰字符串转换成数组
char[] charArray = camelCaseStr.toCharArray();
StringBuilder buffer = new StringBuilder();
//处理字符串
for (int i = 0, l = charArray.length; i < l; i++) {
if (charArray[i] >= 65 && charArray[i] <= 90) {
buffer.append("_").append(charArray[i] += 32);
} else {
buffer.append(charArray[i]);
}
}
return buffer.toString();
}
/**
*
*
* @param clazz
* @return java
*/
public static List<Field> getEntityFields(Class<?> clazz) {
Class<?> temClass = clazz;
List<Field> fieldList = new ArrayList<>();
while (temClass != null && temClass != Object.class) {
fieldList.addAll(Arrays.asList(temClass.getDeclaredFields()));
temClass = temClass.getSuperclass();
}
if (fieldList.size() == 0) {
return null;
}
return fieldList;
}
public static List<Field> getEmptyFields(Class<?> clazz) {
return getEntityFields(clazz);
}
/**
* java
*
* @param query
* @param javaName
* @param packageName
* @throws IOException io
*/
private static void createJavaFile(String query, String javaName, String packageName) throws IOException {
RecordSet rs = new RecordSet();
rs.executeQuery(query);
List<String> list = new ArrayList<>();
int colCounts = rs.getColCounts() == 0 ? rs.getColumnTypeName().length : rs.getColCounts();
for (int i = 1; i <= colCounts; i++) {
list.add(rs.getColumnName(i));
}
String[] columnType = rs.getColumnTypeName();
StringBuilder content = new StringBuilder();
StringBuilder importBuilder = new StringBuilder();
StringBuilder getBuilder = new StringBuilder();
StringBuilder setBuilder = new StringBuilder();
StringBuilder toStringBuilder = new StringBuilder("\n\t@Override\n");
String packageStr = "\npackage " + packageName + "; \n\n";
content.append("\npublic class ");
String className = Util.toCamelCase(javaName);
char[] chars = className.toCharArray();
if (!Character.isUpperCase(chars[0])) {
chars[0] -= 32;
}
className = String.valueOf(chars) + "DTO";
toStringBuilder.append("\tpublic String toString() {\n\t\treturn \"")
.append(className)
.append("{\" +\n");
content.append(className).append(" {\n\n");
for (int i = 0; i < list.size(); i++) {
content.append(getClassField(columnType[i], list.get(i), importBuilder, getBuilder, setBuilder, toStringBuilder));
}
toStringBuilder.append("\t\t\t\t'}';\n\t}");
content.append(setBuilder)
.append(getBuilder)
.append(toStringBuilder.toString().replace("{\" +\n\t\t\t\t\", ", "{\" +\n\t\t\t\t\""))
.append("\n}");
String contentStr = packageStr + importBuilder + content;
String path = GCONST.getRootPath().replace(File.separator + "web" + File.separator,
File.separator + "src" + File.separator) + packageName.replace(".", File.separator) + File.separator + className + ".java";
System.out.println(path);
File file = new File(URLDecoder.decode(path, "utf-8"));
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if (!file.exists()) {
file.createNewFile();
} else {
try {
System.out.println("已存在文件:" + packageName + "." + className + ".java");
System.out.print("你想如何处理该文件:替换[r/R] 重命名[w/W] 跳过[n/N] ? ");
Scanner scanner = new Scanner(System.in);
String next = scanner.next();
System.out.println(next);
if ("w".equalsIgnoreCase(next)) {
System.out.print("请输入新的文件名:");
String newName = scanner.next();
System.out.println(newName);
System.out.println("处理中...");
createJavaFile(query, newName, packageName);
} else if ("n".equalsIgnoreCase(next)) {
System.out.println("跳过文件生成!");
return;
}
} catch (Exception e) {
System.out.println("已存在文件:" + packageName + "." + className + ".java");
}
}
FileOutputStream fos = new FileOutputStream(file);
byte[] contentInBytes = contentStr.getBytes();
fos.write(contentInBytes);
fos.flush();
fos.close();
System.out.println("完成");
}
/**
* java
*
* @param tableName
* @param packageName
* @throws IOException io
*/
public static void creatJavaFileByTable(String tableName, String packageName) throws IOException {
RecordSet recordSet = new RecordSet();
String dbType = recordSet.getDBType();
String query;
System.out.println(dbType);
if ("mysql".equalsIgnoreCase(dbType)) {
query = "select * from " + tableName + " limit 1";
} else if ("sqlserver".equalsIgnoreCase(dbType)) {
query = "select TOP 1 * from " + tableName;
} else {
query = "select TOP 1 * from " + tableName;
}
createJavaFile(query, tableName, packageName);
}
/**
* java
*
* @param sql
* @param fileName java
* @param packageName
* @throws IOException io
*/
public static void creatJavaFileBySql(String sql, String fileName, String packageName) throws IOException {
RecordSet recordSet = new RecordSet();
String dbType = recordSet.getDBType();
String query;
System.out.println(dbType);
if ("mysql".equalsIgnoreCase(dbType)) {
query = "select * from (" + sql + ") as temp limit 1";
} else if ("sqlserver".equalsIgnoreCase(dbType)) {
query = "select TOP 1 * from (" + sql + ") as temp";
} else {
query = "select TOP 1 * from (" + sql + ") as temp";
}
createJavaFile(query, fileName, packageName);
}
/**
* java
*
* @param type
* @param name
* @param importBuilder
* @param getBuilder getter
* @param setBuilder setter
* @param toStringBuilder toString
* @return java
*/
public static String getClassField(String type, String name, StringBuilder importBuilder,
StringBuilder getBuilder, StringBuilder setBuilder, StringBuilder toStringBuilder) {
if ("TINYINT".equalsIgnoreCase(type)) {
getBuilder.append("\n\tpublic boolean is")
.append(getFirstCase(toCamelCase(name)))
.append("(){\n")
.append("\t\treturn this.")
.append(toCamelCase(name))
.append(";\n\t}\n");
setBuilder.append("\n\tpublic void set")
.append(getFirstCase(toCamelCase(name)))
.append("(boolean ")
.append(toCamelCase(name))
.append("){\n")
.append("\t\tthis.")
.append(toCamelCase(name))
.append(" = ")
.append(toCamelCase(name))
.append(";\n\t}\n");
toStringBuilder.append("\t\t\t\t\", ")
.append(toCamelCase(name))
.append("='\" + ")
.append(toCamelCase(name))
.append(" + '\\'' +\n");
return "\tprivate boolean " + toCamelCase(name) + ";\n";
}
if ("int".equalsIgnoreCase(type) || "SMALLINT".equalsIgnoreCase(type)
|| "MEDIUMINT".equalsIgnoreCase(type) || "NUMBER".equalsIgnoreCase(type)
|| "INTEGER".equalsIgnoreCase(type)) {
getBuilder.append("\n\tpublic int get")
.append(getFirstCase(toCamelCase(name)))
.append("(){\n")
.append("\t\treturn this.")
.append(toCamelCase(name))
.append(";\n\t}\n");
setBuilder.append("\n\tpublic void set")
.append(getFirstCase(toCamelCase(name)))
.append("(int ")
.append(toCamelCase(name))
.append("){\n")
.append("\t\tthis.")
.append(toCamelCase(name))
.append(" = ")
.append(toCamelCase(name))
.append(";\n\t}\n");
toStringBuilder.append("\t\t\t\t\", ")
.append(toCamelCase(name))
.append("='\" + ")
.append(toCamelCase(name))
.append(" + '\\'' +\n");
return "\tprivate int " + toCamelCase(name) + ";\n";
}
if ("BIGINT".equalsIgnoreCase(type)) {
getBuilder.append("\n\tpublic long get")
.append(getFirstCase(toCamelCase(name)))
.append("(){\n")
.append("\t\treturn this.")
.append(toCamelCase(name))
.append(";\n\t}\n");
setBuilder.append("\n\tpublic void set")
.append(getFirstCase(toCamelCase(name)))
.append("(long ")
.append(toCamelCase(name))
.append("){\n")
.append("\t\tthis.")
.append(toCamelCase(name))
.append(" = ")
.append(toCamelCase(name))
.append(";\n\t}\n");
toStringBuilder.append("\t\t\t\t\", ")
.append(toCamelCase(name))
.append("='\" + ")
.append(toCamelCase(name))
.append(" + '\\'' +\n");
return "\tprivate long " + toCamelCase(name) + ";\n";
}
if ("double".equalsIgnoreCase(type) || "BINARY_DOUBLE".equalsIgnoreCase(type)) {
getBuilder.append("\n\tpublic double get")
.append(getFirstCase(toCamelCase(name)))
.append("(){\n")
.append("\t\treturn this.")
.append(toCamelCase(name))
.append(";\n\t}\n");
setBuilder.append("\n\tpublic void set")
.append(getFirstCase(toCamelCase(name)))
.append("(double ")
.append(toCamelCase(name))
.append("){\n")
.append("\t\tthis.")
.append(toCamelCase(name))
.append(" = ")
.append(toCamelCase(name))
.append(";\n\t}\n");
toStringBuilder.append("\t\t\t\t\", ")
.append(toCamelCase(name))
.append("='\" + ")
.append(toCamelCase(name))
.append(" + '\\'' +\n");
return "\tprivate double " + toCamelCase(name) + ";\n";
}
if ("float".equalsIgnoreCase(type) || "BINARY_FLOAT".equalsIgnoreCase(type)) {
getBuilder.append("\n\tpublic float get")
.append(getFirstCase(toCamelCase(name)))
.append("(){\n")
.append("\t\treturn this.")
.append(toCamelCase(name))
.append(";\n\t}\n");
setBuilder.append("\n\tpublic void set")
.append(getFirstCase(toCamelCase(name)))
.append("(float ")
.append(toCamelCase(name))
.append("){\n")
.append("\t\tthis.")
.append(toCamelCase(name))
.append(" = ")
.append(toCamelCase(name))
.append(";\n\t}\n");
toStringBuilder.append("\t\t\t\t\", ")
.append(toCamelCase(name))
.append("='\" + ")
.append(toCamelCase(name))
.append(" + '\\'' +\n");
return "\tprivate float " + toCamelCase(name) + ";\n";
}
if ("DECIMAL".equalsIgnoreCase(type)) {
getBuilder.append("\n\tpublic BigDecimal get")
.append(getFirstCase(toCamelCase(name)))
.append("(){\n")
.append("\t\treturn this.")
.append(toCamelCase(name))
.append(";\n\t}\n");
setBuilder.append("\n\tpublic void set")
.append(getFirstCase(toCamelCase(name)))
.append("(BigDecimal ")
.append(toCamelCase(name))
.append("){\n")
.append("\t\tthis.")
.append(toCamelCase(name))
.append(" = ")
.append(toCamelCase(name))
.append(";\n\t}\n");
toStringBuilder.append("\t\t\t\t\", ")
.append(toCamelCase(name))
.append("='\" + ")
.append(toCamelCase(name))
.append(" + '\\'' +\n");
importBuilder.append("import java.math.BigDecimal;\n");
return "\tprivate BigDecimal " + toCamelCase(name) + ";\n";
}
if ("DATE".equalsIgnoreCase(type) || "DATETIME".equalsIgnoreCase(type)) {
getBuilder.append("\n\tpublic Date get")
.append(getFirstCase(toCamelCase(name)))
.append("(){\n")
.append("\t\treturn this.")
.append(toCamelCase(name))
.append(";\n\t}\n");
setBuilder.append("\n\tpublic void set")
.append(getFirstCase(toCamelCase(name)))
.append("(Date ")
.append(toCamelCase(name))
.append("){\n")
.append("\t\tthis.")
.append(toCamelCase(name))
.append(" = ")
.append(toCamelCase(name))
.append(";\n\t}\n");
toStringBuilder.append("\t\t\t\t\", ")
.append(toCamelCase(name))
.append("='\" + ")
.append(toCamelCase(name))
.append(" + '\\'' +\n");
importBuilder.append("import java.util.Date;\n");
return "\tprivate Date" + toCamelCase(name) + ";\n";
}
if ("char".equalsIgnoreCase(type) || "VARCHAR".equalsIgnoreCase(type) || "TEXT".equalsIgnoreCase(type)) {
getBuilder.append("\n\tpublic String get")
.append(getFirstCase(toCamelCase(name)))
.append("(){\n")
.append("\t\treturn this.")
.append(toCamelCase(name))
.append(";\n\t}\n");
setBuilder.append("\n\tpublic void set")
.append(getFirstCase(toCamelCase(name)))
.append("(String ")
.append(toCamelCase(name))
.append("){\n")
.append("\t\tthis.")
.append(toCamelCase(name))
.append(" = ")
.append(toCamelCase(name))
.append(";\n\t}\n");
toStringBuilder.append("\t\t\t\t\", ")
.append(toCamelCase(name))
.append("='\" + ")
.append(toCamelCase(name))
.append(" + '\\'' +\n");
return "\tprivate String " + toCamelCase(name) + ";\n";
}
return "";
}
/**
*
*
* @param str
* @return
*/
public static String getFirstCase(String str) {
char[] chars = str.toCharArray();
if (!Character.isUpperCase(chars[0])) {
chars[0] -= 32;
}
return String.valueOf(chars);
}
/**
* map
*
* @param fileName prop/prop2map/.properties
* @return map
*/
public static Map<String, Object> getProperties2Map(String fileName) {
String propertyPath = GCONST.getPropertyPath();
if (StringUtil.isNullOrEmpty(fileName)) {
return null;
}
if (fileName.contains(".properties")) {
fileName.replace(".properties", "");
}
String path = propertyPath + "prop2map" + File.separator + fileName + ".properties";
Properties prop = new Properties();
Map<String, Object> map = new HashMap<>();
InputStream inputStream = null;
try {
inputStream = new BufferedInputStream(new FileInputStream(path));
prop.load(inputStream);
Enumeration<?> enumeration = prop.propertyNames();
while (enumeration.hasMoreElements()) {
String key = String.valueOf(enumeration.nextElement());
map.put(key, prop.getProperty(key));
}
} catch (IOException e) {
throw new RuntimeException("找不到文件:" + path);
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return map;
}
/**
* map
*
* @param fileName prop/prop2map/.properties
* @param prefix
* @return map
*/
public static Map<String, Object> getProperties2Map(String fileName, String prefix) {
String propertyPath = GCONST.getPropertyPath();
if (StringUtil.isNullOrEmpty(fileName)) {
return null;
}
if (fileName.contains(".properties")) {
fileName.replace(".properties", "");
}
String path = propertyPath + "prop2map" + File.separator + fileName + ".properties";
Properties prop = new Properties();
Map<String, Object> map = new HashMap<>();
InputStream inputStream = null;
try {
inputStream = new BufferedInputStream(new FileInputStream(path));
prop.load(inputStream);
Enumeration<?> enumeration = prop.propertyNames();
while (enumeration.hasMoreElements()) {
String key = (String) enumeration.nextElement();
if (key.contains(prefix + ".")) {
map.put(key.replace(prefix + ".", ""), prop.getProperty(key));
}
}
} catch (IOException e) {
throw new RuntimeException("找不到文件:" + path);
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return map;
}
public static Map<String, List<String>> parsingSq2Map(String sql) {
Map<String, List<String>> map = new HashMap<>();
String pattern = "\\{{2}(?<table>((?!\\$)\\S)+?)\\.(?<field>\\S+?)}{2}";
Pattern compile = Pattern.compile(pattern);
Matcher matcher = compile.matcher(sql);
while (matcher.find()) {
if (map.containsKey(matcher.group("table"))) {
map.get(matcher.group("table")).add(matcher.group("field"));
} else {
map.put(matcher.group("table"), new ArrayList<String>() {{
add(matcher.group("field"));
}});
}
}
return map;
}
public static Matcher parsingSql2Matcher(String sql) {
String pattern = "\\{{2}(?<table>((?!\\$)\\S)+?)\\.(?<field>\\S+?)}{2}";
Pattern compile = Pattern.compile(pattern);
return compile.matcher(sql);
}
public static String parsingSqlByRequestAndWorkflowId(String sql, String requestId, String workflowId) {
String requestIdPattern = "\\{{2}(\\$requestId)?}{2}";
String workflowIdIdPattern = "\\{{2}(\\$workflowId)?}{2}";
String result = sql.replaceAll(requestIdPattern, StringUtil.isNullOrEmpty(requestId) ? "''" : requestId);
result = result.replaceAll(workflowIdIdPattern, StringUtil.isNullOrEmpty(workflowId) ? "''" : workflowId);
return result;
}
public static String parsingSq(String sql, Map<String, Object> params) {
String result = sql;
String requestIdPattern = "\\{{2}(\\$requestId)?}{2}";
String workflowIdIdPattern = "\\{{2}(\\$workflowId)?}{2}";
result = result.replaceAll(requestIdPattern, ObjectUtil.isNull(params.get("requestId")) ? "''" : "'" + params.get("requestId") + "'");
result = result.replaceAll(workflowIdIdPattern, ObjectUtil.isNull(params.get("workflowId")) ? "''" : "'" + params.get("workflowId") + "'");
for (Map.Entry<String, Object> param : params.entrySet()) {
String pattern_ = "#\\{{2}(" + param.getKey() + ")?}{2}";
result = result.replaceAll(pattern_, ObjectUtil.isNull(param.getValue()) ? "''" : String.valueOf(param.getValue()));
String pattern = "\\{{2}(" + param.getKey() + ")?}{2}";
result = result.replaceAll(pattern, ObjectUtil.isNull(param.getValue()) ? "''" : "'" + param.getValue() + "'");
}
return result;
}
public static <T> List<T> deWeight(List<T> list, Function<? super T, ?> keyExtractor) {
return list.stream().filter(distinctByKey(keyExtractor)).collect(Collectors.toList());
}
private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
// putIfAbsent添加不存在的键返回null如果为null表示不重复
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
public static Map<String, Object> object2Map(Object obj) {
if (obj == null) {
return null;
}
Map<String, Object> map = new HashMap<>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (key.compareToIgnoreCase("class") == 0) {
continue;
}
Method getter = property.getReadMethod();
Object value = getter != null ? getter.invoke(obj) : null;
map.put(key, value);
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
public static String getWorkflowNameById(String workflowid) {
RecordSet rs = new RecordSet();
String sql = "select workflowname from workflow_base where id = ?";
rs.executeQuery(sql, workflowid);
if (rs.next()) {
return rs.getString("workflowname");
}
return "";
}
public static Object getRequestTitleById(String requestid) {
RecordSet rs = new RecordSet();
String sql = "select requestnamenew from workflow_requestbase where requestid = ?";
rs.executeQuery(sql, requestid);
if (rs.next()) {
return rs.getString("requestnamenew");
}
return "";
}
public static ApiConfigMainDTO queryApiConfig(String id) {
ApiConfigMainDTO apiConfigMain = utilService.getApiConfigMain(id);
// System.out.println(JSON.toJSONString(apiConfigMain));
return apiConfigMain;
}
public static ApiConfigMainDTO queryApiConfigTree(String id) {
ApiConfigMainDTO apiConfigMain = utilService.getApiConfigMainTree(id);
// System.out.println(JSON.toJSONString(apiConfigMain));
return apiConfigMain;
}
public static <T> AZipOutputStream createZip(List<T> inputList) throws IOException {
return createZip(inputList, File.separator);
}
private static <T> AZipOutputStream createZip(List<T> inputList, String base) throws IOException {
FileOutputStream fileOutputStream = null;
try {
File file = new File(AZipOutputStream.filePath);
if (!file.exists()) {
//先得到文件的上级目录,并创建上级目录,在创建文件
file.getParentFile().mkdirs();
try {
//创建文件
file.createNewFile();
} catch (IOException ex) {
ex.printStackTrace();
}
}
fileOutputStream = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (fileOutputStream == null) {
return null;
}
AZipOutputStream zipOut = new AZipOutputStream(fileOutputStream);
int catchLen = 10 * 1024;
for (int i = 0; i < inputList.size(); i++) {
T item = inputList.get(i);
if (item instanceof InputStream) {
// 属于单级文件,直接压缩并返回
try {
zipOut.putNextEntry(new ZipEntry(base + i));
} catch (IOException e) {
throw new IOException(e.toString());
}
byte[] buffer = new byte[catchLen];
int len = 0;
while ((len = ((InputStream) item).read(buffer)) != -1) {
zipOut.write(buffer, 0, len);
}
zipOut.closeEntry();
}
if (item instanceof AInputStream) {
try {
zipOut.putNextEntry(new ZipEntry(((AInputStream) item).getFileName()));
} catch (IOException e) {
e.printStackTrace();
}
byte[] buffer = new byte[catchLen];
int len = 0;
while ((len = ((AInputStream) item).getInputStream().read(buffer)) != -1) {
try {
zipOut.write(buffer, 0, len);
} catch (IOException e) {
e.printStackTrace();
}
}
zipOut.closeEntry();
}
if (item instanceof ListZipEntity) {
ListZipEntity listZipEntity = (ListZipEntity) item;
if (listZipEntity.isDirectory()) {
// 表示属于文件夹,循环添加处理文件夹
handlerDirectory(listZipEntity.getFileList(), zipOut, base + listZipEntity.getDirectory() + File.separator);
} else {
List<AInputStream> aInputStreams = listZipEntity.getaInputStreamList();
for (AInputStream aInputStream : aInputStreams) {
try {
zipOut.putNextEntry(new ZipEntry(aInputStream.getFileName()));
} catch (IOException e) {
e.printStackTrace();
}
byte[] buffer = new byte[catchLen];
int len = 0;
while ((len = (aInputStream.getInputStream()).read(buffer)) != -1) {
try {
zipOut.write(buffer, 0, len);
} catch (IOException e) {
e.printStackTrace();
}
}
zipOut.closeEntry();
}
}
}
}
return zipOut;
}
private static void handlerDirectory(List<ListZipEntity> fileList, AZipOutputStream zipOut, String base) throws IOException {
int catchLen = 10 * 1024;
for (ListZipEntity listZipEntity : fileList) {
if (listZipEntity.isDirectory()) {
// 如果是文件夹
handlerDirectory(listZipEntity.getFileList(), zipOut, base + listZipEntity.getDirectory() + File.separator);
} else {
List<AInputStream> aInputStreams = listZipEntity.getaInputStreamList();
for (AInputStream aInputStream : aInputStreams) {
try {
zipOut.putNextEntry(new ZipEntry(aInputStream.getFileName()));
} catch (IOException e) {
e.printStackTrace();
}
byte[] buffer = new byte[catchLen];
int len = 0;
while ((len = (aInputStream.getInputStream()).read(buffer)) != -1) {
try {
zipOut.write(buffer, 0, len);
} catch (IOException e) {
e.printStackTrace();
}
}
zipOut.closeEntry();
}
}
}
}
public static Map<String, String> queryLanguage(int groupId, int languageId) {
return utilService.queryLanguage(groupId, languageId);
}
public static Map<String, String> queryLanguage(int groupId, HttpServletRequest request, HttpServletResponse response) {
User user = HrmUserVarify.getUser(request, response);
int languageId = user.getLanguage();
return utilService.queryLanguage(groupId, languageId);
}
public static String getHtmlLabelName(int id, int languageId, String defaultStr){
String htmlLabelName = SystemEnv.getHtmlLabelName(id, languageId);
return htmlLabelName == null ? defaultStr : htmlLabelName;
}
public static String getHtmlLabelName(int id, int languageId){
return SystemEnv.getHtmlLabelName(id, languageId);
}
/**
*
*
* @param workflowId
* @return
*/
public static String getMainTable(String workflowId) {
String versionStringByWfid = WorkflowVersion.getVersionStringByWfid(workflowId);
String query = "select tablename from workflow_bill " +
" where id in (select formid from workflow_base " +
" where id in (" + versionStringByWfid + ") )";
rs.executeQuery(query);
rs.next();
return rs.getString(1);
}
2021-11-16 09:32:09 +08:00
/**
*
* @param workflowId
* @param docField
* @return
*/
public static String getDocCategorys(String workflowId,String docField){
RecordSet rs = new RecordSet();
rs.executeQuery("select formid from workflow_base where id = ?",workflowId);
String formId = Util.recordeSet2Entity(rs, String.class);
String query = "select doccategory from workflow_fileupload where fieldid = (select id from workflow_billfield where fieldname = ? and billid = ?)";
rs.executeQuery(query,docField,formId);
String docCategorys =Util.null2String(Util.recordeSet2Entity(rs, String.class));
if(StringUtils.isNullOrEmpty(docCategorys)){
query = "select doccategory from workflow_base where id = ?";
rs.executeQuery(query,workflowId);
rs.next();
docCategorys = Util.null2String(rs.getString(1));
}
if(StringUtils.isNullOrEmpty(docCategorys)){
docCategorys = ",,1";
}
return docCategorys;
}
public static String null2DefaultStr(Object obj, String defaultStr){
String objStr = Util.null2String(obj);
if(StringUtils.isNullOrEmpty(objStr) && StringUtils.isNullOrEmpty(defaultStr)){
return "";
}
if(StringUtils.isNullOrEmpty(objStr) && !StringUtils.isNullOrEmpty(defaultStr)){
return defaultStr;
}
return "";
}
2021-11-14 15:29:16 +08:00
}