|
@@ -5,17 +5,19 @@ import com.alibaba.fastjson.JSONObject;
|
|
|
import com.boman.common.core.utils.SecurityUtils;
|
|
|
import com.boman.common.core.utils.obj.ObjectUtils;
|
|
|
import com.boman.gen.domain.GenTableColumn;
|
|
|
-import com.boman.web.core.constant.FormDataConstant;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import org.apache.commons.collections4.MapUtils;
|
|
|
+import org.apache.commons.lang3.ArrayUtils;
|
|
|
+import org.apache.commons.lang3.BooleanUtils;
|
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.sql.Timestamp;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.function.Predicate;
|
|
|
|
|
|
import static com.boman.common.core.utils.obj.ObjectUtils.*;
|
|
|
-import static com.boman.web.core.constant.FormDataConstant.HR;
|
|
|
+import static com.boman.web.core.constant.FormDataConstant.*;
|
|
|
|
|
|
/**
|
|
|
* @author shiqian
|
|
@@ -29,21 +31,41 @@ public class ColumnUtils {
|
|
|
* @param columns 所有的列
|
|
|
* @param jsonObject 需要返回的数据
|
|
|
* @param currentTime currentTime
|
|
|
+ * @param isSave 新增true、修改false
|
|
|
*/
|
|
|
- public static void packUpdateByAndTime(List<GenTableColumn> columns, JSONObject jsonObject, Timestamp currentTime) {
|
|
|
+ public static void packUpdateByAndTime(List<GenTableColumn> columns, JSONObject jsonObject, Timestamp currentTime, boolean isSave) {
|
|
|
for (GenTableColumn column : columns) {
|
|
|
- // 判断是否有修改人、修改时间
|
|
|
- if (FormDataConstant.UPDATE_BY.equalsIgnoreCase(column.getColumnName())) {
|
|
|
- jsonObject.put(FormDataConstant.UPDATE_BY, SecurityUtils.getUserId());
|
|
|
+ String columnName = column.getColumnName();
|
|
|
+ if (UPDATE_BY.equalsIgnoreCase(columnName)) {
|
|
|
+ jsonObject.put(columnName, SecurityUtils.getUsername());
|
|
|
+ continue;
|
|
|
}
|
|
|
- if (FormDataConstant.UPDATE_TIME.equalsIgnoreCase(column.getColumnName())) {
|
|
|
- jsonObject.put(FormDataConstant.UPDATE_TIME, currentTime);
|
|
|
+
|
|
|
+ if (UPDATE_TIME.equalsIgnoreCase(columnName)) {
|
|
|
+ jsonObject.put(columnName, currentTime.toString());
|
|
|
+ continue;
|
|
|
}
|
|
|
|
|
|
- // 如果某一列是datetime类型,需要把类型转成datetime,否则数据库会报错
|
|
|
- if (FormDataConstant.DATETIME.equalsIgnoreCase(column.getColumnType())) {
|
|
|
- String columnName = column.getColumnName().toUpperCase();
|
|
|
- jsonObject.put(columnName, jsonObject.getTimestamp(columnName));
|
|
|
+ if (isSave) {
|
|
|
+ if (CREATE_TIME.equalsIgnoreCase(columnName)) {
|
|
|
+ jsonObject.put(columnName, currentTime.toString());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (CREATE_BY.equalsIgnoreCase(columnName)) {
|
|
|
+ jsonObject.put(columnName, SecurityUtils.getUsername());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (DATETIME.equalsIgnoreCase(column.getColumnType())) {
|
|
|
+ Object value = jsonObject.get(columnName);
|
|
|
+ if (isEmpty(((String) value))) {
|
|
|
+ jsonObject.put(columnName, currentTime.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 修改时,修改非创建时间的其他时间类型
|
|
|
+ if (!CREATE_TIME.equalsIgnoreCase(columnName) && DATETIME.equalsIgnoreCase(column.getColumnType())) {
|
|
|
+ jsonObject.put(columnName, currentTime.toString());
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -177,4 +199,54 @@ public class ColumnUtils {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述: 根据从数据库查出来的返回值来确实是否是blob, 转为字符串! 转为字符串! 转为字符串!
|
|
|
+ *
|
|
|
+ * @param isContainsBlob 此表中的字段是否含有blob的列, true含
|
|
|
+ * @param result result
|
|
|
+ */
|
|
|
+ public static void handlerBlobWithJSONObject(Boolean isContainsBlob, List<JSONObject> result) {
|
|
|
+ // 不包含
|
|
|
+ if (BooleanUtils.isFalse(isContainsBlob)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (JSONObject jsonObject : result) {
|
|
|
+ for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
|
|
|
+ Object value = entry.getValue();
|
|
|
+ if (isNotEmpty(value) && BYTE_ARRAY.equals(value.getClass().getSimpleName())) {
|
|
|
+ entry.setValue(byteToString(((byte[]) value)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述: 根据从数据库查出来的返回值来确实是否是blob, 转为字符串! 转为字符串! 转为字符串!
|
|
|
+ *
|
|
|
+ * @param result result
|
|
|
+ */
|
|
|
+ public static void handlerBlobWithTableColumn(Boolean isContainsBlob, List<GenTableColumn> result) {
|
|
|
+ // 不包含
|
|
|
+ if (BooleanUtils.isFalse(isContainsBlob)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (GenTableColumn column : result) {
|
|
|
+ Object value = column.getColumnValue();
|
|
|
+ if (isNotEmpty(value) && BYTE_ARRAY.equals(value.getClass().getSimpleName())) {
|
|
|
+ column.setColumnValue(byteToString(((byte[]) value)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String byteToString(byte[] bytes) {
|
|
|
+ String result = "";
|
|
|
+ if (ArrayUtils.isNotEmpty(bytes)) {
|
|
|
+ result = new String(bytes, StandardCharsets.UTF_8);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|