perryhsu
2020-10-17 a543f4de1c91ef6f43aace92975c6cf28de23618
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
package com.fgkj.services;
 
import com.fgkj.util.*;
import com.fgkj.dto.ServiceModel;
import com.fgkj.dto.User;
import com.fgkj.dto.UserClient;
import com.fgkj.mapper.impl.UserMapper;
import com.fgkj.report.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.List;
import java.util.Map;
 
@Service
public class UserService {
 
    @Autowired
    private UserMapper mapper;
 
    // 添加员工
    public ServiceModel addUser(User user) {
        ServiceModel model = new ServiceModel();
        /*if (mapper.add(user)) {
            user = (User) mapper.serchByCondition(user).get(0);
            dao = BaseDAOFactory.getBaseDAO(Basemapper.USERROLE);
            UserRole userrole = new UserRole(user, new Roles(user.getPid()));
            if (dao.add(userrole)) {
                model.setCode(1);
                model.setMsg("添加成功");
            }
        } else {
            model.setMsg("添加失败");
        }
        return model;*/
        return model;
    }
 
    public ServiceModel delUser(User user) {
        ServiceModel model = new ServiceModel();
        if (mapper.del(user)) {
            model.setCode(1);
            model.setMsg("删除成功");
        } else {
            model.setMsg("删除失败");
        }
        return model;
    }
 
    public ServiceModel updateUser(User user) {
        ServiceModel model = new ServiceModel();
        /*if (mapper.update(user)) {
            dao = BaseDAOFactory.getBaseDAO(Basemapper.USERROLE);
            boolean flag = dao.update(new UserRole(user, new Roles(user.getPid())));
            if (flag) {
                model.setCode(1);
                model.setMsg("修改成功");
            }
        } else {
            model.setMsg("修改成功");
        }*/
        return model;
    }
 
    public ServiceModel findUser(User user) {
        ServiceModel model = new ServiceModel();
        List list = mapper.serchByCondition(user);
        if (list != null && list.size() > 0) {
            model.setData(list);
            model.setCode(1);
        } else {
            model.setMsg("查询失败");
        }
        return model;
    }
 
    public ServiceModel findUserAll() {
        ServiceModel model = new ServiceModel();
        List list = mapper.searchAll();
        if (list != null && list.size() > 0) {
            FileUtil.WriterFile(list);
            model.setCode(1);
            model.setData(list);
        }
        return model;
    }
 
    public ServiceModel findByInfo(Object obj) {
        ServiceModel model = new ServiceModel();
        User user = (User) obj;
        List list = mapper.serchByInfo(user);
        if (list == null || list.size() < 1) {
            model.setMsg("没有匹配的员工");
        } else {
            model.setData(list);
            model.setCode(1);
        }
        return model;
    }
 
    public ServiceModel updatePass(Object obj) {
        ServiceModel model = new ServiceModel();
        boolean flag = mapper.updatePass(obj);
        if (!flag) {
            model.setMsg("修改失败");
        } else {
            model.setCode(1);
            List list = mapper.serchByCondition(obj);
            if (list != null && list.size() > 0) {
                User u = (User) list.get(0);
                ActionUtil.getSession().setAttribute("user", u);
            }
        }
        return model;
    }
 
    public ServiceModel findByAge(int currentPage, int PageSize) {
        ServiceModel model = new ServiceModel();
        List list = mapper.serchByPage(currentPage, PageSize);
        if (list != null && list.size() > 0) {
            model.setCode(1);
            model.setData(list);
        } else {
            model.setMsg("查询失败");
        }
        return model;
    }
 
//    public ServiceModel login(Object obj) {
//        User user = (User) obj;
//        List list = mapper.serchByCondition(user);
//        if (list == null || list.size() < 1) {
//            model.setMsg(this.getText("Username_error"));
//            // System.out.println("用户名不存在");
//        } else {
//            User u = (User) list.get(0);
//
//            if (!user.getUpass().equals(u.getUpass())) {
//                model.setMsg(getText("Password_error"));
//            } else {
//                model.setCode(1);
//                // 将登陆成功的用户存入session
//                ActionUtil.getSession().setAttribute("user", u);
//                ActionUtil.getSession().setAttribute("ip",
//                        ActionUtil.getRequest().getRemoteAddr());
//                // 将新登录的用户存入application
//                setApplication(u);
//            }
//        }
//        return model;
//    }
 
//    public void setApplication(User user) {
//        ServletContext application = ActionUtil.getApplication();
//        Map<String, UserClient> map = (Map) application.getAttribute("users");
//        if (map == null) {
//            map = new HashMap<String, UserClient>();
//        } else {
//            UserClient client = map.get(user.getUname());
//            if (client != null) {
//                map.remove(user.getUname());
//            }
//        }
//        //map.put(user.getUname(), new UserClient(ActionUtil.getRequest().getRemoteAddr(), user));
//        application.setAttribute("users", map);
//    }
 
    /**
     * 开始查看application中是否有另一用使用该账号登陆
     *
     * @return
     */
    public ServiceModel checkUser() {
        ServiceModel model = new ServiceModel();
        Map<String, UserClient> map = (Map) ActionUtil.getApplication()
                .getAttribute("users");
        //System.out.println(map);
        if (map != null && map.size()>0) {
            User user = (User) ActionUtil.getSession().getAttribute("user");
            if(user!=null){
                UserClient client = map.get(user.getUname());
                if(client!=null){
                    if (!ActionUtil.getRequest().getRemoteAddr().equals(client.getIp())) {
                        model.setCode(1);
                        model.setMsg("The landing on the account in another host, please log in again");
                    }
                }
            }else{
                model.setCode(1);
                model.setMsg("You are not logged in, please log in");
            }
        } else {
            model.setCode(1);
            model.setMsg("You are not logged in, please log in");
        }
        return model;
    }
}