xiaoyong931011
2022-07-08 bd4ab82f2e0c485f36a254e3cbf72f21dabfd628
src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/UserServiceImpl.java
@@ -6,6 +6,7 @@
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.*;
import com.xcong.farmer.cms.modules.system.entity.*;
@@ -13,7 +14,9 @@
import com.xcong.farmer.cms.modules.system.service.IUserService;
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;
@@ -36,19 +39,27 @@
    @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 belongId = userlogin.getBelongId() == null ? UserEntity.USER_BELONG_TOP : userlogin.getBelongId();
        if(belongId > UserEntity.USER_BELONG_TOP){
            userEntity.setBelongId(belongId);
            userEntity.setRoleName(RoleEntity.ROLE_ADMIN_NAME);
        }else if(belongId == UserEntity.USER_BELONG_TOP){
            userEntity.setRoleName(RoleEntity.ROLE_SUPER_NAME);
        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());
@@ -82,24 +93,14 @@
    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);
//        objectQueryWrapper.eq("belong_id",userlogin.getBelongId());
        List<UserEntity> userEntities = this.baseMapper.selectList(objectQueryWrapper);
        if(CollUtil.isNotEmpty(userEntities)){
            return Result.fail("用户名重复");
        }
        String roleIds = adminAddUserDto.getRoleIds();
        if(StrUtil.isEmpty(roleIds)){
            return Result.fail("请选择用户角色");
        }
        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("请选择所属公司");
@@ -115,7 +116,7 @@
        }
        userEntity.setPassword(SecureUtil.md5(UserEntity.PASSWORD_DEFAULT));
        userEntity.setStatus(UserEntity.STATUS_ENABLE);
        userEntity.setBelongId(belongId);
        userEntity.setCompanyId(belongId);
        int insert = this.baseMapper.insert(userEntity);
        if(insert > 0){
            saveUserRole(roleIds,userEntity.getId());
@@ -175,7 +176,7 @@
        adminSeeUserInfoVo.setNickname(userEntity.getNickname());
        adminSeeUserInfoVo.setPhone(userEntity.getPhone());
        adminSeeUserInfoVo.setEmail(userEntity.getEmail());
        adminSeeUserInfoVo.setBelongId(userEntity.getBelongId());
        adminSeeUserInfoVo.setBelongId(userEntity.getCompanyId());
        List<AdminRoleVo> adminRoleVos = new ArrayList<>();
        QueryWrapper<UserRoleEntity> objectQueryWrapper = new QueryWrapper<>();
@@ -217,7 +218,7 @@
        if(ObjectUtil.isEmpty(userEntity)){
            return Result.fail("用户不存在");
        }
        userEntity.setBelongId(belongId);
        userEntity.setCompanyId(belongId);
        userEntity.setUsername(username);
        userEntity.setPhone(phone);
        if(StrUtil.isNotEmpty(adminUpdateUserDto.getNickname())){
@@ -261,11 +262,20 @@
        List<UserRoleEntity> userRoleEntities = userRoleMapper.selectList(userRoleQueryWrapper);
        if(CollUtil.isNotEmpty(userRoleEntities)){
            List menuIds = new ArrayList<Long>();
            for(UserRoleEntity userRoleEntity : userRoleEntities){
                List<RoleMenuEntity> roleMenuEntities = roleMenuMapper.selectListByRoleId(userRoleEntity.getRoleId());
                if(CollUtil.isNotEmpty(roleMenuEntities)){
                    for(RoleMenuEntity roleMenuEntity : roleMenuEntities){
                        menuIds.add(roleMenuEntity.getMenuId());
            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());
                        }
                    }
                }
            }
@@ -345,6 +355,11 @@
                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();
            }
        }
@@ -394,6 +409,40 @@
        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);