新增:新增系统管理/用户管理(列表、查看详情、新增、修改、删除、导出)

This commit is contained in:
2023-02-22 23:58:28 +08:00
parent 297fbd3675
commit efa8f4fcf5
48 changed files with 1805 additions and 154 deletions

View File

@@ -16,8 +16,7 @@
package top.charles7c.cnadmin.system.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import top.charles7c.cnadmin.common.base.BaseMapper;
import top.charles7c.cnadmin.system.model.entity.DeptDO;
/**

View File

@@ -16,8 +16,7 @@
package top.charles7c.cnadmin.system.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import top.charles7c.cnadmin.common.base.BaseMapper;
import top.charles7c.cnadmin.system.model.entity.MenuDO;
/**

View File

@@ -16,8 +16,7 @@
package top.charles7c.cnadmin.system.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import top.charles7c.cnadmin.common.base.BaseMapper;
import top.charles7c.cnadmin.system.model.entity.RoleDO;
/**

View File

@@ -16,8 +16,7 @@
package top.charles7c.cnadmin.system.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import top.charles7c.cnadmin.common.base.BaseMapper;
import top.charles7c.cnadmin.system.model.entity.UserDO;
/**

View File

@@ -16,8 +16,7 @@
package top.charles7c.cnadmin.system.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import top.charles7c.cnadmin.common.base.BaseMapper;
import top.charles7c.cnadmin.system.model.entity.UserRoleDO;
/**

View File

@@ -65,16 +65,16 @@ public class UserDO extends BaseDO {
*/
private GenderEnum gender;
/**
* 手机号码
*/
private String phone;
/**
* 邮箱
*/
private String email;
/**
* 手机号码
*/
private String phone;
/**
* 头像地址
*/

View File

