xiaoyong931011
2022-06-16 a92d63ba13a300e48d02d9d67905c105f24dde08
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package com.xcong.farmer.cms.modules.system.service.Impl;
 
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
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.*;
import com.xcong.farmer.cms.modules.system.entity.*;
import com.xcong.farmer.cms.modules.system.mapper.*;
import com.xcong.farmer.cms.modules.system.service.IUserService;
import com.xcong.farmer.cms.modules.system.util.LoginUserUtil;
import com.xcong.farmer.cms.modules.system.vo.*;
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.collection.CollUtil;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.*;
 
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {
 
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;
    @Resource
    private MenuMapper menuMapper;
 
    @Override
    public Result getUserInPage(AdminUserDto adminUserDto) {
        UserEntity userlogin = LoginUserUtil.getLoginUser();
        Page<AdminUserVo> page = new Page<>(adminUserDto.getPageNum(), adminUserDto.getPageSize());
        UserEntity userEntity = new UserEntity();
        long companyId = userlogin.getCompanyId() == null ? UserEntity.USER_BELONG_TOP : userlogin.getCompanyId();
        if(companyId > UserEntity.USER_BELONG_TOP){
            userEntity.setCompanyId(companyId);
            userEntity.setRoleName(RoleEntity.ROLE_ADMIN_NAME);
        }else if(companyId == UserEntity.USER_BELONG_TOP){
            userEntity.setRoleName(RoleEntity.ROLE_SUPER_NAME);
        }
        if(StrUtil.isNotEmpty(adminUserDto.getUsername())){
            userEntity.setUsername(adminUserDto.getUsername());
        }
        IPage<AdminUserVo> AdminUserVos = this.baseMapper.selectAdminUserVoInPage(page,userEntity);
        List<AdminUserVo> records = AdminUserVos.getRecords();
        if(CollUtil.isNotEmpty(records)){
            for(AdminUserVo adminUserVo : records){
                List<AdminRoleVo> adminRoleVos = new ArrayList<>();
                QueryWrapper<UserRoleEntity> objectQueryWrapper = new QueryWrapper<>();
                objectQueryWrapper.eq("user_id",adminUserVo.getId());
                List<UserRoleEntity> userRoleEntities = userRoleMapper.selectList(objectQueryWrapper);
                if(CollUtil.isNotEmpty(userRoleEntities)){
                    for(UserRoleEntity userRoleEntity : userRoleEntities){
                        RoleEntity roleEntity = roleMapper.selectById(userRoleEntity.getRoleId());
                        AdminRoleVo adminRoleVo = new AdminRoleVo();
                        adminRoleVo.setId(roleEntity.getId());
                        adminRoleVo.setRoleName(roleEntity.getRoleName());
                        adminRoleVos.add(adminRoleVo);
                    }
                }
                adminUserVo.setAdminRoleVos(adminRoleVos);
            }
        }
 
        return Result.ok(AdminUserVos);
    }
 
    @Override
    @Transactional
    public Result addUser(AdminAddUserDto adminAddUserDto) {
        UserEntity userlogin = LoginUserUtil.getLoginUser();
        String username = adminAddUserDto.getUsername();
        QueryWrapper<UserEntity> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("username",username);
        List<UserEntity> userEntities = this.baseMapper.selectList(objectQueryWrapper);
        if(CollUtil.isNotEmpty(userEntities)){
            return Result.fail("用户名重复");
        }
        String roleIds = adminAddUserDto.getRoleIds();
        String phone = adminAddUserDto.getPhone();
        Long belongId = adminAddUserDto.getBelongId() == null ? UserEntity.USER_BELONG_TOP:adminAddUserDto.getBelongId();
        if(belongId == UserEntity.USER_BELONG_TOP){
            return Result.fail("请选择所属公司");
        }
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(username);
        userEntity.setPhone(phone);
        if(StrUtil.isNotEmpty(adminAddUserDto.getNickname())){
            userEntity.setNickname(adminAddUserDto.getNickname());
        }
        if(StrUtil.isNotEmpty(adminAddUserDto.getEmail())){
            userEntity.setEmail(adminAddUserDto.getEmail());
        }
        userEntity.setPassword(SecureUtil.md5(UserEntity.PASSWORD_DEFAULT));
        userEntity.setStatus(UserEntity.STATUS_ENABLE);
        userEntity.setCompanyId(belongId);
        int insert = this.baseMapper.insert(userEntity);
        if(insert > 0){
            saveUserRole(roleIds,userEntity.getId());
            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);
 
        QueryWrapper<UserRoleEntity> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("user_id",userEntity.getId());
        userRoleMapper.delete(objectQueryWrapper);
        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.setBelongId(userEntity.getCompanyId());
 
        List<AdminRoleVo> adminRoleVos = new ArrayList<>();
        QueryWrapper<UserRoleEntity> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("user_id",userEntity.getId());
        List<UserRoleEntity> userRoleEntities = userRoleMapper.selectList(objectQueryWrapper);
        if(CollUtil.isNotEmpty(userRoleEntities)){
            for(UserRoleEntity userRoleEntity : userRoleEntities){
                RoleEntity roleEntity = roleMapper.selectById(userRoleEntity.getRoleId());
                AdminRoleVo adminRoleVo = new AdminRoleVo();
                adminRoleVo.setId(roleEntity.getId());
                adminRoleVo.setRoleName(roleEntity.getRoleName());
                adminRoleVos.add(adminRoleVo);
            }
        }
        adminSeeUserInfoVo.setAdminRoleVos(adminRoleVos);
        return Result.ok(adminSeeUserInfoVo);
    }
 
    @Override
    @Transactional
    public Result updateUser(AdminUpdateUserDto adminUpdateUserDto) {
        String username = adminUpdateUserDto.getUsername();
        if(StrUtil.isEmpty(username)){
            return Result.fail("请输入用户名");
        }
        String roleIds = adminUpdateUserDto.getRoleIds();
        if(StrUtil.isEmpty(roleIds)){
            return Result.fail("请选择用户角色");
        }
        String phone = adminUpdateUserDto.getPhone();
        if(StrUtil.isEmpty(phone)){
            return Result.fail("请输入联系电话");
        }
        Long belongId = adminUpdateUserDto.getBelongId() == null ? UserEntity.USER_BELONG_TOP : adminUpdateUserDto.getBelongId();
        if(UserEntity.USER_BELONG_TOP == belongId){
            return Result.fail("请输入所属公司");
        }
        UserEntity userEntity =  this.baseMapper.selectById(adminUpdateUserDto.getId());
        if(ObjectUtil.isEmpty(userEntity)){
            return Result.fail("用户不存在");
        }
        userEntity.setCompanyId(belongId);
        userEntity.setUsername(username);
        userEntity.setPhone(phone);
        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){
            QueryWrapper<UserRoleEntity> objectQueryWrapper = new QueryWrapper<>();
            objectQueryWrapper.eq("user_id",userEntity.getId());
            userRoleMapper.delete(objectQueryWrapper);
 
            saveUserRole(roleIds,userEntity.getId());
            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(SecureUtil.md5(UserEntity.PASSWORD_DEFAULT));
        this.baseMapper.updateById(userEntity);
        return Result.ok("重置成功");
    }
 
    @Override
    public Result userMenu() {
        UserEntity loginUser = LoginUserUtil.getLoginUser();
        Long userId = loginUser.getId();
        List<AdminUserMenuVo> adminUserMenuVos = new ArrayList<>();
 
        QueryWrapper<UserRoleEntity> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.eq("user_id",userId);
        List<UserRoleEntity> userRoleEntities = userRoleMapper.selectList(userRoleQueryWrapper);
        if(CollUtil.isNotEmpty(userRoleEntities)){
            List menuIds = new ArrayList<Long>();
            if(UserEntity.USERNAME_DEFAULT.equals(loginUser.getUsername())){
                List<MenuEntity> menuEntities = menuMapper.selectList(new QueryWrapper<>());
                if(CollUtil.isNotEmpty(menuEntities)){
                    for(MenuEntity menuEntity : menuEntities){
                        menuIds.add(menuEntity.getId());
                    }
                }
            }else{
                for(UserRoleEntity userRoleEntity : userRoleEntities){
                    List<RoleMenuEntity> roleMenuEntities = roleMenuMapper.selectListByRoleId(userRoleEntity.getRoleId());
                    if(CollUtil.isNotEmpty(roleMenuEntities)){
                        for(RoleMenuEntity roleMenuEntity : roleMenuEntities){
                            menuIds.add(roleMenuEntity.getMenuId());
                        }
                    }
                }
            }
            if(CollUtil.isNotEmpty(menuIds)){
                HashSet hashSet = CollUtil.newHashSet(menuIds);
                List<Long> parentMenuIds = new ArrayList<>();
                List<Long> childMenuIds = new ArrayList<>();
                for(Object s : hashSet){
                    MenuEntity menuEntity = menuMapper.selectById(Long.parseLong(s.toString()));
                    Long parentId = menuEntity.getParentId();
                    if(parentId == 0L){
                        parentMenuIds.add(menuEntity.getId());
                    }else{
                        childMenuIds.add(menuEntity.getId());
                    }
                }
                Map<Long, AdminUserMenuVo> menuMap = new HashMap<>();
                if(CollUtil.isNotEmpty(parentMenuIds)){
                    for(Long parentId : parentMenuIds){
                        AdminUserMenuVo adminUserMenuVo = menuMapper.selectAdminRoleMenuVoById(parentId);
                        menuMap.put(parentId,adminUserMenuVo);
                    }
                }
                if(CollUtil.isNotEmpty(childMenuIds)){
                    for(Long childId : childMenuIds){
                        AdminUserMenuVo adminUserMenuVo = menuMapper.selectAdminRoleMenuVoById(childId);
                        Long parentId = adminUserMenuVo.getParentId();
                        AdminUserMenuVo adminUserMenuVoParent = menuMap.get(parentId);
                        if(ObjectUtil.isNotEmpty(adminUserMenuVoParent)){
                            List<AdminUserMenuVo> child = adminUserMenuVoParent.getChild();
                            if(CollUtil.isEmpty(child)){
                                List<AdminUserMenuVo> list = new ArrayList<>();
                                list.add(adminUserMenuVo);
                                adminUserMenuVoParent.setChild(list);
                            }else{
                                child.add(adminUserMenuVo);
                            }
                        }
                    }
                }
                Set<Map.Entry<Long, AdminUserMenuVo>> entries = menuMap.entrySet();
                Iterator<Map.Entry<Long, AdminUserMenuVo>> iterator = entries.iterator();
                while (iterator.hasNext()){
                    adminUserMenuVos.add(iterator.next().getValue());
                }
 
            }
        }
        if(CollUtil.isNotEmpty(adminUserMenuVos)){
            for(AdminUserMenuVo adminUserMenuVo : adminUserMenuVos){
                List<AdminUserMenuVo> child = adminUserMenuVo.getChild();
                if(CollUtil.isNotEmpty(child)){
                    List<Long> menuIdChilds = new ArrayList<>();
                    for(AdminUserMenuVo adminUserMenuVoChild : child){
                        Long id = adminUserMenuVoChild.getId();
                        menuIdChilds.add(id);
                    }
                    List<AdminUserMenuVo> adminUserMenuVoByOrderNum = menuMapper.selectAdminUserMenuVoOrderByOrderNum(menuIdChilds);
                    adminUserMenuVo.setChild(adminUserMenuVoByOrderNum);
                }
            }
        }
        return Result.ok(adminUserMenuVos);
    }
 
    @Override
    @Transactional
    public Result delObjs(AdminDeleteDto adminDeleteDto) {
        String ids = adminDeleteDto.getIds();
        if(StrUtil.isNotEmpty(ids)){
            String[] userIds = ids.split(StringPool.COMMA);
            for(String userIdStr : userIds){
                Long userId = Long.valueOf(userIdStr);
 
                QueryWrapper<UserRoleEntity> objectQueryWrapper = new QueryWrapper<>();
                objectQueryWrapper.eq("user_id",userId);
                userRoleMapper.delete(objectQueryWrapper);
 
                this.baseMapper.deleteById(userId);
            }
 
        }
        return Result.ok("删除成功");
    }
 
    @Override
    @Transactional
    public Result updatePassword(AdminUpdatePasswordDto adminUpdatePasswordDto) {
        UserEntity loginUser = LoginUserUtil.getLoginUser();
        Long userId = loginUser.getId();
        UserEntity userEntity =  this.baseMapper.selectById(userId);
        if(ObjectUtil.isEmpty(userEntity)){
            return Result.fail("用户不存在");
        }
        String oldPassword = adminUpdatePasswordDto.getOldPassword();
        if(StrUtil.isEmpty(oldPassword)){
            return Result.fail("请输入原密码");
        }
        String newPassword = adminUpdatePasswordDto.getNewPassword();
        if(StrUtil.isEmpty(newPassword)){
            return Result.fail("请输入新密码");
        }
        String s = SecureUtil.md5(oldPassword);
        if(!userEntity.getPassword().equals(s)){
            return Result.fail("原密码不正确,请联系管理员重置密码");
        }
        userEntity.setPassword(SecureUtil.md5(newPassword));
        this.baseMapper.updateById(userEntity);
        return Result.ok("修改成功");
    }
 
    @Override
    public Result userInfo() {
        UserEntity loginUser = LoginUserUtil.getLoginUser();
        Long id = loginUser.getId();
        UserEntity userEntity = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(userEntity)){
            return Result.fail("用户不存在");
        }
        AdminUserInfoVo adminUserInfoVo = new AdminUserInfoVo();
        adminUserInfoVo.setId(userEntity.getId());
        adminUserInfoVo.setUsername(userEntity.getUsername());
        adminUserInfoVo.setNickname(userEntity.getNickname());
        adminUserInfoVo.setPhone(userEntity.getPhone());
        adminUserInfoVo.setEmail(userEntity.getEmail());
        return Result.ok(adminUserInfoVo);
    }
 
    private void saveUserRole(String roleIds,Long userId) {
        if (StrUtil.isNotEmpty(roleIds)) {
            String[] roleIdList = roleIds.split(StringPool.COMMA);
            Arrays.stream(roleIdList).forEach(roleId -> {
                UserRoleEntity userRoleEntity = new UserRoleEntity();
                userRoleEntity.setRoleId(Long.valueOf(roleId));
                userRoleEntity.setUserId(userId);
                userRoleMapper.insert(userRoleEntity);
            });
        }
    }
 
}