青羊经侦大队-数据平台
baizonghao
2023-03-23 0261d270c9f9b5282d923e585c307aee44b0612c
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
package com.example.jz.controller;
 
 
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.jz.auth.TokenJwtManager;
import com.example.jz.dao.UserDao;
import com.example.jz.modle.R;
import com.example.jz.modle.entity.*;
import com.example.jz.modle.vo.*;
import com.example.jz.service.*;
import com.example.jz.utils.HttpUtil;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
 
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
@RestController
@RequestMapping("/wx")
public class WxAppController {
 
    private final String wxApp_userCode_verify_url = "https://api.weixin.qq.com/sns/jscode2session";
 
    private final String wxApp_phoneNumber_verify_url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber";
 
    private final String wxApp_accessToken_verify_url = "https://api.weixin.qq.com/cgi-bin/token";
 
    @Value("${wx.appid}")
    private String appid;
 
    @Value("${wx.secret}")
    private String secret;
 
    private final String js_code = "";
 
    private final String grant_type = "authorization_code";
 
    @Autowired
    UserDao userDao;
    @Autowired
    UserService userService;
    @Autowired
    TokenJwtManager tokenJwtManager;
    @Autowired
    private RestTemplate restTemplate;
 
    @Autowired
    ReportService reportService;
    @Autowired
    CauseService causeService;
    @Autowired
    GroupService groupService;
    @Autowired
    MessageService messageService;
    @Autowired
    GroupUserService groupUserService;
    @Autowired
    PublicityService publicityService;
 
    @GetMapping("/checkIdentity")
    public R checkIdentity(@RequestParam String code, @RequestParam String phoneNumberCode) throws Exception {
        //小程序登录openid
        Map<String, Object> pathUrl = new HashMap<>();
        pathUrl.put("appid", appid);
        pathUrl.put("secret", secret);
        pathUrl.put("js_code", code);
        pathUrl.put("grant_type", grant_type);
        JSONObject result = JSONUtil.parseObj(HttpUtil.doGet(wxApp_userCode_verify_url, pathUrl));
 
        //手机号码的access token
        Map<String, Object> accessToken = new HashMap<>();
        accessToken.put("appid", appid);
        accessToken.put("secret", secret);
        accessToken.put("grant_type", "client_credential");
        JSONObject accessTokenResult = JSONUtil.parseObj(HttpUtil.doGet(wxApp_accessToken_verify_url, accessToken));
 
        //电话号码
        String url = wxApp_phoneNumber_verify_url + "?access_token=" + accessTokenResult.get("access_token");
        Map<String, Object> phoneNumber = new HashMap<>();
        phoneNumber.put("code", phoneNumberCode);
 
        HttpHeaders headers = new HttpHeaders();
        HttpEntity httpEntity = new HttpEntity<>(phoneNumber, headers);
        System.out.println(httpEntity);
        ResponseEntity response = restTemplate.postForEntity(url, httpEntity, Object.class);
        response.getBody();
        JSONObject entries = JSONUtil.parseObj(response.getBody());
        PhoneInFo phone_info = entries.get("phone_info", PhoneInFo.class);
        User user = userDao.selectOne(new QueryWrapper<User>().eq("user_mobile", phone_info.getPurePhoneNumber()));
        if(Objects.isNull(user)){
            return R.ok(2, phone_info.getPurePhoneNumber());
        }else {
            if (user.getRole() == 1){
                return R.ok(1, phone_info.getPurePhoneNumber());
            } else  {
                return R.ok(0, phone_info.getPurePhoneNumber());
            }
        }
    }
 
//    @GetMapping("/login")
//    @SneakyThrows
//    public R login(@RequestParam String code, @RequestParam String phoneNumberCode) {
//        //小程序登录openid
//        Map<String, Object> pathUrl = new HashMap<>();
//        pathUrl.put("appid", appid);
//        pathUrl.put("secret", secret);
//        pathUrl.put("js_code", code);
//        pathUrl.put("grant_type", grant_type);
//        JSONObject result = JSONUtil.parseObj(HttpUtil.doGet(wxApp_userCode_verify_url, pathUrl));
//
//        //手机号码的access token
//        Map<String, Object> accessToken = new HashMap<>();
//        accessToken.put("appid", appid);
//        accessToken.put("secret", secret);
//        accessToken.put("grant_type", "client_credential");
//        JSONObject accessTokenResult = JSONUtil.parseObj(HttpUtil.doGet(wxApp_accessToken_verify_url, accessToken));
//
//        //电话号码
//        String url = wxApp_phoneNumber_verify_url + "?access_token=" + accessTokenResult.get("access_token");
//        Map<String, Object> phoneNumber = new HashMap<>();
//        phoneNumber.put("code", phoneNumberCode);
//
//        HttpHeaders headers = new HttpHeaders();
//        HttpEntity httpEntity = new HttpEntity<>(phoneNumber, headers);
//        System.out.println(httpEntity);
//        ResponseEntity response = restTemplate.postForEntity(url, httpEntity, Object.class);
//        response.getBody();
//        JSONObject entries = JSONUtil.parseObj(response.getBody());
//        PhoneInFo phone_info = entries.get("phone_info", PhoneInFo.class);
//        User user = userDao.selectOne(new QueryWrapper<User>().eq("user_mobile", phone_info.getPurePhoneNumber()));
//        String token;
//        Integer role;
//        Integer id = null;
//        if (user == null || user.getId() == null) {
//            User newUser = new User();
//            newUser.setUserMobile(phone_info.getPurePhoneNumber());
//            newUser.setUserRegtime(new Date());
//            newUser.setOpenid(result.get("openid").toString());
//            newUser.setStatus(1);
//            newUser.setRole(0);
//            newUser.setCtime(new Date());
//            userService.save(newUser);
//            token = tokenJwtManager.createToken(newUser);
//            role = 0;
//            id = newUser.getId();
//        } else {
//            user.setOpenid(result.get("openid").toString());
//            user.setUserLasttime(new Date());
//            userDao.updateById(user);
//            token = tokenJwtManager.createToken(user);
//            role = user.getRole();
//            id = user.getId();
//        }
//        User user1 = userDao.selectOne(new QueryWrapper<User>().eq("user_mobile", phone_info.getPurePhoneNumber()));
//        HashMap<String, Object> loginMessage = new HashMap<>();
//        loginMessage.put("id", id);
//        loginMessage.put("token", token);
//        loginMessage.put("role", role);
//        loginMessage.put("time", user1.getUserIdcard());
//        return R.ok(loginMessage);
//    }
 