@@ -19,6 +19,7 @@ package top.charles7c.cnadmin.system.model.entity;
import java.io.Serializable;
import lombok.Data;
import lombok.NoArgsConstructor;
import com.baomidou.mybatisplus.annotation.TableName;
@@ -29,6 +30,7 @@ import com.baomidou.mybatisplus.annotation.TableName;
* @since 2023/2/13 23:13
*/
@Data
@NoArgsConstructor
@TableName("sys_user_role")
public class UserRoleDO implements Serializable {
@@ -43,4 +45,9 @@ public class UserRoleDO implements Serializable {
* 角色 ID
*/
private Long roleId;
public UserRoleDO(Long userId, Long roleId) {
this.userId = userId;
this.roleId = roleId;
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.system.model.query;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import lombok.Data;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.format.annotation.DateTimeFormat;
import top.charles7c.cnadmin.common.annotation.Query;
/**
* 用户查询条件
*
* @author Charles7c
* @since 2023/2/20 21:01
*/
@Data
@ParameterObject
@Schema(description = "用户查询条件")
public class UserQuery implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 用户名
*/
@Schema(description = "用户名")
@Query(blurry = "username,nickname,email,phone")
private String username;
/**
* 状态1启用 2禁用
*/
@Schema(description = "状态1启用 2禁用")
@Query
private Integer status;
/**
* 创建时间
*/
@Schema(description = "创建时间")
@Query(type = Query.Type.BETWEEN)
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private List<Date> createTime;
/**
* 部门 ID
*/
@Schema(description = "部门 ID")
@Query
private Long deptId;
}

View File

@@ -0,0 +1,117 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.system.model.request;
import java.util.List;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import javax.validation.constraints.Pattern;
import lombok.Data;
import io.swagger.v3.oas.annotations.media.Schema;
import org.hibernate.validator.constraints.Length;
import cn.hutool.core.lang.RegexPool;
import top.charles7c.cnadmin.common.base.BaseRequest;
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
import top.charles7c.cnadmin.common.enums.GenderEnum;
/**
* 创建或修改用户信息
*
* @author Charles7c
* @since 2023/2/20 21:03
*/
@Data
@Schema(description = "创建或修改用户信息")
public class UserRequest extends BaseRequest {
private static final long serialVersionUID = 1L;
/**
* 用户 ID
*/
@Schema(description = "角色 ID")
@Null(message = "新增时ID 必须为空", groups = Create.class)
@NotNull(message = "修改时ID 不能为空", groups = Update.class)
private Long userId;
/**
* 用户名
*/
@Schema(description = "用户名")
@NotBlank(message = "用户名不能为空")
private String username;
/**
* 昵称
*/
@Schema(description = "昵称")
@Length(max = 32, message = "昵称长度不能超过 {max} 个字符")
private String nickname;
/**
* 性别0未知 1男 2女
*/
@Schema(description = "性别0未知 1男 2女", type = "Integer", allowableValues = {"0", "1", "2"})
@NotNull(message = "性别非法")
private GenderEnum gender;
/**
* 邮箱
*/
@Schema(description = "邮箱")
@Pattern(regexp = RegexPool.EMAIL, message = "邮箱格式错误")
private String email;
/**
* 手机号码
*/
@Schema(description = "手机号码")
@Pattern(regexp = RegexPool.MOBILE, message = "手机号码格式错误")
private String phone;
/**
* 描述
*/
@Schema(description = "描述")
@Length(max = 200, message = "描述长度不能超过 {max} 个字符")
private String description;
/**
* 状态1启用 2禁用
*/
@Schema(description = "状态1启用 2禁用", type = "Integer", allowableValues = {"1", "2"})
private DisEnableStatusEnum status;
/**
* 部门 ID
*/
@Schema(description = "部门 ID")
private Long deptId;
/**
* 角色 ID 列表
*/
@Schema(description = "角色 ID 列表")
private List<Long> roleIds;
}

View File

@@ -0,0 +1,134 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.system.model.vo;
import java.util.List;
import lombok.Data;
import io.swagger.v3.oas.annotations.media.Schema;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import top.charles7c.cnadmin.common.base.BaseDetailVO;
import top.charles7c.cnadmin.common.config.easyexcel.ExcelBaseEnumConverter;
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
import top.charles7c.cnadmin.common.enums.GenderEnum;
/**
* 用户详情信息
*
* @author Charles7c
* @since 2023/2/20 21:11
*/
@Data
@ExcelIgnoreUnannotated
@Schema(description = "用户详情信息")
public class UserDetailVO extends BaseDetailVO {
private static final long serialVersionUID = 1L;
/**
* 用户 ID
*/
@Schema(description = "用户 ID")
@ExcelProperty(value = "用户ID")
private Long userId;
/**
* 用户名
*/
@Schema(description = "用户名")
@ExcelProperty(value = "用户名")
private String username;
/**
* 昵称
*/
@Schema(description = "昵称")
@ExcelProperty(value = "昵称")
private String nickname;
/**
* 性别0未知 1男 2女
*/
@Schema(description = "性别0未知 1男 2女")
@ExcelProperty(value = "性别", converter = ExcelBaseEnumConverter.class)
private GenderEnum gender;
/**
* 邮箱
*/
@Schema(description = "邮箱")
@ExcelProperty(value = "邮箱")
private String email;
/**
* 手机号码
*/
@Schema(description = "手机号码")
@ExcelProperty(value = "手机号码")
private String phone;
/**
* 头像地址
*/
@Schema(description = "头像地址")
@ExcelProperty(value = "头像地址")
private String avatar;
/**
* 状态1启用 2禁用
*/
@Schema(description = "状态1启用 2禁用")
@ExcelProperty(value = "状态", converter = ExcelBaseEnumConverter.class)
private DisEnableStatusEnum status;
/**
* 描述
*/
@Schema(description = "描述")
@ExcelProperty(value = "描述")
private String description;
/**
* 角色 ID 列表
*/
@Schema(description = "角色 ID 列表")
private List<Long> roleIds;
/**
* 所属角色
*/
@Schema(description = "所属角色")
@ExcelProperty(value = "所属角色")
private String roleNames;
/**
* 部门 ID
*/
@Schema(description = "部门 ID")
private Long deptId;
/**
* 所属部门
*/
@Schema(description = "所属部门")
@ExcelProperty(value = "所属部门")
private String deptName;
}

View File

@@ -0,0 +1,119 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.system.model.vo;
import lombok.Data;
import lombok.experimental.Accessors;
import io.swagger.v3.oas.annotations.media.Schema;
import com.fasterxml.jackson.annotation.JsonInclude;
import cn.hutool.core.util.DesensitizedUtil;
import top.charles7c.cnadmin.common.base.BaseVO;
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
import top.charles7c.cnadmin.common.enums.GenderEnum;
import top.charles7c.cnadmin.common.util.helper.LoginHelper;
/**
* 用户信息
*
* @author Charles7c
* @since 2023/2/20 21:08
*/
@Data
@Accessors(chain = true)
@Schema(description = "用户信息")
public class UserVO extends BaseVO {
private static final long serialVersionUID = 1L;
/**
* 用户 ID
*/
@Schema(description = "用户 ID")
private Long userId;
/**
* 用户名
*/
@Schema(description = "用户名")
private String username;
/**
* 昵称
*/
@Schema(description = "昵称")
private String nickname;
/**
* 性别0未知 1男 2女
*/
@Schema(description = "性别0未知 1男 2女")
private GenderEnum gender;
/**
* 邮箱
*/
@Schema(description = "邮箱")
private String email;
/**
* 手机号码
*/
@Schema(description = "手机号码")
private String phone;
/**
* 头像地址
*/
@Schema(description = "头像地址")
private String avatar;
/**
* 状态1启用 2禁用
*/
@Schema(description = "状态1启用 2禁用")
private DisEnableStatusEnum status;
/**
* 描述
*/
@Schema(description = "描述")
private String description;
/**
* 是否禁用修改
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
private Boolean disabled;
public Boolean getDisabled() {
if (userId.equals(LoginHelper.getUserId())) {
return true;
}
return disabled;
}
public String getPhone() {
if (phone == null) {
return null;
}
return DesensitizedUtil.mobilePhone(phone);
}
}

View File

@@ -16,6 +16,10 @@
package top.charles7c.cnadmin.system.service;
import java.util.List;
import cn.hutool.core.lang.tree.Tree;
import top.charles7c.cnadmin.common.base.BaseService;
import top.charles7c.cnadmin.system.model.query.RoleQuery;
import top.charles7c.cnadmin.system.model.request.RoleRequest;
@@ -28,4 +32,23 @@ import top.charles7c.cnadmin.system.model.vo.RoleVO;
* @author Charles7c
* @since 2023/2/8 23:15
*/
public interface RoleService extends BaseService<RoleVO, RoleDetailVO, RoleQuery, RoleRequest> {}
public interface RoleService extends BaseService<RoleVO, RoleDetailVO, RoleQuery, RoleRequest> {
/**
* 构建树
*
* @param list
* 原始列表数据
* @return 树列表
*/
List<Tree<Long>> buildTree(List<RoleVO> list);
/**
* 根据角色 ID 列表查询
*
* @param roleIds
* 角色 ID 列表
* @return 角色名称列表
*/
List<String> listRoleNamesByRoleIds(List<Long> roleIds);
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.system.service;
import java.util.List;
/**
* 用户和角色业务接口
*
* @author Charles7c
* @since 2023/2/20 21:30
*/
public interface UserRoleService {
/**
* 保存
*
* @param roleIds
* 角色 ID 列表
* @param userId
* 用户 ID
*/
void save(List<Long> roleIds, Long userId);
/**
* 根据角色 ID 列表查询
*
* @param roleIds
* 角色 ID 列表
* @return 总记录数
*/
Long countByRoleIds(List<Long> roleIds);
/**
* 根据用户 ID 查询
*
* @param userId
* 用户 ID
* @return 角色 ID 列表
*/
List<Long> listRoleIdsByUserId(Long userId);
}

View File

@@ -20,7 +20,12 @@ import java.util.List;
import org.springframework.web.multipart.MultipartFile;
import top.charles7c.cnadmin.common.base.BaseService;
import top.charles7c.cnadmin.system.model.entity.UserDO;
import top.charles7c.cnadmin.system.model.query.UserQuery;
import top.charles7c.cnadmin.system.model.request.UserRequest;
import top.charles7c.cnadmin.system.model.vo.UserDetailVO;
import top.charles7c.cnadmin.system.model.vo.UserVO;
/**
* 用户业务接口
@@ -28,7 +33,7 @@ import top.charles7c.cnadmin.system.model.entity.UserDO;
* @author Charles7c
* @since 2022/12/21 21:48
*/
public interface UserService {
public interface UserService extends BaseService<UserVO, UserDetailVO, UserQuery, UserRequest> {
/**
* 根据用户名查询
@@ -50,14 +55,6 @@ public interface UserService {
*/
String uploadAvatar(MultipartFile avatar, Long userId);
/**
* 修改信息
*
* @param user
* 用户信息
*/
void update(UserDO user);
/**
* 修改密码
*
@@ -82,15 +79,6 @@ public interface UserService {
*/
void updateEmail(String newEmail, String currentPassword, Long userId);
/**
* 根据 ID 查询
*
* @param userId
* 用户 ID
* @return 用户信息
*/
UserDO getById(Long userId);
/**
* 根据部门 ID 列表查询
*

View File

@@ -21,6 +21,8 @@ import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@@ -54,7 +56,8 @@ import top.charles7c.cnadmin.system.service.UserService;
public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO, DeptVO, DeptDetailVO, DeptQuery, DeptRequest>
implements DeptService {
private final UserService userService;
@Resource
private UserService userService;
@Override
@Transactional(rollbackFor = Exception.class)

View File

@@ -16,17 +16,24 @@
package top.charles7c.cnadmin.system.service.impl;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import top.charles7c.cnadmin.common.base.BaseServiceImpl;
import top.charles7c.cnadmin.common.consts.Constants;
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
import top.charles7c.cnadmin.common.util.TreeUtils;
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
import top.charles7c.cnadmin.system.mapper.RoleMapper;
import top.charles7c.cnadmin.system.model.entity.RoleDO;
@@ -51,7 +58,8 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleVO,
private final RoleMenuService roleMenuService;
private final RoleDeptService roleDeptService;
private final MenuService menuService;
private final UserService userService;
@Resource
private UserService userService;
@Override
@Transactional(rollbackFor = Exception.class)
@@ -122,4 +130,22 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleVO,
detailVO.setDeptIds(roleDeptService.listDeptIdByRoleId(roleId));
}
}
@Override
public List<Tree<Long>> buildTree(List<RoleVO> list) {
return TreeUtils.build(list, (r, tree) -> {
tree.setId(r.getRoleId());
tree.setName(r.getRoleName());
tree.setWeight(r.getRoleSort());
});
}
@Override
public List<String> listRoleNamesByRoleIds(List<Long> roleIds) {
List<RoleDO> roleList = super.lambdaQuery().select(RoleDO::getRoleName).in(RoleDO::getRoleId, roleIds).list();
if (CollUtil.isEmpty(roleList)) {
return Collections.emptyList();
}
return roleList.stream().map(RoleDO::getRoleName).collect(Collectors.toList());
}
}

View File

@@ -0,0 +1,74 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.system.service.impl;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.hutool.core.collection.CollUtil;
import top.charles7c.cnadmin.system.mapper.UserRoleMapper;
import top.charles7c.cnadmin.system.model.entity.UserRoleDO;
import top.charles7c.cnadmin.system.service.UserRoleService;
/**
* 用户和角色业务实现类
*
* @author Charles7c
* @since 2023/2/20 21:30
*/
@Service
@RequiredArgsConstructor
public class UserRoleServiceImpl implements UserRoleService {
private final UserRoleMapper userRoleMapper;
@Override
public void save(List<Long> roleIds, Long userId) {
if (CollUtil.isEmpty(roleIds)) {
return;
}
// 删除原有关联
userRoleMapper.delete(Wrappers.<UserRoleDO>lambdaQuery().eq(UserRoleDO::getUserId, userId));
// 保存最新关联
List<UserRoleDO> userRoleList =
roleIds.stream().map(roleId -> new UserRoleDO(userId, roleId)).collect(Collectors.toList());
userRoleMapper.insertBatch(userRoleList);
}
@Override
public Long countByRoleIds(List<Long> roleIds) {
return userRoleMapper.selectCount(Wrappers.<UserRoleDO>lambdaQuery().in(UserRoleDO::getRoleId, roleIds));
}
@Override
public List<Long> listRoleIdsByUserId(Long userId) {
List<UserRoleDO> userRoleList = userRoleMapper.selectList(
Wrappers.<UserRoleDO>lambdaQuery().select(UserRoleDO::getRoleId).eq(UserRoleDO::getUserId, userId));
if (CollUtil.isEmpty(userRoleList)) {
return Collections.emptyList();
}
return userRoleList.stream().map(UserRoleDO::getRoleId).collect(Collectors.toList());
}
}

View File

@@ -20,32 +20,39 @@ import java.io.File;
import java.time.LocalDateTime;
import java.util.List;
import javax.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.StrUtil;
import top.charles7c.cnadmin.common.base.BaseServiceImpl;
import top.charles7c.cnadmin.common.config.properties.LocalStorageProperties;
import top.charles7c.cnadmin.common.consts.Constants;
import top.charles7c.cnadmin.common.consts.FileConstants;
import top.charles7c.cnadmin.common.enums.DisEnableStatusEnum;
import top.charles7c.cnadmin.common.model.dto.LoginUser;
import top.charles7c.cnadmin.common.service.CommonUserService;
import top.charles7c.cnadmin.common.util.ExceptionUtils;
import top.charles7c.cnadmin.common.util.FileUtils;
import top.charles7c.cnadmin.common.util.SecureUtils;
import top.charles7c.cnadmin.common.util.helper.LoginHelper;
import top.charles7c.cnadmin.common.util.validate.CheckUtils;
import top.charles7c.cnadmin.system.mapper.UserMapper;
import top.charles7c.cnadmin.system.mapper.UserRoleMapper;
import top.charles7c.cnadmin.system.model.entity.UserDO;
import top.charles7c.cnadmin.system.model.entity.UserRoleDO;
import top.charles7c.cnadmin.system.model.query.UserQuery;
import top.charles7c.cnadmin.system.model.request.UserRequest;
import top.charles7c.cnadmin.system.model.vo.UserDetailVO;
import top.charles7c.cnadmin.system.model.vo.UserVO;
import top.charles7c.cnadmin.system.service.DeptService;
import top.charles7c.cnadmin.system.service.RoleService;
import top.charles7c.cnadmin.system.service.UserRoleService;
import top.charles7c.cnadmin.system.service.UserService;
/**
@@ -56,15 +63,76 @@ import top.charles7c.cnadmin.system.service.UserService;
*/
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService, CommonUserService {
public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserVO, UserDetailVO, UserQuery, UserRequest>
implements UserService, CommonUserService {
private final UserMapper userMapper;
private final UserRoleMapper userRoleMapper;
private final UserRoleService userRoleService;
private final LocalStorageProperties localStorageProperties;
@Resource
private RoleService roleService;
@Resource
private DeptService deptService;
@Override
@Transactional(rollbackFor = Exception.class)
public Long add(UserRequest request) {
String username = request.getUsername();
boolean isExist = this.checkNameExists(username, request.getUserId());
CheckUtils.throwIf(() -> isExist, String.format("新增失败,'%s'已存在", username));
// 新增用户
request.setStatus(DisEnableStatusEnum.ENABLE);
Long userId = super.add(request);
super.lambdaUpdate()
.set(UserDO::getPassword, SecureUtils.md5Salt(Constants.DEFAULT_PASSWORD, userId.toString()))
.set(UserDO::getPwdResetTime, LocalDateTime.now()).eq(UserDO::getUserId, userId).update();
// 保存用户和角色关联
userRoleService.save(request.getRoleIds(), userId);
return userId;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(UserRequest request) {
String username = request.getUsername();
boolean isExist = this.checkNameExists(username, request.getUserId());
CheckUtils.throwIf(() -> isExist, String.format("修改失败,'%s'已存在", username));
// 更新用户
super.update(request);
Long userId = request.getUserId();
// 保存用户和角色关联
userRoleService.save(request.getRoleIds(), userId);
}
/**
* 检查名称是否存在
*
* @param name
* 名称
* @param id
* ID
* @return 是否存在
*/
private boolean checkNameExists(String name, Long id) {
return super.lambdaQuery().eq(UserDO::getUsername, name).ne(id != null, UserDO::getUserId, id).exists();
}
@Override
public void fillDetail(Object detailObj) {
super.fillDetail(detailObj);
if (detailObj instanceof UserDetailVO) {
UserDetailVO detailVO = (UserDetailVO)detailObj;
detailVO.setDeptName(ExceptionUtils.exToNull(() -> deptService.get(detailVO.getDeptId()).getDeptName()));
List<Long> roleIds = userRoleService.listRoleIdsByUserId(detailVO.getUserId());
detailVO.setRoleIds(roleIds);
detailVO.setRoleNames(String.join(",", roleService.listRoleNamesByRoleIds(roleIds)));
}
}
@Override
public UserDO getByUsername(String username) {
return userMapper.selectOne(Wrappers.<UserDO>lambdaQuery().eq(UserDO::getUsername, username));
return super.lambdaQuery().eq(UserDO::getUsername, username).one();
}
@Override
@@ -86,8 +154,7 @@ public class UserServiceImpl implements UserService, CommonUserService {
// 更新用户头像
String newAvatar = newAvatarFile.getName();
userMapper.update(null,
new LambdaUpdateWrapper<UserDO>().set(UserDO::getAvatar, newAvatar).eq(UserDO::getUserId, userId));
super.lambdaUpdate().set(UserDO::getAvatar, newAvatar).eq(UserDO::getUserId, userId).update();
// 删除原头像
LoginUser loginUser = LoginHelper.getLoginUser();
@@ -102,18 +169,6 @@ public class UserServiceImpl implements UserService, CommonUserService {
return newAvatar;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(UserDO user) {
userMapper.updateById(user);
// 更新登录用户信息
UserDO userDO = this.getById(user.getUserId());
LoginUser loginUser = LoginHelper.getLoginUser();
BeanUtil.copyProperties(userDO, loginUser);
LoginHelper.updateLoginUser(loginUser);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updatePassword(String oldPassword, String newPassword, Long userId) {
@@ -123,10 +178,8 @@ public class UserServiceImpl implements UserService, CommonUserService {
// 更新密码和密码重置时间
LocalDateTime now = LocalDateTime.now();
userMapper.update(null,
new LambdaUpdateWrapper<UserDO>()
.set(UserDO::getPassword, SecureUtils.md5Salt(newPassword, userId.toString()))
.set(UserDO::getPwdResetTime, now).eq(UserDO::getUserId, userId));
super.lambdaUpdate().set(UserDO::getPassword, SecureUtils.md5Salt(newPassword, userId.toString()))
.set(UserDO::getPwdResetTime, now).eq(UserDO::getUserId, userId).update();
// 更新登录用户信息
LoginUser loginUser = LoginHelper.getLoginUser();
@@ -140,13 +193,12 @@ public class UserServiceImpl implements UserService, CommonUserService {
UserDO userDO = this.getById(userId);
CheckUtils.throwIfNotEqual(SecureUtils.md5Salt(currentPassword, userId.toString()), userDO.getPassword(),
"当前密码错误");
Long count = userMapper.selectCount(Wrappers.<UserDO>lambdaQuery().eq(UserDO::getEmail, newEmail));
Long count = super.lambdaQuery().eq(UserDO::getEmail, newEmail).count();
CheckUtils.throwIf(() -> count > 0, "邮箱已绑定其他账号,请更换其他邮箱");
CheckUtils.throwIfEqual(newEmail, userDO.getEmail(), "新邮箱不能与当前邮箱相同");
// 更新邮箱
userMapper.update(null,
new LambdaUpdateWrapper<UserDO>().set(UserDO::getEmail, newEmail).eq(UserDO::getUserId, userId));
super.lambdaUpdate().set(UserDO::getEmail, newEmail).eq(UserDO::getUserId, userId).update();
// 更新登录用户信息
LoginUser loginUser = LoginHelper.getLoginUser();
@@ -154,25 +206,18 @@ public class UserServiceImpl implements UserService, CommonUserService {
LoginHelper.updateLoginUser(loginUser);
}
@Override
public UserDO getById(Long userId) {
UserDO userDO = userMapper.selectById(userId);
CheckUtils.throwIfNull(userDO, String.format("ID为 [%s] 的用户已不存在", userId));
return userDO;
}
@Override
public Long countByDeptIds(List<Long> deptIds) {
return userMapper.selectCount(Wrappers.<UserDO>lambdaQuery().in(UserDO::getDeptId, deptIds));
return super.lambdaQuery().in(UserDO::getDeptId, deptIds).count();
}
@Override
public Long countByRoleIds(List<Long> roleIds) {
return userRoleMapper.selectCount(Wrappers.<UserRoleDO>lambdaQuery().in(UserRoleDO::getRoleId, roleIds));
return userRoleService.countByRoleIds(roleIds);
}
@Override
public String getNicknameById(Long userId) {
return this.getById(userId).getNickname();
return super.getById(userId).getNickname();
}
}