| | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.sandu.common.enums.RoleLevelStatus; |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.object.BaseConditionVO; |
| | | import com.sandu.common.service.impl.BaseServiceImpl; |
| | | import com.sandu.ximon.admin.param.AdminParam; |
| | | import com.sandu.ximon.admin.param.PwdParam; |
| | | import com.sandu.ximon.admin.security.SecurityUtils; |
| | | import com.sandu.ximon.dao.bo.AdminBo; |
| | | import com.sandu.ximon.dao.domain.Admin; |
| | | import com.sandu.ximon.dao.domain.AdminRoleRelation; |
| | | import com.sandu.ximon.dao.domain.Client; |
| | | import com.sandu.ximon.dao.domain.Role; |
| | | import com.sandu.ximon.dao.mapper.AdminMapper; |
| | | import lombok.AllArgsConstructor; |
| | |
| | | private final PasswordEncoder passwordEncoder; |
| | | private final AdminRoleRelationService adminRoleRelationService; |
| | | private final RoleService roleService; |
| | | private AdminMapper adminMapper; |
| | | |
| | | public Admin findByUserName(String username) { |
| | | return getOne(Wrappers.lambdaQuery(Admin.class).eq(Admin::getUsername, username).last("limit 1")); |
| | |
| | | return true; |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean updateAdmin(Long adminId, AdminParam param) { |
| | | Admin admin = getById(adminId); |
| | | if(admin == null){ |
| | | if (admin == null) { |
| | | throw new BusinessException("找不到管理员"); |
| | | } |
| | | List<Role> roles = roleService.listByAdminId(admin.getId()); |
| | | if(CollectionUtil.isEmpty(roles)){ |
| | | if (CollectionUtil.isEmpty(roles)) { |
| | | throw new BusinessException("当前用户没有角色"); |
| | | } |
| | | List<Integer> levels = roles.stream().map(Role::getLevel).collect(Collectors.toList()); |
| | | int min = Collections.min(levels); |
| | | roleService.assertLevels(min); |
| | | |
| | | Admin update = new Admin(); |
| | | update.setId(adminId); |
| | | update.setPassword(passwordEncoder.encode(param.getPassword())); |
| | | update.setNickName(param.getNickName()); |
| | | update.setMobile(param.getMobile()); |
| | | update.setNote(param.getNote()); |
| | | return updateById(update); |
| | | if (!updateById(update)) { |
| | | throw new BusinessException("编辑管理员失败"); |
| | | } |
| | | |
| | | adminRoleRelationService.remove(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getAdminId, admin.getId())); |
| | | AdminRoleRelation adminRoleRelation = new AdminRoleRelation(); |
| | | adminRoleRelation.setAdminId(adminId); |
| | | adminRoleRelation.setRoleId(param.getRoleId()); |
| | | if (!adminRoleRelationService.save(adminRoleRelation)) { |
| | | throw new BusinessException("添加管理员角色失败"); |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 修改密码 |
| | | * |
| | | * @param param |
| | | * @return |
| | | */ |
| | | public boolean updateMyPassword(PwdParam param) { |
| | | if(!StrUtil.equals(param.getNewPass(),param.getConfirmPass())){ |
| | | throw new BusinessException("两次密码不一致"); |
| | | } |
| | | Long userId = SecurityUtils.getUserId(); |
| | | Admin admin = getById(userId); |
| | | if (admin == null) { |
| | | throw new BusinessException("用户不存在"); |
| | | } |
| | | if(!passwordEncoder.matches(param.getOldPass(),admin.getPassword())){ |
| | | throw new BusinessException("旧密码不正确"); |
| | | //判断旧密码与数据库是否一致 |
| | | if (passwordEncoder.matches(param.getOldPass(), admin.getPassword())) { |
| | | //加密新密码 |
| | | admin.setId(userId); |
| | | admin.setPassword(passwordEncoder.encode(param.getNewPass())); |
| | | return updateById(admin); |
| | | } else { |
| | | throw new BusinessException("旧密码不正确,请重新确认密码!"); |
| | | } |
| | | Admin update = new Admin(); |
| | | update.setId(userId); |
| | | update.setPassword(passwordEncoder.encode(param.getNewPass())); |
| | | return updateById(update); |
| | | } |
| | | |
| | | public boolean deleteAdmin(Long adminId) { |
| | | Admin admin = getById(adminId); |
| | | if (admin == null) { |
| | | throw new BusinessException("找不到管理员"); |
| | | } |
| | | List<Role> roles = roleService.listByAdminId(admin.getId()); |
| | | if (CollectionUtil.isEmpty(roles)) { |
| | | throw new BusinessException("当前用户没有角色"); |
| | | } |
| | | List<Integer> levels = roles.stream().map(Role::getLevel).collect(Collectors.toList()); |
| | | int min = Collections.min(levels); |
| | | int maxLevel = roleService.assertLevels(min); |
| | | if (!RoleLevelStatus.SUPER.getCode().equals(maxLevel)) { |
| | | throw new BusinessException("只有超级管理员才能删除用户"); |
| | | } |
| | | |
| | | return removeById(adminId); |
| | | } |
| | | |
| | | /** |
| | | * 修改超级管理员、管理员用户头像 |
| | | * |
| | | * @param userId |
| | | * @param IconUrl |
| | | * @return |
| | | */ |
| | | public boolean updateIcon(Long userId, String IconUrl) { |
| | | Admin one = getOne(Wrappers.lambdaQuery(Admin.class).eq(Admin::getId, userId)); |
| | | if (one == null) { |
| | | throw new BusinessException("用户不存在"); |
| | | } |
| | | one.setIcon(IconUrl); |
| | | return updateById(one); |
| | | } |
| | | |
| | | public List<AdminBo> listAdmin(BaseConditionVO baseConditionVO, String keyword) { |
| | | if (baseConditionVO != null) { |
| | | PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize()); |
| | | } |
| | | |
| | | return adminMapper.listAdmin(keyword); |
| | | } |
| | | } |