From 49dbcdadd860b7613063ee618fbe38a0593d5bd9 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Mon, 11 Jul 2022 15:45:29 +0800
Subject: [PATCH] fix
---
src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/UserServiceImpl.java | 343 +++++++++++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 307 insertions(+), 36 deletions(-)
diff --git a/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/UserServiceImpl.java b/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/UserServiceImpl.java
index 7b15c11..9926a42 100644
--- a/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/UserServiceImpl.java
+++ b/src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/UserServiceImpl.java
@@ -1,27 +1,32 @@
package com.xcong.farmer.cms.modules.system.service.Impl;
import cn.hutool.crypto.SecureUtil;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.xcong.farmer.cms.common.contants.AppContants;
import com.xcong.farmer.cms.common.response.Result;
-import com.xcong.farmer.cms.modules.system.dto.AdminAddUserDto;
-import com.xcong.farmer.cms.modules.system.dto.AdminUpdateUserDto;
-import com.xcong.farmer.cms.modules.system.dto.AdminUserDto;
-import com.xcong.farmer.cms.modules.system.entity.RoleEntity;
-import com.xcong.farmer.cms.modules.system.entity.UserEntity;
-import com.xcong.farmer.cms.modules.system.mapper.RoleMapper;
-import com.xcong.farmer.cms.modules.system.mapper.UserMapper;
+import com.xcong.farmer.cms.modules.system.dto.*;
+import com.xcong.farmer.cms.modules.system.entity.*;
+import com.xcong.farmer.cms.modules.system.mapper.*;
import com.xcong.farmer.cms.modules.system.service.IUserService;
-import com.xcong.farmer.cms.modules.system.vo.AdminSeeUserInfoVo;
-import com.xcong.farmer.cms.modules.system.vo.AdminUserVo;
+import com.xcong.farmer.cms.modules.system.util.LoginUserUtil;
+import com.xcong.farmer.cms.modules.system.vo.*;
+import com.xcong.farmer.cms.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
+import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ObjectUtil;
+
+import cn.hutool.core.collection.CollUtil;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
+import java.io.Serializable;
+import java.util.*;
@Service
@Slf4j
@@ -29,53 +34,92 @@
@Resource
private RoleMapper roleMapper;
+ @Resource
+ private UserRoleMapper userRoleMapper;
+ @Resource
+ private RoleMenuMapper roleMenuMapper;
+ @Resource
+ private ColumnMapper columnMapper;
+ @Resource
+ private ArticleMapper articleMapper;
+ @Resource
+ private MessageBoardMapper messageBoardMapper;
+ @Resource
+ private MenuMapper menuMapper;
+ @Resource
+ private RedisUtils redisUtils;
@Override
public Result getUserInPage(AdminUserDto adminUserDto) {
+ UserEntity userlogin = LoginUserUtil.getLoginUser();
Page<AdminUserVo> page = new Page<>(adminUserDto.getPageNum(), adminUserDto.getPageSize());
UserEntity userEntity = new UserEntity();
+ long companyId = userlogin.getCompanyId() == null ? UserEntity.USER_BELONG_TOP : userlogin.getCompanyId();
+ if(companyId > UserEntity.USER_BELONG_TOP){
+ userEntity.setCompanyId(companyId);
+ userEntity.setRoleId(RoleEntity.ROLE_ADMIN_NAME_ID);
+ }else if(companyId == UserEntity.USER_BELONG_TOP){
+ userEntity.setRoleId(RoleEntity.ROLE_SUPER_NAME_ID);
+ }
if(StrUtil.isNotEmpty(adminUserDto.getUsername())){
userEntity.setUsername(adminUserDto.getUsername());
}
- IPage<AdminUserVo> list = this.baseMapper.selectAdminUserVoInPage(page,userEntity);
- return Result.ok(list);
+ IPage<AdminUserVo> AdminUserVos = this.baseMapper.selectAdminUserVoInPage(page,userEntity);
+ List<AdminUserVo> records = AdminUserVos.getRecords();
+ if(CollUtil.isNotEmpty(records)){
+ for(AdminUserVo adminUserVo : records){
+ List<AdminRoleVo> adminRoleVos = new ArrayList<>();
+ QueryWrapper<UserRoleEntity> objectQueryWrapper = new QueryWrapper<>();
+ objectQueryWrapper.eq("user_id",adminUserVo.getId());
+ List<UserRoleEntity> userRoleEntities = userRoleMapper.selectList(objectQueryWrapper);
+ if(CollUtil.isNotEmpty(userRoleEntities)){
+ for(UserRoleEntity userRoleEntity : userRoleEntities){
+ RoleEntity roleEntity = roleMapper.selectById(userRoleEntity.getRoleId());
+ AdminRoleVo adminRoleVo = new AdminRoleVo();
+ adminRoleVo.setId(roleEntity.getId());
+ adminRoleVo.setRoleName(roleEntity.getRoleName());
+ adminRoleVos.add(adminRoleVo);
+ }
+ }
+ adminUserVo.setAdminRoleVos(adminRoleVos);
+ }
+ }
+
+ return Result.ok(AdminUserVos);
}
@Override
@Transactional
public Result addUser(AdminAddUserDto adminAddUserDto) {
+ UserEntity userlogin = LoginUserUtil.getLoginUser();
String username = adminAddUserDto.getUsername();
- if(StrUtil.isEmpty(username)){
- return Result.fail("请输入用户名");
+ QueryWrapper<UserEntity> objectQueryWrapper = new QueryWrapper<>();
+ objectQueryWrapper.eq("username",username);
+ List<UserEntity> userEntities = this.baseMapper.selectList(objectQueryWrapper);
+ if(CollUtil.isNotEmpty(userEntities)){
+ return Result.fail("用户名重复");
}
- Long roleId = adminAddUserDto.getRoleId();
- if(ObjectUtil.isEmpty(roleId)){
- return Result.fail("请选择用户角色");
- }
- RoleEntity roleEntity = roleMapper.selectById(roleId);
- if(ObjectUtil.isEmpty(roleEntity)){
- return Result.fail("请选择用户角色");
- }
+ String roleIds = adminAddUserDto.getRoleIds();
String phone = adminAddUserDto.getPhone();
- if(StrUtil.isEmpty(phone)){
- return Result.fail("请输入联系电话");
+ Long belongId = adminAddUserDto.getBelongId() == null ? UserEntity.USER_BELONG_TOP:adminAddUserDto.getBelongId();
+ if(belongId == UserEntity.USER_BELONG_TOP){
+ return Result.fail("请选择所属公司");
}
UserEntity userEntity = new UserEntity();
userEntity.setUsername(username);
userEntity.setPhone(phone);
- userEntity.setRoleId(roleId);
- userEntity.setRoleName(roleEntity.getRoleName());
if(StrUtil.isNotEmpty(adminAddUserDto.getNickname())){
userEntity.setNickname(adminAddUserDto.getNickname());
}
if(StrUtil.isNotEmpty(adminAddUserDto.getEmail())){
userEntity.setEmail(adminAddUserDto.getEmail());
}
-
userEntity.setPassword(SecureUtil.md5(UserEntity.PASSWORD_DEFAULT));
userEntity.setStatus(UserEntity.STATUS_ENABLE);
+ userEntity.setCompanyId(belongId);
int insert = this.baseMapper.insert(userEntity);
if(insert > 0){
+ saveUserRole(roleIds,userEntity.getId());
return Result.ok("添加成功");
}
return Result.fail("添加失败");
@@ -113,6 +157,10 @@
return Result.fail("用户不存在");
}
this.baseMapper.deleteById(id);
+
+ QueryWrapper<UserRoleEntity> objectQueryWrapper = new QueryWrapper<>();
+ objectQueryWrapper.eq("user_id",userEntity.getId());
+ userRoleMapper.delete(objectQueryWrapper);
return Result.ok("删除成功");
}
@@ -128,8 +176,22 @@
adminSeeUserInfoVo.setNickname(userEntity.getNickname());
adminSeeUserInfoVo.setPhone(userEntity.getPhone());
adminSeeUserInfoVo.setEmail(userEntity.getEmail());
- adminSeeUserInfoVo.setRoleId(userEntity.getRoleId());
- adminSeeUserInfoVo.setRoleName(userEntity.getRoleName());
+ adminSeeUserInfoVo.setBelongId(userEntity.getCompanyId());
+
+ List<AdminRoleVo> adminRoleVos = new ArrayList<>();
+ QueryWrapper<UserRoleEntity> objectQueryWrapper = new QueryWrapper<>();
+ objectQueryWrapper.eq("user_id",userEntity.getId());
+ List<UserRoleEntity> userRoleEntities = userRoleMapper.selectList(objectQueryWrapper);
+ if(CollUtil.isNotEmpty(userRoleEntities)){
+ for(UserRoleEntity userRoleEntity : userRoleEntities){
+ RoleEntity roleEntity = roleMapper.selectById(userRoleEntity.getRoleId());
+ AdminRoleVo adminRoleVo = new AdminRoleVo();
+ adminRoleVo.setId(roleEntity.getId());
+ adminRoleVo.setRoleName(roleEntity.getRoleName());
+ adminRoleVos.add(adminRoleVo);
+ }
+ }
+ adminSeeUserInfoVo.setAdminRoleVos(adminRoleVos);
return Result.ok(adminSeeUserInfoVo);
}
@@ -140,26 +202,25 @@
if(StrUtil.isEmpty(username)){
return Result.fail("请输入用户名");
}
- Long roleId = adminUpdateUserDto.getRoleId();
- if(ObjectUtil.isEmpty(roleId)){
- return Result.fail("请选择用户角色");
- }
- RoleEntity roleEntity = roleMapper.selectById(roleId);
- if(ObjectUtil.isEmpty(roleEntity)){
+ String roleIds = adminUpdateUserDto.getRoleIds();
+ if(StrUtil.isEmpty(roleIds)){
return Result.fail("请选择用户角色");
}
String phone = adminUpdateUserDto.getPhone();
if(StrUtil.isEmpty(phone)){
return Result.fail("请输入联系电话");
}
+ Long belongId = adminUpdateUserDto.getBelongId() == null ? UserEntity.USER_BELONG_TOP : adminUpdateUserDto.getBelongId();
+ if(UserEntity.USER_BELONG_TOP == belongId){
+ return Result.fail("请输入所属公司");
+ }
UserEntity userEntity = this.baseMapper.selectById(adminUpdateUserDto.getId());
if(ObjectUtil.isEmpty(userEntity)){
return Result.fail("用户不存在");
}
+ userEntity.setCompanyId(belongId);
userEntity.setUsername(username);
userEntity.setPhone(phone);
- userEntity.setRoleId(roleId);
- userEntity.setRoleName(roleEntity.getRoleName());
if(StrUtil.isNotEmpty(adminUpdateUserDto.getNickname())){
userEntity.setNickname(adminUpdateUserDto.getNickname());
}
@@ -168,6 +229,11 @@
}
int insert = this.baseMapper.updateById(userEntity);
if(insert > 0){
+ QueryWrapper<UserRoleEntity> objectQueryWrapper = new QueryWrapper<>();
+ objectQueryWrapper.eq("user_id",userEntity.getId());
+ userRoleMapper.delete(objectQueryWrapper);
+
+ saveUserRole(roleIds,userEntity.getId());
return Result.ok("更新成功");
}
return Result.fail("更新失败");
@@ -184,4 +250,209 @@
this.baseMapper.updateById(userEntity);
return Result.ok("重置成功");
}
+
+ @Override
+ public Result userMenu() {
+ UserEntity loginUser = LoginUserUtil.getLoginUser();
+ Long userId = loginUser.getId();
+ List<AdminUserMenuVo> adminUserMenuVos = new ArrayList<>();
+
+ QueryWrapper<UserRoleEntity> userRoleQueryWrapper = new QueryWrapper<>();
+ userRoleQueryWrapper.eq("user_id",userId);
+ List<UserRoleEntity> userRoleEntities = userRoleMapper.selectList(userRoleQueryWrapper);
+ if(CollUtil.isNotEmpty(userRoleEntities)){
+ List menuIds = new ArrayList<Long>();
+ if(UserEntity.USERNAME_DEFAULT.equals(loginUser.getUsername())){
+ List<MenuEntity> menuEntities = menuMapper.selectList(new QueryWrapper<>());
+ if(CollUtil.isNotEmpty(menuEntities)){
+ for(MenuEntity menuEntity : menuEntities){
+ menuIds.add(menuEntity.getId());
+ }
+ }
+ }else{
+ for(UserRoleEntity userRoleEntity : userRoleEntities){
+ List<RoleMenuEntity> roleMenuEntities = roleMenuMapper.selectListByRoleId(userRoleEntity.getRoleId());
+ if(CollUtil.isNotEmpty(roleMenuEntities)){
+ for(RoleMenuEntity roleMenuEntity : roleMenuEntities){
+ menuIds.add(roleMenuEntity.getMenuId());
+ }
+ }
+ }
+ }
+ if(CollUtil.isNotEmpty(menuIds)){
+ HashSet hashSet = CollUtil.newHashSet(menuIds);
+ List<Long> parentMenuIds = new ArrayList<>();
+ List<Long> childMenuIds = new ArrayList<>();
+ for(Object s : hashSet){
+ MenuEntity menuEntity = menuMapper.selectById(Long.parseLong(s.toString()));
+ Long parentId = menuEntity.getParentId();
+ if(parentId == 0L){
+ parentMenuIds.add(menuEntity.getId());
+ }else{
+ childMenuIds.add(menuEntity.getId());
+ }
+ }
+ Map<Long, AdminUserMenuVo> menuMap = new HashMap<>();
+ if(CollUtil.isNotEmpty(parentMenuIds)){
+ for(Long parentId : parentMenuIds){
+ AdminUserMenuVo adminUserMenuVo = menuMapper.selectAdminRoleMenuVoById(parentId);
+ menuMap.put(parentId,adminUserMenuVo);
+ }
+ }
+ if(CollUtil.isNotEmpty(childMenuIds)){
+ for(Long childId : childMenuIds){
+ AdminUserMenuVo adminUserMenuVo = menuMapper.selectAdminRoleMenuVoById(childId);
+ Long parentId = adminUserMenuVo.getParentId();
+ AdminUserMenuVo adminUserMenuVoParent = menuMap.get(parentId);
+ if(ObjectUtil.isNotEmpty(adminUserMenuVoParent)){
+ List<AdminUserMenuVo> child = adminUserMenuVoParent.getChild();
+ if(CollUtil.isEmpty(child)){
+ List<AdminUserMenuVo> list = new ArrayList<>();
+ list.add(adminUserMenuVo);
+ adminUserMenuVoParent.setChild(list);
+ }else{
+ child.add(adminUserMenuVo);
+ }
+ }
+ }
+ }
+ Set<Map.Entry<Long, AdminUserMenuVo>> entries = menuMap.entrySet();
+ Iterator<Map.Entry<Long, AdminUserMenuVo>> iterator = entries.iterator();
+ while (iterator.hasNext()){
+ adminUserMenuVos.add(iterator.next().getValue());
+ }
+
+ }
+ }
+ if(CollUtil.isNotEmpty(adminUserMenuVos)){
+ for(AdminUserMenuVo adminUserMenuVo : adminUserMenuVos){
+ List<AdminUserMenuVo> child = adminUserMenuVo.getChild();
+ if(CollUtil.isNotEmpty(child)){
+ List<Long> menuIdChilds = new ArrayList<>();
+ for(AdminUserMenuVo adminUserMenuVoChild : child){
+ Long id = adminUserMenuVoChild.getId();
+ menuIdChilds.add(id);
+ }
+ List<AdminUserMenuVo> adminUserMenuVoByOrderNum = menuMapper.selectAdminUserMenuVoOrderByOrderNum(menuIdChilds);
+ adminUserMenuVo.setChild(adminUserMenuVoByOrderNum);
+ }
+ }
+ }
+ return Result.ok(adminUserMenuVos);
+ }
+
+ @Override
+ @Transactional
+ public Result delObjs(AdminDeleteDto adminDeleteDto) {
+ String ids = adminDeleteDto.getIds();
+ if(StrUtil.isNotEmpty(ids)){
+ String[] userIds = ids.split(StringPool.COMMA);
+ for(String userIdStr : userIds){
+ Long userId = Long.valueOf(userIdStr);
+
+ QueryWrapper<UserRoleEntity> objectQueryWrapper = new QueryWrapper<>();
+ objectQueryWrapper.eq("user_id",userId);
+ userRoleMapper.delete(objectQueryWrapper);
+
+ this.baseMapper.deleteById(userId);
+
+ String redisMember = AppContants.APP_LOGIN_PREFIX + userId;
+ String token = redisUtils.getString(redisMember);
+ redisUtils.del(AppContants.APP_LOGIN_PREFIX + token);
+ SecurityContextHolder.clearContext();
+ }
+
+ }
+ return Result.ok("删除成功");
+ }
+
+ @Override
+ @Transactional
+ public Result updatePassword(AdminUpdatePasswordDto adminUpdatePasswordDto) {
+ UserEntity loginUser = LoginUserUtil.getLoginUser();
+ Long userId = loginUser.getId();
+ UserEntity userEntity = this.baseMapper.selectById(userId);
+ if(ObjectUtil.isEmpty(userEntity)){
+ return Result.fail("用户不存在");
+ }
+ String oldPassword = adminUpdatePasswordDto.getOldPassword();
+ if(StrUtil.isEmpty(oldPassword)){
+ return Result.fail("请输入原密码");
+ }
+ String newPassword = adminUpdatePasswordDto.getNewPassword();
+ if(StrUtil.isEmpty(newPassword)){
+ return Result.fail("请输入新密码");
+ }
+ String s = SecureUtil.md5(oldPassword);
+ if(!userEntity.getPassword().equals(s)){
+ return Result.fail("原密码不正确,请联系管理员重置密码");
+ }
+ userEntity.setPassword(SecureUtil.md5(newPassword));
+ this.baseMapper.updateById(userEntity);
+ return Result.ok("修改成功");
+ }
+
+ @Override
+ public Result userInfo() {
+ UserEntity loginUser = LoginUserUtil.getLoginUser();
+ Long id = loginUser.getId();
+ UserEntity userEntity = this.baseMapper.selectById(id);
+ if(ObjectUtil.isEmpty(userEntity)){
+ return Result.fail("用户不存在");
+ }
+ AdminUserInfoVo adminUserInfoVo = new AdminUserInfoVo();
+ adminUserInfoVo.setId(userEntity.getId());
+ adminUserInfoVo.setUsername(userEntity.getUsername());
+ adminUserInfoVo.setNickname(userEntity.getNickname());
+ adminUserInfoVo.setPhone(userEntity.getPhone());
+ adminUserInfoVo.setEmail(userEntity.getEmail());
+ return Result.ok(adminUserInfoVo);
+ }
+
+ @Override
+ public Result viewHomePage() {
+ UserEntity loginUser = LoginUserUtil.getLoginUser();
+ Long companyId = LoginUserUtil.getCompanyId();
+
+ AdminHomePageInfoVo adminHomePageInfoVo = new AdminHomePageInfoVo();
+
+ QueryWrapper<UserRoleEntity> userRoleQueryWrapper = new QueryWrapper<>();
+ userRoleQueryWrapper.eq("user_id",loginUser.getId());
+ List<UserRoleEntity> userRoleEntities = userRoleMapper.selectList(userRoleQueryWrapper);
+ if(CollUtil.isNotEmpty(userRoleEntities)){
+ List<String> roleNames = new ArrayList<>();
+ for(UserRoleEntity userRoleEntity : userRoleEntities){
+ RoleEntity roleEntity = roleMapper.selectById(userRoleEntity.getRoleId());
+ roleNames.add(roleEntity.getRoleName());
+ }
+ adminHomePageInfoVo.setRoleNames(roleNames);
+ }
+
+ QueryWrapper<ColumnEntity> columnQueryWrapper = new QueryWrapper<>();
+ columnQueryWrapper.eq("company_id",companyId);
+ adminHomePageInfoVo.setColumnNum(columnMapper.selectCount(columnQueryWrapper));
+
+ QueryWrapper<ArticleEntity> articleQueryWrapper = new QueryWrapper<>();
+ articleQueryWrapper.eq("company_id",companyId);
+ articleQueryWrapper.eq("del_status",ArticleEntity.DELETE_STATUS_NO);
+ adminHomePageInfoVo.setArticleNum(articleMapper.selectCount(articleQueryWrapper));
+
+ QueryWrapper<MessageBoardEntity> messageQueryWrapper = new QueryWrapper<>();
+ messageQueryWrapper.eq("company_id",companyId);
+ adminHomePageInfoVo.setMessageNum(messageBoardMapper.selectCount(messageQueryWrapper));
+ return Result.ok(adminHomePageInfoVo);
+ }
+
+ private void saveUserRole(String roleIds,Long userId) {
+ if (StrUtil.isNotEmpty(roleIds)) {
+ String[] roleIdList = roleIds.split(StringPool.COMMA);
+ Arrays.stream(roleIdList).forEach(roleId -> {
+ UserRoleEntity userRoleEntity = new UserRoleEntity();
+ userRoleEntity.setRoleId(Long.valueOf(roleId));
+ userRoleEntity.setUserId(userId);
+ userRoleMapper.insert(userRoleEntity);
+ });
+ }
+ }
+
}
--
Gitblit v1.9.1