whyclj
2020-07-22 a5729100cb1eaa3584b3a194e46e1b8b52b3ed1a
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package com.fgkj.services;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
 
import com.fgkj.actions.ActionUtil;
import com.fgkj.dao.BaseDAO;
import com.fgkj.dao.BaseDAOFactory;
import com.fgkj.dao.impl.User_infImpl;
import com.fgkj.dto.Batt_User_Permit;
import com.fgkj.dto.Page;
import com.fgkj.dto.ServiceModel;
import com.fgkj.dto.UserClient;
import com.fgkj.dto.User_battgroup_baojigroup;
import com.fgkj.dto.User_battgroup_baojigroup_battgroup;
import com.fgkj.dto.User_inf;
import com.fgkj.dto.User_permitgroup;
 
public class User_infService extends ActionUtil {
    private ServiceModel model;
    private User_permitgroupService upservice;
    private BaseDAO dao;
 
    public User_infService() {
        model = new ServiceModel();
        dao = BaseDAOFactory.getBaseDAO(BaseDAO.USER_INF);
        upservice = new User_permitgroupService();
    }
    //web版登录直接访问(只验证账号不验证密码)
    public ServiceModel login_direct(Object obj) {
        User_inf uinf = (User_inf) obj;
        List list = dao.serchByCondition(uinf);
        if (list == null || list.size() < 1) {
            model.setMsg(this.getText("Username_error"));
        } else {
            User_inf u = (User_inf) list.get(0);
            uinf.setUSnId(u.getUSnId());
            model.setCode(1);
            //model.setMsg("index.jsp");
            model.setMsg("index-outline.jsp");
            // 将登陆成功的用户存入session
            ActionUtil.getSession().setAttribute("user", u);
            
            //设置session不活动时间为30分
            ActionUtil.getSession().setMaxInactiveInterval(60*30);
            ActionUtil.getSession().setAttribute("ip",ActionUtil.getRequest().getRemoteAddr());
            User_permitgroupService upgs=new User_permitgroupService();
            ServiceModel model1=upgs.serchItem(u);
            //System.out.println(model.getData());
            if(model1.getCode()==1){
                ActionUtil.getSession().setAttribute("permits", ActionUtil.tojson(model1.getData()));
            }else{
                model.setCode(0);
                model.setMsg("还未分配权限");
            }
            //将新登录的用户存入application
            setApplication(u);
          }
        return model;
    }
    //5.1用户管理(添加)
    public ServiceModel add(Object obj) {
        int flag=((User_infImpl)dao).addJudge(obj);
        //System.out.println(flag);
        if(flag!=0){
            Boolean bl=dao.add(obj);
            if(bl){
                model.setCode(1);
                model.setMsg("添加成功!");
            }
            else{
                model.setCode(0);
                model.setMsg("添加失败!");
            }
        }else{
            model.setCode(0);
            model.setMsg("用户名存在,请重新输入!");
        }
        return model;
    }
 
    // 5.1用户管理(编辑)
    public ServiceModel update(Object obj) {
        Boolean bl = false;
        bl=dao.update(obj);
        if(bl){
            model.setCode(1);
            model.setMsg("修改成功");
        }else{
            model.setCode(0);
            model.setMsg("修改失败");
        }
        return model;
    }
 
    // 5.1用户管理(删除)
    public ServiceModel delete(Object obj) {
        Boolean bl = dao.del(obj);
        //System.out.println(obj);
        if (bl) {
            model.setCode(1);
            model.setMsg("删除成功!");
        } else {
            model.setMsg("删除失败!");
        }
        return model;
    }
 
    public ServiceModel serchByCondition(Object obj) {
        List list = dao.serchByCondition(obj);
        /*
         * for (Object object : list) { System.out.println(object); }
         */
 
        if (list != null && list.size() > 0) {
            model.setCode(1);
            model.setData(list);
        }
        // System.out.println(model);
        return model;
    }
    //5.1查询所有员工信息
    public ServiceModel searchAll() {
        List<User_inf> list = dao.searchAll();
        if (list != null && list.size() > 0) {
            model.setCode(1);
            model.setData(list);
        }
        return model;
    }
 
