危化品全生命周期管理后端
kongzy
2024-08-22 0c73654f55844e34772732914af8cc1e247aab63
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
package com.gkhy.hazmat.system.service.impl;
 
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.hazmat.common.api.CommonPage;
import com.gkhy.hazmat.common.constant.CacheConstant;
import com.gkhy.hazmat.common.constant.UserConstant;
import com.gkhy.hazmat.common.domain.entity.SysUser;
import com.gkhy.hazmat.common.enums.UserTypeEnum;
import com.gkhy.hazmat.common.exception.ApiException;
import com.gkhy.hazmat.common.utils.PageUtils;
import com.gkhy.hazmat.common.utils.RedisUtils;
import com.gkhy.hazmat.common.utils.SecurityUtils;
import com.gkhy.hazmat.common.utils.StringUtils;
import com.gkhy.hazmat.system.mapper.SysUserMapper;
import com.gkhy.hazmat.system.service.SysConfigService;
import com.gkhy.hazmat.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.validation.Validator;
import java.util.*;
import java.util.concurrent.TimeUnit;
 
 
/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2023-10-17 14:26:29
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Autowired
    private RedisUtils redisUtils;
 
    @Autowired
    private SysConfigService configService;
    @Autowired
    private Validator validator;
 
    @Override
    public CommonPage<SysUser> selectUserList(SysUser user) {
        SysUser currentUser= SecurityUtils.getLoginUser().getUser();
        List<SysUser> users=new ArrayList<>();
        if(currentUser.getUserType().equals(UserTypeEnum.COMPANY_USER.getCode())){
            user.setCompanyId(currentUser.getCompanyId());
            Map<String,Object> paramsMap=new HashMap<>();
            paramsMap.put("userType",currentUser.getUserType());
            user.setParams(paramsMap);
        }
        PageUtils.startPage();
        users=baseMapper.userList(user);
        return CommonPage.restPage(users);
    }
 
 
    @Override
    public SysUser selectUserByUsername(String username) {
        String key=redisUtils.generateKey(CacheConstant.SYS_USER_NAME+username);
        SysUser sysUser =null;
        if(redisUtils.hasKey(key)){
            sysUser= (SysUser) redisUtils.get(key);
        }else {
            sysUser = baseMapper.getUserByUsername(username);
            redisUtils.set(key,sysUser,10, TimeUnit.MINUTES);
        }
        return sysUser;
    }
 
    public void delCacheByUsername(String username){
        String key=redisUtils.generateKey(CacheConstant.SYS_USER_NAME+username);
        redisUtils.del(key);
    }
 
    @Override
    public SysUser selectUserByPhone(String phone) {
        return baseMapper.getUserByPhone(phone);
    }
 
    @Override
    public SysUser selectUserById(Long userId) {
        SysUser user = baseMapper.getUserById(userId);
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        if(currentUser.getUserType().equals(UserTypeEnum.NORMAL_USER.getCode())&&user!=null){
            if(!Objects.equals(user.getId(), currentUser.getId())){
                throw new ApiException("无权限查重其他用户信息");
            }
        }
        return user;
 
    }
 
    @Override
    public int deleteUserById(Long userId) {
        SysUser user=checkUserDataScope(userId);
        delCacheByUsername(user.getUsername());
        return baseMapper.deleteUserById(userId);
    }
 
    @Override
    public int addUser(SysUser user) {
        checkRequestData(user);
        checkUserAllowed(user);
        user.setCreateBy(SecurityUtils.getUsername());
        user.setPassword(SecurityUtils.encryptPassword(Base64.decodeStr(user.getPassword())));
        int row=baseMapper.insert(user);
        if(row<1){
            throw new ApiException("新增用户失败");
        }
        return row;
    }
 
    @Override
    public int updateUser(SysUser user) {
        checkRequestData(user);
        checkUserAllowed(user);
        user.setUpdateBy(SecurityUtils.getUsername());
        user.setPassword(null);
        int row=baseMapper.updateById(user);
        if(row<1){
            throw new ApiException("更新用户信息失败");
        }
        delCacheByUsername(user.getUsername());
        return row;
    }
 
    public void checkRequestData(SysUser user){
        if(!user.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())&&user.getCompanyId()==null){
            throw new ApiException("所属公司不能为空");
        }
        if(!checkUsernameUnique(user)){
            throw new ApiException("登录账号已存在");
        }
        if(StringUtils.isNotBlank(user.getPhone())&&!checkPhoneUnique(user)){
            throw new ApiException("手机号已存在");
        }
    }
 
    @Override
    public int updateUserStatus(SysUser user) {
        SysUser existUser=checkUserDataScope(user.getId());
        SysUser su=new SysUser().setId(user.getId()).setStatus(user.getStatus());
        su.setUpdateBy(SecurityUtils.getUsername());
 
        int row= baseMapper.updateById(su);
        if(row<1){
            throw new ApiException("更新用户状态失败");
        }
        delCacheByUsername(existUser.getUsername());
        return row;
    }
 
 
    @Override
    public boolean resetUserPwd(SysUser user) {
        SysUser existUser=checkUserDataScope(user.getId());
        SysUser su=new SysUser().setId(user.getId()).setPassword(SecurityUtils.encryptPassword(Base64.decodeStr(user.getPassword())));
        su.setUpdateBy(SecurityUtils.getUsername());
        delCacheByUsername(existUser.getUsername());
        return updateById(su);
    }
 
    @Override
    public boolean checkUsernameUnique(SysUser user) {
        Long userId = user.getId()==null? -1L : user.getId();
        SysUser info = baseMapper.checkLoginNameUnique(user.getUsername());
        if (info!=null && info.getId().longValue() != userId.longValue())
        {
            return UserConstant.NOT_UNIQUE;
        }
        return UserConstant.UNIQUE;
    }
 
    @Override
    public boolean checkPhoneUnique(SysUser user) {
        Long userId = user.getId()==null ? -1L : user.getId();
        SysUser info = baseMapper.checkPhoneUnique(user.getPhone());
        if (info!=null && info.getId().longValue() != userId.longValue())
        {
            return UserConstant.NOT_UNIQUE;
        }
        return UserConstant.UNIQUE;
    }
 
 
    @Override
    public void checkUserAllowed(SysUser user) {
        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
        Integer currentUserType=currentUser.getUserType();
        Integer userType=user.getUserType();
        if(currentUserType.equals(UserTypeEnum.SYSTEM_USER.getCode())){
            if(!userType.equals(UserTypeEnum.COMPANY_USER.getCode())&&!userType.equals(UserTypeEnum.SYSTEM_USER.getCode())){
                throw new ApiException("管理员只能操管理员和企业级用户");
            }
        }else if(user.getId()!=null&& user.getId().equals(currentUser.getId())){
            return;
        }else if(userType<=currentUserType){
            throw new ApiException("没有权限操作高级用户类型的用户");
        }else{
            if(user.getCompanyId()!=null){
                if(!user.getCompanyId().equals(currentUser.getCompanyId())){
                    throw new ApiException("无权限操作其他公司用户");
                }
            }
        }
 
    }
 
    @Override
    public SysUser checkUserDataScope(Long userId) {
        if(userId==null){
            throw new ApiException("用户id为空!");
        }
        SysUser user = getById(userId);
        if (ObjectUtil.isNull(user))
        {
            throw new ApiException("用户数据不存在!");
        }
        checkUserAllowed(user);
        return user;
    }
 
}