2021与蓝度共同重构项目,服务端
chenjiantian
2022-01-18 a98228c5e829bff75969b51a37652d7dbd6bee7e
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
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.sandu.common.enums.RoleLevelStatus;
import com.sandu.common.execption.BusinessException;
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.domain.Admin;
import com.sandu.ximon.dao.domain.AdminRoleRelation;
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;
 
    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;
    }
 
    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());
        return updateById(update);
    }
 
    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("旧密码不正确");
        }
        Admin update = new Admin();
        update.setId(userId);
        update.setPassword(passwordEncoder.encode(param.getNewPass()));
        return updateById(update);
    }
}