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() .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(); } }