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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whyc.dto.Response;
import com.whyc.mapper.UserMapper;
import com.whyc.pojo.UserClient;
import com.whyc.pojo.UserInf;
import com.whyc.util.ActionUtil;
import com.whyc.util.DigestsUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
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.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 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, "用户已存在");
            }
            //user.setCreateTime(new Date());
            Map<String, String> encryptMap = DigestsUtil.encryptPassword(user.getUpassword());
            user.setUpassword(encryptMap.get("password"));
            if (userMapper.insert(user) > 0) {
                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) {
        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);
    }
}