    @GetMapping("/login")
    @SneakyThrows
    public R login(@RequestParam String phone) {
        User user = userDao.selectOne(new QueryWrapper<User>().eq("user_mobile", phone));
        String token;
        Integer role;
        Integer id = null;
        if (user == null || user.getId() == null) {
            User newUser = new User();
            newUser.setUserMobile(phone);
            newUser.setUserRegtime(new Date());
            newUser.setOpenid(phone);
            newUser.setStatus(1);
            newUser.setRole(0);
            newUser.setCtime(new Date());
            userService.save(newUser);
            token = tokenJwtManager.createToken(newUser);
            role = 0;
            id = newUser.getId();
        } else {
            user.setUserLasttime(new Date());
            userDao.updateById(user);
            token = tokenJwtManager.createToken(user);
            role = user.getRole();
            id = user.getId();
        }
        User user1 = userDao.selectOne(new QueryWrapper<User>().eq("user_mobile", phone));
        HashMap<String, Object> loginMessage = new HashMap<>();
        loginMessage.put("id", id);
        loginMessage.put("token", token);
        loginMessage.put("role", role);
        loginMessage.put("time", user1.getUserIdcard());
        return R.ok(loginMessage);
    }
 
    @GetMapping("/manager/myself")
    public R getReport(Integer id) {
        HashMap<String, Object> myselfMessage = new HashMap<>();
        User user = userService.getOne(new QueryWrapper<User>().eq("id", id));
        int causeNum = causeService.count(new QueryWrapper<Cause>().eq("user_id", id));
        Integer reporterNum = causeService.list(new QueryWrapper<Cause>().eq("user_id", id)).stream().mapToInt(item -> reportService.count(new QueryWrapper<Report>().eq("cause_id", item.getId()))).sum();
        myselfMessage.put("name", user.getRealName());
        myselfMessage.put("phoneNum", user.getUserMobile());
        myselfMessage.put("causeNum", causeNum);
        myselfMessage.put("reporterNum", reporterNum);
        return R.ok(myselfMessage);
    }
 
    @GetMapping("/user/myself")
    public R getMyself(Integer id) {
        return R.ok(userService.getOne(new QueryWrapper<User>().eq("id", id)));
    }
 
