无关风月
2025-02-08 bbc55de9bb0f6e5d3d8267c628d25780c19ebf36
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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
package com.xinquan.user.service.impl;
 
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinquan.common.core.constant.Constants;
import com.xinquan.common.core.constant.SecurityConstants;
import com.xinquan.common.core.domain.R;
import com.xinquan.common.core.enums.AnswerTagEnum;
import com.xinquan.common.core.enums.AppUserStatusEnum;
import com.xinquan.common.core.enums.DisabledEnum;
import com.xinquan.common.core.enums.TreeLevelEnum;
import com.xinquan.common.core.exception.ServiceException;
import com.xinquan.common.core.utils.page.BeanUtils;
import com.xinquan.common.core.utils.page.CollUtils;
import com.xinquan.common.security.service.TokenService;
import com.xinquan.common.security.utils.SecurityUtils;
import com.xinquan.meditation.api.feign.RemoteMeditationService;
import com.xinquan.system.api.RemoteUserService;
import com.xinquan.system.api.domain.SysUser;
import com.xinquan.system.api.feignClient.SysUserClient;
import com.xinquan.system.api.model.*;
import com.xinquan.system.api.domain.AppUser;
import com.xinquan.user.api.domain.dto.AppUserDTO;
import com.xinquan.system.api.domain.AppUserQuestion;
import com.xinquan.system.api.domain.AppUserTree;
import com.xinquan.system.api.domain.Tag;
import com.xinquan.user.domain.dto.UserAnswerDTO;
import com.xinquan.system.api.domain.vo.AppUserVO;
import com.xinquan.user.domain.vo.TagVO;
import com.xinquan.user.mapper.AppUserMapper;
import com.xinquan.user.service.AppUserQuestionService;
import com.xinquan.user.service.AppUserService;
import com.xinquan.user.service.AppUserTreeService;
import com.xinquan.user.service.TagService;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
 
import javax.annotation.Resource;
 
/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author mitao
 * @since 2024-08-21
 */
