jinlin
2024-01-31 9025b9cf7ec8610003d445a31d93e35e7bd73c2e
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
/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有,侵权必究!
 */
 
package com.zt.modules.sys.service;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.PrettyPrinter;
import com.zt.common.constant.CacheKey;
import com.zt.common.constant.Constant;
import com.zt.common.db.query.QueryFilter;
import com.zt.common.exception.RenException;
import com.zt.common.service.BaseService;
import com.zt.common.servlet.Result;
import com.zt.common.utils.CacheUtils;
import com.zt.common.utils.TreeUtils;
import com.zt.core.context.User;
import com.zt.core.context.UserContext;
import com.zt.core.oss.service.ISysOssConfigService;
import com.zt.core.oss.service.ISysOssService;
import com.zt.core.security.BCryptPasswordEncoder;
import com.zt.core.security.Md5Utils;
import com.zt.core.sys.model.SysDept;
import com.zt.core.sys.model.SysUser;
import com.zt.core.sys.service.ISysUserService;
import com.zt.life.sys.dto.OssDto;
import com.zt.modules.sys.dao.SysUserDao;
import com.zt.modules.sys.dto.RoleDto;
import com.zt.modules.sys.dto.UserTreeDto;
import com.zt.modules.sys.enums.UserStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 系统用户
 *
 * @author hehz
 */
@Service
public class SysUserService extends BaseService<SysUserDao, SysUser> implements ISysUserService {
 
    @Autowired
    private SysRoleUserService sysRoleUserService;
    @Autowired
    private SysJobUserService sysJobUserService;
    @Autowired
    private SysPostUserService sysPostUserService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysParamsService paramsService;
    @Autowired
    SysMapService sysMapService;
    @Autowired
    private ISysOssConfigService sysOssConfigService;
 
 
 
    public List<SysUser> page(QueryFilter queryFilter) {
        // 普通管理员,只能查询所属部门及子部门的数据
        User user = UserContext.getUser();
        if (!user.isSuperAdmin()) {
            queryFilter.addParam("deptIds", sysDeptService.getWithDescendantIds(user.getDeptId()));
        }
        // 查询
        return queryFilter.getPageList(baseDao.getList(queryFilter.getParams()));
    }
 
    public List<SysUser> list(QueryFilter queryFilter) {
        // 普通管理员,只能查询所属部门及子部门的数据
        User user = UserContext.getUser();
        if (!user.isSuperAdmin()) {
            queryFilter.addParam("deptIds", sysDeptService.getWithDescendantIds(user.getDeptId()));
        }
        return baseDao.getList(queryFilter.getParams());
    }
 
    /**
     * 用户树
     *
     * @return
     */
    public List<UserTreeDto> getUserDeptTree() {
        List<SysDept> deptList = TreeUtils.toList(sysDeptService.getDeptTree());
        List<UserTreeDto> list = deptList.stream().map(d -> {
            UserTreeDto dto = new UserTreeDto();
            dto.setId(d.getId());
            dto.setPid(d.getPid());
            dto.setName(d.getName());
            dto.setType(d.isCompany() ? 1 : 2);
            return dto;
        }).collect(Collectors.toList());
 
        List<SysUser> users = this.getByDeptIds(deptList.stream().map(d -> d.getId()).collect(Collectors.toList()));
        users.stream().forEach(u -> {
            UserTreeDto dto = new UserTreeDto();
            dto.setId(u.getId());
            dto.setPid(u.getDeptId());
            dto.setName(u.getRealName());
            dto.setType(3);
            list.add(dto);
        });
        return TreeUtils.build(list);
    }
 
    public SysUser getByUsername(String username) {
        return baseDao.getByUsername(username);
    }
 
    public List<SysUser> checkUserArea(Long userId,String localServer) {
        return baseDao.checkUserArea(userId,localServer);
    }
 
    @Override
    @Cacheable(value = Constant.Cache.USER, key = "'id:' + #id")
    public SysUser get(Long id) {
/*        SysUser entity = baseDao.getEntity(id);
        return entity;*/
        return super.get(id);
    }
 
    /**
     * 根据部门ID查询
     */
    @Override
    public List<SysUser> getByDeptId(Long deptId) {
        return baseDao.selectList(new QueryWrapper<SysUser>().lambda().eq(SysUser::getDeptId, deptId));
    }
 
    /**
     * 根据部门ID查询
     */
    @Override
    public List<SysUser> getByDeptIds(List<Long> deptIds) {
        if (CollectionUtil.isNotEmpty(deptIds)) {
            return baseDao.getByDeptIds(deptIds);
            //return baseDao.selectList(new QueryWrapper<SysUser>().lambda().in(SysUser::getDeptId, deptIds));
        } else {
            return new ArrayList<>();
        }
    }
 
    @Transactional(rollbackFor = Exception.class)
    public void insert(SysUser entity) {
        if (this.getByUsername(entity.getUsername()) != null) {
            throw new RenException("用户名已存在!");
        }
        //entity.setStatus(UserStatus.ENABLE.getValue());
        // 密码加密
        String password = Md5Utils.hash(paramsService.getValue(Constant.ParamKey.INIT_PASSWORD_KEY));
        entity.setPassword(password);
        entity.setIsLocked(0);
 
        entity.setCompanyId(sysDeptService.getCompanyIdByDeptId(entity.getDeptId()));
        // 保存用户
        sysOssConfigService.updateOss(entity.getId(), entity.getFiles());
        sysOssConfigService.updateOss(entity.getId(), entity.getFiles2());
        super.insert(entity);
 
        sysPostUserService.saveOrUpdate(entity.getId(), entity.getPostIdList());
        sysJobUserService.saveOrUpdate(entity.getId(), entity.getJobIdList());
        sysMapService.saveOrUpdate("userTeamggroup", entity.getId(), entity.getTeamgroupIds());
        sysMapService.saveOrUpdate("userShip", entity.getId(), entity.getShipIds());
    }
 