    @GetMapping("/user/cause")
    public R getCause(Integer id) {
        List<Report> reports = reportService.list(new QueryWrapper<Report>().eq("user_id", id));
        List<ReportVXVO> res = reports.stream()
                .map(item -> {
                    ReportVXVO res1 = new ReportVXVO();
                    BeanUtils.copyProperties(item, res1);
                    if (!Objects.isNull(causeService.getById(item.getCauseId()))){
                        res1.setReportDescription(causeService.getById(item.getCauseId()).getName());
                    }
                    return res1;
                }).collect(Collectors.toList());
        return R.ok(res);
    }
 
    @PostMapping("/user/addReport")
    public R addReport(@RequestBody ReportVXVO reportVXVO) {
        User user = userService.getOne(new QueryWrapper<User>().eq("id", reportVXVO.getUserId()));
        user.setRealName(reportVXVO.getRealName());
        user.setSex(reportVXVO.getSex());
        user.setUserIdcard(reportVXVO.getUserIdCard());
        user.setLocation(reportVXVO.getLocation());
        user.setWorkingLocation(reportVXVO.getWorkingLocation());
 
        userService.update(user, new LambdaQueryWrapper<User>().eq(User::getId, user.getId()));
        Report report = new Report();
        BeanUtils.copyProperties(reportVXVO, report);
        report.setAmountInvolved(reportVXVO.getAmountInvolved());
        report.setPic(user.getPic());
        report.setIsInGroup(0);
        report.setCtime(new Date());
        report.setStatus(0);
        report.setReportMethod("扫码报案");
        return R.ok(reportService.save(report));
    }
 
    @PutMapping ("/user/addReport")
    public R addReport1(@RequestBody ReportVXVO reportVXVO) {
        User user = userService.getOne(new QueryWrapper<User>().eq("id", reportVXVO.getUserId()));
        user.setRealName(reportVXVO.getRealName());
        user.setSex(reportVXVO.getSex());
        user.setUserIdcard(reportVXVO.getUserIdCard());
        user.setLocation(reportVXVO.getLocation());
        user.setWorkingLocation(reportVXVO.getWorkingLocation());
 
        userService.update(user, new LambdaQueryWrapper<User>().eq(User::getId, user.getId()));
        Report report = new Report();
        BeanUtils.copyProperties(reportVXVO, report);
        report.setAmountInvolved(reportVXVO.getAmountInvolved());
        report.setPic(user.getPic());
        report.setIsInGroup(0);
        report.setCtime(new Date());
        report.setStatus(0);
        report.setReportMethod("扫码报案");
 
        reportService.update(report, new LambdaQueryWrapper<Report>().eq(Report::getId, report.getId()));
        return R.ok("修改成功");
    }
 
    @PostMapping("/user/add")
    public R addUser(@RequestBody User user) {
        user.setUserLasttime(new Date());
        return R.ok(userService.updateById(user));
    }
 
    @GetMapping("/manager/group/list")
    public R getMessage(@RequestParam Integer id, @RequestParam String newDate, @RequestParam String lastDate) {
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        List<Group> groupUsers = groupService.list(new QueryWrapper<Group>().eq("user_id", id));
        if (!groupUsers.isEmpty()) {
            groupUsers.forEach(item -> {
                HashMap<String, Object> groupMap = new HashMap<>();
                List<Message> message = messageService.list(new QueryWrapper<Message>().eq("group_id", item.getId()).orderByDesc("ctime"));
                groupMap.put("groupName", item.getGroupName());
                getMessage(groupMap, message);
                if (StringUtils.isNotBlank(lastDate)) {
                    int count = messageService.count(new QueryWrapper<Message>().eq("group_id", item.getId()).between("ctime", lastDate, newDate));
                    groupMap.put("count", count);
                }
                groupMap.put("id", item.getId());
                list.add(groupMap);
            });
        }
        return R.ok(list);
    }
 
