fix: 修复根据部门查询用户列表数据错误

This commit is contained in:
Charles7c 2024-05-01 22:05:46 +08:00
parent 15c966f7bb
commit 42ac82e7ce
6 changed files with 123 additions and 51 deletions

View File

@ -16,10 +16,14 @@
package top.continew.admin.system.mapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import top.continew.admin.common.config.mybatis.DataPermissionMapper;
import top.continew.admin.system.model.entity.UserDO;
import top.continew.starter.data.mybatis.plus.datapermission.DataPermission;
import top.continew.starter.security.crypto.annotation.FieldEncrypt;
/**
@ -30,6 +34,17 @@ import top.continew.starter.security.crypto.annotation.FieldEncrypt;
*/
public interface UserMapper extends DataPermissionMapper<UserDO> {
/**
* 分页查询列表
*
* @param page 分页条件
* @param queryWrapper 查询条件
* @return 分页列表信息
*/
@DataPermission
IPage<UserDO> selectUserPage(@Param("page") IPage<UserDO> page,
@Param(Constants.WRAPPER) QueryWrapper<UserDO> queryWrapper);
/**
* 根据用户名查询
*

View File

@ -18,10 +18,9 @@ package top.continew.admin.system.model.query;
import cn.hutool.core.date.DatePattern;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.Size;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import top.continew.starter.data.core.annotation.Query;
import top.continew.starter.data.core.enums.QueryType;
import java.io.Serial;
import java.io.Serializable;
@ -45,7 +44,6 @@ public class UserQuery implements Serializable {
* 关键词
*/
@Schema(description = "关键词", example = "zhangsan")
@Query(columns = {"username", "nickname", "description"}, type = QueryType.LIKE)
private String description;
/**
@ -58,8 +56,8 @@ public class UserQuery implements Serializable {
* 创建时间
*/
@Schema(description = "创建时间", example = "2023-08-08 00:00:00,2023-08-08 23:59:59")
@Query(type = QueryType.BETWEEN)
@DateTimeFormat(pattern = DatePattern.NORM_DATETIME_PATTERN)
@Size(max = 2, message = "创建时间必须是一个范围")
private List<Date> createTime;
/**

View File

@ -23,6 +23,8 @@ import top.continew.admin.system.model.resp.DeptResp;
import top.continew.starter.data.mybatis.plus.service.IService;
import top.continew.starter.extension.crud.service.BaseService;
import java.util.List;
/**
* 部门业务接口
*
@ -30,4 +32,12 @@ import top.continew.starter.extension.crud.service.BaseService;
* @since 2023/1/22 17:54
*/
public interface DeptService extends BaseService<DeptResp, DeptResp, DeptQuery, DeptReq>, IService<DeptDO> {
/**
* 查询子部门列表
*
* @param id ID
* @return 子部门列表
*/
List<DeptDO> listChildren(Long id);
}

View File

@ -20,6 +20,7 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.common.enums.DisEnableStatusEnum;
@ -50,9 +51,17 @@ import java.util.Optional;
@RequiredArgsConstructor
public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptResp, DeptResp, DeptQuery, DeptReq> implements DeptService {
private final UserService userService;
@Resource
private UserService userService;
private final RoleDeptService roleDeptService;
@Override
public List<DeptDO> listChildren(Long id) {
DatabaseType databaseType = MetaUtils.getDatabaseTypeOrDefault(SpringUtil
.getBean(DynamicRoutingDataSource.class), DatabaseType.MYSQL);
return baseMapper.lambdaQuery().apply(databaseType.findInSet(id, "ancestors")).list();
}
@Override
protected void beforeAdd(DeptReq req) {
String name = req.getName();
@ -150,18 +159,6 @@ public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptRes
return parentDept;
}
/**
* 查询子部门列表
*
* @param id ID
* @return 子部门列表
*/
private List<DeptDO> listChildren(Long id) {
DatabaseType databaseType = MetaUtils.getDatabaseTypeOrDefault(SpringUtil
.getBean(DynamicRoutingDataSource.class), DatabaseType.MYSQL);
return baseMapper.lambdaQuery().apply(databaseType.findInSet(id, "ancestors")).list();
}
/**
* 查询子部门数量
*

View File

@ -25,6 +25,9 @@ import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CacheUpdate;
import com.alicp.jetcache.anno.Cached;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
@ -38,6 +41,7 @@ import top.continew.admin.common.constant.CacheConstants;
import top.continew.admin.common.enums.DisEnableStatusEnum;
import top.continew.admin.common.util.helper.LoginHelper;
import top.continew.admin.system.mapper.UserMapper;
import top.continew.admin.system.model.entity.DeptDO;
import top.continew.admin.system.model.entity.UserDO;
import top.continew.admin.system.model.query.UserQuery;
import top.continew.admin.system.model.req.UserBasicInfoUpdateReq;
@ -46,19 +50,20 @@ import top.continew.admin.system.model.req.UserReq;
import top.continew.admin.system.model.req.UserRoleUpdateReq;
import top.continew.admin.system.model.resp.UserDetailResp;
import top.continew.admin.system.model.resp.UserResp;
import top.continew.admin.system.service.FileService;
import top.continew.admin.system.service.RoleService;
import top.continew.admin.system.service.UserRoleService;
import top.continew.admin.system.service.UserService;
import top.continew.admin.system.service.*;
import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.core.util.validate.CheckUtils;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.extension.crud.service.CommonUserService;
import top.continew.starter.extension.crud.service.impl.BaseServiceImpl;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* 用户业务实现
@ -76,9 +81,20 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
private final FileService fileService;
private final FileStorageService fileStorageService;
private final PasswordEncoder passwordEncoder;
@Resource
private DeptService deptService;
@Value("${avatar.support-suffix}")
private String[] avatarSupportSuffix;
@Override
public PageResp<UserResp> page(UserQuery query, PageQuery pageQuery) {
QueryWrapper<UserDO> queryWrapper = this.buildQueryWrapper(query);
IPage<UserDO> page = baseMapper.selectUserPage(pageQuery.toPage(), queryWrapper);
PageResp<UserResp> pageResp = PageResp.build(page, this.listClass);
pageResp.getList().forEach(this::fill);
return pageResp;
}
@Override
public Long add(UserDO user) {
user.setStatus(DisEnableStatusEnum.ENABLE);
@ -86,25 +102,6 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
return user.getId();
}
@Override
protected void beforeAdd(UserReq req) {
final String errorMsgTemplate = "新增失败,[{}] 已存在";
String username = req.getUsername();
CheckUtils.throwIf(this.isNameExists(username, null), errorMsgTemplate, username);
String email = req.getEmail();
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.isEmailExists(email, null), errorMsgTemplate, email);
String phone = req.getPhone();
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.isPhoneExists(phone, null), errorMsgTemplate, phone);
}
@Override
protected void afterAdd(UserReq req, UserDO user) {
Long userId = user.getId();
baseMapper.lambdaUpdate().set(UserDO::getPwdResetTime, LocalDateTime.now()).eq(UserDO::getId, userId).update();
// 保存用户和角色关联
userRoleService.add(req.getRoleIds(), userId);
}
@Override
@Transactional(rollbackFor = Exception.class)
@CacheUpdate(key = "#id", value = "#req.nickname", name = CacheConstants.USER_KEY_PREFIX)
@ -157,17 +154,6 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
super.delete(ids);
}
@Override
protected void fill(Object obj) {
super.fill(obj);
if (obj instanceof UserDetailResp detail) {
List<Long> roleIdList = detail.getRoleIds();
if (CollUtil.isNotEmpty(roleIdList)) {
detail.setRoleNames(String.join(StringConstants.CHINESE_COMMA, roleService.listNameByIds(roleIdList)));
}
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public String uploadAvatar(MultipartFile avatarFile, Long id) {
@ -275,6 +261,65 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
return baseMapper.selectNicknameById(id);
}
@Override
protected void fill(Object obj) {
super.fill(obj);
if (obj instanceof UserDetailResp detail) {
List<Long> roleIdList = detail.getRoleIds();
if (CollUtil.isNotEmpty(roleIdList)) {
detail.setRoleNames(String.join(StringConstants.CHINESE_COMMA, roleService.listNameByIds(roleIdList)));
}
}
}
@Override
protected void beforeAdd(UserReq req) {
final String errorMsgTemplate = "新增失败,[{}] 已存在";
String username = req.getUsername();
CheckUtils.throwIf(this.isNameExists(username, null), errorMsgTemplate, username);
String email = req.getEmail();
CheckUtils.throwIf(StrUtil.isNotBlank(email) && this.isEmailExists(email, null), errorMsgTemplate, email);
String phone = req.getPhone();
CheckUtils.throwIf(StrUtil.isNotBlank(phone) && this.isPhoneExists(phone, null), errorMsgTemplate, phone);
}
@Override
protected void afterAdd(UserReq req, UserDO user) {
Long userId = user.getId();
baseMapper.lambdaUpdate().set(UserDO::getPwdResetTime, LocalDateTime.now()).eq(UserDO::getId, userId).update();
// 保存用户和角色关联
userRoleService.add(req.getRoleIds(), userId);
}
/**
* 构建 QueryWrapper
*
* @param query 查询条件
* @return QueryWrapper
*/
private QueryWrapper<UserDO> buildQueryWrapper(UserQuery query) {
String description = query.getDescription();
Integer status = query.getStatus();
List<Date> createTimeList = query.getCreateTime();
Long deptId = query.getDeptId();
return new QueryWrapper<UserDO>().and(StrUtil.isNotBlank(description), q -> q.like("t1.username", description)
.or()
.like("t1.nickname", description)
.or()
.like("t1.description", description))
.eq(null != status, "t1.status", status)
.between(CollUtil.isNotEmpty(createTimeList), "t1.create_time", CollUtil.getFirst(createTimeList), CollUtil
.getLast(createTimeList))
.and(null != deptId, q -> {
List<Long> deptIdList = deptService.listChildren(deptId)
.stream()
.map(DeptDO::getId)
.collect(Collectors.toList());
deptIdList.add(deptId);
q.in("t1.dept_id", deptIdList);
});
}
/**
* 名称是否存在
*

View File

@ -2,6 +2,13 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="top.continew.admin.system.mapper.UserMapper">
<select id="selectUserPage" resultType="top.continew.admin.system.model.entity.UserDO">
SELECT t1.*
FROM sys_user AS t1
LEFT JOIN sys_dept AS t2 ON t2.id = t1.dept_id
${ew.customSqlSegment}
</select>
<select id="selectCountByEmail" resultType="java.lang.Long">
SELECT count(*)
FROM sys_user