whyczh
2022-05-13 c4b24f0763164fb75932dfe2ede69fe8c6110b93
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whyc.constant.UserConstant;
import com.whyc.dto.Response;
import com.whyc.mapper.UserMapper;
import com.whyc.mapper.UserPermitGroupDataMapper;
import com.whyc.pojo.UserClient;
import com.whyc.pojo.UserInf;
import com.whyc.pojo.UserPermitGroupData;
import com.whyc.util.ActionUtil;
import com.whyc.util.RSAUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.InvalidParameterException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
@Slf4j
public class UserService {
 
    @Resource
    private UserMapper userMapper;
 
    @Resource
    private UserPermitGroupDataMapper permitMapper;
    @Resource
    private UserBridgeService userBridgeService;
 
    @Autowired
    private ApplicationContext applicationContext;
 
    public Response add(UserInf user) {
        try {
            UserInf userFound = userBridgeService.findPasswordByUserName(user.getUName());
            if (userFound.getUId() != 0) {
                return new Response<>().set(1, false, "用户已存在");
            }
            if (userMapper.addJudge(user.getUName(),user.getUMobilephone())>0){
                return new Response<>().set(1, false, "用户已存在");
            }
            //user.setCreateTime(new Date());
            String[] dataArr = RSAUtil.decryptFrontP(user.getUpassword(), RSAUtil.fontSeparator);
            String password = dataArr[0];
            String passwordMD5 = dataArr[1];
            if(!ActionUtil.EncryptionMD5(password).equals(passwordMD5)){
                throw new InvalidParameterException("参数校验失败");
            }
            user.setUpassword(RSAUtil.encrypt(password,RSAUtil.getPublicKey()));
            int flag = userMapper.insert(user);
            if (flag > 0) {
                Integer uId= userMapper.getUserInfoByPhoneNumber(user.getUMobilephone()).getUId().intValue();
                UserPermitGroupData permitGroupData = new UserPermitGroupData();
                permitGroupData.setPermitGroupId(user.getPermitGroupId());
                permitGroupData.setUId(uId);
                permitMapper.insert(permitGroupData);
                return new Response<>().set(1, true);
            } else {
                return new Response<>().set(1, false, "添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Response<>().set(0);
        }
    }
    public Response addByRSA(UserInf user) {
        try {
            UserInf userFound = userBridgeService.findPasswordByUserName(user.getUName());
            if (userFound.getUId() != 0) {
                return new Response<>().set(1, false, "用户已存在");
            }
            String password = "";
            try {
                password = URLDecoder.decode(user.getUpassword(), "utf-8");
            }catch (UnsupportedEncodingException e){
                e.printStackTrace();
            }
            String[] dataArr = RSAUtil.decryptFront(password, RSAUtil.fontSeparator);
            String pwd = RSAUtil.encrypt(dataArr[0],RSAUtil.getPublicKey());
            user.setUpassword(pwd);
            int flag = userMapper.insert(user);
            if (flag > 0) {
                Integer uId= userMapper.getUserInfoByPhoneNumber(user.getUMobilephone()).getUId().intValue();
                UserPermitGroupData permitGroupData = new UserPermitGroupData();
                permitGroupData.setPermitGroupId(user.getPermitGroupId());
                permitGroupData.setUId(uId);
                permitMapper.insert(permitGroupData);
                return new Response<>().set(1, true);
            } else {
                return new Response<>().set(1, false, "添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Response<>().set(0);
        }
    }
 
    public UserInf getById(int id) {
        return userMapper.selectById(id);
    }
 
    public List<UserInf> getAll() {
        List<UserInf> users = userMapper.selectList(null);
        return users;
    }
 
    public IPage<UserInf> getAllWithPage(Page page) {
        return userMapper.selectPage(page, null);
    }
 
    public boolean update(UserInf user) {
        //非系统管理员用户,禁止修改访问ip和访问时间
        if(user.getUId()!=1 && user.getUName().equals("sys_admin")){
            user.setVisitIp(null);
            user.setVisitTime(null);
        }
        return userMapper.updateById(user)>0;
    }
 
    public boolean delete(int id) {
        return userMapper.deleteById(id)>0;
    }
 
    // 将所有登陆的用户的信息存到application中
    public void setApplication(UserInf user) {
        ServletContext application = ActionUtil.getApplication();
        //查看全局中存储的users的Map的key-value集合
        Map<String, UserClient> map = (Map) application.getAttribute("users");
        if (map == null) {
            map = new HashMap<String, UserClient>();
        } else {
            //如果集合中有值,则获取当前用户对应的用户信息,key为用户名username,Value为用户名,存储的时间
            UserClient client = map.get(user.getUName());
            if (client != null) { //已存在
                map.remove(user.getUName());
            }
        }
        Long login_time = new Date().getTime();
        ActionUtil.getSession().setAttribute("login_time", login_time);
        map.put(user.getUName(), new UserClient(ActionUtil.getRequest().getRemoteAddr(),user,login_time));
        application.setAttribute("users", map);
    }
 
    public UserInf getUserByUKeyId(String uKeyId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uKey_ID",uKeyId);
        UserInf userInf = userMapper.selectOne(queryWrapper);
        return userInf;
    }
 
    public UserInf getUserByUserName(String userName){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("Uname",userName);
        UserInf userInf = userMapper.selectOne(queryWrapper);
        return userInf;
    }
 
    public boolean bindUkey(UserInf userInf){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("uName",userInf.getUName());
        int affectRows = userMapper.update(userInf,updateWrapper);
        return affectRows>0;
    }
 
 
    public Response updatePassword(UserInf userInf,String password){
        String[] dataArr = RSAUtil.decryptFrontP(password, RSAUtil.fontSeparator);
        password = dataArr[0];
        String passwordMD5 = dataArr[1];
        if(!ActionUtil.EncryptionMD5(password).equals(passwordMD5)){
            throw new InvalidParameterException("参数校验失败");
        }
        userInf.setUpassword(RSAUtil.encrypt(password,RSAUtil.getPublicKey()));
        if (userMapper.updateById(userInf)>0){
            return new Response().set(1,"修改成功");
        }else {
            return new Response().set(0,"修改失败");
        }
    }
 
    public Response updatePasswordByRSA(UserInf user,String newPwd){
        String[] dataArr = RSAUtil.decryptFrontP(newPwd, RSAUtil.fontSeparator);
        newPwd = dataArr[0];
        String passwordMD5 = dataArr[1];
        if(!ActionUtil.EncryptionMD5(newPwd).equals(passwordMD5)){
            throw new InvalidParameterException("参数校验失败");
        }
        user.setUpassword(RSAUtil.encrypt(newPwd,RSAUtil.getPublicKey()));
        if (userMapper.updateById(user)>0){
            return new Response().set(1,"修改成功");
        }else {
            return new Response().set(0,"修改失败");
        }
    }
 
 
    public Response updatePassword2(UserInf userInf, String passwordOld, String passwordNew) {
        //检查旧密码
        QueryWrapper<UserInf> queryWrapper = Wrappers.query();
        queryWrapper.select("upassword").eq("uName",userInf.getUName()).last(" limit 1");
        String passwordDB = userMapper.selectOne(queryWrapper).getUpassword();
        if(!RSAUtil.decrypt(passwordDB,RSAUtil.getPrivateKey()).equals(passwordOld)){
            return new Response().set(1,false,"旧密码输入错误");
        }
        //更新新密码
        UpdateWrapper<UserInf> updateWrapper = Wrappers.update();
        updateWrapper.set("upassword",RSAUtil.encrypt(passwordNew,RSAUtil.getPublicKey())).eq("uName",userInf.getUName());
        userMapper.update(null,updateWrapper);
        return new Response().set(1,true,"密码修改成功");
    }
 
    /**锁定账号*/
    public void lock(Long uId) {
        UpdateWrapper<UserInf> wrapper = Wrappers.update();
        wrapper.set("status", UserConstant.ACCOUNT_STATUS_LOCK_FAIL.getValue()).eq("uId",uId);
        userMapper.update(null,wrapper);
    }
 
    /**更新登录时间*/
    public void updateLoginTime(Long uId) {
        UpdateWrapper<UserInf> wrapper = Wrappers.update();
        wrapper.set("last_login_time", new Date()).eq("uId",uId);
        userMapper.update(null,wrapper);
    }
 
 
    public void updateExpiredAccount() {
        UpdateWrapper<UserInf> wrapper = Wrappers.update();
        wrapper.set("status",UserConstant.ACCOUNT_STATUS_CANCEL).eq("type",UserConstant.ACCOUNT_TYPE_TEMP).gt("expiration_time",new Date());
        userMapper.update(null,wrapper);
    }
 
    public void unLock() {
        UpdateWrapper<UserInf> wrapper = Wrappers.update();
        wrapper.set("status", UserConstant.ACCOUNT_STATUS_ACTIVE.getValue()).set("lock_time",null)
                .eq("status",UserConstant.ACCOUNT_STATUS_LOCK_FAIL.getValue())
                .last(" and date_add(lock_time,interval 20 MINUTE)<now() and uId is not null");
        userMapper.update(null,wrapper);
    }
 
    public void updateAccountStatus(int status) {
        UpdateWrapper<UserInf> wrapper = Wrappers.update();
        wrapper.set("status", status)
                .eq("status",UserConstant.ACCOUNT_STATUS_ACTIVE.getValue())
                .last(" and date_add(last_login_time,interval 3 MONTH)<now() and uId is not null");
        userMapper.update(null,wrapper);
    }
 
    public void unLock(int uId, int unLockType) {
        UpdateWrapper<UserInf> wrapper = Wrappers.update();
        wrapper.set("status",1).eq("uId",uId);
        if(unLockType==1){
            wrapper.set("lock_time",null);
        }else{
            wrapper.set("last_login_time",new Date());
        }
        userMapper.update(null,wrapper);
    }
 
    public List<UserInf> searchCS_All2() {
        return userMapper.searchCS_All2();
    }
}