    @GetMapping("/isExist")
    public R isExist(@RequestParam String phone){
        User one = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUserMobile, phone));
        if(Objects.isNull(one)){
            return R.ok(false, "该手机号没有被注册");
        }else {
            return R.ok(true, "该手机号已被注册");
        }
    }
 
    /**
     *  显示用户信息
     */
    @GetMapping("/getGroupUserInfo")
    public R getGroupUserInfo(@RequestParam Integer id){
        User one = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getId, id));
        MessageUserInfoVO messageUserInfoVO = new MessageUserInfoVO();
        BeanUtils.copyProperties(messageUserInfoVO, one);
        return R.ok(messageUserInfoVO);
    }
 
    private void getMessage(HashMap<String, Object> groupMap, List<Message> message) {
        if (!message.isEmpty()) {
            groupMap.put("message", message.get(0).getText());
            if (message.get(0).getUserId() == null) {
                groupMap.put("name", userDao.selectOne(new QueryWrapper<User>().eq("id", message.get(0).getCopId())).getRealName());
            } else {
                groupMap.put("name", message.get(0).getReportName());
            }
            groupMap.put("time", message.get(0).getCtime());
        }
    }
 
 
    @GetMapping("/user/group/list")
    public R getUserMessage(@RequestParam Integer id, @RequestParam String newDate, @RequestParam String lastDate) {
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        reportService.listGroup(id)
                .forEach(item ->
                        {
                            GroupUser groupUser = groupUserService.getOne(new QueryWrapper<GroupUser>().eq("user_id", item.getId()));
                            if (groupUser != null) {
                                HashMap<String, Object> groupMap = new HashMap<>();
                                List<Message> message = messageService.list(new QueryWrapper<Message>().eq("group_id", groupUser.getGroupId()).orderByDesc("ctime"));
                                getMessage(groupMap, message);
                                Group group = groupService.getOne(new QueryWrapper<Group>().eq("id", groupUser.getGroupId()));
                                groupMap.put("groupName", group.getGroupName());
                                groupMap.put("id", group.getId());
                                groupMap.put("ban", groupUser.getBanSpeech());
                                groupMap.put("reportId", groupUser.getUserId());
                                if (StringUtils.isNotBlank(lastDate)) {
                                    int count = messageService.count(new QueryWrapper<Message>().eq("group_id", groupUser.getGroupId()).between("ctime", lastDate, newDate));
                                    groupMap.put("count", count);
                                }
                                list.add(groupMap);
                            }
                        }
                );
 
        return R.ok(list);
    }
 
    @GetMapping("/message")
    public R getMessage(@RequestParam Integer groupId) {
        ArrayList<HashMap<String, Object>> message = new ArrayList<>();
        messageService.list(new QueryWrapper<Message>().eq("group_id", groupId).orderByAsc("ctime")).stream()
                .forEach(item -> {
                    HashMap<String, Object> messageMap = new HashMap<>();
                    messageMap.put("text", item.getText());
                    messageMap.put("time", item.getCtime());
                    if (item.getUserId() != null) {
                        Report report = reportService.getOne(new QueryWrapper<Report>().eq("id", item.getUserId()));
                        User user = userService.getOne(new QueryWrapper<User>().eq("id", report.getUserId()));
                        if (Objects.isNull(user)) {
                            return;
                        }
                        messageMap.put("id", user.getId());
                        messageMap.put("name", item.getReportName());
                        messageMap.put("mobile", user.getUserMobile());
                        messageMap.put("idcard", user.getUserIdcard());
                        messageMap.put("sex", user.getSex());
                        messageMap.put("pic", user.getPic());
                        messageMap.put("role", 0);
                    } else {
                        messageMap.put("id", item.getCopId());
                        User user = userService.getOne(new QueryWrapper<User>().eq("id", item.getCopId()));
                        if (Objects.isNull(user)) {
                            return;
                        }
                        messageMap.put("name", user.getRealName());
                        messageMap.put("mobile", user.getUserMobile());
                        messageMap.put("sex", user.getSex());
                        messageMap.put("role", 1);
                    }
                    message.add(messageMap);
                });
        return R.ok(message);
    }
 
    @GetMapping("/group/cause")
    public R getGroupCause(@RequestParam Integer id) {
        Integer causeId = groupService.getOne(new QueryWrapper<Group>().eq("id", id)).getCauseId();
        Cause cause = causeService.getOne(new QueryWrapper<Cause>().eq("id", causeId));
        CauseVo causeVo = new CauseVo();
        BeanUtils.copyProperties(cause, causeVo);
        User user = userService.getOne(new QueryWrapper<User>().eq("id", cause.getUserId()));
        causeVo.setUserName(user.getRealName());
        causeVo.setMobile(user.getUserMobile());
        causeVo.setReportNumber(reportService.count(new QueryWrapper<Report>().eq("cause_id", causeId)));
        return R.ok(causeVo);
    }
 
    @GetMapping("/group/member")
    public R getGroupMember(@RequestParam Integer id) {
        Map<String, Object> map = new HashMap<>();
        List<GroupUser> groupUserList = groupUserService.list(new QueryWrapper<GroupUser>().eq("group_id", id));
        List<MemberVo> memberVoList = groupUserList.stream()
                .map(item -> {
                    Report report = reportService.getOne(new QueryWrapper<Report>().eq("id", item.getUserId()));
                    User user = userService.getOne(new QueryWrapper<User>().eq("id", report.getUserId()));
                    MemberVo memberVo = new MemberVo();
                    memberVo.setRealName(user.getRealName());
                    memberVo.setPic(user.getPic());
                    memberVo.setId(item.getUserId());
                    memberVo.setBanSpeech(item.getBanSpeech());
                    return memberVo;
                }).collect(Collectors.toList());
        List<Group> groupList = groupService.list(new QueryWrapper<Group>().eq("id", id));
        List<MemberVo> managerMemberVoList = groupList.stream().map(item -> {
                    User user = userService.getOne(new QueryWrapper<User>().eq("id", item.getUserId()));
                    MemberVo memberVo = new MemberVo();
                    memberVo.setRealName(user.getRealName());
                    memberVo.setPic(user.getPic());
                    memberVo.setId(item.getUserId());
                    return memberVo;
                }
        ).collect(Collectors.toList());
        map.put("managerMemberList", managerMemberVoList);
        map.put("managerMemberCount", managerMemberVoList.size());
        map.put("memberList", memberVoList);
        map.put("memberCount", memberVoList.size());
        return R.ok(map);
    }
 
    @GetMapping("/group/member/find")
    public R getGroupMemberByName(@RequestParam(required = false) String name, @RequestParam Integer id) {
        Map<String, Object> map = new HashMap<>();
        List<GroupUser> groupUserList = groupUserService.list(new QueryWrapper<GroupUser>().eq("group_id", id));
        List<MemberVo> memberVoList = groupUserList.stream()
                .filter(item ->
                        StringUtils
                                .matches(userService.getOne(new QueryWrapper<User>()
                                        .eq("id", reportService.getOne(new QueryWrapper<Report>()
                                                .eq("id", item.getUserId())).getUserId())).getRealName(), name))
                .map(item -> {
                    Report report = reportService.getOne(new QueryWrapper<Report>().eq("id", item.getUserId()));
                    User user = userService.getOne(new QueryWrapper<User>().eq("id", report.getUserId()));
                    MemberVo memberVo = new MemberVo();
                    memberVo.setRealName(user.getRealName());
                    memberVo.setPic(user.getPic());
                    memberVo.setId(item.getUserId());
                    memberVo.setBanSpeech(item.getBanSpeech());
                    return memberVo;
                }).collect(Collectors.toList());
        map.put("memberList", memberVoList);
        map.put("memberCount", memberVoList.size());
        return R.ok(map);
    }
 
    @GetMapping("/group/member/banSpeech")
    public R banSpeech(@RequestParam Integer id, @RequestParam Integer groupId) {
        return R.ok(groupUserService.update(new UpdateWrapper<GroupUser>().set("ban_speech", 1).eq("user_id", id).eq("group_id", groupId)));
    }
 
    @GetMapping("/group/member/allBanSpeech")
    public R allBanSpeech(@RequestParam Integer id) {
        return R.ok(groupUserService.update(new UpdateWrapper<GroupUser>().set("ban_speech", 1).eq("group_id", id)));
    }
 
    @GetMapping("/group/member/allowSpeech")
    public R allowSpeech(@RequestParam Integer id, @RequestParam Integer groupId) {
        return R.ok(groupUserService.update(new UpdateWrapper<GroupUser>().set("ban_speech", 0).eq("user_id", id).eq("group_id", groupId)));
    }
 
    @GetMapping("/group/member/allAllowSpeech")
    public R allAllowSpeech(@RequestParam Integer id) {
        return R.ok(groupUserService.update(new UpdateWrapper<GroupUser>().set("ban_speech", 0).eq("group_id", id)));
    }
 
    @GetMapping("/group/records/keyword")
    public R getRecords(@RequestParam String keyWord, @RequestParam Integer groupId) {
        return R.ok(messageService.list(new QueryWrapper<Message>().eq("group_id", groupId).like(StringUtils.isNotBlank(keyWord), "text", keyWord).isNotNull("user_id"))
                .stream().map(item -> {
                    Report report = reportService.getOne(new QueryWrapper<Report>().eq("id", item.getUserId()));
                    String pic = report.getPic();
                    if (StringUtils.isBlank(pic)) {
                        pic = userService.getOne(new QueryWrapper<User>().eq("id", report.getUserId())).getPic();
                    }
                    MemberVo memberVo = new MemberVo();
                    memberVo.setCtime(item.getCtime());
                    memberVo.setText(item.getText());
                    memberVo.setPic(pic);
                    memberVo.setRealName(item.getReportName());
                    return memberVo;
                }).collect(Collectors.toList()));
    }
 
    @GetMapping("/group/records/member")
    public R getRecords(@RequestParam Integer id, @RequestParam Integer groupId) {
        return R.ok(messageService.list(new QueryWrapper<Message>().eq("group_id", groupId).eq("user_id", id))
                .stream().map(item -> {
                    Report report = reportService.getOne(new QueryWrapper<Report>().eq("id", item.getUserId()));
                    String pic = report.getPic();
                    if (StringUtils.isBlank(pic)) {
                        pic = userService.getOne(new QueryWrapper<User>().eq("id", report.getUserId())).getPic();
                    }
                    MemberVo memberVo = new MemberVo();
                    memberVo.setCtime(item.getCtime());
                    memberVo.setText(item.getText());
                    memberVo.setPic(pic);
                    memberVo.setRealName(item.getReportName());
                    return memberVo;
                }).collect(Collectors.toList()));
    }
 
    @GetMapping("/group/manager/sendMsg")
    public R sendMsg(@RequestParam Integer id, @RequestParam Integer groupId, @RequestParam String msg) {
        Message message = new Message();
        message.setCopId(id);
        message.setText(msg);
        message.setCtime(new Date());
        message.setGroupId(groupId);
        return R.ok(messageService.save(message));
    }
 
 
 
    @GetMapping("/group/user/sendMsg")
    public R sendUserMsg(@RequestParam Integer id, @RequestParam Integer groupId, @RequestParam String msg) {
        if (StringUtils.isBlank(msg)) {
            return R.failed("msg is blank");
        }
        GroupUser one = groupUserService.getOne(new LambdaQueryWrapper<GroupUser>().eq(GroupUser::getUserId, id).eq(GroupUser::getGroupId, groupId));
        if (one.getBanSpeech() == 1){
            return R.failed("该用户已经被禁言");
        }
        Message message = new Message();
        message.setUserId(id);
        message.setText(msg);
        message.setCtime(new Date());
        message.setGroupId(groupId);
        message.setReportName(userService.getOne(new QueryWrapper<User>().eq("id", reportService.getOne(new QueryWrapper<Report>().eq("id", id)).getUserId())).getRealName());
        return R.ok(messageService.save(message));
    }
 
    @GetMapping("/group/member/detail")
    public R getMemberDetail(@RequestParam Integer id) {
        Report report = reportService.getOne(new LambdaQueryWrapper<Report>().eq(Report::getId, id));
        ReportVo reportVo = new ReportVo();
        BeanUtils.copyProperties(report, reportVo);
        User user = userDao.selectOne(new LambdaQueryWrapper<User>().eq(User::getId, report.getUserId()));
        reportVo.setReporterName(user.getRealName());
        reportVo.setMobile(user.getUserMobile());
        reportVo.setIdcard(user.getUserIdcard());
        return R.ok(reportVo);
    }
 
    @GetMapping("/manager/report")
    public R queryReport() {
        return R.ok(reportService.list(new LambdaQueryWrapper<Report>().eq(Report::getStatus, 0))
                .stream()
                .map(item -> {
                    ReportVo reportVo = new ReportVo();
                    BeanUtils.copyProperties(item, reportVo);
                    User user = userDao.selectOne(new LambdaQueryWrapper<User>().eq(User::getId, item.getUserId()));
                    Cause one = causeService.getOne(new LambdaQueryWrapper<Cause>().eq(Cause::getId, item.getCauseId()));
                    reportVo.setReportDescription(one.getName());
                    reportVo.setReporterName(user.getRealName());
                    reportVo.setMobile(user.getUserMobile());
                    reportVo.setIdcard(user.getUserIdcard());
                    return reportVo;
                })
                .collect(Collectors.toList()));
    }
 
    @GetMapping("/publicity")
    public R queryPublicity(@RequestParam(required = false) Integer size) {
        List<Publicity> publicityList = publicityService.list(new LambdaQueryWrapper<Publicity>().eq(Publicity::getStatus, 1));
        if (size != null) {
            return R.ok(publicityList.stream().limit(size));
        }
        return R.ok(publicityList);
    }
}