2021与蓝度共同重构项目,服务端
fix
zhanzhiqin
2022-04-24 724c79463fc7c4f345fc2dd508aeab139b57afa5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.sandu.ximon.admin.service;
 
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.param.UserPwsParm;
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;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * @author chenjiantian
 * @date 2021/11/24 11:02
 */
@Service
@AllArgsConstructor
public class AdminService extends BaseServiceImpl<AdminMapper, Admin> {
 
    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"));
    }
 
    @Transactional(rollbackFor = Exception.class)
    public boolean register(AdminParam param) {
        Admin admin = findByUserName(param.getUsername());
        if (admin != null) {
            throw new BusinessException("当前账号" + param.getUsername() + "已经存在");
        }
        Admin save = new Admin();
        save.setUsername(param.getUsername());
        save.setPassword(passwordEncoder.encode(param.getPassword()));
        save.setNickName(param.getNickName());
        save.setMobile(param.getMobile());
        save.setNote(param.getNote());
        if (!save(save)) {
            throw new BusinessException("注册管理员失败");
        }
 
        Role role = roleService.getById(param.getRoleId());
        if (role == null) {
            throw new BusinessException("角色不存在");
        }
        if (RoleLevelStatus.SUPER.getCode().equals(role.getLevel())) {
            throw new BusinessException("无法添加超级管理员");
        }
 
        AdminRoleRelation adminRoleRelation = new AdminRoleRelation();
        adminRoleRelation.setAdminId(save.getId());
        adminRoleRelation.setRoleId(param.getRoleId());
        if (!adminRoleRelationService.save(adminRoleRelation)) {
            throw new BusinessException("添加管理员角色失败");
        }
        return true;
    }
 
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAdmin(Long adminId, AdminParam param) {
        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);
        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());
        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) {
        Long userId = SecurityUtils.getUserId();
        Admin admin = getById(userId);
        if (admin == null) {
            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("旧密码不正确,请重新确认密码!");
        }
    }
 
    /**
     * 修改管理员的密码
     *
     * @param param
     * @return
     */
    public boolean updateAdminPassword(UserPwsParm param) {
        Admin admin = getOne(Wrappers.lambdaQuery(Admin.class).eq(Admin::getId, param.getUserid()));
        if (admin == null) {
            throw new BusinessException("用户不存在");
        }
 
        //加密新密码
        admin.setPassword(passwordEncoder.encode(param.getNewPass()));
        return updateById(admin);
    }
 
    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);
    }
}