package com.highdatas.srs.util;
|
|
|
import com.baomidou.mybatisplus.annotations.TableName;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.highdatas.srs.pojo.Segment;
|
import com.highdatas.srs.pojo.TableSchemaResult;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.lang.reflect.Field;
|
import java.lang.reflect.Method;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.Random;
|
import java.util.UUID;
|
|
/**
|
* @author kimi
|
* @description
|
* @date 2019-12-16 15:13
|
*/
|
|
@Slf4j
|
public class DbUtils {
|
|
|
public static String getUUID() {
|
String id = UUID.randomUUID().toString().replaceAll("-", "");
|
return id;
|
}
|
public static String getUUID(int i) {
|
String id = UUID.randomUUID().toString().replaceAll("-", "").substring(0, i);
|
return id;
|
}
|
|
public static String combieOneSegment(String name, String value) {
|
Segment segment = new Segment(name, value);
|
return segment.toString();
|
}
|
public static String getTableNameFromEntity(Object object) {
|
Field[] fields = object.getClass().getDeclaredFields();
|
for (Field field : fields) {
|
field.setAccessible(true);
|
// 获取表名
|
TableName table = object.getClass().getAnnotation(TableName.class);
|
if (table != null) {
|
String tableName = table.value();
|
return tableName;
|
}
|
}
|
return null;
|
}
|
|
public static Object toSqlToJava(String value, TableSchemaResult tableSchema) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException {
|
String dbType = tableSchema.getDbType();
|
String javaType = toSqlToJava(dbType);
|
Class<?> baseWapperClaz = Class.forName("java.lang." + javaType);
|
Object baseWapper = baseWapperClaz.newInstance();
|
Method valueOfMethod = baseWapperClaz.getDeclaredMethod("valueOf");
|
return null;
|
// valueOfMethod.invoke(baseWapper, )
|
}
|
|
public static int compareDate( Date dt1, Date dt2) {
|
try {
|
if (dt1.getTime() > dt2.getTime()) {
|
System.out.println("dt1 在dt2前");
|
return 1;
|
} else if (dt1.getTime() < dt2.getTime()) {
|
System.out.println("dt1在dt2后");
|
return -1;
|
} else {
|
return 0;
|
}
|
} catch (Exception exception) {
|
exception.printStackTrace();
|
}
|
return 0;
|
}
|
|
public static String toSqlToJava(String sqlType) {
|
if( sqlType == null || sqlType.trim().length() == 0 ) return sqlType;
|
sqlType = sqlType.toLowerCase();
|
switch(sqlType){
|
case "nvarchar":return "String";
|
case "char":return "String";
|
case "varchar":return "String";
|
case "text":return "String";
|
case "nchar":return "String";
|
case "blob":return "byte[]";
|
case "integer":return "Long";
|
case "tinyint":return "Integer";
|
case "smallint":return "Integer";
|
case "mediumint":return "Integer";
|
case "bit":return "Boolean";
|
case "bigint":return "java.math.BigInteger";
|
case "float":return "Fload";
|
case "double":return "Double";
|
case "decimal":return "java.math.BigDecimal";
|
case "boolean":return "Boolean";
|
case "id":return "Long";
|
case "date":return "java.util.Date";
|
case "datetime":return "java.util.Date";
|
case "year":return "java.util.Date";
|
case "time":return "java.sql.Time";
|
case "timestamp":return "java.sql.Timestamp";
|
case "numeric":return "java.math.BigDecimal";
|
case "real":return "java.math.BigDecimal";
|
case "money":return "Double";
|
case "smallmoney":return "Double";
|
case "image":return "byte[]";
|
default:
|
log.error("-----------------》转化失败:未发现的类型:{0}",sqlType);
|
break;
|
}
|
return sqlType;
|
}
|
|
public static void sqlValueToJava(Object value, String dbType) throws Exception {
|
String javaType = toSqlToJava(dbType);
|
Class<?> baseWapperClaz = Class.forName("java.lang." + javaType);
|
Object baseWapper = baseWapperClaz.newInstance();
|
Method valueOfMethod = baseWapperClaz.getDeclaredMethod("valueOf");
|
}
|
|
public static String quotedStr(Object o) {
|
return quotedStr(String.valueOf(o));
|
}
|
|
public static String quotedStr(String str) {
|
if (str != null)
|
return "'" + str + "'";
|
else
|
return "''";
|
}
|
|
public static String bracketStr(String str) {
|
if (str != null)
|
return "(" + str + ")";
|
else
|
return "()";
|
}
|
public static <T> T json2Bean(String json, Class<T> claz) throws JsonProcessingException {
|
ObjectMapper objectMapper = new ObjectMapper();
|
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
return objectMapper.readValue(json, claz);
|
}
|
|
public static String StrJoin(String... objects){
|
StringBuilder builder = new StringBuilder();
|
for (String object : objects) {
|
builder.append(object);
|
}
|
return builder.toString();
|
}
|
public static String getRandomString(int length) {
|
Random random = new Random();
|
StringBuffer sb = new StringBuffer();
|
for (int i = 0; i < length; i++) {
|
int number = random.nextInt(3);
|
long result = 0;
|
switch (number) {
|
case 0:
|
result = Math.round(Math.random() * 25 + 65);
|
sb.append(String.valueOf((char) result));
|
break;
|
case 1:
|
result = Math.round(Math.random() * 25 + 97);
|
sb.append(String.valueOf((char) result));
|
break;
|
case 2:
|
sb.append(String.valueOf(new Random().nextInt(10)));
|
break;
|
}
|
}
|
return sb.toString();
|
}
|
|
public static int getOrderNoAdd(int orderNo) {
|
return orderNo + 1;
|
}
|
|
public static final String getChineseOrEnglishOrNumber(String str) {
|
StringBuffer sbf = new StringBuffer();
|
char[] charArray = str.toCharArray();
|
for (int i = 0; i < charArray.length; i++) {
|
if ((charArray[i] >= 0x4e00) && (charArray[i] <= 0x9fbb)) {
|
sbf.append(charArray[i]);
|
}
|
if ((charArray[i] >= 65) && (charArray[i] <= 122)) {
|
sbf.append(charArray[i]);
|
}
|
if (str.charAt(i) >= 48 && str.charAt(i) <= 57) {
|
sbf.append(charArray[i]);
|
}
|
}
|
return sbf.toString();
|
}
|
|
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
|
if (nowTime.getTime() == startTime.getTime()
|
|| nowTime.getTime() == endTime.getTime()) {
|
return true;
|
}
|
|
Calendar date = Calendar.getInstance();
|
date.setTime(nowTime);
|
|
Calendar begin = Calendar.getInstance();
|
begin.setTime(startTime);
|
|
Calendar end = Calendar.getInstance();
|
end.setTime(endTime);
|
|
if (date.after(begin) && date.before(end)) {
|
return true;
|
} else {
|
return false;
|
}
|
}
|
public static int getDayDiffer(Date startDate, Date endDate) throws ParseException {
|
//判断是否跨年
|
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
|
String startYear = yearFormat.format(startDate);
|
String endYear = yearFormat.format(endDate);
|
if (startYear.equals(endYear)) {
|
/* 使用Calendar跨年的情况会出现问题 */
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(startDate);
|
int startDay = calendar.get(Calendar.DAY_OF_YEAR);
|
calendar.setTime(endDate);
|
int endDay = calendar.get(Calendar.DAY_OF_YEAR);
|
return endDay - startDay;
|
} else {
|
/* 跨年不会出现问题,需要注意不满24小时情况(2016-03-18 11:59:59 和 2016-03-19 00:00:01的话差值为 0) */
|
// 只格式化日期,消除不满24小时影响
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
long startDateTime = dateFormat.parse(dateFormat.format(startDate)).getTime();
|
long endDateTime = dateFormat.parse(dateFormat.format(endDate)).getTime();
|
return (int) ((endDateTime - startDateTime) / (1000 * 3600 * 24));
|
}
|
}
|
public static String convert_before(long time) {
|
if (time < 0)
|
return String.valueOf(time);
|
|
int difftime = (int) ((System.currentTimeMillis() - time) / 1000);
|
if (difftime < 86400 && difftime > 0) {
|
if (difftime < 3600) {
|
int min = (int) (difftime / 60);
|
if (min == 0)
|
return "刚刚";
|
else
|
return (int) (difftime / 60) + "分钟前";
|
} else {
|
return (int) (difftime / 3600) + "小时前";
|
}
|
} else {
|
Calendar now = Calendar.getInstance();
|
Calendar c = Calendar.getInstance();
|
c.setTimeInMillis(time);
|
if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH)
|
&& c.get(Calendar.DATE) == now.get(Calendar.DATE)) {
|
return new SimpleDateFormat("HH:mm").format(c.getTime());
|
}
|
if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR) && c.get(Calendar.MONTH) == now.get(Calendar.MONTH)
|
&& c.get(Calendar.DATE) == now.get(Calendar.DATE) - 1) {
|
return new SimpleDateFormat("昨天 HH:mm").format(c.getTime());
|
} else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)
|
&& c.get(Calendar.MONTH) == now.get(Calendar.MONTH)
|
&& c.get(Calendar.DATE) == now.get(Calendar.DATE) - 2) {
|
return new SimpleDateFormat("前天 HH:mm").format(c.getTime());
|
} else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)) {
|
return new SimpleDateFormat("M月dd日 HH:mm").format(c.getTime());
|
} else {
|
return new SimpleDateFormat("yy年M月dd日").format(c.getTime());
|
}
|
}
|
}
|
|
}
|