refactor: 系统功能重构
This commit is contained in:
@@ -12,10 +12,12 @@ import com.youlai.system.common.constant.SystemConstants;
|
||||
import com.youlai.system.common.enums.GenderEnum;
|
||||
import com.youlai.system.common.enums.StatusEnum;
|
||||
import com.youlai.system.converter.UserConverter;
|
||||
import com.youlai.system.model.dto.UserImportDTO;
|
||||
import com.youlai.system.model.entity.SysDept;
|
||||
import com.youlai.system.model.entity.SysRole;
|
||||
import com.youlai.system.model.entity.SysUser;
|
||||
import com.youlai.system.model.entity.SysUserRole;
|
||||
import com.youlai.system.model.vo.UserImportVO;
|
||||
import com.youlai.system.service.SysDeptService;
|
||||
import com.youlai.system.service.SysRoleService;
|
||||
import com.youlai.system.service.SysUserRoleService;
|
||||
import com.youlai.system.service.SysUserService;
|
||||
@@ -28,13 +30,13 @@ import java.util.stream.Collectors;
|
||||
/**
|
||||
* 用户导入监听器
|
||||
* <p>
|
||||
* <a href="https://easyexcel.opensource.alibaba.com/docs/current/quickstart/read#%E6%9C%80%E7%AE%80%E5%8D%95%E7%9A%84%E8%AF%BB%E7%9A%84%E7%9B%91%E5%90%AC%E5%99%A8">最简单的读的监听器</a>
|
||||
* <a href="https://easyexcel.opensource.alibaba.com/docs/current/quickstart/read#%E6%9C%80%E7%AE%80%E5%8D%95%E7%9A%84%E8%AF%BB%E7%9A%84%E7%9B%91%E5%90%AC%E5%99%A8">最简单的读的监听器</a>
|
||||
*
|
||||
* @author haoxr
|
||||
* @since 2022/4/10 20:49
|
||||
* @author Ray
|
||||
* @since 2022/4/10
|
||||
*/
|
||||
@Slf4j
|
||||
public class UserImportListener extends MyAnalysisEventListener<UserImportVO> {
|
||||
public class UserImportListener extends MyAnalysisEventListener<UserImportDTO> {
|
||||
|
||||
|
||||
// 有效条数
|
||||
@@ -46,25 +48,19 @@ public class UserImportListener extends MyAnalysisEventListener<UserImportVO> {
|
||||
// 导入返回信息
|
||||
StringBuilder msg = new StringBuilder();
|
||||
|
||||
// 部门ID
|
||||
private final Long deptId;
|
||||
|
||||
private final SysUserService userService;
|
||||
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
private final UserConverter userConverter;
|
||||
|
||||
private final SysRoleService roleService;
|
||||
|
||||
private final SysUserRoleService userRoleService;
|
||||
private final SysDeptService deptService;
|
||||
|
||||
public UserImportListener(Long deptId) {
|
||||
this.deptId = deptId;
|
||||
public UserImportListener() {
|
||||
this.userService = SpringUtil.getBean(SysUserService.class);
|
||||
this.passwordEncoder = SpringUtil.getBean(PasswordEncoder.class);
|
||||
this.roleService = SpringUtil.getBean(SysRoleService.class);
|
||||
this.userRoleService = SpringUtil.getBean(SysUserRoleService.class);
|
||||
this.deptService = SpringUtil.getBean(SysDeptService.class);
|
||||
this.userConverter = SpringUtil.getBean(UserConverter.class);
|
||||
}
|
||||
|
||||
@@ -74,16 +70,15 @@ public class UserImportListener extends MyAnalysisEventListener<UserImportVO> {
|
||||
* 1. 数据校验;全字段校验
|
||||
* 2. 数据持久化;
|
||||
*
|
||||
* @param userImportVO 一行数据,类似于 {@link AnalysisContext#readRowHolder()}
|
||||
* @param analysisContext
|
||||
* @param userImportDTO 一行数据,类似于 {@link AnalysisContext#readRowHolder()}
|
||||
*/
|
||||
@Override
|
||||
public void invoke(UserImportVO userImportVO, AnalysisContext analysisContext) {
|
||||
log.info("解析到一条用户数据:{}", JSONUtil.toJsonStr(userImportVO));
|
||||
public void invoke(UserImportDTO userImportDTO, AnalysisContext analysisContext) {
|
||||
log.info("解析到一条用户数据:{}", JSONUtil.toJsonStr(userImportDTO));
|
||||
// 校验数据
|
||||
StringBuilder validationMsg = new StringBuilder();
|
||||
|
||||
String username = userImportVO.getUsername();
|
||||
String username = userImportDTO.getUsername();
|
||||
if (StrUtil.isBlank(username)) {
|
||||
validationMsg.append("用户名为空;");
|
||||
} else {
|
||||
@@ -93,12 +88,12 @@ public class UserImportListener extends MyAnalysisEventListener<UserImportVO> {
|
||||
}
|
||||
}
|
||||
|
||||
String nickname = userImportVO.getNickname();
|
||||
String nickname = userImportDTO.getNickname();
|
||||
if (StrUtil.isBlank(nickname)) {
|
||||
validationMsg.append("用户昵称为空;");
|
||||
}
|
||||
|
||||
String mobile = userImportVO.getMobile();
|
||||
String mobile = userImportDTO.getMobile();
|
||||
if (StrUtil.isBlank(mobile)) {
|
||||
validationMsg.append("手机号码为空;");
|
||||
} else {
|
||||
@@ -109,29 +104,38 @@ public class UserImportListener extends MyAnalysisEventListener<UserImportVO> {
|
||||
|
||||
if (validationMsg.isEmpty()) {
|
||||
// 校验通过,持久化至数据库
|
||||
SysUser entity = userConverter.importVo2Entity(userImportVO);
|
||||
entity.setDeptId(deptId); // 部门
|
||||
SysUser entity = userConverter.convertToEntity(userImportDTO);
|
||||
entity.setPassword(passwordEncoder.encode(SystemConstants.DEFAULT_PASSWORD)); // 默认密码
|
||||
// 性别翻译
|
||||
String genderLabel = userImportVO.getGenderLabel();
|
||||
String genderLabel = userImportDTO.getGenderLabel();
|
||||
if (StrUtil.isNotBlank(genderLabel)) {
|
||||
Integer genderValue = (Integer) IBaseEnum.getValueByLabel(genderLabel, GenderEnum.class);
|
||||
entity.setGender(genderValue);
|
||||
}
|
||||
|
||||
// 角色解析
|
||||
String roleCodes = userImportVO.getRoleCodes();
|
||||
String roleCodes = userImportDTO.getRoleCodes();
|
||||
List<Long> roleIds = null;
|
||||
if (StrUtil.isNotBlank(roleCodes)) {
|
||||
roleIds = roleService.list(
|
||||
new LambdaQueryWrapper<SysRole>()
|
||||
.in(SysRole::getCode, roleCodes.split(","))
|
||||
.in(SysRole::getCode, (Object) roleCodes.split(","))
|
||||
.eq(SysRole::getStatus, StatusEnum.ENABLE.getValue())
|
||||
.select(SysRole::getId)
|
||||
).stream()
|
||||
.map(SysRole::getId)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
// 部门解析
|
||||
String deptCode = userImportDTO.getDeptCode();
|
||||
if (StrUtil.isNotBlank(deptCode)) {
|
||||
SysDept dept = deptService.getOne(new LambdaQueryWrapper<SysDept>().eq(SysDept::getCode, deptCode)
|
||||
.select(SysDept::getId)
|
||||
);
|
||||
if (dept != null) {
|
||||
entity.setDeptId(dept.getId());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
boolean saveResult = userService.save(entity);
|
||||
@@ -150,18 +154,17 @@ public class UserImportListener extends MyAnalysisEventListener<UserImportVO> {
|
||||
}
|
||||
} else {
|
||||
invalidCount++;
|
||||
msg.append("第" + (validCount + invalidCount) + "行数据校验失败:").append(validationMsg + "<br/>");
|
||||
msg.append("第").append(validCount + invalidCount).append("行数据校验失败:").append(validationMsg + "<br/>");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 所有数据解析完成会来调用
|
||||
*
|
||||
* @param analysisContext
|
||||
*/
|
||||
@Override
|
||||
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
||||
log.info("所有数据解析完成!");
|
||||
|
||||
}
|
||||
|
||||
@@ -169,7 +172,6 @@ public class UserImportListener extends MyAnalysisEventListener<UserImportVO> {
|
||||
@Override
|
||||
public String getMsg() {
|
||||
// 总结信息
|
||||
String summaryMsg = StrUtil.format("导入用户结束:成功{}条,失败{}条;<br/>{}", validCount, invalidCount, msg);
|
||||
return summaryMsg;
|
||||
return StrUtil.format("导入用户结束:成功{}条,失败{}条;<br/>{}", validCount, invalidCount, msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package com.youlai.system.plugin.dupsubmit.annotation;
|
||||
package com.youlai.system.plugin.norepeat.annotation;
|
||||
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 防止重复提交注解
|
||||
* <p>
|
||||
* 该注解用于方法上,防止在指定时间内的重复提交。
|
||||
* 默认时间为5秒。
|
||||
*
|
||||
* @author haoxr
|
||||
* @since 2.3.0
|
||||
@@ -13,10 +16,10 @@ import java.lang.annotation.*;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
public @interface PreventDuplicateSubmit {
|
||||
public @interface PreventRepeatSubmit {
|
||||
|
||||
/**
|
||||
* 防重提交锁过期时间(秒)
|
||||
* 锁过期时间(秒)
|
||||
* <p>
|
||||
* 默认5秒内不允许重复提交
|
||||
*/
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.youlai.system.plugin.dupsubmit.aspect;
|
||||
package com.youlai.system.plugin.norepeat.aspect;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.jwt.JWTUtil;
|
||||
@@ -6,7 +6,7 @@ import cn.hutool.jwt.RegisteredPayload;
|
||||
import com.youlai.system.common.constant.SecurityConstants;
|
||||
import com.youlai.system.common.exception.BusinessException;
|
||||
import com.youlai.system.common.result.ResultCode;
|
||||
import com.youlai.system.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
|
||||
import com.youlai.system.plugin.norepeat.annotation.PreventRepeatSubmit;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -41,17 +41,17 @@ public class DuplicateSubmitAspect {
|
||||
/**
|
||||
* 防重复提交切点
|
||||
*/
|
||||
@Pointcut("@annotation(preventDuplicateSubmit)")
|
||||
public void preventDuplicateSubmitPointCut(PreventDuplicateSubmit preventDuplicateSubmit) {
|
||||
@Pointcut("@annotation(preventRepeatSubmit)")
|
||||
public void preventDuplicateSubmitPointCut(PreventRepeatSubmit preventRepeatSubmit) {
|
||||
log.info("定义防重复提交切点");
|
||||
}
|
||||
|
||||
@Around("preventDuplicateSubmitPointCut(preventDuplicateSubmit)")
|
||||
public Object doAround(ProceedingJoinPoint pjp, PreventDuplicateSubmit preventDuplicateSubmit) throws Throwable {
|
||||
@Around("preventDuplicateSubmitPointCut(preventRepeatSubmit)")
|
||||
public Object doAround(ProceedingJoinPoint pjp, PreventRepeatSubmit preventRepeatSubmit) throws Throwable {
|
||||
|
||||
String resubmitLockKey = generateResubmitLockKey();
|
||||
if (resubmitLockKey != null) {
|
||||
int expire = preventDuplicateSubmit.expire(); // 防重提交锁过期时间
|
||||
int expire = preventRepeatSubmit.expire(); // 防重提交锁过期时间
|
||||
RLock lock = redissonClient.getLock(resubmitLockKey);
|
||||
boolean lockResult = lock.tryLock(0, expire, TimeUnit.SECONDS); // 获取锁失败,直接返回 false
|
||||
if (!lockResult) {
|
||||
Reference in New Issue
Block a user