xiebin
2022-09-20 689f6b7f55519874daca1a1ffb52dae6f456e360
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package org.springblade.common.tool;
 
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.*;
 
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
 
/**
 * @ClassName EnhancementServicesTool
 * @Description TODO 给框架重写save方法导致没有默认技术字段赋值的解决方法
 * @Author aix
 * @Date 2022/9/20 16:16
 * @Version 1.0
 */
public class EnhancementServicesTool <T extends BaseEntity> {
    public void resolveEntity(T entity) {
        try {
            BladeUser user = AuthUtil.getUser();
            Date now = DateUtil.now();
            if (entity.getId() == null) {
                if (user != null) {
                    entity.setCreateUser(user.getUserId());
                    entity.setCreateDept(Func.firstLong(user.getDeptId()));
                    entity.setUpdateUser(user.getUserId());
                }
 
                if (entity.getStatus() == null) {
                    entity.setStatus(1);
                }
 
                entity.setCreateTime(now);
            } else if (user != null) {
                entity.setUpdateUser(user.getUserId());
            }
 
            entity.setUpdateTime(now);
            entity.setIsDeleted(0);
            Field field = ReflectUtil.getField(entity.getClass(), "tenantId");
            if (ObjectUtil.isNotEmpty(field)) {
                Method getTenantId = ClassUtil.getMethod(entity.getClass(), "getTenantId", new Class[0]);
                String tenantId = String.valueOf(getTenantId.invoke(entity));
                if (ObjectUtil.isEmpty(tenantId)) {
                    Method setTenantId = ClassUtil.getMethod(entity.getClass(), "setTenantId", new Class[]{String.class});
                    setTenantId.invoke(entity, null);
                }
            }
 
        } catch (Throwable var8) {
            try {
                throw var8;
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }
    }
}