hejianhao
2026-09-01 540ef65220e05b75c183cd3554aaeb554927600a
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.tcm.training.admin.app.user.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tcm.training.admin.app.auth.service.AppAuthSessionService;
import com.tcm.training.admin.app.user.vo.AppUserCenterVO;
import com.tcm.training.admin.live.entity.Clinic;
import com.tcm.training.admin.live.mapper.ClinicMapper;
import com.tcm.training.admin.member.entity.AppUser;
import com.tcm.training.admin.member.mapper.UserMapper;
import com.tcm.training.admin.points.entity.UserPointsAccount;
import com.tcm.training.admin.points.mapper.UserPointsAccountMapper;
import org.springframework.stereotype.Service;
 
/**
 * APP 个人中心聚合服务。
 */
@Service
public class AppUserCenterService {
 
    private static final int USER_STATUS_ENABLED = 1;
    private static final int USER_TYPE_PERSONAL = 0;
    private static final int USER_TYPE_DOCTOR = 1;
 
    private final AppAuthSessionService appAuthSessionService;
    private final UserMapper userMapper;
    private final UserPointsAccountMapper userPointsAccountMapper;
    private final ClinicMapper clinicMapper;
 
    /**
     * 注入 APP 个人中心聚合所需依赖。
     */
    public AppUserCenterService(AppAuthSessionService appAuthSessionService,
                                UserMapper userMapper,
                                UserPointsAccountMapper userPointsAccountMapper,
                                ClinicMapper clinicMapper) {
        this.appAuthSessionService = appAuthSessionService;
        this.userMapper = userMapper;
        this.userPointsAccountMapper = userPointsAccountMapper;
        this.clinicMapper = clinicMapper;
    }
 
    /**
     * 获取当前登录用户的个人中心聚合信息。
     */
    public AppUserCenterVO getCurrentUserCenter() {
        Long userId = appAuthSessionService.requireLoginUserId();
        AppUser user = requireEnabledUser(userId);
        AppUserCenterVO result = new AppUserCenterVO();
        result.setId(user.getId());
        result.setNickname(resolveNickname(user));
        result.setAvatar(resolveAvatar(user));
        result.setPhone(trimToEmpty(user.getPhone()));
        result.setType(user.getType());
        result.setTypeText(resolveTypeText(user.getType()));
        result.setMemberExpireAt(user.getMemberExpireAt());
        result.setPointsBalance(resolvePointsBalance(user.getId()));
        result.setClinicId(user.getClinicId());
        // 医生用户才需要回显所属诊所,个人用户统一返回空诊所名称。
        result.setClinicName(resolveClinicName(user));
        return result;
    }
 
    /**
     * 校验当前登录用户存在且可用。
     */
    private AppUser requireEnabledUser(Long userId) {
        AppUser user = userMapper.selectById(userId);
        if (user == null || user.getDeleted() == null || user.getDeleted().intValue() != 0) {
            throw new IllegalArgumentException("用户不存在");
        }
        if (user.getStatus() == null || user.getStatus().intValue() != USER_STATUS_ENABLED) {
            throw new IllegalArgumentException("用户已禁用");
        }
        return user;
    }
 
    /**
     * 读取当前用户积分余额,没有积分账户时按 0 返回。
     */
    private Integer resolvePointsBalance(Long userId) {
        UserPointsAccount account = userPointsAccountMapper.selectOne(new LambdaQueryWrapper<UserPointsAccount>()
                .eq(UserPointsAccount::getAppUserId, userId)
                .eq(UserPointsAccount::getDeleted, 0)
                .orderByDesc(UserPointsAccount::getId)
                .last("LIMIT 1"));
        if (account == null || account.getPointsBalance() == null) {
            return Integer.valueOf(0);
        }
        return account.getPointsBalance();
    }
 
    /**
     * 读取医生用户所属诊所名称,不满足条件时返回空字符串。
     */
    private String resolveClinicName(AppUser user) {
        if (user == null || user.getType() == null || user.getType().intValue() != USER_TYPE_DOCTOR || user.getClinicId() == null) {
            return "";
        }
        Clinic clinic = clinicMapper.selectById(user.getClinicId());
        if (clinic == null || clinic.getDeleted() == null || clinic.getDeleted().intValue() != 0) {
            return "";
        }
        return trimToEmpty(clinic.getName());
    }
 
    /**
     * 解析用户类型展示文案。
     */
    private String resolveTypeText(Integer userType) {
        if (userType != null && userType.intValue() == USER_TYPE_DOCTOR) {
            return "医生";
        }
        if (userType != null && userType.intValue() == USER_TYPE_PERSONAL) {
            return "个人";
        }
        return "";
    }
 
    /**
     * 优先使用本地昵称,没有时回退微信昵称和手机号。
     */
    private String resolveNickname(AppUser user) {
        String nickname = trimToEmpty(user == null ? null : user.getNickname());
        if (!nickname.isEmpty()) {
            return nickname;
        }
        String wechatNickname = trimToEmpty(user == null ? null : user.getWechatNickname());
        if (!wechatNickname.isEmpty()) {
            return wechatNickname;
        }
        return trimToEmpty(user == null ? null : user.getPhone());
    }
 
    /**
     * 优先使用本地头像,没有时回退微信头像。
     */
    private String resolveAvatar(AppUser user) {
        String avatar = trimToEmpty(user == null ? null : user.getAvatar());
        if (!avatar.isEmpty()) {
            return avatar;
        }
        return trimToEmpty(user == null ? null : user.getWechatAvatar());
    }
 
    /**
     * 将可空文本归一化为空字符串。
     */
    private String trimToEmpty(String value) {
        if (value == null) {
            return "";
        }
        return value.trim();
    }
}