@Service
@RequiredArgsConstructor
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements AppUserService {
 
    private final SysUserClient sysUserClient;
    private final RemoteUserService remoteUserService;
    private final AppUserTreeService appUserTreeService;
    private final AppUserQuestionService appUserQuestionService;
    private final TagService tagService;
    @Resource
    private RemoteMeditationService remoteMeditationService;
    /**
     * 获取用户信息
     *
     * @param dto
     * @return
     */
    @Override
    public Optional<AppUser> getUserByCondition(AppUserDTO dto) {
        String device = dto.getDevice();
        if (StringUtils.hasLength(dto.getDevice())) {
            Optional<AppUser> one = this.lambdaQuery()
                    .eq(AppUser::getDevice, device)
                    .isNull(AppUser::getCellPhone).oneOpt();
            Optional<AppUser> appUser = this.lambdaQuery()
                    .eq(Objects.nonNull(dto.getId()), AppUser::getId, dto.getId())
                    .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getAppleId()), AppUser::getAppleId, dto.getAppleId())
                    .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getCellPhone()), AppUser::getCellPhone,
                            dto.getCellPhone())
                    .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getWxOpenId()), AppUser::getWxOpenId,
                            dto.getWxOpenId())
                    .ne(AppUser::getUserStatus, AppUserStatusEnum.LOGOUT.getCode()).oneOpt();
            // 如果不存在
            if (!appUser.isPresent()){
                if (one.isPresent()) {
                    initUserTree(one.get().getId());
                    this.updateById(one.get());
                    return one;
                } else {
                    return this.lambdaQuery()
                            .eq(Objects.nonNull(dto.getId()), AppUser::getId, dto.getId())
                            .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getAppleId()), AppUser::getAppleId, dto.getAppleId())
                            .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getCellPhone()), AppUser::getCellPhone,
                                    dto.getCellPhone())
                            .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getWxOpenId()), AppUser::getWxOpenId,
                                    dto.getWxOpenId())
                            .ne(AppUser::getUserStatus, AppUserStatusEnum.LOGOUT.getCode()).oneOpt();
                }
            }
 
        }
        return this.lambdaQuery()
                .eq(Objects.nonNull(dto.getId()), AppUser::getId, dto.getId())
                .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getAppleId()), AppUser::getAppleId, dto.getAppleId())
                .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getCellPhone()), AppUser::getCellPhone,
                        dto.getCellPhone())
                .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getWxOpenId()), AppUser::getWxOpenId,
                        dto.getWxOpenId())
                .ne(AppUser::getUserStatus, AppUserStatusEnum.LOGOUT.getCode()).oneOpt();
    }
    @Override
    public Optional<AppUser> getUserByCondition1(AppUserDTO dto) {
        return this.lambdaQuery()
                .eq(AppUser::getCellPhone,dto.getCellPhone())
                .ne(AppUser::getUserStatus,3).oneOpt();
    }
 
 
    /**
     * app用户注册
     * @param appUserDTO
     * @return
     */
    @Override
    public AppUser registerAppUser(AppUserDTO appUserDTO) {
        if (StringUtils.hasLength(appUserDTO.getDevice())){
            AppUser one = this.lambdaQuery().eq(AppUser::getDevice, appUserDTO.getDevice())
                    .isNull(AppUser::getCellPhone).one();
            if (one == null){
                AppUser appUser = BeanUtils.copyBean(appUserDTO, AppUser.class);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                String format = simpleDateFormat.format(new Date());
                // 生成四位随机数字
                String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
                appUser.setCode("YH"+format+randomCode);
                this.save(appUser);
                remoteMeditationService.addHomeBackgroundMusicDefault(appUser.getId());
                initUserTree(appUser.getId());
                return appUser;
            }else{
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                String format = simpleDateFormat.format(new Date());
                // 生成四位随机数字
                String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
                one.setCode("YH"+format+randomCode);
                this.updateById(one);
                initUserTree(one.getId());
                return one;
            }
        }
        AppUser appUser = BeanUtils.copyBean(appUserDTO, AppUser.class);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = simpleDateFormat.format(new Date());
        // 生成四位随机数字
        String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
        appUser.setCode("YH"+format+randomCode);
        this.save(appUser);
        remoteMeditationService.addHomeBackgroundMusicDefault(appUser.getId());
 
        initUserTree(appUser.getId());
        return appUser;
    }
 
    /**
     * APP微信登录
     *
     * @param body
     * @return
     */
    @Override
    public AppLoginUser wxLogin(AppWXLoginBody body) {
        AppLoginUser appLoginUser = new AppLoginUser();
        Optional<AppUser> optionalAppUser = this.getUserByCondition(
                AppUserDTO.builder().wxOpenId(body.getWxOpenId()).device(body.getDevice()).build());
        AppUser appUser = new AppUser();
        SysUser sysUser = new SysUser();
        // 存在账户,查询关联系统用户
        if (optionalAppUser.isPresent()) {
            appUser = optionalAppUser.get();
            if (appUser.getUserStatus().equals(AppUserStatusEnum.FROZEN.getCode())) {
                throw new ServiceException("账号已被冻结");
            }
            sysUser = sysUserClient.getSysUser(appUser.getUserId()).getData();
            appUser.setAvatar(body.getHeadImgUrl());
            appUser.setWxName(body.getNickname());
            if (appUser.getCellPhone()!=null){
                appLoginUser.setBindStatus(DisabledEnum.NO.getCode());
                appLoginUser.setCellPhone(appUser.getCellPhone());
            }
        } else {
            if (org.springframework.util.StringUtils.hasLength(body.getDevice())){
                AppUser one = this.getOne(new LambdaQueryWrapper<>(AppUser.class)
                        .eq(AppUser::getDevice, body.getDevice())
                        .isNull(AppUser::getCellPhone));
                if (one!=null){
                    initUserTree(appUser.getId());
                    appUser = one;
                }else{
                    // 不存在账户,创建账户
                    // 注册用户信息
                    sysUser = new SysUser();
                    sysUser.setNickName(body.getNickname());
                    sysUser.setAvatar(body.getHeadImgUrl());
                    sysUser.setUserName(body.getNickname());
                    sysUser.setUserType("01"); // app用户
                    R<SysUser> registerResult = remoteUserService.registerAppUserInfo(sysUser,
                            SecurityConstants.INNER);
                    if (registerResult.getCode() == Constants.FAIL) {
                        throw new ServiceException(registerResult.getMsg());
                    }
                    SysUser sysUserRes = registerResult.getData();
                    appUser = new AppUser();
                    appUser.setUserId(sysUserRes.getUserId());
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                    String format = simpleDateFormat.format(new Date());
                    // 生成四位随机数字
                    String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
                    appUser.setCode("YH"+format+randomCode);
                    appUser.setWxOpenId(body.getWxOpenId());
                    String nickname = "用户" + IdUtil.fastSimpleUUID().substring(0, 6);
                    sysUser.setNickName(nickname);
                    appUser.setNickname(nickname);
                    appUser.setDevice(body.getDevice());
                    appUser.setWxName(body.getNickname());
                    appUser.setAvatar(body.getHeadImgUrl());
                    appUser.setGender(3);
                    appUser.setBalance(BigDecimal.ZERO);
                    appUser.setSanskritFlag(DisabledEnum.NO.getCode());
                    appUser.setBalance(BigDecimal.ZERO);
                    appUser.setIncome(BigDecimal.ZERO);
                    appUser.setTotalEnergyValue(0);
                    appUser.setRegisterTime(LocalDateTime.now());
                    appUser.setLevelSettingId(TreeLevelEnum.SEED.getCode());
                    appLoginUser.setBindStatus(DisabledEnum.YES.getCode());
                    this.save(appUser);
                    remoteMeditationService.addHomeBackgroundMusicDefault(appUser.getId());
 
                    // 初始化用户树苗
                    initUserTree(appUser.getId());
                }
            }else{
                // 不存在账户,创建账户
                // 注册用户信息
                sysUser = new SysUser();
                sysUser.setNickName(body.getNickname());
                sysUser.setAvatar(body.getHeadImgUrl());
                sysUser.setUserName(body.getNickname());
                sysUser.setUserType("01"); // app用户
                R<SysUser> registerResult = remoteUserService.registerAppUserInfo(sysUser,
                        SecurityConstants.INNER);
                if (registerResult.getCode() == Constants.FAIL) {
                    throw new ServiceException(registerResult.getMsg());
                }
                SysUser sysUserRes = registerResult.getData();
                appUser = new AppUser();
                appUser.setUserId(sysUserRes.getUserId());
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                String format = simpleDateFormat.format(new Date());
                // 生成四位随机数字
                String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
                appUser.setCode("YH"+format+randomCode);
                appUser.setWxOpenId(body.getWxOpenId());
                String nickname = "用户" + IdUtil.fastSimpleUUID().substring(0, 6);
                sysUser.setNickName(nickname);
                appUser.setNickname(nickname);
                appUser.setWxName(body.getNickname());
                appUser.setAvatar(body.getHeadImgUrl());
                appUser.setDevice(body.getDevice());
                appUser.setGender(3);
                appUser.setBalance(BigDecimal.ZERO);
                appUser.setSanskritFlag(DisabledEnum.NO.getCode());
                appUser.setBalance(BigDecimal.ZERO);
                appUser.setIncome(BigDecimal.ZERO);
                appUser.setTotalEnergyValue(0);
                appUser.setRegisterTime(LocalDateTime.now());
                appUser.setLevelSettingId(TreeLevelEnum.SEED.getCode());
                appLoginUser.setBindStatus(DisabledEnum.YES.getCode());
                this.save(appUser);
                remoteMeditationService.addHomeBackgroundMusicDefault(appUser.getId());
 
                // 初始化用户树苗
                initUserTree(appUser.getId());
            }
        }
        appLoginUser.setUserid(sysUser.getUserId());
        appLoginUser.setUsername(appUser.getNickname());
        appLoginUser.setAppUserId(appUser.getId());
        appLoginUser.setSysUser(sysUser);
        this.updateById(appUser);
//        if (Objects.nonNull(sysUser.getUserName())) {
//            appLoginUser.setCellPhone(appUser.getCellPhone());
//        }
        return appLoginUser;
    }
 
    @Override
    public AppLoginUser appleLogin(String appleId,String device) {
        AppLoginUser appLoginUser = new AppLoginUser();
        Optional<AppUser> optionalAppUser = this.getUserByCondition(
                AppUserDTO.builder().appleId(appleId).device(device).build());
        SysUser sysUser = new SysUser();
        AppUser appUser = new AppUser();
        // 存在账户,查询关联系统用户
        if (optionalAppUser.isPresent()) {
            appUser = optionalAppUser.get();
            if (appUser.getUserStatus().equals(AppUserStatusEnum.FROZEN.getCode())) {
                throw new ServiceException("账号已被冻结");
            }
            sysUser = sysUserClient.getSysUser(appUser.getUserId()).getData();
            if (appUser.getCellPhone() != null) {
                appLoginUser.setBindStatus(DisabledEnum.NO.getCode());
                appLoginUser.setCellPhone(appUser.getCellPhone());
            }
        } else {
            if (org.springframework.util.StringUtils.hasLength(device)) {
                AppUser one = this.getOne(new LambdaQueryWrapper<>(AppUser.class)
                        .eq(AppUser::getDevice, device)
                        .isNull(AppUser::getCellPhone));
                if (one != null) {
                    initUserTree(one.getId());
                    appUser.setAppleId(appleId);
                    appUser = one;
                    this.updateById(appUser);
                } else {
                    // 不存在账户,创建账户
                    // 注册用户信息
                    sysUser = new SysUser();
                    String nickname = "用户" + IdUtil.fastSimpleUUID().substring(0, 6);
                    sysUser.setNickName(nickname);
                    sysUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg");
                    sysUser.setUserName(nickname);
                    sysUser.setUserType("01"); // app用户
                    R<SysUser> registerResult = remoteUserService.registerAppUserInfo(sysUser,
                            SecurityConstants.INNER);
                    if (registerResult.getCode() == Constants.FAIL) {
                        throw new ServiceException(registerResult.getMsg());
                    }
                    SysUser sysUserRes = registerResult.getData();
                    appUser = new AppUser();
                    appUser.setAppleId(appleId);
                    appUser.setUserId(sysUserRes.getUserId());
                    appUser.setNickname(nickname);
                    appUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg"); // TODO 待完善默认头像
                    appUser.setBalance(BigDecimal.ZERO);
                    appUser.setSanskritFlag(DisabledEnum.NO.getCode());
                    appUser.setBalance(BigDecimal.ZERO);
                    appUser.setIncome(BigDecimal.ZERO);
                    appUser.setTotalEnergyValue(0);
                    appUser.setDevice(device);
                    appUser.setRegisterTime(LocalDateTime.now());
                    appUser.setLevelSettingId(TreeLevelEnum.SEED.getCode());
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                    String format = simpleDateFormat.format(new Date());
                    appLoginUser.setBindStatus(DisabledEnum.YES.getCode());
                    // 生成四位随机数字
                    String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
                    appUser.setCode("YH" + format + randomCode);
                    this.save(appUser);
                    remoteMeditationService.addHomeBackgroundMusicDefault(appUser.getId());
 
                    // 初始化用户树苗
                    initUserTree(appUser.getId());
                }
            }else{
                // 不存在账户,创建账户
                // 注册用户信息
                sysUser = new SysUser();
                String nickname = "用户" + IdUtil.fastSimpleUUID().substring(0, 6);
                sysUser.setNickName(nickname);
                sysUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg");
                sysUser.setUserName(nickname);
                sysUser.setUserType("01"); // app用户
                R<SysUser> registerResult = remoteUserService.registerAppUserInfo(sysUser,
                        SecurityConstants.INNER);
                if (registerResult.getCode() == Constants.FAIL) {
                    throw new ServiceException(registerResult.getMsg());
                }
                SysUser sysUserRes = registerResult.getData();
                appUser = new AppUser();
                appUser.setAppleId(appleId);
                appUser.setUserId(sysUserRes.getUserId());
                appUser.setNickname(nickname);
                appUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg"); // TODO 待完善默认头像
                appUser.setBalance(BigDecimal.ZERO);
                appUser.setSanskritFlag(DisabledEnum.NO.getCode());
                appUser.setBalance(BigDecimal.ZERO);
                appUser.setIncome(BigDecimal.ZERO);
                appUser.setTotalEnergyValue(0);
                appUser.setDevice(device);
 
                appUser.setRegisterTime(LocalDateTime.now());
                appUser.setLevelSettingId(TreeLevelEnum.SEED.getCode());
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                String format = simpleDateFormat.format(new Date());
                appLoginUser.setBindStatus(DisabledEnum.YES.getCode());
                // 生成四位随机数字
                String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
                appUser.setCode("YH" + format + randomCode);
                this.save(appUser);
                remoteMeditationService.addHomeBackgroundMusicDefault(appUser.getId());
 
                // 初始化用户树苗
                initUserTree(appUser.getId());
            }
        }
            appLoginUser.setUserid(sysUser.getUserId());
            appLoginUser.setUsername(appUser.getNickname());
            appLoginUser.setAppUserId(appUser.getId());
            appLoginUser.setSysUser(sysUser);
            return appLoginUser;
        }
 
        /**
         * 验证码登录
         *
         * @param body
         * @return
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public AppLoginUser appCaptchaLogin (AppCaptchaBody body){
            AppLoginUser appLoginUser = new AppLoginUser();
            Optional<AppUser> optionalAppUser = this.getUserByCondition(
                    AppUserDTO.builder().device(body.getDevice()).cellPhone(body.getCellPhone()).build());
            AppUser appUser = new AppUser();
            SysUser sysUser = new SysUser();
            if (optionalAppUser.isPresent()) {
                appUser = optionalAppUser.get();
                sysUser = sysUserClient.getSysUser(appUser.getUserId()).getData();
            } else {
                if (org.springframework.util.StringUtils.hasLength(body.getDevice())) {
                    AppUser one = this.getOne(new LambdaQueryWrapper<>(AppUser.class)
                            .eq(AppUser::getDevice, body.getDevice())
                            .isNull(AppUser::getCellPhone));
                    if (one != null) {
                        initUserTree(appUser.getId());
                        appUser = one;
                    } else {
                        // 注册用户信息
                        sysUser = new SysUser();
                        String nickname = "用户" + IdUtil.fastSimpleUUID().substring(0, 6);
                        sysUser.setNickName(nickname);
                        sysUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg");
                        sysUser.setUserName(body.getCellPhone());
                        sysUser.setUserType("01"); // app用户
                        // 设置默认密码123456
                        sysUser.setPassword(SecurityUtils.encryptPassword(Constants.DEFAULT_PASSWORD));
 
                        R<SysUser> registerResult = remoteUserService.registerAppUserInfo(sysUser,
                                SecurityConstants.INNER);
                        if (registerResult.getCode() == Constants.FAIL) {
                            throw new ServiceException(registerResult.getMsg());
                        }
                        SysUser sysUserRes = registerResult.getData();
                        appUser = new AppUser();
                        appUser.setUserId(sysUserRes.getUserId());
                        appUser.setNickname(nickname);
                        appUser.setCellPhone(body.getCellPhone());
                        appUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg"); // TODO 待完善默认头像
                        appUser.setBalance(BigDecimal.ZERO);
                        appUser.setSanskritFlag(DisabledEnum.NO.getCode());
                        appUser.setBalance(BigDecimal.ZERO);
                        appUser.setIncome(BigDecimal.ZERO);
                        appUser.setTotalEnergyValue(0);
                        appUser.setRegisterTime(LocalDateTime.now());
                        appUser.setLevelSettingId(TreeLevelEnum.SEED.getCode());
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                        String format = simpleDateFormat.format(new Date());
                        // 生成四位随机数字
                        String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
                        appUser.setCode("YH" + format + randomCode);
                        this.save(appUser);
                        remoteMeditationService.addHomeBackgroundMusicDefault(appUser.getId());
                        // 初始化用户树苗
                        initUserTree(appUser.getId());
                    }
                } else {
                    // 注册用户信息
                    sysUser = new SysUser();
                    String nickname = "用户" + IdUtil.fastSimpleUUID().substring(0, 6);
                    sysUser.setNickName(nickname);
                    sysUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg");
                    sysUser.setUserName(body.getCellPhone());
                    sysUser.setUserType("01"); // app用户
                    // 设置默认密码123456
                    sysUser.setPassword(SecurityUtils.encryptPassword(Constants.DEFAULT_PASSWORD));
 
                    R<SysUser> registerResult = remoteUserService.registerAppUserInfo(sysUser,
                            SecurityConstants.INNER);
                    if (registerResult.getCode() == Constants.FAIL) {
                        throw new ServiceException(registerResult.getMsg());
                    }
                    SysUser sysUserRes = registerResult.getData();
                    appUser = new AppUser();
                    appUser.setUserId(sysUserRes.getUserId());
                    appUser.setNickname(nickname);
                    appUser.setCellPhone(body.getCellPhone());
                    appUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg"); // TODO 待完善默认头像
                    appUser.setBalance(BigDecimal.ZERO);
                    appUser.setSanskritFlag(DisabledEnum.NO.getCode());
                    appUser.setBalance(BigDecimal.ZERO);
                    appUser.setIncome(BigDecimal.ZERO);
                    appUser.setTotalEnergyValue(0);
                    appUser.setRegisterTime(LocalDateTime.now());
                    appUser.setLevelSettingId(TreeLevelEnum.SEED.getCode());
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                    String format = simpleDateFormat.format(new Date());
                    // 生成四位随机数字
                    String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
                    appUser.setCode("YH" + format + randomCode);
                    this.save(appUser);
                    remoteMeditationService.addHomeBackgroundMusicDefault(appUser.getId());
 
                    // 初始化用户树苗
                    initUserTree(appUser.getId());
                }
            }
            appLoginUser.setUserid(sysUser.getUserId());
            appLoginUser.setUsername(appUser.getNickname());
            appLoginUser.setAppUserId(appUser.getId());
            appLoginUser.setSysUser(sysUser);
            appLoginUser.setCellPhone(appUser.getCellPhone());
            return appLoginUser;
        }
 
    /**
     * 初始化用户树苗
     *
     * @param appUserId 用户id
     */
    private void initUserTree(Long appUserId) {
        if (appUserTreeService.lambdaQuery().eq(AppUserTree::getSowAgain,2).eq(AppUserTree::getAppUserId,appUserId).one()!=null){
            return;
        }
        AppUserTree appUserTree = new AppUserTree();
        appUserTree.setAppUserId(appUserId);
        appUserTree.setTreeLevelType(TreeLevelEnum.SEED.getCode());
        appUserTree.setGrowthValue(0);
        appUserTree.setSowAgain(2);
        appUserTree.setTime(LocalDateTime.now());
        appUserTreeService.save(appUserTree);
    }
 
    /**
     * 微信苹果登录验证手机号码操作
     *
     * @param body
     * @return
     */
    @Override
    public AppLoginUser verifyCellPhone(AppVerifyCellPhoneBody body) {
        Integer loginType = body.getLoginType();
        String wxOrAppleId = body.getWxOrAppleId();
        SysUser sysUser;
        AppUser appUser;
        // 先通过手机号查询用户,如果用户先使用手机号码登录过,那么就已经存在这个手机号码的用户了。
        Optional<AppUser> baseOptionalAppUser = this.getUserByCondition(
                AppUserDTO.builder().cellPhone(body.getCellPhone()).build());
        // 如果存在 更新wxOpenId 或appleId
        if (baseOptionalAppUser.isPresent()) {
            appUser = baseOptionalAppUser.get();
            if (appUser.getWxOpenId() !=null){
                throw new ServiceException("该手机号已绑定其他微信号,请更换手机号重新绑定");
            }
            if (appUser.getAppleId() !=null){
                throw new ServiceException("该手机号已绑定其他苹果号,请更换手机号重新绑定");
            }
            switch (body.getLoginType()){
                case 1:
                    this.remove(new LambdaQueryWrapper<AppUser>()
                            .eq(AppUser::getWxOpenId,body.getWxOrAppleId())
                            .ne(AppUser::getId,appUser.getId()));
                    break;
                case 2:
                    this.remove(new LambdaQueryWrapper<AppUser>()
                            .eq(AppUser::getAppleId,body.getWxOrAppleId())
                            .ne(AppUser::getId,appUser.getId()));
                    break;
            }
 
            if (loginType.equals(1)) {
                appUser.setWxOpenId(wxOrAppleId);
            } else {
                appUser.setAppleId(wxOrAppleId);
            }
            appUser.setEmail(body.getEmail());
            appUser.setNickname(body.getName());
            // 查询系统用户
            sysUser = sysUserClient.getSysUser(appUser.getUserId()).getData();
            if (Objects.isNull(sysUser)) {
                throw new ServiceException("系统用户不存在");
            }
        } else {
            Optional<AppUser> userOptional = getAppUserByThirdId(loginType, wxOrAppleId);
            if (!userOptional.isPresent()) {
                if (loginType.equals(1)) {
                    // 微信登录
                    handleUserNotPresent(userOptional, "该用户不存在,请重新进行微信授权登录");
                } else {
                    // 苹果登录
                    handleUserNotPresent(userOptional, "该用户不存在,请重新进行苹果授权登录");
                }
            }
            appUser = userOptional.get();
            appUser.setCellPhone(body.getCellPhone());
            appUser.setEmail(body.getEmail());
            appUser.setNickname(body.getName());
            // 更新系统用户
            sysUser = sysUserClient.getSysUser(appUser.getUserId()).getData();
            if (Objects.isNull(sysUser)) {
                throw new ServiceException("系统用户不存在");
            }
            sysUser.setUserName(body.getCellPhone());
            sysUser.setPassword(
                    SecurityUtils.encryptPassword(Constants.DEFAULT_PASSWORD))
            ; // 默认密码为a12345678
            sysUser.setPhonenumber(body.getCellPhone());
            sysUserClient.updateSysUser(sysUser);
        }
        // 更新用户信息
        this.updateById(appUser);
        AppLoginUser appLoginUser = new AppLoginUser();
        appLoginUser.setAppUserId(appUser.getId());
        appLoginUser.setBindStatus(1);
        appLoginUser.setCellPhone(body.getCellPhone());
        appLoginUser.setSysUser(sysUser);
        return appLoginUser;
    }
    // 定义一个方法来处理用户不存在的情况
    private void handleUserNotPresent(Optional<AppUser> userOptional, String errorMessage) {
        if (!userOptional.isPresent()) {
            throw new ServiceException(errorMessage);
        }
    }
 
    /**
     * 根据第三方id获取未注销用户
     *
     * @param loginType   登录类型 1=微信 2=苹果
     * @param wxOrAppleId 微信openid或苹果appleId
     * @return Optional<AppUser>
     */
    private Optional<AppUser> getAppUserByThirdId(Integer loginType, String wxOrAppleId) {
        return this.lambdaQuery()
                .eq(loginType.equals(1), AppUser::getWxOpenId, wxOrAppleId)
                .eq(loginType.equals(2), AppUser::getAppleId, wxOrAppleId)
                .ne(AppUser::getUserStatus, AppUserStatusEnum.LOGOUT.getCode())
                .oneOpt();
    }
 
    /**
     * 保存计划引导页用户的答案
     *
     * @param dto 用户计划引导答案数据传输对象
     */
    @Override
    public void saveUserAnswers(UserAnswerDTO dto) {
        if (tokenService.getLoginUser()==null){
            // 获取当前登录用户
            if (org.springframework.util.StringUtils.hasLength(dto.getDevice())){
                AppUser one = this.lambdaQuery().eq(AppUser::getDevice, dto.getDevice())
                        .isNull(AppUser::getCellPhone).one();
                if (one==null){
                    SysUser sysUser = new SysUser();
                    AppUser appUser = new AppUser();
                    // 注册用户信息
                    sysUser = new SysUser();
                    String nickname = "用户" + IdUtil.fastSimpleUUID().substring(0, 6);
                    sysUser.setNickName(nickname);
                    sysUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg");
                    sysUser.setUserName(nickname);
                    sysUser.setUserType("01"); // app用户
                    // 设置默认密码123456
                    sysUser.setPassword(SecurityUtils.encryptPassword(Constants.DEFAULT_PASSWORD));
 
                    R<SysUser> registerResult = remoteUserService.registerAppUserInfo(sysUser,
                            SecurityConstants.INNER);
                    if (registerResult.getCode() == Constants.FAIL) {
                        throw new ServiceException(registerResult.getMsg());
                    }
                    SysUser sysUserRes = registerResult.getData();
                    appUser = new AppUser();
                    appUser.setUserId(sysUserRes.getUserId());
                    appUser.setNickname(nickname);
                    appUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg"); // TODO 待完善默认头像
                    appUser.setBalance(BigDecimal.ZERO);
                    appUser.setSanskritFlag(DisabledEnum.NO.getCode());
                    appUser.setBalance(BigDecimal.ZERO);
                    appUser.setIncome(BigDecimal.ZERO);
                    appUser.setTotalEnergyValue(0);
                    appUser.setRegisterTime(LocalDateTime.now());
                    appUser.setLevelSettingId(TreeLevelEnum.SEED.getCode());
                    appUser.setDevice(dto.getDevice());
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
                    String format = simpleDateFormat.format(new Date());
                    // 生成四位随机数字
                    String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
                    appUser.setCode("YH"+format+randomCode);
                    this.save(appUser);
                    Long userId = appUser.getId();
                    List<AppUserQuestion> appUserQuestionList = BeanUtils.copyList(
                            dto.getUserAnswerOneDTOList(), AppUserQuestion.class);
                    // 查询用户已填写的答案
                    List<AppUserQuestion> baseAppUserQuestionList = appUserQuestionService.lambdaQuery()
                            .eq(AppUserQuestion::getAppUserId, appUser.getUserId())
                            .list();
                    if (CollUtils.isNotEmpty(baseAppUserQuestionList)) {
                        // 根据类型更新答案
                        for (AppUserQuestion appUserQuestion : appUserQuestionList) {
 
                            Optional<AppUserQuestion> baseAppUserQuestionOptional = baseAppUserQuestionList.stream()
                                    .filter(baseAppUserQuestion -> baseAppUserQuestion.getType()
                                            .equals(appUserQuestion.getType())).findFirst();
                            baseAppUserQuestionOptional.ifPresent(
                                    userQuestion -> appUserQuestion.setId(userQuestion.getId()));
                            appUserQuestion.setAppUserId(userId);
                        }
                    }
                    for (AppUserQuestion appUserQuestion : appUserQuestionList) {
                        appUserQuestion.setAppUserId(userId);
                    }
                    appUserQuestionService.saveOrUpdateBatch(appUserQuestionList);
                    // 筛选答案为是和偶尔有的问题,为用户设置对应的标签
                    List<AppUserQuestion> appUserQuestionYesList = appUserQuestionList.stream()
                            .filter(appUserQuestion -> appUserQuestion.getAnswer().equals("1")
                                    || appUserQuestion.getAnswer().equals("3")).collect(Collectors.toList());
                    appUser.setTagId(dto.getTagIds());
                    this.updateById(appUser);
                }else{
                    List<AppUserQuestion> appUserQuestionList = BeanUtils.copyList(
                            dto.getUserAnswerOneDTOList(), AppUserQuestion.class);
                    // 查询用户已填写的答案
                    // 删除之前答案
                    appUserQuestionService.lambdaUpdate().eq(AppUserQuestion::getAppUserId,one.getId()).remove();
 
                    // 根据类型更新答案
                    for (AppUserQuestion appUserQuestion : appUserQuestionList) {
                        appUserQuestion.setAppUserId(one.getId());
                    }
                    appUserQuestionService.saveBatch(appUserQuestionList);
                    // 筛选答案为是和偶尔有的问题,为用户设置对应的标签
                    List<AppUserQuestion> appUserQuestionYesList = appUserQuestionList.stream()
                            .filter(appUserQuestion -> appUserQuestion.getAnswer().equals("1")
                                    || appUserQuestion.getAnswer().equals("3")).collect(Collectors.toList());
                    one.setTagId(dto.getTagIds());
                    this.updateById(one);
                }
 
                }
        }else{
            Long userId = tokenService.getLoginUser().getUserid();
            AppUser appUser = this.getById(userId);
            List<AppUserQuestion> appUserQuestionList = BeanUtils.copyList(
                    dto.getUserAnswerOneDTOList(), AppUserQuestion.class);
            // 查询用户已填写的答案
            // 删除之前答案
            appUserQuestionService.lambdaUpdate().eq(AppUserQuestion::getAppUserId,userId).remove();
 
            // 根据类型更新答案
            for (AppUserQuestion appUserQuestion : appUserQuestionList) {
                    appUserQuestion.setAppUserId(userId);
            }
            appUserQuestionService.saveBatch(appUserQuestionList);
            // 筛选答案为是和偶尔有的问题,为用户设置对应的标签
            List<AppUserQuestion> appUserQuestionYesList = appUserQuestionList.stream()
                    .filter(appUserQuestion -> appUserQuestion.getAnswer().equals("1")
                            || appUserQuestion.getAnswer().equals("3")).collect(Collectors.toList());
            appUser.setTagId(dto.getTagIds());
            this.updateById(appUser);
        }
    }
 
    /**
     * 获取AppUser
     *
     * @param userId 系统用户userId
     * @return AppUser
     */
    private AppUser getUserBySysUserId(Long userId) {
        Optional<AppUser> appUserOptional = this.lambdaQuery().eq(AppUser::getUserId, userId)
                .oneOpt();
        if (!appUserOptional.isPresent()) {
            throw new ServiceException("获取用户信息失败,请重新登录");
        }
        return appUserOptional.get();
    }
 
    /**
     * 获取问题二的标签列表
     *
     * @return List<TagVO>
     */
    @Override
    public List<TagVO> getTagList() {
        List<Tag> list = tagService.list(Wrappers.lambdaQuery(Tag.class).eq(Tag::getTagType, 2));
        return BeanUtils.copyList(list, TagVO.class);
    }
 
    /**
     * 更新APP用户信息
     *
     * @param dto APP用户数据传输对象
     */
    @Override
    public void updateAppUser(AppUserDTO dto) {
        // 拷贝数据
        AppUser appUser = BeanUtils.copyBean(dto, AppUser.class);
        this.updateById(appUser);
    }
 
    @Autowired
    private TokenService tokenService;
    /**
     * 获取当前登录用户信息
     *
     * @return 用户信息
     * @see AppUserVO
     */
    @Override
    public AppUserVO getCurrentUser() {
        LoginUser loginUser = tokenService.getLoginUser();
 
        Long userId = loginUser.getUserid();
        AppUser appUser = this.getById(userId);
        return BeanUtils.copyBean(appUser, AppUserVO.class);
    }
}