    // 根据battgroupid查对应的包机人
    public ServiceModel searchByBattGroupId(Object obj) {
        model = new ServiceModel();
        List<User_inf> list = ((User_infImpl) dao).searchByBattGroupId(obj);
        // System.out.println(list.size());
        /*
         * for(User_inf u:list){ System.out.println(u); }
         */
        if (list != null && list.size() > 0) {
            model.setCode(1);
            model.setData(list);
            // battgroupid对应的所有包机人姓名和id
            String uStrN = "";
            String uStrI = "";
            for (int i = 0; i < list.size(); i++) {
                uStrN += ",";
                uStrI += ",";
                uStrN += list.get(i).getUName().toString();
                uStrI += list.get(i).getUId().toString();
            }
            uStrN = uStrN.substring(1, uStrN.length());
            uStrI = uStrI.substring(1, uStrI.length());
            model.setMsg(uStrN);
            model.setMsgO(uStrI);
        } else {
            model.setCode(0);
        }
        // System.out.println(model.getMsg());
        // System.out.println(model);
        return model;
    }
 
    // 3.1根据uid查用户信息(uname)
    public ServiceModel serchuName(Object obj) {
        model = new ServiceModel();
        List list = ((User_infImpl) dao).serchUname(obj);
        if (list != null && list.size() > 0) {
            model.setCode(1);
            model.setData(list);
        } else {
            model.setCode(0);
        }
        // System.out.println(model);
        return model;
    }
 
    // 5.1查询员工的所有信息
    public ServiceModel searchAll(Object obj) {
        // System.out.println((Batt_User_Permit)obj);
        model = new ServiceModel();
        Batt_User_Permit bup = new Batt_User_Permit();
        User_permitgroup usergroup = new User_permitgroup();
        List<Batt_User_Permit> list = ((User_infImpl) dao).searchAll(obj);// 存放的是员工信息
        if (list != null && list.size() > 0) {
            model.setCode(1);
            for (int i = 0; i < list.size(); i++) {
                ServiceModel model1 = upservice.serchByCondition(list.get(i).getUinf());// 存放权限
                // System.out.println(model1.getMsgN());
                if (model1.getCode() > 0) {
                    list.get(i).getUinf().setPermitgroupName(model1.getMsgN());
                } else {
                    list.get(i).getUinf().setPermitgroupName("");
                }
            }
            model.setData(list);
        }
        // System.out.println(model);
        return model;
    }
 
    // 5.3根据uid查所在的包机组
    public ServiceModel serchByInfo(Object obj) {
        model = new ServiceModel();
        List<User_battgroup_baojigroup> list = dao.serchByInfo(obj);
        // System.out.println(list.size());
        // for(User_battgroup_baojigroup u:list){
        // System.out.println(u);
        // }
        if (list != null && list.size() > 0) {
            model.setCode(1);
            model.setData(list);
 
        } else {
            model.setCode(0);
        }
        // System.out.println(model);
        return model;
    }
 
    // 用户登录
    public ServiceModel login(Object obj) {
        User_inf uinf = (User_inf) obj;
        //String usnId64 = (String) ActionUtil.DecryptionBase64(uinf.getUpassword());
        //String usnIdMd5 = (String) ActionUtil.EncryptionMD5(usnId64);
        // System.out.println(uinf.getUSnId()+"    "+usnId64+"   "+usnIdMd5+"     :service");
        //uinf.setUpassword(usnIdMd5);
        List list = dao.serchByCondition(uinf);
        if (list == null || list.size() < 1) {
            model.setMsg(this.getText("Username_error"));
            // System.out.println("用户名不存在");
        } else {
            User_inf u = (User_inf) list.get(0);
            //System.out.println(uinf.getUpassword());
            // System.out.println(uinf.getUSnId()+"=="+u.getUNote());
            if (!uinf.getUpassword().equals(u.getUpassword())) {
                //System.out.println("密码错误"+uinf.getUpassword()+"=="+u.getUpassword());
                model.setMsg(getText("Password_error"));
            } else {
                //uinf.setUSnId(u.getUSnId());
                model.setCode(1);
                model.setMsg("index.jsp");
                //model.setMsg("index-outline.jsp");
                
                setMyCookie("user",u,30,uinf.getUId() == 1);                //用户记住密码功能
                
                // 将登陆成功的用户存入session
                u.setUSnId("");
                u.setUpassword("");
                ActionUtil.getSession().setAttribute("user", u);
                
                //设置session不活动时间为30分
                ActionUtil.getSession().setMaxInactiveInterval(60*30);
                ActionUtil.getSession().setAttribute("ip",ActionUtil.getRequest().getRemoteAddr());
                User_permitgroupService upgs=new User_permitgroupService();
                ServiceModel model1=upgs.serchItem(u);
                //System.out.println(model.getData());
                if(model1.getCode()==1){
                    ActionUtil.getSession().setAttribute("permits", ActionUtil.tojson(model1.getData()));
                }else{
                    model.setCode(0);
                    model.setMsg("还未分配权限");
                }
                //将新登录的用户存入application
                setApplication(u);
            }
        }
        return model;
    }
 
