luodangjia
2024-04-28 6d72bb2e7f223304290ebebdb30960a9586b9dac
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
package cn.stylefeng.guns.modular.business.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.stylefeng.guns.modular.business.dto.CounsellingOrderResponseDTO;
import cn.stylefeng.guns.modular.business.dto.ImPushDataDTO;
import cn.stylefeng.guns.modular.business.dto.request.CounsellingOrderRequest;
import cn.stylefeng.guns.modular.business.dto.request.CreateCounsellingOrderRequest;
import cn.stylefeng.guns.modular.business.dto.request.CustomerUpdateRequest;
import cn.stylefeng.guns.modular.business.dto.request.OrderPayRequest;
import cn.stylefeng.guns.modular.business.entity.*;
import cn.stylefeng.guns.modular.business.mapper.CounsellingOrderMapper;
import cn.stylefeng.guns.modular.business.service.*;
import cn.stylefeng.guns.utils.GeneralUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.customer.api.pojo.CustomerInfo;
import cn.stylefeng.roses.kernel.customer.modular.entity.Customer;
import cn.stylefeng.roses.kernel.customer.modular.service.CustomerService;
import cn.stylefeng.roses.kernel.rule.enums.*;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
 
/**
 * <p>
 * 咨询订单信息 服务实现类
 * </p>
 *
 * @author guohongjin
 * @since 2024-01-02
 */
@Service
public class CounsellingOrderServiceImpl extends ServiceImpl<CounsellingOrderMapper, CounsellingOrder> implements ICounsellingOrderService {
 
    @Autowired
    private CustomerService customerService;
 
    @Autowired
    private ICounsellingInfoService counsellingInfoService;
 
    @Autowired
    private ICounsellingSetMealService counsellingSetMealService;
 
    @Autowired
    private ICounsellingOrderReservationService counsellingOrderReservationService;
 
    @Autowired
    private IImGroupService iImGroupService;
 
    @Resource
    private ImBizService imBizService;
 
    @Resource
    private ICounsellingUserService counsellingUserService;
    @Resource
    private IAreaService areaService;
    @Resource
    IMentalAppointmentService mentalAppointmentService;
 
    @Override
    public Page<CounsellingOrderResponseDTO> findCounsellingOrderPage(Page<CounsellingOrderRequest> page, CounsellingOrderRequest counsellingOrderRequest) {
        return this.baseMapper.findCounsellingOrderPage(page, counsellingOrderRequest);
    }
 
    @Override
    public Page<CounsellingOrderResponseDTO> findWorkCounsellingOrderPage(Page<CounsellingOrderRequest> page, CounsellingOrderRequest counsellingOrderRequest) {
        return this.baseMapper.findWorkCounsellingOrderPage(page, counsellingOrderRequest);
    }
 
