Browse Source

createBy、updateBy使用username插入

陈长荣 8 hours ago
parent
commit
e600fa8ce1

+ 21 - 23
jfcloud-gene-common/src/main/java/com/github/jfcloud/gene/common/interceptor/interceptor/StuffInterceptor.java

@@ -35,17 +35,12 @@ public class StuffInterceptor implements Interceptor {
             final MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
             final Object paramObj = invocation.getArgs()[1];
 
-            Long userId = UserUtil.getUserId();
-            String userName = UserUtil.getUserName();
-            Long tenantId = UserUtil.getTenantId();
-            Long deptId = UserUtil.getDeptId();
-
             if (ms.getSqlCommandType() == SqlCommandType.INSERT) {
-                return this.executeInsert(executor, ms, paramObj, tenantId, deptId, userId, userName);
+                return this.executeInsert(executor, ms, paramObj);
             }
 
             if (ms.getSqlCommandType() == SqlCommandType.UPDATE) {
-                return this.executeUpdate(executor, ms, paramObj, userId);
+                return this.executeUpdate(executor, ms, paramObj);
             }
         }
         return invocation.proceed();
@@ -54,7 +49,7 @@ public class StuffInterceptor implements Interceptor {
     /**
      * 新增操作
      */
-    private Object executeInsert(final Executor executor, final MappedStatement ms, final Object paramObj, final Long tenantId, final Long deptId, final Long userId, final String userName) throws Exception {
+    private Object executeInsert(final Executor executor, final MappedStatement ms, final Object paramObj) throws Exception {
 
         // 获取所有字段
         final Field[] fields = ReflectUtil.getFields(paramObj.getClass());
@@ -69,29 +64,32 @@ public class StuffInterceptor implements Interceptor {
                 String annoType = autoStuff.annoType().getAnnoType();
                 // 获取原来的值
                 Object originalValue = field.get(paramObj);
+                if (Objects.isNull(originalValue)) {
+                    continue;
+                }
 
                 // 先进行字段类型判断,再判断归属,所有信息都是通过operInfo接口获取的
-                if (AnnoEnum.TENANT_ID.getAnnoType().equals(annoType) && tenantId != null) {
-                    field.set(paramObj, tenantId);
+                if (AnnoEnum.TENANT_ID.getAnnoType().equals(annoType)) {
+                    field.set(paramObj, UserUtil.getTenantId());
                     continue;
                 }
-                if (AnnoEnum.DEPT_ID.getAnnoType().equals(annoType) && deptId != null) {
-                    field.set(paramObj, deptId);
+                if (AnnoEnum.DEPT_ID.getAnnoType().equals(annoType)) {
+                    field.set(paramObj, UserUtil.getDeptId());
                     continue;
                 }
-                if (AnnoEnum.CREATE_BY.getAnnoType().equals(annoType) && Objects.isNull(originalValue) && userId != null) {
-                    field.set(paramObj, userId.toString());
+                if (AnnoEnum.CREATE_BY.getAnnoType().equals(annoType)) {
+                    field.set(paramObj, UserUtil.getUserName());
                     continue;
                 }
-                if (AnnoEnum.CREATE_TIME.getAnnoType().equals(annoType) && Objects.isNull(originalValue)) {
+                if (AnnoEnum.CREATE_TIME.getAnnoType().equals(annoType)) {
                     field.set(paramObj, new Date());
                     continue;
                 }
-                if (AnnoEnum.UPDATE_BY.getAnnoType().equals(annoType) && Objects.isNull(originalValue) && userId != null) {
-                    field.set(paramObj, userId.toString());
+                if (AnnoEnum.UPDATE_BY.getAnnoType().equals(annoType)) {
+                    field.set(paramObj, UserUtil.getUserName());
                     continue;
                 }
-                if (AnnoEnum.UPDATE_TIME.getAnnoType().equals(annoType) && Objects.isNull(originalValue)) {
+                if (AnnoEnum.UPDATE_TIME.getAnnoType().equals(annoType)) {
                     field.set(paramObj, new Date());
                     continue;
                 }
@@ -112,7 +110,7 @@ public class StuffInterceptor implements Interceptor {
      * @date: 2022/8/15 11:05
      */
     @SuppressWarnings({"unchecked"})
-    private Object executeUpdate(final Executor executor, final MappedStatement ms, final Object paramObj, final Long userId) throws Exception {
+    private Object executeUpdate(final Executor executor, final MappedStatement ms, final Object paramObj) throws Exception {
         if (paramObj instanceof Map) {
             Map<String, Object> map = (Map<String, Object>) paramObj;
             Map<String, Object> newParam = new HashMap<>(map.size());
@@ -133,8 +131,8 @@ public class StuffInterceptor implements Interceptor {
                     AutoStuff autoStuff = field.getAnnotation(AutoStuff.class);
                     if (autoStuff != null) {
                         String annoType = autoStuff.annoType().getAnnoType();
-                        if (AnnoEnum.UPDATE_BY.getAnnoType().equals(annoType) && userId != null) {
-                            field.set(value, userId.toString());
+                        if (AnnoEnum.UPDATE_BY.getAnnoType().equals(annoType)) {
+                            field.set(value, UserUtil.getUserName());
                         }
                         if (AnnoEnum.UPDATE_TIME.getAnnoType().equals(annoType)) {
                             field.set(value, new Date());
@@ -152,8 +150,8 @@ public class StuffInterceptor implements Interceptor {
             AutoStuff autoStuff = field.getAnnotation(AutoStuff.class);
             if (autoStuff != null) {
                 String annoType = autoStuff.annoType().getAnnoType();
-                if (AnnoEnum.UPDATE_BY.getAnnoType().equals(annoType) && userId != null) {
-                    field.set(paramObj, userId.toString());
+                if (AnnoEnum.UPDATE_BY.getAnnoType().equals(annoType)) {
+                    field.set(paramObj, UserUtil.getUserName());
                 }
                 if (AnnoEnum.UPDATE_TIME.getAnnoType().equals(annoType)) {
                     field.set(paramObj, new Date());