    // 将所有登陆的用户的信息存到application中
    public void setApplication(User_inf 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());
            }
        }
        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);
    }
    
    /**
     *     保存指定的cookie值
     * @param key        cookie 键
     * @param obj        值
     * @param timelong    保存时长(秒)
     * @param flag        是否保存
     */
    public void setMyCookie(String key,Object obj,long timelong,boolean flag){
        Cookie c = null;
        if(flag){
            //记住密码
            c = new Cookie(key, ActionUtil.EncryptionBase64(ActionUtil.getGson().toJson(obj)).toString());
        }else{
            c = new Cookie(key, "");
        }
        //让Cookie保留30天
        c.setMaxAge(30*24*60*60);
        ActionUtil.getResponse().addCookie(c);
    }
 
    /**
     * 开始查看application中是否有另一用使用该账号登陆
     * 
     * @return
     */
    
    public ServiceModel checkUser() {
        Map<String, UserClient> map = (Map) ActionUtil.getApplication().getAttribute("users");
        // System.out.println(map);
        if (map != null && map.size() > 0) {
            User_inf user = (User_inf) ActionUtil.getSession().getAttribute("user");
            Long login_time = (Long) ActionUtil.getSession().getAttribute("login_time");
            if (user != null && login_time != null) {
                UserClient client = map.get(user.getUName());
                if (client != null) {
                    if (login_time == client.getLogin_times()) {
                        model.setCode(0);
                    }else {
                        model.setCode(1);
                        model.setMsg(getText("The landing on the account in another host, please log in again"));
                    }
                }else {
                    model.setCode(1);
                    model.setMsg(getText("You are not logged in, please log in"));
                }
            } else {
                model.setCode(1);
                model.setMsg(getText("You are not logged in, please log in"));
            }
        } else {
            model.setCode(1);
            model.setMsg(getText("You are not logged in, please log in"));
        }
        return model;
    }
 
    //修改用户密码
    public ServiceModel updatePassword(Object obj){
        User_inf uinf=(User_inf) obj;
        uinf.setUpassword((String)ActionUtil.EncryptionMD5(uinf.getUSnId()));
        //System.out.println(uinf);
        Boolean bl=((User_infImpl)dao).updatePassword(uinf);
        if(bl){
            model.setCode(1);
            model.setMsg("修改成功!");
        }
        else{
            model.setMsg("修改失败!");
        }
        return model;
    }
    
    //根据员工信息和员工编号去匹配合适的员工
    public ServiceModel searchByNameOrId(Object obj){
        List<User_inf> list=((User_infImpl)dao).searchByNameOrId(obj);
        
        if(list!=null&&list.size()>0){
            model.setCode(1);
            model.setData(list);
            model.setMsg("查询成功!");
        }
        else{
            model.setCode(0);
            model.setMsg("查询失败!");
        }
        return model;
    }
 
    //重置用户密码
    public ServiceModel resetPassword(Object obj){
        Boolean bl=((User_infImpl)dao).resetPassword(obj);
        if(bl){
            model.setCode(1);
            model.setMsg("修改成功!");
        }
        else{
            model.setMsg("修改失败!");
        }
        return model;
    }
    public static void main(String[] args) {
        User_infService us = new User_infService();
        User_inf u = new User_inf();
        u.setUName("a");
        u.setUEmployeeId("a");
        u.setUId(1003);
        Batt_User_Permit bup = new Batt_User_Permit();
        Page page = new Page();
        page.setPageSize(10);
        page.setPageCurr(1);
        bup.setPage(page);
        bup.setUinf(u);
 
        //User_battgroup_baojigroup_battgroup ub = new User_battgroup_baojigroup_battgroup();
        // ub.setBattGroupId(1000012);
        // us.searchByBattGroupId(ub);
        // us.serchByInfo(u);
        // System.out.println(us.searchAll(bup).getData());
        us.searchByNameOrId(bup);
    }
}