fix(user-invite-codes): 修复字段名拼写错误并新增代理邀请码生成
修正 owenrSystemUserId → ownerSystemUserId 拼写错误; 新增 UserInviteCodeApi 及实现,为代理租户自动生成 6 位唯一邀请码。
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package com.yolo.keyboard.api.invitecode;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.yolo.keyboard.dal.dataobject.userinvitecodes.KeyboardUserInviteCodesDO;
|
||||
import com.yolo.keyboard.dal.mysql.userinvitecodes.KeyboardUserInviteCodesMapper;
|
||||
import com.yolo.keyboard.module.system.api.invitecode.UserInviteCodeApi;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 用户邀请码 API 实现类
|
||||
*
|
||||
* @author ziin
|
||||
*/
|
||||
@Service
|
||||
public class UserInviteCodeApiImpl implements UserInviteCodeApi {
|
||||
|
||||
@Resource
|
||||
private KeyboardUserInviteCodesMapper userInviteCodesMapper;
|
||||
|
||||
@Override
|
||||
public String createInviteCodeForAgent(Long userId, Long tenantId) {
|
||||
String inviteCode = generateUniqueInviteCode();
|
||||
KeyboardUserInviteCodesDO inviteCodeDO = KeyboardUserInviteCodesDO.builder()
|
||||
.code(inviteCode)
|
||||
.ownerSystemUserId(userId)
|
||||
.ownerTenantId(tenantId)
|
||||
.status((short) 1)
|
||||
.createdAt(LocalDateTime.now())
|
||||
.usedCount(0)
|
||||
.inviteType("AGENT")
|
||||
.build();
|
||||
userInviteCodesMapper.insert(inviteCodeDO);
|
||||
return inviteCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成唯一的6位邀请码(字母和数字混合)
|
||||
*/
|
||||
private String generateUniqueInviteCode() {
|
||||
String code;
|
||||
int maxAttempts = 100;
|
||||
int attempt = 0;
|
||||
do {
|
||||
code = RandomUtil.randomString("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 6);
|
||||
attempt++;
|
||||
if (attempt >= maxAttempts) {
|
||||
throw new RuntimeException("无法生成唯一的邀请码,请稍后重试");
|
||||
}
|
||||
} while (userInviteCodesMapper.selectOne(KeyboardUserInviteCodesDO::getCode, code) != null);
|
||||
return code;
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@ public class KeyboardUserInviteCodesPageReqVO extends PageParam {
|
||||
private Long ownerTenantId;
|
||||
|
||||
@Schema(description = "邀请码所属系统用户", example = "772")
|
||||
private Long owenrSystemUserId;
|
||||
private Long ownerSystemUserId;
|
||||
|
||||
@Schema(description = "邀请码类型", example = "1")
|
||||
private String inviteType;
|
||||
|
||||
@@ -54,7 +54,7 @@ public class KeyboardUserInviteCodesRespVO {
|
||||
|
||||
@Schema(description = "邀请码所属系统用户", example = "772")
|
||||
@ExcelProperty("邀请码所属系统用户")
|
||||
private Long owenrSystemUserId;
|
||||
private Long ownerSystemUserId;
|
||||
|
||||
@Schema(description = "邀请码类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||
@ExcelProperty("邀请码类型")
|
||||
|
||||
@@ -47,7 +47,7 @@ public class KeyboardUserInviteCodesSaveReqVO {
|
||||
private Long ownerTenantId;
|
||||
|
||||
@Schema(description = "邀请码所属系统用户", example = "772")
|
||||
private Long owenrSystemUserId;
|
||||
private Long ownerSystemUserId;
|
||||
|
||||
@Schema(description = "邀请码类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||
@NotEmpty(message = "邀请码类型不能为空")
|
||||
|
||||
@@ -14,7 +14,7 @@ import com.yolo.keyboard.framework.mybatis.core.dataobject.BaseDO;
|
||||
* @author ziin
|
||||
*/
|
||||
@TableName("keyboard_user_invite_codes")
|
||||
@KeySequence("keyboard_user_invite_codes_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
|
||||
@KeySequence("invite_codes_id_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
|
||||
@Data
|
||||
@ToString(callSuper = true)
|
||||
@Builder
|
||||
@@ -67,7 +67,7 @@ public class KeyboardUserInviteCodesDO {
|
||||
/**
|
||||
* 邀请码所属系统用户
|
||||
*/
|
||||
private Long owenrSystemUserId;
|
||||
private Long ownerSystemUserId;
|
||||
/**
|
||||
* 邀请码类型
|
||||
*/
|
||||
|
||||
@@ -28,7 +28,7 @@ public interface KeyboardUserInviteCodesMapper extends BaseMapperX<KeyboardUserI
|
||||
.eqIfPresent(KeyboardUserInviteCodesDO::getUsedCount, reqVO.getUsedCount())
|
||||
.eqIfPresent(KeyboardUserInviteCodesDO::getSystemUserId, reqVO.getSystemUserId())
|
||||
.eqIfPresent(KeyboardUserInviteCodesDO::getOwnerTenantId, reqVO.getOwnerTenantId())
|
||||
.eqIfPresent(KeyboardUserInviteCodesDO::getOwenrSystemUserId, reqVO.getOwenrSystemUserId())
|
||||
.eqIfPresent(KeyboardUserInviteCodesDO::getOwnerSystemUserId, reqVO.getOwnerSystemUserId())
|
||||
.eqIfPresent(KeyboardUserInviteCodesDO::getInviteType, reqVO.getInviteType())
|
||||
.orderByDesc(KeyboardUserInviteCodesDO::getId));
|
||||
}
|
||||
|
||||
@@ -5,21 +5,18 @@ import com.yolo.keyboard.controller.admin.themes.vo.KeyboardThemesPageReqVO;
|
||||
import com.yolo.keyboard.controller.admin.themes.vo.KeyboardThemesSaveReqVO;
|
||||
import com.yolo.keyboard.dal.dataobject.themes.KeyboardThemesDO;
|
||||
import com.yolo.keyboard.dal.mysql.themes.KeyboardThemesMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import com.yolo.keyboard.framework.common.pojo.PageResult;
|
||||
import com.yolo.keyboard.framework.common.pojo.PageParam;
|
||||
import com.yolo.keyboard.framework.common.util.object.BeanUtils;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static com.yolo.keyboard.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static com.yolo.keyboard.framework.common.util.collection.CollectionUtils.convertList;
|
||||
import static com.yolo.keyboard.framework.common.util.collection.CollectionUtils.diffList;
|
||||
import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.THEMES_NOT_EXISTS;
|
||||
|
||||
/**
|
||||
@@ -31,15 +28,25 @@ import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.THEMES_NOT
|
||||
@Validated
|
||||
public class KeyboardThemesServiceImpl implements KeyboardThemesService {
|
||||
|
||||
private static final String THEME_STYLE_KEY_PREFIX = "theme:style:";
|
||||
private static final String THEME_STYLE_ALL_KEY = "theme:style:all";
|
||||
private static final Long ALL_STYLE_ID = 9999L;
|
||||
|
||||
@Resource
|
||||
private KeyboardThemesMapper themesMapper;
|
||||
|
||||
@Resource
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
@Override
|
||||
public Long createThemes(KeyboardThemesSaveReqVO createReqVO) {
|
||||
// 插入
|
||||
KeyboardThemesDO themes = BeanUtils.toBean(createReqVO, KeyboardThemesDO.class);
|
||||
themesMapper.insert(themes);
|
||||
|
||||
// 同步更新 Redis 缓存
|
||||
refreshThemeStyleCache(themes.getThemeStyle());
|
||||
|
||||
// 返回
|
||||
return themes.getId();
|
||||
}
|
||||
@@ -47,25 +54,57 @@ public class KeyboardThemesServiceImpl implements KeyboardThemesService {
|
||||
@Override
|
||||
public void updateThemes(KeyboardThemesSaveReqVO updateReqVO) {
|
||||
// 校验存在
|
||||
KeyboardThemesDO oldTheme = themesMapper.selectById(updateReqVO.getId());
|
||||
validateThemesExists(updateReqVO.getId());
|
||||
|
||||
// 更新
|
||||
KeyboardThemesDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardThemesDO.class);
|
||||
themesMapper.updateById(updateObj);
|
||||
|
||||
// 同步更新 Redis 缓存(如果风格变更,需要刷新新旧两个风格的缓存)
|
||||
refreshThemeStyleCache(updateReqVO.getThemeStyle());
|
||||
if (oldTheme != null && oldTheme.getThemeStyle() != null
|
||||
&& !oldTheme.getThemeStyle().equals(updateReqVO.getThemeStyle())) {
|
||||
refreshThemeStyleCache(oldTheme.getThemeStyle());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteThemes(Long id) {
|
||||
// 校验存在
|
||||
KeyboardThemesDO theme = themesMapper.selectById(id);
|
||||
validateThemesExists(id);
|
||||
|
||||
// 删除
|
||||
themesMapper.deleteById(id);
|
||||
|
||||
// 同步更新 Redis 缓存
|
||||
if (theme != null && theme.getThemeStyle() != null) {
|
||||
refreshThemeStyleCache(theme.getThemeStyle());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteThemesListByIds(List<Long> ids) {
|
||||
public void deleteThemesListByIds(List<Long> ids) {
|
||||
// 获取要删除的主题列表,记录其风格
|
||||
List<KeyboardThemesDO> themes = themesMapper.selectBatchIds(ids);
|
||||
Set<Long> styleIds = new HashSet<>();
|
||||
if (CollUtil.isNotEmpty(themes)) {
|
||||
for (KeyboardThemesDO theme : themes) {
|
||||
if (theme.getThemeStyle() != null) {
|
||||
styleIds.add(theme.getThemeStyle());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 删除
|
||||
themesMapper.deleteByIds(ids);
|
||||
|
||||
// 同步更新 Redis 缓存
|
||||
for (Long styleId : styleIds) {
|
||||
refreshThemeStyleCache(styleId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void validateThemesExists(Long id) {
|
||||
@@ -84,4 +123,32 @@ public class KeyboardThemesServiceImpl implements KeyboardThemesService {
|
||||
return themesMapper.selectPage(pageReqVO);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新主题风格缓存
|
||||
*
|
||||
* @param styleId 风格ID
|
||||
*/
|
||||
private void refreshThemeStyleCache(Long styleId) {
|
||||
if (styleId == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. 刷新指定风格的缓存
|
||||
String styleKey = THEME_STYLE_KEY_PREFIX + styleId;
|
||||
List<KeyboardThemesDO> themesByStyle = themesMapper.selectList(KeyboardThemesDO::getThemeStyle, styleId);
|
||||
if (CollUtil.isNotEmpty(themesByStyle)) {
|
||||
redisTemplate.opsForValue().set(styleKey, themesByStyle);
|
||||
} else {
|
||||
redisTemplate.delete(styleKey);
|
||||
}
|
||||
|
||||
// 2. 刷新全部主题缓存(theme:style:all)
|
||||
List<KeyboardThemesDO> allThemes = themesMapper.selectList();
|
||||
if (CollUtil.isNotEmpty(allThemes)) {
|
||||
redisTemplate.opsForValue().set(THEME_STYLE_ALL_KEY, allThemes);
|
||||
} else {
|
||||
redisTemplate.delete(THEME_STYLE_ALL_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user