2021与蓝度共同重构项目,服务端
fix
zhanzhiqin
2022-04-14 4658117e1102431e25194ceecb64a184b3b33575
ximon-admin/src/main/java/com/sandu/ximon/admin/service/AdminService.java
@@ -11,6 +11,7 @@
import com.sandu.ximon.admin.security.SecurityUtils;
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;
@@ -71,29 +72,43 @@
        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;
    }
    public boolean updateMyPassword(PwdParam param) {
        if(!StrUtil.equals(param.getNewPass(),param.getConfirmPass())){
        if (!StrUtil.equals(param.getNewPass(), param.getConfirmPass())) {
            throw new BusinessException("两次密码不一致");
        }
        Long userId = SecurityUtils.getUserId();
@@ -101,7 +116,7 @@
        if (admin == null) {
            throw new BusinessException("用户不存在");
        }
        if(!passwordEncoder.matches(param.getOldPass(),admin.getPassword())){
        if (!passwordEncoder.matches(param.getOldPass(), admin.getPassword())) {
            throw new BusinessException("旧密码不正确");
        }
        Admin update = new Admin();
@@ -109,4 +124,39 @@
        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);
    }
}