goupan
2024-04-03 5506e9a45e717ffcb67ec313b5a4e8206d9b3a39
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
64
65
66
67
68
69
70
71
package cn.stylefeng.roses.kernel.system.api.format;
 
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.dsctn.api.context.CurrentDataSourceContext;
import cn.stylefeng.roses.kernel.rule.constants.TenantConstants;
import cn.stylefeng.roses.kernel.rule.format.BaseSimpleFieldFormatProcess;
import cn.stylefeng.roses.kernel.system.api.UserServiceApi;
import cn.stylefeng.roses.kernel.system.api.pojo.user.SysUserDTO;
 
import static cn.stylefeng.roses.kernel.rule.constants.RuleConstants.TENANT_DB_PREFIX;
 
/**
 * Json响应的针对用户的处理
 *
 * @author fengshuonan
 * @date 2022/9/7 10:09
 */
public class UserFormatProcess extends BaseSimpleFieldFormatProcess {
 
    /**
     * 未知人员的名称
     */
    private static final String NOT_FIND_USER_NAME = "未知人员";
 
    @Override
    public Class<?> getItemClass() {
        return Long.class;
    }
 
    @Override
    public Object simpleItemFormat(Object businessId) {
 
        LoginUser loginUserNullable = LoginContext.me().getLoginUserNullable();
        if (loginUserNullable == null) {
            return execute(businessId);
        }
 
        // 如果当前登录用户有租户标识
        try {
            String tenantCode = loginUserNullable.getTenantCode();
            if (StrUtil.isNotEmpty(tenantCode) && !TenantConstants.MASTER_DATASOURCE_NAME.equals(tenantCode)) {
                CurrentDataSourceContext.setDataSourceName(TENANT_DB_PREFIX + tenantCode);
            }
            return execute(businessId);
        } finally {
            // 清除数据源信息
            CurrentDataSourceContext.clearDataSourceName();
        }
    }
 
    /**
     * 业务逻辑执行
     *
     * @author fengshuonan
     * @date 2022/11/10 1:29
     */
    private Object execute(Object businessId) {
        Long userId = Convert.toLong(businessId);
        UserServiceApi bean = SpringUtil.getBean(UserServiceApi.class);
        SysUserDTO userInfoByUserId = bean.getUserInfoByUserId(userId);
        if (userInfoByUserId == null) {
            return NOT_FIND_USER_NAME;
        }
        return userInfoByUserId.getRealName();
    }
 
}