    @Override
    public CounsellingOrder createCounsellingOrder(CreateCounsellingOrderRequest counsellingOrderRequest) {
 
        if (counsellingOrderRequest.getUserId() == null){
            counsellingOrderRequest.setUserId(LoginContext.me().getLoginUser().getUserId());
        }
        //判断是否有用户咨询师信息
        CounsellingUser counsellingUserOld = this.counsellingUserService.getOne(new LambdaQueryWrapper<CounsellingUser>().eq(CounsellingUser::getCounsellingInfoId,counsellingOrderRequest.getCounsellingInfoId())
                .eq(CounsellingUser::getUserId,counsellingOrderRequest.getUserId()));
        if(counsellingOrderRequest.getOrderType().intValue() != 1){
            if (counsellingUserOld == null){
                throw new ServiceException("请先购买首次咨询!");
            } else if (counsellingUserOld != null && counsellingUserOld.getIsFirstAppointment() != null && counsellingUserOld.getIsFirstAppointment().intValue() != 3) {
                throw new ServiceException("请先完成首次咨询,再进行疗程购买!");
            }
 
        }
        //查询客户信息
        Customer customerOld = this.customerService.getById(counsellingOrderRequest.getUserId());
        CounsellingInfo counsellingInfo = this.counsellingInfoService.getById(counsellingOrderRequest.getCounsellingInfoId());
        CounsellingOrder counsellingOrder = BeanUtil.copyProperties(counsellingOrderRequest,CounsellingOrder.class);
        if (counsellingOrderRequest.getCustomerUpdateRequest() != null){
            counsellingOrder.setPhone(counsellingOrderRequest.getCustomerUpdateRequest().getLinkPhone());
        }
        counsellingOrder.setOrderNo(GeneralUtil.generateOrderNo(OrderTypeEnum.COUNSELLING_ORDER));
        //订单类型 1-首次咨询,2-咨询疗程,3-课程续费
        if (counsellingOrderRequest.getOrderType().intValue() ==1){
            //是否后台创建
            if (counsellingOrderRequest.getIsBack()){
                Customer customerUser = this.customerService.getById(counsellingOrderRequest.getUserId());
                if (customerUser != null){
                    CustomerUpdateRequest customerUpdateRequest = BeanUtil.copyProperties(customerUser, CustomerUpdateRequest.class);
                    counsellingOrder.setUserInfoJson(JSONUtil.toJsonStr(customerUpdateRequest));
                }
 
                counsellingOrder.setStatusFlag(1);
//                if (counsellingUserOld != null && counsellingUserOld.getCompanionUserId() != null){
//                    //陪护教练id
//                    counsellingOrder.setCompanionUserId(counsellingUserOld.getCompanionUserId());
//                }else {
//
//                    Customer customerCompanion = customerService.randomWorkerByLineStatusAndPost(ImStatusEnum.ON_LINE.getCode(), null, PostIdEnum.PO_23.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
//                    if (customerCompanion != null){
//                        //陪护教练id
//                        counsellingOrder.setCompanionUserId(customerCompanion.getCustomerId());
////                        counsellingUserOld.setCompanionUserId(customerCompanion.getCustomerId());
//                    }else{
//                        Customer customerCompanion2 = customerService.randomWorkerByLineStatusAndPost(null, null, PostIdEnum.PO_23.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
//                        if (customerCompanion2 != null){
//                            //陪护教练id
//                            counsellingOrder.setCompanionUserId(customerCompanion2.getCustomerId());
////                            counsellingUserOld.setCompanionUserId(customerCompanion2.getCustomerId());
//                        }else{
//                            throw new ServiceException("没有在线的陪护教练!");
//                        }
//
//                    }
//                }
 
 
//                counsellingOrder.setPhone(customerUser.getLinkPhone());
                counsellingOrder.setOrderAmount(counsellingInfo.getFristPrice());
                counsellingOrder.setPayAmount(counsellingInfo.getFristPrice());
                counsellingOrder.setCreateUser(LoginContext.me().getLoginUser().getUserId());
 
 
            }else{
                counsellingOrder.setStatusFlag(0);
                CustomerUpdateRequest customerUpdateRequest = counsellingOrderRequest.getCustomerUpdateRequest();
                if (customerUpdateRequest != null){
                    customerUpdateRequest.setCustomerId(counsellingOrderRequest.getUserId());
                     Customer customer = BeanUtil.toBean(customerUpdateRequest, Customer.class);
                     customer.setCustomerId(counsellingOrderRequest.getUserId());
                    // 修改用户信息
                    Boolean update = customerService.updateCustomerRemoveCache(customer);
                    counsellingOrder.setUserInfoJson(JSONUtil.toJsonStr(customerUpdateRequest));
                }
//                Date firstAppointmentDate = counsellingOrderRequest.getFirstAppointmentDate();
//// 创建一个 SimpleDateFormat 实例,指定日期格式为 "yyyy-MM-dd"
//                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//
//// 使用 formatter 将 Date 对象转换为字符串
//                String formattedDate = formatter.format(firstAppointmentDate);
//                // 发送IM消息
//            ImPushDataDTO pushData1 = ImPushDataDTO.builder()
//                    .type(ImPushTypeEnum.C_TO_W_IM_1V1_START_CONSULT.getCode())
//                    .title(ImPushTypeEnum.C_TO_W_IM_1V1_START_CONSULT.getName())
//                    .content("预约成功"+",请注意预约时间:"+formattedDate+counsellingOrderRequest.getFirstAppointmentTimes())
////                    .content("预约成功!")
//                    .objId(ObjUtil.toString(counsellingInfo.getId()))
//                    .data1(ObjUtil.toString(counsellingOrder.getUserId()))
//                    .data2(ObjUtil.toString(counsellingInfo.getUserId()))
//                    .build();
//            imBizService.messageSendPrivate(
//                    ObjUtil.toString(counsellingOrder.getUserId()),
//                    new String[]{ObjUtil.toString(counsellingInfo.getUserId())},
//                    pushData1
//
//
//            );
 
//                //将此条消息加入到可聊天的表中t_mental_appointment
//                MentalAppointment mentalAppointment = MentalAppointment.builder()
//                        .userId(counsellingOrder.getUserId())
//                        .appointmentDay(counsellingOrderRequest.getFirstAppointmentDate())
//                        .beginTimePoint(counsellingOrderRequest.getFirstAppointmentTimes().split("-")[0])
//                        .endTimePoint(counsellingOrderRequest.getFirstAppointmentTimes().split("-")[1])
//                        .workerId(counsellingInfo.getUserId())
//                        .build();
//                // 用户信息
//                CustomerInfo customerInfo = customerService.getCustomerInfoById(counsellingOrder.getUserId());
//                mentalAppointment.setUserName(customerInfo.getRealName());
//                mentalAppointment.setPhone(customerInfo.getLinkPhone());
//                mentalAppointmentService.save(mentalAppointment);
 
            }
            if (counsellingUserOld != null && counsellingUserOld.getConsultantUserId() != null){
                counsellingOrder.setConsultantUserId(counsellingUserOld.getConsultantUserId());
            }else{
                if (customerOld.getConsultWorkerId() != null){
                    counsellingOrder.setConsultantUserId(customerOld.getConsultWorkerId());
                }else{
                    Customer customer2 = customerService.randomWorkerByLineStatusAndPost(ImStatusEnum.ON_LINE.getCode(), null, PostIdEnum.PO_21.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                    if (customer2 != null){
                        //咨询顾问id
                        counsellingOrder.setConsultantUserId(customer2.getCustomerId());
//                        counsellingUserOld.setConsultantUserId(customer2.getCustomerId());
                    }else {
                        Customer customer3 = customerService.randomWorkerByLineStatusAndPost(null, null, PostIdEnum.PO_21.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                        if (customer3 != null){
                            //咨询顾问id
                            counsellingOrder.setConsultantUserId(customer3.getCustomerId());
//                            counsellingUserOld.setConsultantUserId(customer3.getCustomerId());
                        }else{
                            throw new ServiceException("没有在线的咨询顾问!");
                        }
                    }
                }
 
            }
//            iImGroupService.createGroup(2,null,null,counsellingInfo.getUserId(),null,null,counsellingOrder.getUserId(),counsellingOrder.getCounsellingInfoId());
 
 
 
 
 
 
//            // 推送消息内容
//            String pushContent = "你有新的预约,请注意查收。预约用户:"+customerOld.getNickName();
////                    +"预约时间:"+counsellingOrder.getEffectiveEndTime()+"~"+counsellingOrder.getEffectiveEndTime();
//            // IM推送数据json
//            ImPushDataDTO pushData = ImPushDataDTO.builder()
//                    .type(ImPushTypeEnum.S_TO_W_TIP_CONSULT_PAY_GROUP_SUCCESS_ONLY.getCode())
//                    .objId(ObjUtil.toString(counsellingInfo.getUserId()))
//                    .title("通知")
//                    .content(pushContent)
////                    .extra("去查看。")
//                    .build();
//            // 发送预约提示
//            imBizService.messageSendSystem(counsellingOrder.getUserId()+"", new String[]{counsellingInfo.getUserId()+""}, pushData, ImUserTypeEnum.WORKER, PostIdEnum.PO_22, true);
        }else if (counsellingOrderRequest.getOrderType().intValue() ==2){
            //获取下单人个人信息
            Customer customerUser = this.customerService.getById(counsellingOrderRequest.getUserId());
            if (customerUser != null){
                CustomerUpdateRequest customerUpdateRequest = BeanUtil.copyProperties(customerUser, CustomerUpdateRequest.class);
                if (StrUtil.isNotBlank(customerUser.getCityCode())) {
                    customerUpdateRequest.setCityCodeStr(areaService.getNameByCodeAddPrefix("/", customerUpdateRequest.getCityCode()));
                }
                counsellingOrder.setUserInfoJson(JSONUtil.toJsonStr(customerUpdateRequest));
            }
            //跟进套餐设置订单信息
            CounsellingSetMeal counsellingSetMeal = this.counsellingSetMealService.getById(counsellingOrderRequest.getCounsellingSetMealId());
            if (counsellingSetMeal != null){
                counsellingOrder.setCounsellingSetMealId(counsellingSetMeal.getId());
                counsellingOrder.setSetMealJson(JSONUtil.toJsonStr(counsellingSetMeal));
                counsellingOrder.setPayAmount(counsellingSetMeal.getPrice());
                counsellingOrder.setOrderAmount(counsellingSetMeal.getOriginalPrice());
                counsellingOrder.setClassHours(counsellingSetMeal.getClassHours());
                counsellingOrder.setResidueClassHours(counsellingSetMeal.getClassHours());
 
            }else {
                throw new ServiceException("咨询套餐不存在!");
            }
            counsellingOrder.setPhone(customerUser.getTelephone());
            if (counsellingUserOld != null && counsellingUserOld.getConsultantUserId() != null){
                counsellingOrder.setConsultantUserId(counsellingUserOld.getConsultantUserId());
            }else{
                if (counsellingUserOld != null && counsellingUserOld.getConsultantUserId() != null){
                    counsellingOrder.setConsultantUserId(counsellingUserOld.getConsultantUserId());
                }else {
                    if (customerOld.getConsultWorkerId() != null) {
                        counsellingOrder.setConsultantUserId(customerOld.getConsultWorkerId());
                    } else {
                        Customer customer2 = customerService.randomWorkerByLineStatusAndPost(ImStatusEnum.ON_LINE.getCode(), null, PostIdEnum.PO_21.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                        if (customer2 != null) {
                            //咨询顾问id
                            counsellingOrder.setConsultantUserId(customer2.getCustomerId());
//                        counsellingUserOld.setConsultantUserId(customer2.getCustomerId());
                        } else {
                            Customer customer3 = customerService.randomWorkerByLineStatusAndPost(null, null, PostIdEnum.PO_21.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                            if (customer3 != null) {
                                //咨询顾问id
                                counsellingOrder.setConsultantUserId(customer3.getCustomerId());
//                            counsellingUserOld.setConsultantUserId(customer3.getCustomerId());
                            } else {
                                throw new ServiceException("没有在线的咨询顾问!");
                            }
                        }
                    }
                }
            }
            //是否赠送陪护
            if (counsellingSetMeal.getIsGive() != null && counsellingSetMeal.getIsGive().intValue() == 1){
                if (counsellingUserOld != null && counsellingUserOld.getCompanionUserId() != null){
                    //陪护教练id
                    counsellingOrder.setCompanionUserId(counsellingUserOld.getCompanionUserId());
                }else {
 
                    Customer customerCompanion = customerService.randomWorkerByLineStatusAndPost(ImStatusEnum.ON_LINE.getCode(), null, PostIdEnum.PO_23.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                    if (customerCompanion != null){
                        //陪护教练id
                        counsellingOrder.setCompanionUserId(customerCompanion.getCustomerId());
                        counsellingUserOld.setCompanionUserId(customerCompanion.getCustomerId());
                    }else{
                        Customer customerCompanion2 = customerService.randomWorkerByLineStatusAndPost(null, null, PostIdEnum.PO_23.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                        if (customerCompanion2 != null){
                            //陪护教练id
                            counsellingOrder.setCompanionUserId(customerCompanion2.getCustomerId());
                            counsellingUserOld.setCompanionUserId(customerCompanion2.getCustomerId());
                        }else{
                            throw new ServiceException("没有在线的陪护教练!");
                        }
 
                    }
                }
 
            }
            //是否后台创建
            if (counsellingOrderRequest.getIsBack()){
 
                counsellingOrder.setStatusFlag(1);
                counsellingOrder.setEffectiveBeginTime(new Date());
                counsellingOrder.setEffectiveEndTime(DateUtil.endOfDay(DateUtil.offsetDay(counsellingOrder.getEffectiveBeginTime(),counsellingSetMeal.getPeriodValidityDay())));
                counsellingOrder.setCreateUser(LoginContext.me().getLoginUser().getUserId());
                //查询是否已经存在群聊
                //查询是否已经存在群聊
//                long imCount = this.iImGroupService.count(new LambdaQueryWrapper<ImGroup>().eq(ImGroup::getOrderId,counsellingOrder.getCounsellingInfoId())
//                        .eq(ImGroup::getGroupType,2).eq(ImGroup::getCreateUser,counsellingOrder.getUserId()).eq(ImGroup::getIsDelete,0));
//                if ( imCount ==0l  ){
                if (counsellingSetMeal.getIsGive() != null && counsellingSetMeal.getIsGive().intValue() == 1){
                    //生成群聊
                    iImGroupService.createGroup(2,counsellingOrder.getConsultantUserId(),counsellingOrder.getCompanionUserId(),counsellingInfo.getUserId(),null,null,counsellingOrder.getUserId(),counsellingOrder.getCounsellingInfoId());
 
                }else{
                    //生成群聊
                    iImGroupService.createGroup(2,null,null,counsellingInfo.getUserId(),null,null,counsellingOrder.getUserId(),counsellingOrder.getCounsellingInfoId());
 
                }
 
//                }
 
            }else {
                counsellingOrder.setStatusFlag(0);
 
            }
        } else if (counsellingOrderRequest.getOrderType().intValue() ==3) {
            //获取下单人个人信息
            Customer customerUser = this.customerService.getById(counsellingOrderRequest.getUserId());
            if (customerUser != null){
                CustomerUpdateRequest customerUpdateRequest = BeanUtil.copyProperties(customerUser, CustomerUpdateRequest.class);
                if (StrUtil.isNotBlank(customerUser.getCityCode())) {
                    customerUpdateRequest.setCityCodeStr(areaService.getNameByCodeAddPrefix("/", customerUpdateRequest.getCityCode()));
                }
                counsellingOrder.setUserInfoJson(JSONUtil.toJsonStr(customerUpdateRequest));
            }
            //跟进套餐设置订单信息
            CounsellingSetMeal counsellingSetMeal = this.counsellingSetMealService.getById(counsellingOrderRequest.getCounsellingSetMealId());
            if (counsellingSetMeal != null){
                counsellingOrder.setCounsellingSetMealId(counsellingSetMeal.getId());
                counsellingOrder.setSetMealJson(JSONUtil.toJsonStr(counsellingSetMeal));
                counsellingOrder.setPayAmount(counsellingSetMeal.getPrice());
                counsellingOrder.setOrderAmount(counsellingSetMeal.getOriginalPrice());
 
                counsellingOrder.setClassHours(counsellingSetMeal.getClassHours());
                counsellingOrder.setResidueClassHours(counsellingSetMeal.getClassHours());
//                counsellingOrder.setEffectiveBeginTime(new Date());
//                counsellingOrder.setEffectiveEndTime(DateUtil.offsetDay(counsellingOrder.getEffectiveBeginTime(),counsellingSetMeal.getPeriodValidityDay()));
 
            }else {
                throw new ServiceException("咨询套餐不存在!");
            }
            counsellingOrder.setPhone(customerUser.getTelephone());
            if (counsellingUserOld != null && counsellingUserOld.getConsultantUserId() != null){
                counsellingOrder.setConsultantUserId(counsellingUserOld.getConsultantUserId());
            }else{
                if (counsellingUserOld != null && counsellingUserOld.getConsultantUserId() != null){
                    counsellingOrder.setConsultantUserId(counsellingUserOld.getConsultantUserId());
                }else {
                    if (customerOld.getConsultWorkerId() != null) {
                        counsellingOrder.setConsultantUserId(customerOld.getConsultWorkerId());
                    } else {
                        Customer customer2 = customerService.randomWorkerByLineStatusAndPost(ImStatusEnum.ON_LINE.getCode(), null, PostIdEnum.PO_21.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                        if (customer2 != null) {
                            //咨询顾问id
                            counsellingOrder.setConsultantUserId(customer2.getCustomerId());
//                        counsellingUserOld.setConsultantUserId(customer2.getCustomerId());
                        } else {
                            Customer customer3 = customerService.randomWorkerByLineStatusAndPost(null, null, PostIdEnum.PO_21.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                            if (customer3 != null) {
                                //咨询顾问id
                                counsellingOrder.setConsultantUserId(customer3.getCustomerId());
//                            counsellingUserOld.setConsultantUserId(customer3.getCustomerId());
                            } else {
                                throw new ServiceException("没有在线的咨询顾问!");
                            }
                        }
                    }
                }
            }
            //是否后台创建
            if (counsellingOrderRequest.getIsBack()){
                Date nowDate = new Date();
                if (counsellingUserOld == null || (counsellingUserOld != null && (counsellingUserOld.getEffectiveEndTime() == null ||
                        (counsellingUserOld.getEffectiveEndTime() != null && counsellingUserOld.getEffectiveEndTime().getTime() < nowDate.getTime())))){
                    counsellingOrder.setEffectiveBeginTime(nowDate);
                    counsellingOrder.setEffectiveEndTime(DateUtil.endOfDay(DateUtil.offsetDay(counsellingOrder.getEffectiveBeginTime(),counsellingSetMeal.getPeriodValidityDay())));
                }else{
                    counsellingOrder.setEffectiveBeginTime(counsellingUserOld.getEffectiveEndTime());
                    counsellingOrder.setEffectiveEndTime(DateUtil.endOfDay(DateUtil.offsetDay(counsellingOrder.getEffectiveBeginTime(),counsellingSetMeal.getPeriodValidityDay())));
 
                }
                 counsellingOrder.setStatusFlag(1);
 
                counsellingOrder.setCreateUser(LoginContext.me().getLoginUser().getUserId());
                //查询是否已经存在群聊
//                long imCount = this.iImGroupService.count(new LambdaQueryWrapper<ImGroup>().eq(ImGroup::getOrderId,counsellingOrder.getCounsellingInfoId())
//                        .eq(ImGroup::getGroupType,2).eq(ImGroup::getCreateUser,counsellingOrder.getUserId()).eq(ImGroup::getIsDelete,0));
//                if ( imCount ==0l  ){
                    //生成群聊
                    iImGroupService.createGroup(2,counsellingOrder.getConsultantUserId(),counsellingOrder.getCompanionUserId(),counsellingInfo.getUserId(),null,null,counsellingOrder.getUserId(),counsellingOrder.getCounsellingInfoId());
 
//                }
 
            }else {
                counsellingOrder.setStatusFlag(0);
 
            }
        }
 
        //是否后台创建
        if (counsellingOrderRequest.getIsBack()){
 
            if (counsellingUserOld == null ){
                CounsellingUser counsellingUser = BeanUtil.copyProperties(counsellingOrder,CounsellingUser.class);
                counsellingUser.setId(null);
                counsellingUser.setIsFirstAppointment(1);
                this.counsellingUserService.save(counsellingUser);
            }else{
                if (counsellingOrder.getOrderType().intValue() != 1){
                    if (counsellingUserOld.getEffectiveBeginTime() == null){
                        counsellingUserOld.setEffectiveBeginTime(counsellingOrder.getEffectiveBeginTime());
                    }
                    counsellingUserOld.setEffectiveEndTime(counsellingOrder.getEffectiveEndTime());
                    counsellingUserOld.setClassHours(counsellingUserOld.getClassHours() !=null ? counsellingUserOld.getClassHours().intValue() + counsellingOrder.getClassHours().intValue():counsellingOrder.getClassHours());
                    counsellingUserOld.setResidueClassHours(counsellingUserOld.getResidueClassHours() !=null ? counsellingUserOld.getResidueClassHours().intValue() + counsellingOrder.getResidueClassHours().intValue():counsellingOrder.getResidueClassHours());
                    counsellingUserOld.setStatusFlag(counsellingOrder.getStatusFlag());
                }else {
                    counsellingUserOld.setUserInfoJson(counsellingOrder.getUserInfoJson());
                    counsellingUserOld.setIsFirstAppointment(1);
                }
                if (counsellingUserOld.getCompanionUserId() != null){
                    counsellingOrder.setCompanionUserId(counsellingUserOld.getCompanionUserId());
                }else{
                    counsellingUserOld.setCompanionUserId(counsellingOrder.getCompanionUserId());
                }
                if (counsellingUserOld.getConsultantUserId() != null){
                    counsellingOrder.setConsultantUserId(counsellingUserOld.getConsultantUserId());
                }else{
                    counsellingUserOld.setConsultantUserId(counsellingOrder.getConsultantUserId());
                }
                if (StrUtil.isBlank(counsellingUserOld.getPhone())  && StrUtil.isNotBlank(counsellingOrder.getPhone())){
                    counsellingUserOld.setPhone(counsellingOrder.getPhone());
                }
                if (StrUtil.isBlank(counsellingUserOld.getUserInfoJson()) && StrUtil.isNotBlank(counsellingOrder.getUserInfoJson())){
                    counsellingUserOld.setUserInfoJson(counsellingOrder.getUserInfoJson());
                }
                counsellingUserOld.setStatusFlag(counsellingOrder.getStatusFlag());
                this.counsellingUserService.updateById(counsellingUserOld);
 
            }
        }
        if (counsellingOrderRequest.getIsBack()){
            counsellingOrder.setPayTime(new Date());
        }
        this.save(counsellingOrder);
        //分配咨询顾问
        if (customerOld.getConsultWorkerId() == null && counsellingOrder.getConsultantUserId() != null ){
            customerOld.setConsultWorkerId(counsellingOrder.getConsultantUserId());
            this.customerService.updateCustomerRemoveCache(customerOld);
 
        }
 
        return counsellingOrder;
    }
 
    @Override
    public void payCounsellingOrder(OrderPayRequest orderPayRequest) {
        CounsellingOrder counsellingOrder = this.getOne(new LambdaQueryWrapper<CounsellingOrder>().eq(CounsellingOrder::getOrderNo,orderPayRequest.getOrderNo()));
        counsellingOrder.setTransactionNo(orderPayRequest.getTransactionNo());
        counsellingOrder.setStatusFlag(1);
        counsellingOrder.setPayTime(new Date());
        counsellingOrder.setPayType(orderPayRequest.getPayType());
        //查询客户信息
        Customer customerold = this.customerService.getById(counsellingOrder.getUserId());
        CounsellingInfo counsellingInfo = this.counsellingInfoService.getById(counsellingOrder.getCounsellingInfoId());
        //判断是否有用户咨询师信息
        CounsellingUser counsellingUserOld = this.counsellingUserService.getOne(new LambdaQueryWrapper<CounsellingUser>().eq(CounsellingUser::getCounsellingInfoId,counsellingOrder.getCounsellingInfoId())
                .eq(CounsellingUser::getUserId,counsellingOrder.getUserId()));
        //订单类型 1-首次咨询,2-咨询疗程,3-课程续费
        if (counsellingOrder.getOrderType().intValue() == 1){
            Customer customer = JSONUtil.toBean(counsellingOrder.getUserInfoJson(),Customer.class);
            //生成预约记录
            CounsellingOrderReservation counsellingOrderReservation = new CounsellingOrderReservation();
            counsellingOrderReservation.setCounsellingOrderId(counsellingOrder.getId());
            counsellingOrderReservation.setCounsellingInfoId(counsellingOrder.getCounsellingInfoId());
            counsellingOrderReservation.setCompanionUserId(counsellingOrder.getCompanionUserId());
            counsellingOrderReservation.setConsultantUserId(counsellingOrder.getConsultantUserId());
            counsellingOrderReservation.setRemark(customer.getCurrentRemark());
            counsellingOrderReservation.setUserId(counsellingOrder.getUserId());
            //预约类型 1-首次面诊,2-咨询疗程
            counsellingOrderReservation.setReservationType(1);
            String reservDate = DateUtil.formatDate(counsellingOrder.getFirstAppointmentDate());
            String[] times = counsellingOrder.getFirstAppointmentTimes().split("-");
            counsellingOrderReservation.setReservationBeginTime(DateUtil.parse(reservDate+" "+times[0]+":00"));
            counsellingOrderReservation.setReservationEndTime(DateUtil.parse(reservDate+" "+times[1]+":00"));
            //预约状态 1-待审批,2-待服务,3-服务中,4-已完成,5-已取消,6-已拒绝 - 首次面诊直接待服务
            counsellingOrderReservation.setStauts(2);
            this.counsellingOrderReservationService.save(counsellingOrderReservation);
            //新增
//            iImGroupService.createGroup(2,null,null,counsellingInfo.getUserId(),null,null,counsellingOrder.getUserId(),counsellingOrder.getCounsellingInfoId());
 
 
            //新增
 
 
            //将此条消息加入到可聊天的表中t_mental_appointment
            MentalAppointment mentalAppointment = MentalAppointment.builder()
                    .userId(counsellingOrder.getUserId())
                    .type("1")
                    .statusFlag(1)
                    .appointmentDay(counsellingOrder.getFirstAppointmentDate())
                    .beginTimePoint(counsellingOrder.getFirstAppointmentTimes().split("-")[0])
                    .endTimePoint(counsellingOrder.getFirstAppointmentTimes().split("-")[1])
                    .workerId(counsellingInfo.getUserId())
                    .build();
            // 用户信息
            CustomerInfo customerInfo = customerService.getCustomerInfoById(counsellingOrder.getUserId());
            mentalAppointment.setUserName(customerInfo.getRealName());
            mentalAppointment.setPhone(customerInfo.getLinkPhone());
            mentalAppointmentService.save(mentalAppointment);
 
            // 创建一个 SimpleDateFormat 实例,指定日期格式为 "yyyy-MM-dd"
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
 
// 使用 formatter 将 Date 对象转换为字符串
            String formattedDate = formatter.format(counsellingOrder.getFirstAppointmentDate());
            // 发送IM消息
            ImPushDataDTO pushData1 = ImPushDataDTO.builder()
                    .type(ImPushTypeEnum.C_TO_W_IM_1V1_START_CONSULT_FIRST.getCode())
                    .title(ImPushTypeEnum.C_TO_W_IM_1V1_START_CONSULT_FIRST.getName())
                    .content("预约成功"+",请注意预约时间:"+formattedDate+counsellingOrder.getFirstAppointmentTimes())
//                    .content("预约成功!")
                    .objId(ObjUtil.toString(counsellingInfo.getId()))
                    .data1(ObjUtil.toString(counsellingOrder.getUserId()))
                    .data2(ObjUtil.toString(counsellingInfo.getUserId()))
                    .build();
            imBizService.messageSendPrivate(
                    ObjUtil.toString(counsellingOrder.getUserId()),
                    new String[]{ObjUtil.toString(counsellingInfo.getUserId())},
                    pushData1);
 
            // 推送消息内容
            String pushContent = "你的预约("+DateUtil.formatDate(counsellingOrder.getFirstAppointmentDate())+" "+counsellingOrder.getFirstAppointmentTimes()+")已确认,请按时参加";
            // IM推送数据json
            ImPushDataDTO pushData = ImPushDataDTO.builder()
                    .type(ImPushTypeEnum.S_TO_W_TIP_CONSULT_PAY_SUCCESS.getCode())
                    .objId(ObjUtil.toString(counsellingOrderReservation.getId()))
                    .title("通知")
                    .data1(ObjUtil.toString(counsellingInfo.getUserId()))
                    .data2(ObjUtil.toString(counsellingOrder.getUserId()))
                    .content(pushContent)
//                    .extra("("+DateUtil.formatDate(counsellingOrder.getFirstAppointmentDate())+" "+counsellingOrder.getFirstAppointmentTimes()+")")
                    .build();
            // 发送首次预约
            imBizService.messageSendSystem(counsellingOrderReservation.getUserId()+"", new String[]{counsellingOrderReservation.getUserId()+""}, pushData, ImUserTypeEnum.USER, null, true);
 
 
            //给咨询师发消息
            Customer customerOld = this.customerService.getById(counsellingOrderReservation.getUserId());
 
            String pushContent1 = "你有新的预约,请注意查收。预约用户:"+customerOld.getNickName()+",预约时间:"+DateUtil.formatDate(counsellingOrder.getFirstAppointmentDate())+" "+counsellingOrder.getFirstAppointmentTimes();
//                    +"预约时间:"+counsellingOrder.getEffectiveEndTime()+"~"+counsellingOrder.getEffectiveEndTime();
            // IM推送数据json
            ImPushDataDTO pushData2 = ImPushDataDTO.builder()
                    .type(ImPushTypeEnum.S_TO_W_TIP_CONSULT_PAY_GROUP_SUCCESS.getCode())
                    .objId(ObjUtil.toString(counsellingInfo.getUserId()))
                    .title("通知")
                    .content(pushContent1)
                    .data1(ObjUtil.toString(counsellingOrder.getUserId()))
                    .data2(ObjUtil.toString(counsellingInfo.getUserId()))
//                    .extra("去查看。")
                    .build();
            // 发送预约提示
            imBizService.messageSendSystem(counsellingOrder.getUserId()+"", new String[]{counsellingInfo.getUserId()+""}, pushData2, ImUserTypeEnum.WORKER, PostIdEnum.PO_22, true);
 
 
            // 推送消息内容
//            String pushContent = "我购买了心理咨询疗程,需要咨询";
//            // IM推送数据json
//            ImPushDataDTO pushData = ImPushDataDTO.builder()
//                    .type(ImPushTypeEnum.S_TO_W_TIP_CONSULT_PAY_SUCCESS.getCode())
//                    .objId(ObjUtil.toString(counsellingOrderReservation.getId()))
//                    .title("通知")
//                    .content(pushContent)
////                    .extra("心理咨询疗程")
//                    .build();
//            // 发送首次预约
//            imBizService.messageSendGroup(counsellingOrderReservation.getUserId()+"", new String[]{counsellingOrderReservation.getUserId()+""}, pushData);
        } else if (counsellingOrder.getOrderType().intValue() == 2){
 
            if (counsellingUserOld != null && counsellingUserOld.getConsultantUserId() != null){
                counsellingOrder.setConsultantUserId(counsellingUserOld.getConsultantUserId());
            }else{
                if (customerold.getConsultWorkerId() != null){
                    counsellingOrder.setConsultantUserId(customerold.getConsultWorkerId());
                }else{
                    Customer customer2 = customerService.randomWorkerByLineStatusAndPost(ImStatusEnum.ON_LINE.getCode(), null, PostIdEnum.PO_21.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                    if (customer2 != null){
                        //咨询顾问id
                        counsellingOrder.setConsultantUserId(customer2.getCustomerId());
//                        counsellingUserOld.setConsultantUserId(customer2.getCustomerId());
                    }else {
                        Customer customer3 = customerService.randomWorkerByLineStatusAndPost(null, null, PostIdEnum.PO_21.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                        if (customer3 != null){
                            //咨询顾问id
                            counsellingOrder.setConsultantUserId(customer3.getCustomerId());
//                            counsellingUserOld.setConsultantUserId(customer3.getCustomerId());
                        }else{
                            throw new ServiceException("没有在线的咨询顾问!");
                        }
                    }
                }
 
            }
//            Customer customer2 = customerService.randomWorkerByLineStatusAndPost(ImStatusEnum.ON_LINE.getCode(), null, PostIdEnum.PO_21.getCode());
//            if (customer2 != null){
//                //咨询顾问id
//                counsellingOrder.setConsultantUserId(customer2.getCustomerId());
//            }else {
//                throw new ServiceException("没有在线的咨询顾问!");
//            }
            CounsellingSetMeal counsellingSetMeal = JSONUtil.toBean(counsellingOrder.getSetMealJson(),CounsellingSetMeal.class);
            //是否赠送陪护
            if (counsellingSetMeal.getIsGive() != null && counsellingSetMeal.getIsGive().intValue() == 1){
                if (counsellingUserOld != null && counsellingUserOld.getCompanionUserId() != null){
                    //陪护教练id
                    counsellingOrder.setCompanionUserId(counsellingUserOld.getCompanionUserId());
                }else if (counsellingUserOld != null && counsellingUserOld.getCompanionUserId() == null){
 
                    Customer customerCompanion = customerService.randomWorkerByLineStatusAndPost(ImStatusEnum.ON_LINE.getCode(), null, PostIdEnum.PO_23.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                    if (customerCompanion != null){
                        //陪护教练id
                        counsellingOrder.setCompanionUserId(customerCompanion.getCustomerId());
                        counsellingUserOld.setCompanionUserId(customerCompanion.getCustomerId());
                    }else{
                        Customer customerCompanion2 = customerService.randomWorkerByLineStatusAndPost(null, null, PostIdEnum.PO_23.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                        if (customerCompanion2 != null){
                            //陪护教练id
                            counsellingOrder.setCompanionUserId(customerCompanion2.getCustomerId());
                            counsellingUserOld.setCompanionUserId(customerCompanion2.getCustomerId());
                        }else{
                            throw new ServiceException("没有在线的陪护教练!");
                        }
                    }
                }
//                Customer customerCompanion = customerService.randomWorkerByLineStatusAndPost(ImStatusEnum.ON_LINE.getCode(), null, PostIdEnum.PO_23.getCode());
//                if (customerCompanion != null){
//                    //陪护教练id
//                    counsellingOrder.setCompanionUserId(customerCompanion.getCustomerId());
//                }else{
//                    throw new ServiceException("没有在线的陪护教练!");
//                }
            }
            counsellingOrder.setEffectiveBeginTime(new Date());
            counsellingOrder.setEffectiveEndTime(DateUtil.endOfDay(DateUtil.offsetDay(counsellingOrder.getEffectiveBeginTime(),counsellingSetMeal.getPeriodValidityDay())));
 
            //生成群聊
//            //查询是否已经存在群聊
//            long imCount = this.iImGroupService.count(new LambdaQueryWrapper<ImGroup>().eq(ImGroup::getOrderId,counsellingOrder.getCounsellingInfoId())
//                    .eq(ImGroup::getGroupType,2).eq(ImGroup::getCreateUser,counsellingOrder.getUserId()).eq(ImGroup::getIsDelete,0));
//            if ( imCount ==0l  ){
                //生成群聊
//                iImGroupService.createGroup(2,counsellingOrder.getConsultantUserId(),counsellingOrder.getCompanionUserId(),counsellingInfo.getUserId(),null,null,counsellingOrder.getUserId(),counsellingOrder.getCounsellingInfoId());
            if (counsellingSetMeal.getIsGive() != null && counsellingSetMeal.getIsGive().intValue() == 1){
                //生成群聊
                iImGroupService.createGroup(2,counsellingOrder.getConsultantUserId(),counsellingOrder.getCompanionUserId(),counsellingInfo.getUserId(),null,null,counsellingOrder.getUserId(),counsellingOrder.getCounsellingInfoId());
 
            }else{
                //生成群聊
                iImGroupService.createGroup(2,null,null,counsellingInfo.getUserId(),null,null,counsellingOrder.getUserId(),counsellingOrder.getCounsellingInfoId());
 
            }
//            }
        } else if (counsellingOrder.getOrderType().intValue() == 3) {
            if (counsellingUserOld != null && counsellingUserOld.getConsultantUserId() != null){
                counsellingOrder.setConsultantUserId(counsellingUserOld.getConsultantUserId());
            }else{
                if (customerold.getConsultWorkerId() != null){
                    counsellingOrder.setConsultantUserId(customerold.getConsultWorkerId());
                }else{
                    Customer customer2 = customerService.randomWorkerByLineStatusAndPost(ImStatusEnum.ON_LINE.getCode(), null, PostIdEnum.PO_21.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                    if (customer2 != null){
                        //咨询顾问id
                        counsellingOrder.setConsultantUserId(customer2.getCustomerId());
//                        counsellingUserOld.setConsultantUserId(customer2.getCustomerId());
                    }else {
                        Customer customer3 = customerService.randomWorkerByLineStatusAndPost(null, null, PostIdEnum.PO_21.getCode(), CustomerWorkStatusEnum.ON_WORK.getCode());
                        if (customer3 != null){
                            //咨询顾问id
                            counsellingOrder.setConsultantUserId(customer3.getCustomerId());
//                            counsellingUserOld.setConsultantUserId(customer3.getCustomerId());
                        }else{
                            throw new ServiceException("没有在线的咨询顾问!");
                        }
                    }
                }
 
            }
            CounsellingSetMeal counsellingSetMeal = JSONUtil.toBean(counsellingOrder.getSetMealJson(),CounsellingSetMeal.class);
            Date nowDate = new Date();
            if (counsellingUserOld == null || counsellingUserOld.getEffectiveEndTime().getTime() < nowDate.getTime()){
                counsellingOrder.setEffectiveBeginTime(nowDate);
                counsellingOrder.setEffectiveEndTime(DateUtil.endOfDay(DateUtil.offsetDay(counsellingOrder.getEffectiveBeginTime(),counsellingSetMeal.getPeriodValidityDay())));
            }else{
                counsellingOrder.setEffectiveBeginTime(counsellingUserOld.getEffectiveEndTime());
                counsellingOrder.setEffectiveEndTime(DateUtil.endOfDay(DateUtil.offsetDay(counsellingOrder.getEffectiveBeginTime(),counsellingSetMeal.getPeriodValidityDay())));
 
            }
            //查询是否已经存在群聊
//            long imCount = this.iImGroupService.count(new LambdaQueryWrapper<ImGroup>().eq(ImGroup::getOrderId,counsellingOrder.getCounsellingInfoId())
//                    .eq(ImGroup::getGroupType,2).eq(ImGroup::getCreateUser,counsellingOrder.getUserId()).eq(ImGroup::getIsDelete,0));
//            if ( imCount ==0l  ){
                //生成群聊
                iImGroupService.createGroup(2,counsellingOrder.getConsultantUserId(),counsellingOrder.getCompanionUserId(),counsellingInfo.getUserId(),null,null,counsellingOrder.getUserId(),counsellingOrder.getCounsellingInfoId());
 
//            }
            //生成群聊
//            iImGroupService.createGroup(2,counsellingOrder.getConsultantUserId(),counsellingOrder.getCompanionUserId(),counsellingInfo.getUserId(),null,null,counsellingOrder.getUserId(),counsellingOrder.getId());
        }
 
        //添加咨询用户信息
        if (counsellingUserOld == null ){
            CounsellingUser counsellingUser = BeanUtil.copyProperties(counsellingOrder,CounsellingUser.class);
            counsellingUser.setId(null);
            counsellingUser.setIsFirstAppointment(2);
            this.counsellingUserService.save(counsellingUser);
        }else{
            if (counsellingOrder.getOrderType().intValue() != 1){
                if (counsellingUserOld.getEffectiveBeginTime() == null){
                    counsellingUserOld.setEffectiveBeginTime(counsellingOrder.getEffectiveBeginTime());
                }
                counsellingUserOld.setEffectiveEndTime(counsellingOrder.getEffectiveEndTime());
                counsellingUserOld.setClassHours(counsellingUserOld.getClassHours() !=null ? counsellingUserOld.getClassHours().intValue() + counsellingOrder.getClassHours().intValue():counsellingOrder.getClassHours());
                counsellingUserOld.setResidueClassHours(counsellingUserOld.getResidueClassHours() !=null ? counsellingUserOld.getResidueClassHours().intValue() + counsellingOrder.getResidueClassHours().intValue():counsellingOrder.getResidueClassHours());
                counsellingUserOld.setStatusFlag(counsellingOrder.getStatusFlag());
            }else {
                counsellingUserOld.setUserInfoJson(counsellingOrder.getUserInfoJson());
                counsellingUserOld.setIsFirstAppointment(2);
            }
            if (StrUtil.isBlank(counsellingUserOld.getUserInfoJson()) && StrUtil.isNotBlank(counsellingOrder.getUserInfoJson())){
                counsellingUserOld.setUserInfoJson(counsellingOrder.getUserInfoJson());
            }
            if (counsellingUserOld.getCompanionUserId() != null){
                counsellingOrder.setCompanionUserId(counsellingUserOld.getCompanionUserId());
            }else{
                counsellingUserOld.setCompanionUserId(counsellingOrder.getCompanionUserId());
            }
            if (counsellingUserOld.getConsultantUserId() != null){
                counsellingOrder.setConsultantUserId(counsellingUserOld.getConsultantUserId());
            }else{
                counsellingUserOld.setConsultantUserId(counsellingOrder.getConsultantUserId());
            }
            if (StrUtil.isBlank(counsellingUserOld.getPhone())  && StrUtil.isNotBlank(counsellingOrder.getPhone())){
                counsellingUserOld.setPhone(counsellingOrder.getPhone());
            }
            counsellingUserOld.setStatusFlag(counsellingOrder.getStatusFlag());
            this.counsellingUserService.updateById(counsellingUserOld);
 
        }
        this.updateById(counsellingOrder);
        //分配咨询顾问
        if (customerold.getConsultWorkerId() == null && counsellingOrder.getConsultantUserId() != null ){
 
//            Customer customer = JSONUtil.toBean(counsellingOrder.getUserInfoJson(),Customer.class);
//            if (customer != null){
//                customer.setCustomerId(counsellingOrder.getUserId());
//                customerold.setRealName(customer.getRealName());
//                customerold.setSex(customer.getSex());
//                customerold.setAvatar(customer.getAvatar());
//                customerold.setAvatarObjectName(customer.getAvatarObjectName());
//                customerold.setLinkPhone(customer.getLinkPhone());
//                customerold.setBirthday(customer.getBirthday());
//                customerold.setEmail(customer.getEmail());
//                customerold.setEducationLevel(customer.getEducationLevel());
//                customerold.setOccupation(customer.getOccupation());
//                customerold.setMarital(customer.getMarital());
//                customerold.setCityCode(customer.getCityCode());
//                customerold.setIncome(customer.getIncome());
//                customerold.setChildAge(customer.getChildAge());
//            }
 
            customerold.setConsultWorkerId(counsellingOrder.getConsultantUserId());
            this.customerService.updateCustomerRemoveCache(customerold);
 
        }
    }
 
    @Override
    public CounsellingOrder getCounsellingOrderByNo(String orderNo) {
        return this.getOne(
                Wrappers.<CounsellingOrder>lambdaQuery()
                        .eq(CounsellingOrder::getOrderNo, orderNo)
                        .last("limit 1")
        );
    }
 
 
}