|
@@ -0,0 +1,267 @@
|
|
|
+package com.tofly.fees.common.dbhelper;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.annotation.TableName;
|
|
|
+import com.tofly.common.core.entity.ResultRespone;
|
|
|
+import com.tofly.fees.common.enums.LogOperateEnum;
|
|
|
+import com.tofly.fees.common.enums.SysEnum;
|
|
|
+import com.tofly.fees.common.model.CollectionData;
|
|
|
+import com.tofly.fees.common.util.DateUtils;
|
|
|
+import io.swagger.annotations.ApiModelProperty;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+
|
|
|
+import java.lang.annotation.Annotation;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.text.MessageFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+
|
|
|
+ * 公共日志处理类
|
|
|
+ *
|
|
|
+ * @author shudong
|
|
|
+ */
|
|
|
+public class LogHelper {
|
|
|
+
|
|
|
+
|
|
|
+ * 获取修改类型的日志SQL语句【针对传入的2个对象比较,返回的SQL可能为空使用前进行判断】
|
|
|
+ *
|
|
|
+ * @param tOld 原对象,不能为null
|
|
|
+ * @param tNew 新对象,不能为null
|
|
|
+ * @param strCause 变更原因,不能为空否则直接返回null
|
|
|
+ * @param <T> 实体类
|
|
|
+ * @return SQL语句
|
|
|
+ */
|
|
|
+ public static <T> String getModifySQL(T tOld, T tNew, String strCause) {
|
|
|
+ String strResult = "";
|
|
|
+ try {
|
|
|
+ if (tOld == null || tNew == null || !StringUtils.isBlank(strCause)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ StringBuilder sbResult = new StringBuilder();
|
|
|
+ JSONArray jsonArray = new JSONArray();
|
|
|
+ Field[] fields = tOld.getClass().getDeclaredFields();
|
|
|
+ Field[] newFields = tNew.getClass().getDeclaredFields();
|
|
|
+ for (Field oldField : fields) {
|
|
|
+
|
|
|
+ oldField.setAccessible(true);
|
|
|
+
|
|
|
+ Object oldValue = oldField.get(tOld);
|
|
|
+ if (oldValue != null && oldField.getName().equals("serialVersionUID")) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ for (Field newField : newFields) {
|
|
|
+
|
|
|
+ newField.setAccessible(true);
|
|
|
+
|
|
|
+ if (oldField.getName().equals(newField.getName())) {
|
|
|
+ Object newValue = newField.get(tNew);
|
|
|
+ if (newValue != null && !oldValue.equals(newValue)) {
|
|
|
+
|
|
|
+ setModifInfo(tNew, oldValue, sbResult, jsonArray, newField);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ String strNotes = sbResult.toString();
|
|
|
+
|
|
|
+ if (!StringUtils.isBlank(strNotes)) {
|
|
|
+
|
|
|
+ strNotes = strNotes.substring(0, strNotes.length() - 1);
|
|
|
+
|
|
|
+ String strDbTableName = ((TableName) getTableNameAnnotation(tOld)).value();
|
|
|
+
|
|
|
+ strResult = saveLogSQL(strCause, strNotes, jsonArray.toJSONString(), LogOperateEnum.Modif, strDbTableName);
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ strResult = "";
|
|
|
+ }
|
|
|
+ return strResult;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 获取修改类型的日志SQL语句【针对单个对象加where条件入的方式,返回的SQL可能为空使用前进行判断】
|
|
|
+ *
|
|
|
+ * @param tNew 修改的实体对象
|
|
|
+ * @param strWhere 查询当前修改的对象的唯一sql条件;条件必须符合查询出来的数据是唯一,否则直接返回null
|
|
|
+ * @param strCause 变更原因,不能为空否则直接返回null
|
|
|
+ * @param <T> 实体类
|
|
|
+ * @return SQL语句
|
|
|
+ */
|
|
|
+ public static <T> String getModifySQL(T tNew, String strWhere, String strCause) {
|
|
|
+ String strResult = "";
|
|
|
+ try {
|
|
|
+ if (tNew == null || !StringUtils.isBlank(strWhere) || !StringUtils.isBlank(strCause)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ String strDbTableName = ((TableName) getTableNameAnnotation(tNew)).value();
|
|
|
+
|
|
|
+ HashMap hashMap = getModel(strDbTableName, strWhere);
|
|
|
+ if (hashMap == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ StringBuilder sbResult = new StringBuilder();
|
|
|
+ JSONArray jsonArray = new JSONArray();
|
|
|
+ Field[] newFields = tNew.getClass().getDeclaredFields();
|
|
|
+ for (Field newField : newFields) {
|
|
|
+
|
|
|
+ newField.setAccessible(true);
|
|
|
+
|
|
|
+
|
|
|
+ Object newValue = newField.get(tNew);
|
|
|
+ if (newValue != null && newField.getName().equals("serialVersionUID")) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (newValue != null) {
|
|
|
+ Object oldValue = hashMap.get(newField.getName());
|
|
|
+ setModifInfo(tNew, oldValue, sbResult, jsonArray, newField);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ String strNotes = sbResult.toString();
|
|
|
+
|
|
|
+ if (!StringUtils.isBlank(strNotes)) {
|
|
|
+
|
|
|
+ strNotes = strNotes.substring(0, strNotes.length() - 1);
|
|
|
+
|
|
|
+
|
|
|
+ strResult = saveLogSQL(strCause, strNotes, jsonArray.toJSONString(), LogOperateEnum.Modif, strDbTableName);
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ strResult = "";
|
|
|
+ }
|
|
|
+ return strResult;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 设置修改信息
|
|
|
+ *
|
|
|
+ * @param tNew 实体对象
|
|
|
+ * @param oldValue 修改前的值
|
|
|
+ * @param sbResult 存储的StringBuilder对象
|
|
|
+ * @param jsonArray 存储的JsonArray对象
|
|
|
+ * @param newField 实体类Field对象
|
|
|
+ * @param <T> 泛型实体
|
|
|
+ * @throws IllegalAccessException
|
|
|
+ */
|
|
|
+ private static <T> void setModifInfo(T tNew, Object oldValue, StringBuilder sbResult, JSONArray jsonArray, Field newField) throws IllegalAccessException {
|
|
|
+ Object newValue = newField.get(tNew);
|
|
|
+ String fieldText = getApiModelPropertyValue(tNew, newField.getName());
|
|
|
+ String fieldValue = newField.getName();
|
|
|
+ if (oldValue == null) {
|
|
|
+ oldValue = "";
|
|
|
+ } else {
|
|
|
+ if (oldValue instanceof Date) {
|
|
|
+ oldValue = DateUtils.formatDate((Date) oldValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (newValue == null) {
|
|
|
+ newValue = "";
|
|
|
+ } else {
|
|
|
+ if (newValue instanceof Date) {
|
|
|
+ newValue = DateUtils.formatDate((Date) newValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sbResult.append(MessageFormat.format("「{0}({1})」由{2}修改为{3};"
|
|
|
+ , fieldText, fieldValue, oldValue, newValue));
|
|
|
+
|
|
|
+
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("fieldText", fieldText);
|
|
|
+ jsonObject.put("fieldValue", fieldValue);
|
|
|
+ jsonObject.put("oldValue", oldValue);
|
|
|
+ jsonObject.put("newValue", newValue);
|
|
|
+ jsonArray.add(jsonObject);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 获取指定泛型实体类的ApiModelProperty值
|
|
|
+ *
|
|
|
+ * @param t 泛型实体类
|
|
|
+ * @param fieldName 字段名
|
|
|
+ * @param <T> 泛型
|
|
|
+ * @return 实体类的ApiModelProperty值
|
|
|
+ */
|
|
|
+ private static <T> String getApiModelPropertyValue(T t, String fieldName) {
|
|
|
+ String strValue = "";
|
|
|
+ Field[] fields = t.getClass().getDeclaredFields();
|
|
|
+ for (Field field : fields) {
|
|
|
+ for (Annotation annotation : field.getAnnotations()) {
|
|
|
+ if (annotation instanceof ApiModelProperty) {
|
|
|
+ if (field.getName().equals(fieldName)) {
|
|
|
+ strValue = ((ApiModelProperty) annotation).value();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return strValue;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ * 获取泛型实体类对应的TableName注解对象
|
|
|
+ *
|
|
|
+ * @param t 泛型实体类
|
|
|
+ * @param <T> 泛型
|
|
|
+ * @return 实体类对应的TableName注解对象
|
|
|
+ */
|
|
|
+ private static <T> Annotation getTableNameAnnotation(T t) {
|
|
|
+ Annotation[] annotations = t.getClass().getDeclaredAnnotations();
|
|
|
+ for (Annotation annotation : annotations) {
|
|
|
+ if (annotation instanceof TableName) {
|
|
|
+ return annotation;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 保存日志SQL
|
|
|
+ *
|
|
|
+ * @param strCause 变更原因
|
|
|
+ * @param strNotes 修改详情
|
|
|
+ * @param strJsonNotes 修改详情的json字符
|
|
|
+ * @param logOperateEnum 枚举修改类型
|
|
|
+ * @param strTableName 数据库表名
|
|
|
+ * @return 日志存储SQL
|
|
|
+ */
|
|
|
+ private static String saveLogSQL(String strCause, String strNotes, String strJsonNotes, LogOperateEnum logOperateEnum, String strTableName) {
|
|
|
+ String userName = "test";
|
|
|
+ String realName = "测试";
|
|
|
+
|
|
|
+ StringBuilder sbSQL = new StringBuilder();
|
|
|
+ sbSQL.append("insert into tf_ywys_sys_commlog(id,btype,btable,changecause,notes,json_notes,optype,usercode,username,optime)");
|
|
|
+ sbSQL.append(MessageFormat.format("select sys_guid(),name,btable,''{0}'',''{1}'',''{2}'',''{3}'',''{4}'',''{5}'',sysdate from tf_ywys_bm_logtype where btable=''{6}''"
|
|
|
+ , strCause, strNotes, strJsonNotes, logOperateEnum.toString(), userName, realName, strTableName));
|
|
|
+ return sbSQL.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 通过数据库表名,where唯一条件查询数据库实体model,如果where查询出多条则返回null
|
|
|
+ *
|
|
|
+ * @param strTableName 数据库表名
|
|
|
+ * @param strWhere 查询的where条件
|
|
|
+ * @return HashMap对象
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private static HashMap getModel(String strTableName, String strWhere) throws Exception {
|
|
|
+ String strSql = MessageFormat.format("select * from {0} where {1}", strTableName, strWhere);
|
|
|
+ DbHelper dbHelper = DbHelper.getDbHelper();
|
|
|
+ Object object = dbHelper.getList(strSql, SysEnum.MyBatis.getName());
|
|
|
+ ArrayList arrayList = (ArrayList) ((CollectionData) object).getRecords();
|
|
|
+ if (arrayList.size() != 1) {
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return (HashMap) ResultRespone.getConvertList(arrayList).get(0);
|
|
|
+ }
|
|
|
+}
|