forked from xiaowei1118/java_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserTest.java
More file actions
710 lines (631 loc) · 19 KB
/
userTest.java
File metadata and controls
710 lines (631 loc) · 19 KB
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
package com.changyu.test;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.changyu.foryou.model.Feedback;
import com.changyu.foryou.model.Users;
import com.changyu.foryou.service.OrderService;
import com.changyu.foryou.service.UserService;
import com.changyu.foryou.tools.Constants;
import com.changyu.foryou.tools.Md5;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring.xml", "classpath:spring-mybatis.xml" })
public class userTest {
private static final Logger LOGGER = Logger
.getLogger(userTest.class);
private UserService userService;
private OrderService orderService;
public OrderService getOrderService() {
return orderService;
}
@Autowired
public void setOrderService(OrderService orderService) {
this.orderService = orderService;
}
public UserService getUserservice() {
return userService;
}
@Autowired
public void setUserservice(UserService userservice) {
this.userService = userservice;
}
//取得用户信息
public void selectUserById() {
//case1
// String phone = "18896554880";
// case2
String phone = "18860902573";
LOGGER.debug("用户信息为:==================="+JSON.toJSONString(userService.selectByUsername(phone)));
return ;
}
// 将某一用户设置为管理员
public void setUserAdmin(){
// case1
// String phone = "18896554880";
// case2
String phone = "18860902573";
LOGGER.debug("==================="+JSON.toJSONString(userService.setUserAdmin(phone,2)));
return ;
}
/**
* 将某一用户设为常规用户
* @param phone
* @return
*/
@Test
public void setUserCommon(){
// case1
// String phone = "18896554880";
// case2
String phone = "18860902573";
LOGGER.debug("==================="+JSON.toJSONString(userService.setUserCommon(phone,2)));
return ;
}
/**
* 将某一用户设置为超级管理员
* @param phone
* @return
*/
public void setUserSuperAdmin(){
// case1
String phone = "18896554880";
// case2
// String phone = "18860902573";
LOGGER.debug("==================="+JSON.toJSONString(userService.setUserSuperAdmin(phone,2)));
return ;
}
/**
* 取得所有用戶信息
* @return
*/
public
void getAllUser() {
Map<String, Object> map = new HashMap<String, Object>();
Integer limit=null;
Integer offset=null;
// case1
// String order="desc";
// String sort="lastLoginDate";
// String search="188";
// case2
// String order="desc";
// String sort="lastLoginDate";
// String search="小未";
// case3
// String order="desc";
// String sort="lastLoginDate";
// String search="小未11111111";
// case4
// String order="desc";
// String sort="createTime";
// String search="188";
// case5
// String order="desc";
// String sort="defaultAddress";
// String search="188";
// case6
// String order="desc";
// String sort=null;
// String search="188";
// case7
// String order="desc";
// String sort="11111";
// String search="188";
// case8
// String order="asc";
// String sort="createTime";
// String search="188";
// case9
String order="111";
String sort="createTime";
String search="188";
if(sort!=null&&sort.equals("lastLoginDate")){
sort="last_login_date";
}
if(sort!=null&&sort.equals("createTime")){
sort="create_time";
}
if(sort!=null&&sort.equals("defaultAddress")){
sort="default_address";
}
if(search!=null&&!search.trim().equals("")){
search="%"+search+"%";
}
JSONArray json=JSONArray.parseArray(JSON.toJSONStringWithDateFormat(userService.getAllUser(limit,offset,sort,order,search), "yyyy-MM-dd"));
map.put("total", userService.getUserCount(search));
map.put("rows", json);
LOGGER.debug("==================="+JSON.toJSONString(map)+"======================================================");
return ;
}
/**
* 用户登陆
* @param phone
* @param password
* @return
*/
public void toLogin() {
String phone=null;
String password=null;
Map<String, Object> map = new HashMap<String, Object>();
// case1
// phone= "18896554880";
// password="123456789";
// case2
// phone= "1889655488";
// password="123456789";
// case3
phone= "18896554880";
password="12345678";
if (phone!=null&&password!=null&&!phone.trim().equals("") && !password.trim().equals("")) {
Users users = userService.selectByUsername(phone);
if (users != null) {
if (users.getPassword().equals(Md5.GetMD5Code(password))) {
map.put(Constants.STATUS, Constants.SUCCESS);
map.put(Constants.MESSAGE, "登陆成功");
map.put("type", users.getType());
// HttpSession session=request.getSession();
// session.setAttribute("type", users.getType());
// session.setAttribute("phone", users.getPhone());
Date date=new Date();
userService.updateLastLoginTime(date,phone);
} else {
map.put(Constants.STATUS, Constants.FAILURE);
map.put(Constants.MESSAGE, "账号或密码错误,请检查后输入");
}
} else {
map.put(Constants.STATUS, Constants.FAILURE);
map.put(Constants.MESSAGE, "账号或密码错误,请检查后输入");
}
}
LOGGER.debug("==================="+JSON.toJSONString(map)+"======================================================");
return ;
}
/**
* 检查用户是否已经注册
* @param phone
* @return
*/
public void checkUserIsExist() {
String phone = null;
Map<String, Object> map = new HashMap<String, Object>();
// case1
// phone="18896554880";
// case2
phone="18896554881";
if (userService.selectByUsername(phone) != null) {
map.put(Constants.STATUS, Constants.FAILURE);
map.put(Constants.MESSAGE, "该账号已经被注册");
} else {
map.put(Constants.STATUS, Constants.SUCCESS);
map.put(Constants.MESSAGE, "该账号可以注册");
}
LOGGER.debug("==================="+JSON.toJSONString(map)+"======================================================");
return ;
}
/**
* 用户注册
* @param phone
* @param password
* @param nickname
* @return
*/
public void registerIn() {
String phone=null;
String password=null;
String nickname=null;
Map<String, Object> map = new HashMap<String, Object>();
// case1
// phone="18812345678";
// password="1111";
// nickname="11";
// case2
// phone="188145678";
// password="1111";
// nickname="11";
// case3
// password="1111";
// nickname="11";
// case4
// phone="18812345678";
// nickname="11";
// case5
phone="18812345678";
password="1111";
try {
if (!phone.equals("") && phone.length() == 11
&& !password.equals("")) {
String passwordMd5=Md5.GetMD5Code(password);
Users users = new Users(phone, passwordMd5, nickname);
userService.addUsers(users);
map.put(Constants.STATUS, Constants.SUCCESS);
map.put(Constants.MESSAGE, "注册成功");
} else {
map.put(Constants.STATUS, Constants.FAILURE);
map.put(Constants.MESSAGE, "注册失败");
}
} catch (Exception e) {
map.put(Constants.STATUS, Constants.FAILURE); // 捕捉异常
map.put(Constants.MESSAGE, "注册失败");
LOGGER.debug("==================="+JSON.toJSONString(map)+"======================================================");
return ;
}
LOGGER.debug("==================="+JSON.toJSONString(map)+"======================================================");
return ;
}
/** 用户更改密码
* @param phone
* @param newPassword
* @return
*/
public void resetPassword() {
// case1
// String phone="18896554880";
// String newPassword="123456789";
String phone="18896554888";
String newPassword="123456789";
Map<String, Object> map = new HashMap<String, Object>();
try {
Users users = userService.selectByUsername(phone);
if (users != null) {
userService.updatePassword(phone,Md5.GetMD5Code(newPassword)); //对密码做md5加密
map.put(Constants.STATUS, Constants.SUCCESS);
map.put(Constants.MESSAGE, "修改密码成功");
} else {
map.put(Constants.STATUS, Constants.FAILURE);
map.put(Constants.MESSAGE, "修改密码失败");
}
} catch (Exception e) {
e.getStackTrace();
map.put(Constants.STATUS, Constants.FAILURE);
map.put(Constants.MESSAGE, "修改密码失败");
}
LOGGER.debug("==================="+JSON.toJSONString(map)+"======================================================");
return;
}
/**
* 更新用户个人信息
* @param phone
* @param password
* @param nickname
* @param type
* @return
*/
public void updateUserInfo(){
Map<String, Object> map = new HashMap<String, Object>();
// case1
// String phone="18896554880";
// String nickname="我是小未";
// String type="1";
// String sex="1";
// String academy="计算机";
// String qq="1111111111";
// String weiXin="111";
// case2
// String phone="18896554888";
// String nickname="我是小未";
// String type="1";
// String sex="1";
// String academy="计算机";
// String qq="1111111111";
// String weiXin="111";
// case3
// String phone="18896554880";
// String nickname=null;
// String type="1";
// String sex="1";
// String academy="计算机";
// String qq="1111111111";
// String weiXin="111";
// case4:
// String phone="18896554880";
// String nickname="我是小未";
// String type="3";
// String sex="1";
// String academy="计算机";
// String qq="1111111111";
// String weiXin="111";
// CASE5
// String phone="18896554880";
// String nickname="我是小未";
// String type="2";
// String sex=null;
// String academy="计算机";
// String qq="1111111111";
// String weiXin="111";
// case6
// String phone="18896554880";
// String nickname="我是小未";
// String type="2";
// String sex="3";
// String academy="计算机";
// String qq="1111111111";
// String weiXin="111";
// case7
// String phone="18896554880";
// String nickname="我是小未";
// String type="1";
// String sex="1";
// String academy=null;
// String qq="1111111111";
// String weiXin="111";
// case8
// String phone="18896554880";
// String nickname="我是小未";
// String type="1";
// String sex="1";
// String academy="计算机";
// String qq=null;
// String weiXin="111";
// case9
// String phone="18896554880";
// String nickname="我是小未";
// String type="1";
// String sex="1";
// String academy="计算机";
// String qq="1ss1111111";
// String weiXin="111";
// case10
String phone="18896554880";
String nickname="我是小未";
String type="1";
String sex="1";
String academy="计算机";
String qq="1111111111";
String weiXin=null;
try {
Users users=new Users();
users.setPhone(phone);
if(weiXin!=null)
{
users.setWeiXin(weiXin);
}
if(qq!=null)
{
users.setQq(qq);
}
if(academy!=null)
{
users.setAcademy(academy);
}
if(sex!=null){
users.setSex((short)Integer.parseInt(sex));
}
if(nickname!=null){
users.setNickname(nickname);
}
if(type!=null){
users.setType((short)Integer.parseInt(type));
}
if(userService.updateUserInfo(users)!=-1){
map.put(Constants.STATUS, Constants.SUCCESS);
map.put(Constants.MESSAGE, "修改用户信息成功");
}else{
map.put(Constants.STATUS, Constants.FAILURE);
map.put(Constants.MESSAGE, "修改用户信息失败");
}
} catch (Exception e) {
e.printStackTrace();
map.put(Constants.STATUS, Constants.FAILURE);
map.put(Constants.MESSAGE, "修改用户信息失败");
}
LOGGER.debug("==================="+JSON.toJSONString(map)+"======================================================");
return ;
}
public void getMineInfo(){
// case1
// String phone="18896554880";
// case2
String phone="18896554888";
Map<String, Object> map = new HashMap<String, Object>();
try {
Map<String, Object> paramMap = new HashMap<String, Object>();
Users users=userService.selectByUsername(phone);
Map<String, Object> counts=orderService.getOrderSummaryCount(phone);
paramMap.put("phoneId", phone);
paramMap.put("status", 1);
List<String> togetherId1=orderService.getTogetherId(paramMap);
paramMap.put("status", 2);
List<String> togetherId2=orderService.getTogetherId(paramMap);
paramMap.put("status", 3);
List<String> togetherId3=orderService.getTogetherId(paramMap);
paramMap.put("status", 4);
List<String> togetherId4=orderService.getTogetherId(paramMap);
paramMap.put("status", 5);
List<String> togetherId5=orderService.getTogetherId(paramMap);
if(users!=null){
users.setPassword(null);
map.put("userInfo", users);
//map.put("waitDeliveryOrder", counts.get("wait"));
//map.put("waitReceiveOrder",counts.get("deliver"));
//map.put("waitCommentOrder", counts.get("comment"));
map.put(Constants.STATUS,Constants.SUCCESS);
map.put(Constants.MESSAGE, "获取数据成功");
map.put("waitPayOrder", togetherId1.size());
map.put("waitMakeSureOrder", togetherId2.size());
map.put("distribution", togetherId3.size());
map.put("waitCommentOrder", togetherId4.size());
map.put("doneOrder", togetherId5.size());
}
} catch (Exception e) {
e.printStackTrace();
map.put(Constants.MESSAGE, "获取用户信息失败!");
map.put(Constants.STATUS, Constants.FAILURE);
}
LOGGER.debug("==================="+JSON.toJSONString(map)+"======================================================");
return ;
}
/**
* 提交用户反馈信息
* @param phoneId
* @param suggestion
* @return
*/
public void feedbackMessage(){
Map<String, Object> map = new HashMap<String, Object>();
//// case1
// Integer campusId=1;
// String phoneId="18896554880";
// String suggestion="你们的网站做得很好";
// case2
// Integer campusId=s;
// String phoneId="18896554880";
// String suggestion="你们的网站做得很好";
//// case3
// Integer campusId=1;
// String phoneId="18896554888";
// String suggestion="你们的网站做得很好";
// case4
Integer campusId=1;
String phoneId="18896554888";
String suggestion="";
try{
Feedback feedback=new Feedback();
feedback.setCampusId(campusId);
feedback.setPhoneId(phoneId);
feedback.setSuggestion(suggestion);
Calendar calendar=Calendar.getInstance();
Date date=calendar.getTime(); //设置反馈时的日期
feedback.setDate(date);
if(userService.addFeedbackSuggestion(feedback)!=-1){
map.put(Constants.STATUS, Constants.SUCCESS);
map.put(Constants.MESSAGE, "添加意见成功");
}else{
map.put(Constants.STATUS, Constants.FAILURE);
map.put(Constants.MESSAGE, "添加意见失败");
}
}catch(Exception e){
e.getStackTrace();
map.put(Constants.STATUS, Constants.FAILURE);
map.put(Constants.MESSAGE, "添加意见失败");
}
LOGGER.debug("==================="+JSON.toJSONString(map)+"======================================================");
return ;
}
/**
* 获取配送员名单
* @return
*/
public void getDeliverAdmin() {
Map<String, Object> map=new HashMap<String, Object>();
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("campusId", 1);
try{
List<Users> users=userService.getDeliverAdmin(paramMap);
map.put(Constants.STATUS, Constants.SUCCESS);
map.put(Constants.MESSAGE, "获取配送员成功!");
map.put("deliverAdmins", users);
}catch(Exception e){
e.printStackTrace();
map.put(Constants.STATUS, Constants.FAILURE);
map.put(Constants.MESSAGE, "获取配送员失败!");
}
LOGGER.debug("==================="+JSON.toJSONString(map)+"======================================================");
return ;
}
/**
* ios post token到数据表
* @param phoneId
* @param token
* @return
*/
/**
* 获取用户设备占比
*/
public void getUserDevicePercent(){
Map<String, Object> resultMap=new HashMap<String, Object>();
try {
DecimalFormat df = new DecimalFormat("##.0");
Map<String,Object> paramMap=new HashMap<String,Object>();
paramMap.put("device","0"); //设备号0表示ios
Integer iosCount=userService.getCountsByDevice(paramMap);
paramMap.put("device","1");
Integer androidCount=userService.getCountsByDevice(paramMap);
resultMap.put("android",Float.valueOf(df.format(androidCount*1.0/(androidCount+iosCount)*100)));
resultMap.put("ios",Float.valueOf(df.format(iosCount*1.0/(androidCount+iosCount)*100)));
} catch (Exception e) {
e.getStackTrace();
}
LOGGER.debug("==================="+JSON.toJSONString(resultMap)+"======================================================");
return ;
}
/**
* 获取用户反馈
* @return
*/
public void getFeedbacks(){
try {
Integer campusId=2;
Map<String, Object>paramMap=new HashMap<String, Object>();
paramMap.put("campusId", campusId);
List<Feedback> feedbacks=userService.getFeedbacks(paramMap);
System.out.println(JSONArray.parseArray(JSON.toJSONStringWithDateFormat(feedbacks, "yyyy-MM-dd")));
} catch (Exception e) {
e.printStackTrace();
}
return ;
}
@Test
/**
* 用户更改密码(用老密码更改)
* @param phone
* @param oldPassword
* @param newPassword
* @return
*/
public void changePassword()
{
// case1
// String phone="18896554880";
// String oldPassword="123456789";
// String newPassword="123456789";
//// case2
// String phone="18896554888";
// String oldPassword="123456789";
// String newPassword="123456789";
// case3
String phone="18896554880";
String oldPassword="1234567";
String newPassword="123456789";
Map<String, Object> map=new HashMap<String, Object>();
try {
Map<String, Object> paramMap=new HashMap<String, Object>();
paramMap.put("phone",phone);
String passwordMd5=Md5.GetMD5Code(oldPassword);
paramMap.put("password",passwordMd5);
List<Users> users=userService.selectByPhoneAndPassword(paramMap);
if(users.size()==0)
{
map.put(Constants.STATUS, Constants.FAILURE);
map.put(Constants.MESSAGE, "更改失败,原密码错误");
}
else
{
userService.updatePassword(phone,Md5.GetMD5Code(newPassword)); //对密码做md5加密
map.put(Constants.STATUS, Constants.SUCCESS);
map.put(Constants.MESSAGE, "修改密码成功");
}
} catch (Exception e) {
e.printStackTrace();
map.put(Constants.STATUS, Constants.FAILURE);
map.put(Constants.MESSAGE, "修改密码失败");
}
LOGGER.debug("==================="+JSON.toJSONString(map)+"======================================================");
return ;
}
}