    @Transactional(rollbackFor = Exception.class)
    public void update(SysUser entity) {
        // 更新用户
        entity.setCompanyId(sysDeptService.getCompanyIdByDeptId(entity.getDeptId()));
        sysOssConfigService.updateOss(entity.getId(), entity.getFiles());
        sysOssConfigService.updateOss(entity.getId(), entity.getFiles2());
        super.update(entity);
 
        // 保存岗位用户关系
        sysPostUserService.saveOrUpdate(entity.getId(), entity.getPostIdList());
 
        // 保存职位用户关系
        sysJobUserService.saveOrUpdate(entity.getId(), entity.getJobIdList());
        sysMapService.saveOrUpdate("userTeamggroup", entity.getId(), entity.getTeamgroupIds());
        sysMapService.saveOrUpdate("userShip", entity.getId(), entity.getShipIds());
        // 处理缓存
        CacheUtils.remove(Constant.Cache.USER, CacheKey.USER_ID.getKey() + entity.getId());
    }
 
    @CacheEvict(value = Constant.Cache.USER, allEntries = true)
    public void delete(Long[] ids) {
        // 删除用户
        super.deleteLogic(ids);
 
        // 删除角色用户关系
        sysRoleUserService.deleteByUserIds(ids);
    }
 
    /**
     * 修改密码
     *
     * @param id          用户ID
     * @param newPassword 新密码
     */
    @Transactional(rollbackFor = Exception.class)
    public void updatePassword(Long id, String newPassword) {
//        newPassword = Md5Utils.hash(newPassword);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        newPassword = encoder.encode(newPassword);
 
        SysUser entity = get(id);
        if (entity != null) {
            entity.setPassword(newPassword);
            baseDao.updatePassword(entity);
 
            // 处理缓存
            CacheUtils.remove(Constant.Cache.USER, CacheKey.USER_ID.getKey() + id);
        }
    }
 
    /**
     * 重置用户密码
     *
     * @param id
     */
    public void resetPassword(Long id) {
        updatePassword(id, paramsService.getValue(Constant.ParamKey.INIT_PASSWORD_KEY));
    }
 
    /**
     * 登录验证修改当前用户的状态
     *
     * @param id
     * @param loginErrorCount
     * @param isLocked
     * @param lastLoginErrorTime
     */
    public void updateLogin(Long id, Integer loginErrorCount, Integer isLocked, Date lastLoginErrorTime) {
        return;
        //baseDao.updateLogin(id, loginErrorCount, isLocked, lastLoginErrorTime);
    }
 
    public List<SysUser> isGetByUsernameList(String username, Long unitid, String code, String password) {
        List<SysUser> getByUsernameList = baseDao.isGetByUsernameList(username, unitid, code, password);
        return getByUsernameList;
    }
 
    public List<SysUser> isDomainName(String code) {
        return baseDao.isDomainName(code);
    }
 
    public Map adminRole() {
        //User sysUser = new User();
        Map<String, Object> sysUser = new Hashtable();
 
        sysUser.put("isAdmin", false);
        sysUser.put("isAssistant", false);
        List<RoleDto> roles = sysRoleUserService.getUserRoles(UserContext.getUser().getId());
        if (roles != null && roles.size() > 0) {
            if (roles.stream().filter(p -> p.getCode().equals("xtglybm") || p.getCode().equals("all")).count() > 0) {
                sysUser.put("isAdmin", true);
            }
        }
        return sysUser;
    }
 
    public User userRoleInfo(String username, String systemId) {
        Long userId = null;
        if (username == null)
            userId = UserContext.getUser().getId();
        User user = baseDao.userRoleInfo(userId, username, systemId);
        return user;
    }
 
    public String getRoles() {
        Long userId = UserContext.getUser().getId();
        return baseDao.getRoles(userId);
    }
    public String getRoleNames(Long userId) {
        return baseDao.getRoleNames(userId);
    }
 
    public List<SysUser> getNewConnectUser(String newShipTeam) {
        return baseDao.getNewConnectUser(newShipTeam);
    }
 
 
    public Integer checkTestHome(String systemId) {
        Long userId = UserContext.getUser().getId();
        Integer num =  baseDao.checkTestHome(systemId,userId);
        return num;
    }
 
    public List<SysUser> getUsersList(String type, String deptId) {
        List<SysUser> list = baseDao.getUsersList(type,deptId);
        return list;
    }
 
    public String getUsersName(String id) {
        return baseDao.getUsersName(id);
    }
 
    public String getNames(String ids) {
        String[] NamesData = ids.split(",");
        List<String> nameList =new ArrayList<>();
        for (String item : NamesData) {
            nameList.add(this.getUsersName(item));
        }
        String names = String.join(", ", nameList);
        return names;
    }
    public SysUser getUserInfo(Long id) {
        SysUser data = super.get(id);
        if (data != null) {
            OssDto ossDto= sysOssConfigService.getOssByBusiType(data.getId() , "users_avatar");
            if (ossDto != null) {
                data.setFiles(ossDto);
            }
            OssDto ossDto2= sysOssConfigService.getOssByBusiType(data.getId() , "users_sign");
            if (ossDto2 != null) {
                data.setFiles2(ossDto2);
            }
        }
        return data;
    }
 
 
}