xiaoyong931011
2022-05-26 ad2c2d53d756c8c9caa6e043c1868cfdf9736b9d
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
package com.xcong.farmer.cms.modules.system.service.Impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.modules.system.dto.AdminAddUserDto;
import com.xcong.farmer.cms.modules.system.dto.AdminUpdateUserDto;
import com.xcong.farmer.cms.modules.system.dto.AdminUserDto;
import com.xcong.farmer.cms.modules.system.entity.RoleEntity;
import com.xcong.farmer.cms.modules.system.entity.UserEntity;
import com.xcong.farmer.cms.modules.system.mapper.RoleMapper;
import com.xcong.farmer.cms.modules.system.mapper.UserMapper;
import com.xcong.farmer.cms.modules.system.service.IUserService;
import com.xcong.farmer.cms.modules.system.vo.AdminSeeUserInfoVo;
import com.xcong.farmer.cms.modules.system.vo.AdminUserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.codec.Rot;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
 
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {
 
    @Resource
    private RoleMapper roleMapper;
 
    @Override
    public Result getUserInPage(AdminUserDto adminUserDto) {
        Page<AdminUserVo> page = new Page<>(adminUserDto.getPageNum(), adminUserDto.getPageSize());
        UserEntity userEntity = new UserEntity();
        if(StrUtil.isNotEmpty(adminUserDto.getUsername())){
            userEntity.setUsername(adminUserDto.getUsername());
        }
        IPage<AdminUserVo> list = this.baseMapper.selectAdminUserVoInPage(page,userEntity);
        return Result.ok(list);
    }
 
    @Override
    @Transactional
    public Result addUser(AdminAddUserDto adminAddUserDto) {
        String username = adminAddUserDto.getUsername();
        if(StrUtil.isEmpty(username)){
            return Result.fail("请输入用户名");
        }
        Long roleId = adminAddUserDto.getRoleId();
        if(ObjectUtil.isEmpty(roleId)){
            return Result.fail("请选择用户角色");
        }
        RoleEntity roleEntity = roleMapper.selectById(roleId);
        if(ObjectUtil.isEmpty(roleEntity)){
            return Result.fail("请选择用户角色");
        }
        String phone = adminAddUserDto.getPhone();
        if(StrUtil.isEmpty(phone)){
            return Result.fail("请输入联系电话");
        }
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(username);
        userEntity.setPhone(phone);
        userEntity.setRoleId(roleId);
        userEntity.setRoleName(roleEntity.getRoleName());
        if(StrUtil.isNotEmpty(adminAddUserDto.getNickname())){
            userEntity.setNickname(adminAddUserDto.getNickname());
        }
        if(StrUtil.isNotEmpty(adminAddUserDto.getEmail())){
            userEntity.setEmail(adminAddUserDto.getEmail());
        }
        ;
        userEntity.setPassword(Rot.encode13(UserEntity.PASSWORD_DEFAULT));
        userEntity.setStatus(UserEntity.STATUS_ENABLE);
        int insert = this.baseMapper.insert(userEntity);
        if(insert > 0){
            return Result.ok("添加成功");
        }
        return Result.fail("添加失败");
    }
 
    @Override
    @Transactional
    public Result activeUser(Long id) {
        UserEntity userEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(userEntity)){
            return Result.fail("用户不存在");
        }
        userEntity.setStatus(UserEntity.STATUS_ENABLE);
        this.baseMapper.updateById(userEntity);
        return Result.ok("激活成功");
    }
 
    @Override
    @Transactional
    public Result forbiddenUser(Long id) {
        UserEntity userEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(userEntity)){
            return Result.fail("用户不存在");
        }
        userEntity.setStatus(UserEntity.STATUS_DISABLED);
        this.baseMapper.updateById(userEntity);
        return Result.ok("禁用成功");
    }
 
    @Override
    @Transactional
    public Result deleteUser(Long id) {
        UserEntity userEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(userEntity)){
            return Result.fail("用户不存在");
        }
        this.baseMapper.deleteById(id);
        return Result.ok("删除成功");
    }
 
    @Override
    public Result seeUserInfo(Long id) {
        UserEntity userEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(userEntity)){
            return Result.fail("用户不存在");
        }
        AdminSeeUserInfoVo adminSeeUserInfoVo = new AdminSeeUserInfoVo();
        adminSeeUserInfoVo.setId(userEntity.getId());
        adminSeeUserInfoVo.setUsername(userEntity.getUsername());
        adminSeeUserInfoVo.setNickname(userEntity.getNickname());
        adminSeeUserInfoVo.setPhone(userEntity.getPhone());
        adminSeeUserInfoVo.setEmail(userEntity.getEmail());
        adminSeeUserInfoVo.setRoleId(userEntity.getRoleId());
        adminSeeUserInfoVo.setRoleName(userEntity.getRoleName());
        return Result.ok(adminSeeUserInfoVo);
    }
 
    @Override
    @Transactional
    public Result updateUser(AdminUpdateUserDto adminUpdateUserDto) {
        String username = adminUpdateUserDto.getUsername();
        if(StrUtil.isEmpty(username)){
            return Result.fail("请输入用户名");
        }
        Long roleId = adminUpdateUserDto.getRoleId();
        if(ObjectUtil.isEmpty(roleId)){
            return Result.fail("请选择用户角色");
        }
        RoleEntity roleEntity = roleMapper.selectById(roleId);
        if(ObjectUtil.isEmpty(roleEntity)){
            return Result.fail("请选择用户角色");
        }
        String phone = adminUpdateUserDto.getPhone();
        if(StrUtil.isEmpty(phone)){
            return Result.fail("请输入联系电话");
        }
        UserEntity userEntity =  this.baseMapper.selectById(adminUpdateUserDto.getId());
        if(ObjectUtil.isEmpty(userEntity)){
            return Result.fail("用户不存在");
        }
        userEntity.setUsername(username);
        userEntity.setPhone(phone);
        userEntity.setRoleId(roleId);
        userEntity.setRoleName(roleEntity.getRoleName());
        if(StrUtil.isNotEmpty(adminUpdateUserDto.getNickname())){
            userEntity.setNickname(adminUpdateUserDto.getNickname());
        }
        if(StrUtil.isNotEmpty(adminUpdateUserDto.getEmail())){
            userEntity.setEmail(adminUpdateUserDto.getEmail());
        }
        int insert = this.baseMapper.updateById(userEntity);
        if(insert > 0){
            return Result.ok("更新成功");
        }
        return Result.fail("更新失败");
    }
 
    @Override
    @Transactional
    public Result resetPassword(Long id) {
        UserEntity userEntity =  this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(userEntity)){
            return Result.fail("用户不存在");
        }
        userEntity.setPassword(Rot.encode13(UserEntity.PASSWORD_DEFAULT));
        this.baseMapper.updateById(userEntity);
        return Result.ok("重置成功");
    }
}