zhangxu
2022-07-20 6d74163dc2d9675d0af367c86a0c9c70cbd18cd3
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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
package com.hx.phip.util.api;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hx.common.service.CommonService;
import com.hx.mybatisTool.SqlSentence;
import com.hx.phiappt.common.OperationReasonConstants;
import com.hx.phiappt.common.OrderItemConstants;
import com.hx.phiappt.common.OrderLogConstants;
import com.hx.phiappt.constants.tool.CouponTool;
import com.hx.phiappt.constants.tool.SqlSplicingTool;
//import com.hx.phiappt.constants.tool.ValueAddedFundTool;
import com.hx.phiappt.dao.mapper.*;
import com.hx.phiappt.model.*;
import com.hx.phiappt.model.activity.ActivityAction;
import com.hx.phiappt.model.activity.ActivityCondition;
import com.hx.phiappt.model.activity.ActivityGroup;
import com.hx.phiappt.model.activity.ActivityRule;
import com.hx.phiappt.model.cardItem.CardEquity;
import com.hx.phiappt.model.cardItem.CardItem;
import com.hx.phiappt.model.cardItem.CardItemInfo;
import com.hx.phiappt.model.consumables.Consumables;
import com.hx.phiappt.model.coupon.CouponReleaseRecordItem;
import com.hx.phiappt.model.limit.MemberLimit;
import com.hx.phiappt.model.memberLevel.MemberLevel;
import com.hx.phiappt.model.order.*;
import com.hx.phiappt.model.project.ProjectShopPrice;
import com.hx.phiappt.model.promotion.Promotion;
import com.hx.phiappt.model.userMoney.UserMoneyUnclaimed;
import com.hx.phip.config.CustomParameter;
import com.hx.phip.dao.mapper.*;
import com.hx.phip.dao.mapper.CardEquityMapper;
import com.hx.phip.dao.mapper.CardItemInfoMapper;
import com.hx.phip.dao.mapper.CardItemMapper;
import com.hx.phip.dao.mapper.ConsumablesMapper;
import com.hx.phip.dao.mapper.EmployeeMapper;
import com.hx.phip.dao.mapper.KoapOrderItemMapper;
import com.hx.phip.dao.mapper.MemberLevelMapper;
import com.hx.phip.dao.mapper.MemberLimitMapper;
import com.hx.phip.dao.mapper.OrderDeveloperMapper;
import com.hx.phip.dao.mapper.OrderInfoMapper;
import com.hx.phip.dao.mapper.OrderItemSonMapper;
import com.hx.phip.dao.mapper.OrderLogMapper;
import com.hx.phip.dao.mapper.OrdersTotalMapper;
import com.hx.phip.dao.mapper.ProjectMapper;
import com.hx.phip.dao.mapper.PromotionMapper;
import com.hx.phip.dao.mapper.ShopMapper;
import com.hx.phip.dao.mapper.SystemParameterMapper;
import com.hx.phip.dao.mapper.UserMapper;
import com.hx.phip.dto.OperatorDto;
import com.hx.phip.tool.CreateNo;
import com.hx.util.StringUtils;
import com.platform.constants.PlatformPattern;
import com.platform.exception.PlatTipsException;
import com.platform.resultTool.PlatformCode;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * 订单-开单工具类
 */
public class OrderAddUtil {
    private static final Logger logger = LoggerFactory.getLogger(OrderAddUtil.class);
 
 
 
    /**
     * 处理一级子订单信息
     * @param ordersTotal
     * @param items
     * @param shopId
     * @param jsonObject
     * @param map
     */
    public static Map<String,Object> handOrderItem(OrdersTotal ordersTotal, List<OrderItem> items, String shopId, JSONArray jsonObject, Map<String, Object> map,BigDecimal totalPrice,BigDecimal shouldOrderTotal,
                                                   BigDecimal discountTotal,CommonService commonService,SystemParameterMapper systemParameterMapper) {
        BigDecimal buyNum;
        BigDecimal actualTotal =BigDecimal.ZERO;//子订单实付金额
        BigDecimal sonTotalPrice =BigDecimal.ZERO;//子订单订单总额
        SqlSentence sqlSentence;
        Map<String,Object> objectMap=new HashMap<>();;
        //计算总订单金额信息,填补子订单信息
        for(OrderItem vo:items){
            buyNum=new BigDecimal(vo.getBuyNum());
            //子订单编号
            String orderNum = CreateNo.createTimeNo(systemParameterMapper, "SO", 16, "yyyyMMdd");//订单编号
            vo.setOrderNo(orderNum);
            map.put("specsValue","");
            map.put("unit","");
            map.put("specsId","");
            map.put("shopId",shopId);
            if(vo.getType().equals(OrderItemConstants.TYPE_PROJECT)){
                //处理项目
                sqlSentence=new SqlSentence();
                sqlSentence.setM(objectMap);
                objectMap.put("commonId",vo.getCommonId());
                sqlSentence.setSqlSentence("SELECT p.* FROM project p LEFT JOIN syn_project sp ON p.apiId=sp.projectId WHERE  (p.id=#{m.commonId} or sp.id=#{m.commonId}) and p.isDel =0 and p.isUp =1");
                Project project=commonService.selectOne(ProjectMapper.class,sqlSentence);
                if(project==null){
                    throw new PlatTipsException(PlatformCode.ERROR_TIPS,"找不到项目:"+vo.getCommonId());
                }
//                //计算项目价格,如果总订单有康博佳标识代表是康博佳订单不需要任何处理直接拿去康博佳的总金额,每个门店价格不一样,如果没有设置门店价格就使用项目售价
//                BigDecimal price=priceJudge(ordersTotal, commonService,project.getId(),shopId);
//                project.setPrice(BigDecimal.ZERO.compareTo(price)==0?project.getPrice():price);
                Map<String, BigDecimal> bigDecimalMap = handleProject(jsonObject, map, vo, actualTotal, sonTotalPrice, discountTotal, buyNum,commonService,project);
                actualTotal=bigDecimalMap.get("actualTotal");
                sonTotalPrice=bigDecimalMap.get("sonTotalPrice");
                discountTotal=bigDecimalMap.get("discountTotal");
            }else if(vo.getType().equals(OrderItemConstants.TYPE_RETAIL)){
                //处理商品
                Map<String, BigDecimal> bigDecimalMap =handleConsumables(jsonObject, map, vo, actualTotal, sonTotalPrice, discountTotal, buyNum,commonService);
                actualTotal=bigDecimalMap.get("actualTotal");
                sonTotalPrice=bigDecimalMap.get("sonTotalPrice");
                discountTotal=bigDecimalMap.get("discountTotal");
            }else if(vo.getType().equals(OrderItemConstants.TYPE_MEAL)){//套餐  暂定
 
            }else if(vo.getType().equals(OrderItemConstants.TYPE_CARD) ){//卡项  暂定
                //处理卡项
                Map<String, BigDecimal> bigDecimalMap =handleCardItem(jsonObject, map, vo, actualTotal, sonTotalPrice, discountTotal, buyNum,
                        ordersTotal.getUserId(),commonService);
                actualTotal=bigDecimalMap.get("actualTotal");
                sonTotalPrice=bigDecimalMap.get("sonTotalPrice");
                discountTotal=bigDecimalMap.get("discountTotal");
            }else if(vo.getType().equals(OrderItemConstants.TYPE_PROMOTION)){//促销  暂定
                //处理卡项
                Map<String, BigDecimal> bigDecimalMap =handlePromotion(jsonObject, map, vo, actualTotal, sonTotalPrice, discountTotal, buyNum,
                        ordersTotal.getUserId(),commonService);
                actualTotal=bigDecimalMap.get("actualTotal");
                sonTotalPrice=bigDecimalMap.get("sonTotalPrice");
                discountTotal=bigDecimalMap.get("discountTotal");
            }else if(vo.getType().equals(OrderItemConstants.CARD_BAG)){//卡包
                if(StringUtils.isEmpty(vo.getUserCardId())){
                    throw new PlatTipsException(PlatformCode.ERROR_TIPS,"下单:开卡包订单需要卡包标识!");
                }
                //处理卡项
                Map<String, BigDecimal> bigDecimalMap =handleCardBag(jsonObject, map, vo, actualTotal, sonTotalPrice, discountTotal, buyNum,
                        ordersTotal.getUserId(),commonService);
                actualTotal=bigDecimalMap.get("actualTotal");
                sonTotalPrice=bigDecimalMap.get("sonTotalPrice");
                discountTotal=bigDecimalMap.get("discountTotal");
            }
 
 
            //子订单实付金额
            vo.setActualTotal(actualTotal);
            vo.setUserPaidTotal(BigDecimal.ZERO);
            //子订单总额
            vo.setTotal(sonTotalPrice);
 
            totalPrice=totalPrice.add(vo.getTotal());//订单总金额
            shouldOrderTotal=shouldOrderTotal.add(vo.getActualTotal());//订单应付支付总金额
            sonTotalPrice=BigDecimal.ZERO;
        }
        objectMap.clear();
        objectMap.put("items",items);
        objectMap.put("totalPrice",totalPrice);
        objectMap.put("shouldOrderTotal",shouldOrderTotal);
        objectMap.put("discountTotal",discountTotal);
        return objectMap;
    }
 
    /**
     * 创建总订单
     * @param ordersTotal           总订单信息
     * @param operator              操作人map
     * @param user                  用户资料
     * @param userMoney             用户资金信息
     * @param commonService
     */
    public static OrdersTotal createOrder(OrdersTotal ordersTotal, Map<String, String> operator, User user, UserMoney userMoney, CustomParameter customParameter,CommonService commonService) {
        if(ordersTotal!=null && StringUtils.noNull(ordersTotal.getIntegral())){
            User users=commonService.selectOneByKeyBlob(UserMapper.class,ordersTotal.getInviteeId());
            if(users!=null){
                ordersTotal.setUserBelongingType(users.getUserBelongingType());
            }
        }
        //活动规则金额判断
        if( ordersTotal.getActivityIds() !=null && ordersTotal.getActivityIds().size()>0){
            BigDecimal price=activityRule(ordersTotal,commonService,operator,userMoney,customParameter,user,0);
            if(price !=BigDecimal.ZERO){
                ordersTotal.setActivityTotal(ordersTotal.getShouldTotal().subtract(price));
                ordersTotal.setShouldTotal(price);
            }
        }
 
        if(StringUtils.noNull(ordersTotal.getKoapOrderId())){
            BigDecimal koapItemMoney = SynOrderUtil.koapItemMoney(ordersTotal.getKoapOrderId(), commonService);
 
            ordersTotal.setShouldTotal(koapItemMoney);
            ordersTotal.setTotal(koapItemMoney);
        }
        ordersTotal.setUserLevel(user.getUserLevel());
        ordersTotal.setCIQ(user.getCIQ());
        ordersTotal.setStoredValueFund(userMoney ==null ?"":userMoney.getStoredValueFund());
        ordersTotal.setValueAddedFund(userMoney ==null ?"":userMoney.getValueAddedFund());
        ordersTotal.setIntegral(userMoney ==null ?"":userMoney.getIntegral());
        //创建总订单
        commonService.insert(OrdersTotalMapper.class,ordersTotal);
 
        //如果有用活动规则将活动规则保存在orderInfo
        if( ordersTotal.getActivityIds()!=null && ordersTotal.getActivityIds().size()>0){
            //查询活动规则基础信息
            ActivityRule activityRule=commonService.selectOneByKey(ActivityRuleMapper.class,ordersTotal.getActivityIds().get(0));
            if(activityRule!=null){
                OrderInfo orderInfo = new OrderInfo();
                orderInfo.setOrderId(ordersTotal.getId());
                orderInfo.setActivityId(activityRule.getId());
                orderInfo.setActivityName(activityRule.getName());
                commonService.insert(OrderInfoMapper.class,orderInfo);
            }
        }
        //生成订单日志
        commonService.insert(OrderLogMapper.class,ApiOrderUtil.setOrderLog(ordersTotal,null,operator.get("operatorId"),operator.get("operatorName"),"",0, OrderLogConstants.LOG_TYPE_CREATE));
        return ordersTotal;
    }
 
    /**
     * 创建总订单
     * @param ordersTotal           总订单信息
     * @param operator              操作人
     * @param user                  用户资料
     * @param userMoney             用户资金信息
     * @param commonService
     */
    public static OrdersTotal createOrderNew(OrdersTotal ordersTotal, OperatorDto operator, User user, UserMoney userMoney, CommonService commonService) {
        if(ordersTotal!=null && StringUtils.noNull(ordersTotal.getIntegral())){
            User users=commonService.selectOneByKeyBlob(UserMapper.class,ordersTotal.getInviteeId());
            if(users!=null){
                ordersTotal.setUserBelongingType(users.getUserBelongingType());
            }
        }
        ordersTotal.setUserLevel(user.getUserLevel());
        ordersTotal.setCIQ(user.getCIQ());
        ordersTotal.setStoredValueFund(userMoney ==null ?"":userMoney.getStoredValueFund());
        ordersTotal.setValueAddedFund(userMoney ==null ?"":userMoney.getValueAddedFund());
        ordersTotal.setIntegral(userMoney ==null ?"":userMoney.getIntegral());
        //创建总订单
        commonService.insert(OrdersTotalMapper.class,ordersTotal);
        //生成订单日志
        commonService.insert(OrderLogMapper.class,ApiOrderUtil.setOrderLog(ordersTotal,null,operator.getOperatorId(),operator.getOperatorName(),"",0, OrderLogConstants.LOG_TYPE_CREATE));
        return ordersTotal;
    }
 
    /**
     *  开单-处理项目方法
     * @param jsonObject            规格json
     * @param map                   参数集合
     * @param vo                    一级子订单
     * @param actualTotal           商品实付金额
     * @param sonTotalPrice         商品订单总额
     * @param discountTotal         折扣总金额
     * @param buyNum                购买次数
     * @param project                项目对象
     * @return
     */
    public static Map<String, BigDecimal> handleProject(JSONArray jsonObject, Map<String, Object> map, OrderItem vo,
                                                  BigDecimal actualTotal, BigDecimal sonTotalPrice, BigDecimal discountTotal, BigDecimal buyNum,
                                                  CommonService commonService,Project project) {
 
        if(project.getPrice()==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"项目:"+project.getName()+",没有销售价格,请填写销售单价再进行创建订单");
        }
        //项目实付金额=项目的销售价格*购买数量*折扣
        actualTotal = project.getPrice().multiply(buyNum).multiply(vo.getDiscount().divide(new BigDecimal(100)));
 
        //项目订单总额=项目原价*购买数量,如果项目没有原价就用现价
        sonTotalPrice=sonTotalPrice.add(project.getOriginalPrice()==null || BigDecimal.ZERO.compareTo(project.getOriginalPrice())==0?project.getPrice().multiply(buyNum):project.getOriginalPrice().multiply(buyNum));
 
        //优惠金额=项目的销售单价*数量-用户实付金额;
        discountTotal=discountTotal.add(project.getPrice().multiply(buyNum).subtract(actualTotal));
 
        vo.setSingle(project.getTreatmentItemsNum()==null?1:project.getTreatmentItemsNum());//单次疗程数量
        vo.setUsedTotal(vo.getSingle()*vo.getBuyNum()); //总次疗程次数,单次疗程次数*购买的总量
        vo.setOriPrice(project.getOriginalPrice());//原价,项目的原价
        vo.setCurPrice(project.getPrice());//现价,项目的销售单价格
 
        map.put("specsTitle",project.getSpecification());
        map.put("unit","");
 
        jsonObject.add(map);
        vo.setSpecs(StringUtils.isEmpty(project.getSpecification())?"[]":jsonObject.toString());//规格
        vo.setGoodsNo(project.getCoding());//项目编号
        vo.setGoodsName(project.getName());//项目名称
        vo.setGoodsImage("");//项目主图
 
        Map<String,BigDecimal> moneyMap=new HashMap<>();
        moneyMap.put("actualTotal",actualTotal);
        moneyMap.put("sonTotalPrice",sonTotalPrice);
        moneyMap.put("discountTotal",discountTotal);
        return moneyMap;
    }
 
    /**
     *  开单-处理商品方法
     * @param jsonObject            规格json
     * @param map                   参数集合
     * @param vo                    一级子订单
     * @param actualTotal           商品实付金额
     * @param sonTotalPrice         商品订单总额
     * @param discountTotal         折扣总金额
     * @param buyNum                购买次数
     * @return
     */
    public static Map<String,BigDecimal> handleConsumables(JSONArray jsonObject, Map<String, Object> map, OrderItem vo,
                                                     BigDecimal actualTotal,BigDecimal sonTotalPrice,BigDecimal discountTotal,BigDecimal buyNum,
                                                     CommonService commonService) {
        Consumables consumables = commonService.selectOneByKey(ConsumablesMapper.class,vo.getCommonId());
        if(consumables==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"找不到项目:"+vo.getCommonId());
        }
        if(consumables.getPrice()==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"商品:"+consumables.getName()+",没有销售价格,请填写销售单价在进行创建订单");
        }
        //商品实付金额=商品的销售价格*购买数量*折扣
        actualTotal = consumables.getPrice().multiply(buyNum).multiply(vo.getDiscount().divide(new BigDecimal(100)));
 
        //商品订单总额=项目销售价*购买数量
        sonTotalPrice=sonTotalPrice.add(consumables.getPrice().multiply(buyNum));
 
        //优惠金额=商品的销售单价*数量-用户实付金额;
        discountTotal=discountTotal.add(consumables.getPrice().multiply(buyNum).subtract(actualTotal));
 
        vo.setSingle(0);//单次疗程数量
        vo.setUsedTotal(vo.getSingle()*vo.getBuyNum());//总次疗程次数,单次疗程次数*购买的总量
        vo.setOriPrice(consumables.getPrice());//原价,项目的销售价
        vo.setCurPrice(consumables.getPrice());//现价
 
        map.put("specsTitle",consumables.getSpec());
        map.put("unit",consumables.getUnitName());
        jsonObject.add(map);
        vo.setSpecs(StringUtils.isEmpty(consumables.getSpec())?"[]":jsonObject.toString());//规格
        vo.setGoodsNo(consumables.getSerialNumber());//项目编号
        vo.setGoodsName(consumables.getName());//项目名称
        vo.setGoodsImage("");//项目主图
 
        Map<String,BigDecimal> moneyMap=new HashMap<>();
        moneyMap.put("actualTotal",actualTotal);
        moneyMap.put("sonTotalPrice",sonTotalPrice);
        moneyMap.put("discountTotal",discountTotal);
        return moneyMap;
    }
 
    /**
     *  开单-处理卡项方法
     * @param jsonObject            规格json
     * @param map                   参数集合
     * @param vo                    一级子订单
     * @param actualTotal           商品实付金额
     * @param sonTotalPrice         商品订单总额
     * @param discountTotal         折扣总金额
     * @param buyNum                购买次数
     * @param userId                用户id
     * @return
     */
    public static Map<String,BigDecimal> handleCardItem(JSONArray jsonObject, Map<String, Object> map, OrderItem vo,
                                                  BigDecimal actualTotal,BigDecimal sonTotalPrice,BigDecimal discountTotal,
                                                  BigDecimal buyNum,String userId, CommonService commonService) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> sqlMp=new HashMap<>();
        sqlSentence.setM(sqlMp);
        sqlMp.put("status",CardItem.SALES);
        sqlMp.put("isUp",BaseEntity.YES);
        sqlMp.put("isDel",BaseEntity.NO);
        sqlMp.put("commonId",vo.getCommonId());
        sqlSentence.setSqlSentence("select * from card_item where  id=#{m.commonId} and  status=#{m.status} and isUp=#{m.isUp} and isDel=#{m.isDel}");
        CardItem cardItem=commonService.selectOne(CardItemMapper.class,sqlSentence);
        if(cardItem==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"找不到该卡项,请确保卡项状态必须是销售中并且是上架状态,卡项标识:"+vo.getCommonId());
        }
        if(cardItem.getTotal()==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"促销:"+cardItem.getName()+",没有销售价格,请填写销售单价在进行创建订单");
        }
        User user=commonService.selectOneByKey(UserMapper.class,userId);
        if(user==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"处理促销操作:未找到该用户信息");
        }
        sqlMp.clear();
        sqlMp.put("isDel", BaseEntity.NO);
        //限制会员等级购买
        sqlMp.put("foreignKey",cardItem.getId());
        checkMemberLevel(sqlSentence, user, OrderItemConstants.TYPE_CARD,commonService);
 
        JSONObject param = new JSONObject();
        param.put("commonId",cardItem.getId());
        param.put("userId",userId);
        param.put("shopId",map.get("shopId"));
        //卡项/促销/项目购买限制
        LimitPlatformUtil.limitMethod(commonService,param.toJSONString());
 
        //商品实付金额=卡项的销售价格*购买数量*折扣
        actualTotal = cardItem.getTotal().multiply(buyNum).multiply(vo.getDiscount().divide(new BigDecimal(100)));
 
        //商品订单总额=项目销售价*购买数量
        sonTotalPrice=sonTotalPrice.add(cardItem.getTotal().multiply(buyNum));
 
        //优惠金额=商品的销售单价*数量-用户实付金额;
        discountTotal=discountTotal.add(cardItem.getTotal().multiply(buyNum).subtract(actualTotal));
 
        vo.setSingle(0);//单次疗程数量
        vo.setUsedTotal(vo.getSingle()*vo.getBuyNum());//总次疗程次数,单次疗程次数*购买的总量
        vo.setOriPrice(cardItem.getTotal());//原价,项目的销售价
        vo.setCurPrice(cardItem.getTotal());//现价
 
        vo.setSpecs("[]");//规格
        vo.setGoodsNo(cardItem.getCode());//卡项编号
        vo.setGoodsName(cardItem.getName());//卡项名称
        vo.setGoodsImage("");//卡项主图
 
        Map<String,BigDecimal> moneyMap=new HashMap<>();
        moneyMap.put("actualTotal",actualTotal);
        moneyMap.put("sonTotalPrice",sonTotalPrice);
        moneyMap.put("discountTotal",discountTotal);
        return moneyMap;
    }
 
    /**
     * 开单-处理促销方法
     * @param jsonObject            规格json
     * @param map                   参数集合
     * @param vo                    一级子订单
     * @param actualTotal           商品实付金额
     * @param sonTotalPrice         商品订单总额
     * @param discountTotal         折扣总金额
     * @param buyNum                购买次数
     * @param userId                用户id
     * @return
     */
    public static Map<String,BigDecimal> handlePromotion(JSONArray jsonObject, Map<String, Object> map, OrderItem vo,
                                                   BigDecimal actualTotal, BigDecimal sonTotalPrice, BigDecimal discountTotal,
                                                   BigDecimal buyNum, String userId, CommonService commonService) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> sqlMp=new HashMap<>();
        sqlSentence.setM(sqlMp);
        sqlMp.put("status",Promotion.IN_PROGRESS);
        sqlMp.put("isUp",BaseEntity.YES);
        sqlMp.put("isDel",BaseEntity.NO);
        sqlMp.put("commonId",vo.getCommonId());
        sqlSentence.setSqlSentence("select * from promotion where  id=#{m.commonId} and  status=#{m.status} and isUp=#{m.isUp} and isDel=#{m.isDel}");
        Promotion promotion=commonService.selectOne(PromotionMapper.class,sqlSentence);
        if(promotion==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"找不到该促销,请确保促销状态必须是销售中并且是上架状态,促销标识:"+vo.getCommonId());
        }
        if(promotion.getTotal()==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"促销:"+promotion.getName()+",没有销售价格,请填写销售单价在进行创建订单");
        }
        User user=commonService.selectOneByKey(UserMapper.class,userId);
        if(user==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"处理促销操作:未找到该用户信息");
        }
        //限制会员等级购买
        sqlMp.clear();
        sqlSentence.setM(sqlMp);
        sqlMp.put("isDel",BaseEntity.NO);
        sqlMp.put("foreignKey",promotion.getId());
        checkMemberLevel(sqlSentence, user, OrderItemConstants.TYPE_PROMOTION,commonService);
 
        JSONObject param = new JSONObject();
        param.put("commonId",promotion.getId());
        param.put("userId",userId);
        param.put("shopId",map.get("shopId"));
        //卡项/促销/项目购买限制
        LimitPlatformUtil.limitMethod(commonService,param.toJSONString());
 
        //商品实付金额=促销的销售价格*购买数量*折扣
        actualTotal = promotion.getTotal().multiply(buyNum).multiply(vo.getDiscount().divide(new BigDecimal(100)));
 
        //商品订单总额=项目销售价*购买数量
        sonTotalPrice=sonTotalPrice.add(promotion.getTotal().multiply(buyNum));
 
        //优惠金额=商品的销售单价*数量-用户实付金额;
        discountTotal=discountTotal.add(promotion.getTotal().multiply(buyNum).subtract(actualTotal));
 
        vo.setSingle(0);//单次疗程数量
        vo.setUsedTotal(vo.getSingle()*vo.getBuyNum());//总次疗程次数,单次疗程次数*购买的总量
        vo.setOriPrice(promotion.getTotal());//原价,项目的销售价
        vo.setCurPrice(promotion.getTotal());//现价
 
        vo.setSpecs("[]");//规格
        vo.setGoodsNo(promotion.getCode());//促销编号
        vo.setGoodsName(promotion.getName());//促销名称
        vo.setGoodsImage("");//促销主图
 
        Map<String,BigDecimal> moneyMap=new HashMap<>();
        moneyMap.put("actualTotal",actualTotal);
        moneyMap.put("sonTotalPrice",sonTotalPrice);
        moneyMap.put("discountTotal",discountTotal);
        return moneyMap;
    }
 
    /**
     * 检查会员等级限制
     * @param sqlSentence
     * @param user
     * @param type
     * @param commonService
     */
    public static void checkMemberLevel(SqlSentence sqlSentence, User user,String type,CommonService commonService) {
        sqlSentence.setSqlSentence("select * from member_limit where isDel=#{m.isDel} and foreignKey=#{m.foreignKey}");
        List<MemberLimit> list=commonService.selectList(MemberLimitMapper.class,sqlSentence);
        StringBuffer stringBuffer = null;
        if(list!=null && list.size()>0){
            List<String> collect = list.stream().map(o -> o.getMemberId()).collect(Collectors.toList());
            stringBuffer=new StringBuffer();
            stringBuffer.append("select hisName from member_level where isDel=#{m.isDel} and id in( ");
            SqlSplicingTool.selectIn(stringBuffer,collect);
            sqlSentence.setSqlSentence(stringBuffer.toString());
            List<MemberLevel> memberList=commonService.selectList(MemberLevelMapper.class,sqlSentence);
            if(memberList!=null){
                List<String> memberNameList = memberList.stream().map(o -> o.getHisName()).collect(Collectors.toList());
                if(!memberNameList.contains(user.getUserLevel())){
                    String name=OrderItemConstants.TYPE_CARD.equals(type)?"卡项":"促销";
                    throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该"+name+"不能购买,该用户等级不在"+name+"范围之内");
                }
            }
        }
    }
 
    /**
     * 设置用户默认的开发人信息
     * @param ordersTotal
     * @return
     */
    public static User handUserDeveloper(OrdersTotal ordersTotal,CommonService commonService,List<String> orderTypeList) {
        //如果开发人集合为空,那么默认用户的所属顾问
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> map=new HashMap<>();
        sqlSentence.setM(map);
        map.put("shopId",ordersTotal.getDeveloperShopId());
        User user = commonService.selectOneByKey(UserMapper.class,ordersTotal.getUserId());
        if(StringUtils.noNull(user.getHisCorpUserId()) && !orderTypeList.contains(ordersTotal.getType())) {
            map.put("hisCorpUserId",user.getHisCorpUserId());
            sqlSentence.setSqlSentence("SELECT s.* FROM employee_role er LEFT JOIN shop s ON er.shopId = s.id WHERE er.isDel=0 and er.employeeId = #{m.hisCorpUserId} AND er.roleUniqueStr ='adviser_leader' and s.id=#{m.shopId}");
            Shop userShop = commonService.selectOne(ShopMapper.class,sqlSentence);
            if (userShop == null) {
                throw new PlatTipsException(PlatformCode.ERROR_TIPS, "用户绑定的顾问不在该门店");
            }
            Employee userAdiver =commonService.selectOneByKey(EmployeeMapper.class,user.getHisCorpUserId());
            //开发人门店标识(用户绑定的销售顾问)
            ordersTotal.setDeveloperShopId(userShop.getId());
            //开发人门店名称
            ordersTotal.setDeveloperShopName(userShop.getName());
            ordersTotal.setDeveloperId(userAdiver.getId());
            ordersTotal.setDeveloperName(userAdiver.getCnName());
        }
        //荣爷说开发人必填此处不需要默认用户的咨询师  22-05-27
//        else if(!orderTypeList.contains(ordersTotal.getType())){
//            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"请填写好开发人或者用户绑定一个销售顾问在重试");
//        }
        return user;
    }
 
 
 
    /**
     * 创建开发人集合记录
     * @param ordersTotal
     * @param orderDeveloperList
     */
    public static void creatDeveloperList(OrdersTotal ordersTotal, List<OrderDeveloper> orderDeveloperList,CommonService commonService) {
        if(orderDeveloperList !=null && orderDeveloperList.size()>0){
            for(OrderDeveloper vo:orderDeveloperList){
                vo.setOrderId(ordersTotal.getId());//总订单id
                commonService.insert(OrderDeveloperMapper.class,vo);
            }
        }
    }
 
    /**
     * 价格判断
     * @param ordersTotal           总订单
     * @param commonService
     * @param commonId              公共标识,/项目/商品/卡项/促销/
     * @return
     */
    public static BigDecimal priceJudge(OrdersTotal ordersTotal, CommonService commonService,  String commonId,String shopId) {
        BigDecimal price=BigDecimal.ZERO;
        SqlSentence sqlSentence=new SqlSentence();
        Map<String, Object> objectMap=new HashMap<>();
        sqlSentence.setM(objectMap);
        if(StringUtils.noNull(ordersTotal.getKoapOrderId())){
            objectMap.put("koapId",ordersTotal.getKoapOrderId());
            sqlSentence.setSqlSentence("SELECT * FROM koap_order_item WHERE orderId=#{m.koapId}");
            List<KoapOrderItem> koapOrderItemList=commonService.selectList(KoapOrderItemMapper.class,sqlSentence);
            if(koapOrderItemList==null || koapOrderItemList.size()==0){
                throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"结账:未找到康博佳子订单");
            }
            //康博佳子订单的应收金额之和(这个金额要等于paymentList里面的所有之和)
            price=koapOrderItemList.stream().map(o -> o.getChargeAmount()).reduce(BigDecimal.ZERO, BigDecimal::add);
        }else{
            objectMap.put("id",commonId);
            objectMap.put("shopId",shopId);
            objectMap.put("isUp",BaseEntity.YES);
            sqlSentence.setSqlSentence("SELECT * FROM project_shop_price WHERE commonId=#{m.id} and shopId=#{m.shopId} and isUp=#{m.isUp}");
            ProjectShopPrice shopPrice=commonService.selectOne(ProjectShopPriceMapper.class,sqlSentence);
            if(shopPrice!=null){
                price=shopPrice.getPrice();
            }
        }
        return price;
    }
 
 
    /**
     * 计算活动规则价格,如果是折扣或者满减就需要在创建订单计算好价格(荣爷说),其他动作分别都在结账处理
     * @param ordersTotal
     * @param type             0:创建订单,1:结账,类型区分来源,如果是创建订单则只需要判断是否有满减或者折扣进行口减总订单的金额,如果是结账则只需要进行将积分、增值金额、优惠券
     * @return
     */
    public static BigDecimal activityRule(OrdersTotal ordersTotal,CommonService commonService,Map<String, String> operator,UserMoney userMoney,
                                          CustomParameter customParameter,User user,Integer type) {
        BigDecimal price=BigDecimal.ZERO;
        if(ordersTotal.getActivityIds() !=null && ordersTotal.getActivityIds().size()>0){
            //查询活动规则基础信息
            ActivityRule activityRule=commonService.selectOneByKey(ActivityRuleMapper.class,ordersTotal.getActivityIds().get(0));
            if(activityRule!=null){
                SqlSentence sqlSentence = new SqlSentence();
                Map<String,Object> map=new HashedMap();
                sqlSentence.setM(map);
                map.put("isDel",BaseEntity.NO);
                map.put("ruleId",activityRule.getId());
                sqlSentence.setSqlSentence("select * from activity_group where isDel=#{m.isDel} and activityRuleId=#{m.ruleId}");
                //查找活动规则的分组
                List<ActivityGroup> activityGroup = commonService.selectList(ActivityGroupMapper.class, sqlSentence);
                if(activityGroup!=null && activityGroup.size()>0){
                    BigDecimal convert=BigDecimal.ZERO;
                    for (ActivityGroup group : activityGroup) {
                        map.put("activityGroupId",group.getId());
                        //type=0,代表是创建订单,需要将活动规则的满减或者满减(折扣)进行价格计算
                        if(type==0){
                            price=fullReduce(convert,ordersTotal,sqlSentence,commonService);
                        }else if(type==1){
                            //如果是结账需要将活动规则的增值金、积分、优惠券到账
                            otherAction(convert,ordersTotal,operator,userMoney,customParameter,sqlSentence,user,commonService);
                        }
 
                    }
                }
            }
        }
        return price;
    }
 
 
    /**
     * 活动规则-处理满减(金额/折扣)动作方法
     * @param commonService
     * @param convert
     * @param ordersTotal
     * @return
     */
    public static BigDecimal fullReduce(BigDecimal convert,OrdersTotal ordersTotal,SqlSentence sqlSentence,CommonService commonService){
        sqlSentence.setSqlSentence("select * from activity_action where activityGroupId=#{m.activityGroupId} and (type='full_reduce' or type='discount') ");
        //查找活动组的满减金额或者满减(折扣)的动作,满减金额或者满减折扣一个组中只能存在一个
        List<ActivityAction> activityActions = commonService.selectList(ActivityActionMapper.class, sqlSentence);
        if(activityActions!=null && activityActions.size()==1){
            sqlSentence.setSqlSentence("select * from activity_condition where activityGroupId=#{m.activityGroupId}");
            //根据活动组来查询活动的条件集合,同时满足才能进行满减金额或者满减折扣
            List<ActivityCondition> activityConditions = commonService.selectList(ActivityConditionMapper.class, sqlSentence);
            if(activityConditions!=null && activityConditions.size()>0){
                boolean flag=false;
                for(ActivityCondition activityCondition:activityConditions){
                    convert=new BigDecimal(activityCondition.getWorth());
                    boolean isFull = checkCondition(ordersTotal, commonService, convert, activityCondition);
                    //只要有一条条件不满足就直接返回活动价格,一个组里面所有条件都需要同时满足
                    if(!isFull){
                        throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"活动规则:该订单不满足活动规则条件,请仔细检查活动,活动规则条件标识:"+activityCondition.getId());
//                        return BigDecimal.ZERO;
                    }
                    logger.info("开单:isFull:{}",isFull);
                    flag=isFull;
                }
                if(flag){
                    logger.info("开单:isFull:{}",flag);
                    for (ActivityAction activityAction : activityActions) {
                        convert=new BigDecimal(activityAction.getWorth());
                        if(ActivityAction.TYPE_DISCOUNT.equals(activityAction.getType())){
                            return ordersTotal.getShouldTotal().multiply(convert).divide(new BigDecimal(100));
                        }else if(ActivityAction.TYPE_FULL_REDUCE.equals(activityAction.getType())){
                            return ordersTotal.getShouldTotal().subtract(convert);
                        }
                    }
                }
            }
        }else if(activityActions!=null && activityActions.size()>1){
            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"活动规则:创建订单时找到一个组中同时存在折扣和满减");
        }
        return BigDecimal.ZERO;
    }
 
    /**
     * 活动规则-其他动作(增值金、积分、优惠券)  结账使用
     * @param convert
     * @param ordersTotal                   总订单
     * @param operator                      操作人map
     * @param userMoney                     用户资金信息
     * @param customParameter
     * @param sqlSentence
     * @param commonService
     */
    private static void otherAction(BigDecimal convert, OrdersTotal ordersTotal,Map<String, String> operator,UserMoney userMoney,
                                    CustomParameter customParameter,SqlSentence sqlSentence,User user,CommonService commonService) {
        List<String> fullTypeList = Arrays.asList(ActivityAction.TYPE_FULL_REDUCE, ActivityAction.TYPE_DISCOUNT);
        sqlSentence.setSqlSentence("select * from activity_action where activityGroupId=#{m.activityGroupId} ");
        //查找活动组的满减金额或者满减(折扣)的动作,满减金额或者满减折扣一个组中只能存在一个
        List<ActivityAction> activityActions = commonService.selectList(ActivityActionMapper.class, sqlSentence);
        if(activityActions!=null && activityActions.size()>0){
            activityActions= activityActions.stream().filter(o -> !fullTypeList.contains(o.getType())).collect(Collectors.toList());
            sqlSentence.setSqlSentence("select * from activity_condition where activityGroupId=#{m.activityGroupId}");
            //根据活动组来查询活动的条件集合,同时满足才能进行满减金额或者满减折扣
            List<ActivityCondition> activityConditions = commonService.selectList(ActivityConditionMapper.class, sqlSentence);
            if(activityConditions!=null && activityConditions.size()>0){
                boolean flag=false;
                for(ActivityCondition activityCondition:activityConditions){
                    convert=new BigDecimal(activityCondition.getWorth());
                    if(ordersTotal.getActivityTotal()!=null){
                        convert=convert.subtract(ordersTotal.getActivityTotal());
                    }
                    boolean isFull = checkCondition(ordersTotal, commonService, convert, activityCondition);
                    //只要有一条条件不满足就结束
                    if(!isFull){
                        throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"活动规则:该订单结账不满足活动规则条件,请仔细检查活动,活动规则条件标识:"+activityCondition.getId());
                    }
                    logger.info("结账:isFull:{}",isFull);
                    flag=isFull;
                }
                if(flag){
                    logger.info("结账:isFull:{}",flag);
                    //用户信息集合
                    net.sf.json.JSONArray array;
                    net.sf.json.JSONObject object;
                    CouponReleaseRecordItem couponReleaseRecordItem;
                    BigDecimal worth;
                    for (ActivityAction activityAction : activityActions) {
 
                        switch (activityAction.getType()){
                            case ActivityAction.TYPE_VALUEADDEDFUND:
                                worth=new BigDecimal(activityAction.getWorth());
                                //增加增值金
//                                ValueAddedFundTool.add(ordersTotal.getUserId(),ordersTotal.getId(),convert);
//                                UserMoneyUtil.dealValueAddFund(operator,ordersTotal.getUserId(),userMoney,convert,customParameter,commonService,1);
                                UserMoneyUtil.setUserMoneyUnclaimed(ordersTotal, activityAction.getId(),worth,UserMoneyUnclaimed.FUND_TYPE_VALUE_ADDED_FUND,OperationReasonConstants.OP_REASON_ACTIVITY_RULE_GIFT,commonService,UserMoneyUnclaimed.YES);
                                break;
                            case ActivityAction.TYPE_INTEGRAL:
                                worth=new BigDecimal(activityAction.getWorth());
                                //增加积分,积分先暂时不做是否过期
//                                ValueAddedFundTool.add(ordersTotal.getUserId(),ordersTotal.getId(),convert);
//                                UserMoneyUtil.dealIntegral(operator,ordersTotal.getUserId(),userMoney,convert,customParameter,commonService,1);
                                UserMoneyUtil.setUserMoneyUnclaimed(ordersTotal, activityAction.getId(),worth,UserMoneyUnclaimed.FUND_TYPE_INTEGRAL,OperationReasonConstants.OP_REASON_ACTIVITY_RULE_GIFT,commonService,UserMoneyUnclaimed.YES);
                                break;
                            case ActivityAction.TYPE_COUPON:
                                if(ActivityAction.TYPE_PLATFORM_CRM.equals(activityAction.getPlatformType())){
                                    SynOrderUtil.sendCrmCoupon(customParameter, user.getTel(),user.getApiId(), activityAction.getCrmCouponId());
                                }else if(ActivityAction.TYPE_PLATFORM_PHIS.equals(activityAction.getPlatformType())){
                                    object=new net.sf.json.JSONObject();
                                    object.put("apiId",user.getApiId());
                                    object.put("createTime",user.getCreateTime());
                                    object.put("gender",user.getGender());
                                    object.put("id",user.getId());
                                    object.put("imgUrl",user.getImgUrl());
                                    object.put("name",user.getName());
                                    object.put("tel",user.getTel());
                                    array=new net.sf.json.JSONArray();
                                    array.add(object);
                                    couponReleaseRecordItem=new CouponReleaseRecordItem();
                                    couponReleaseRecordItem.setCouponId(activityAction.getWorth());
                                    couponReleaseRecordItem.setRecordName("系统赠送");
                                    //赠送优惠券给用户
                                    CouponTool.sendCoupon(couponReleaseRecordItem,operator.get("operatorId"),operator.get("operatorName"),operator.get("roleId"),array,commonService);
                                }
                                break;
                        }
 
                    }
                }
            }
        }
    }
 
    /**
     * 活动规则-判断条件是否符合
     * @param ordersTotal               总订单
     * @param commonService
     * @param convert                   条件值
     * @param activityCondition         条件对象
     * @return
     */
    private static boolean checkCondition(OrdersTotal ordersTotal, CommonService commonService, BigDecimal convert, ActivityCondition activityCondition) {
        boolean flag=false;
        if(ActivityCondition.TYPE_ORDER.equals(activityCondition.getGenre())){
            switch (activityCondition.getSymbol()){
                case "<":
                    if(ordersTotal.getShouldTotal().compareTo(convert)<0){
                        flag=true;
                    }
                    break;
                case ">":
                    if(ordersTotal.getShouldTotal().compareTo(convert)>0){
                        flag=true;
                    }
                    break;
                case "<=":
                    if(ordersTotal.getShouldTotal().compareTo(convert)<=0){
                        flag=true;
                    }
                    break;
                case ">=":
                    if(ordersTotal.getShouldTotal().compareTo(convert)>=0){
                        flag=true;
                    }
                case "=":
                    if(ordersTotal.getShouldTotal().compareTo(convert)==0){
                        flag=true;
                    }
                    break;
                default:
                    break;
            }
        }else if(ActivityCondition.TYPE_USER.equals(activityCondition.getGenre())){
            switch (activityCondition.getSymbol()){
                case "=":
                    if(ActivityCondition.CONDITION_USER_LEVEL.equals(activityCondition.getPrerequisite())){
                        User user=commonService.selectOneByKey(UserMapper.class,ordersTotal.getUserId());
                        if(user==null){
                            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"活动规则:条件为用户条件时根据订单未找到该用户信息");
                        }
                        if(activityCondition.getWorth().equals(user.getUserLevel())){
                            flag=true;
                        }
                    }else if(ActivityCondition.CONDITION_USER_CLINCH_TAG.equals(activityCondition.getPrerequisite())){
 
                    }
                    break;
                case "in":
                    if(ActivityCondition.CONDITION_USER_LEVEL.equals(activityCondition.getPrerequisite())){
                        List<String> worths = Arrays.asList(activityCondition.getWorth().split(","));
                        User user=commonService.selectOneByKey(UserMapper.class,ordersTotal.getUserId());
                        if(user==null){
                            throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"活动规则:条件为用户条件时根据订单未找到该用户信息");
                        }
                        if(worths.contains(user.getUserLevel())){
                            flag=true;
                        }
                    }else if(ActivityCondition.CONDITION_USER_CLINCH_TAG.equals(activityCondition.getPrerequisite())){
 
                    }
                    break;
                default:
                    break;
            }
        }
        return flag;
    }
 
 
    /**
     *  开单-处理卡包方法
     * @param jsonObject            规格json
     * @param map                   参数集合
     * @param vo                    一级子订单
     * @param actualTotal           商品实付金额
     * @param sonTotalPrice         商品订单总额
     * @param discountTotal         折扣总金额
     * @param buyNum                购买次数
     * @param userId                用户id
     * @return
     */
    public static Map<String,BigDecimal> handleCardBag(JSONArray jsonObject, Map<String, Object> map, OrderItem vo,
                                                        BigDecimal actualTotal,BigDecimal sonTotalPrice,BigDecimal discountTotal,
                                                        BigDecimal buyNum,String userId, CommonService commonService) {
        SqlSentence sqlSentence = new SqlSentence();
        Map<String,Object> sqlMp=new HashMap<>();
        sqlSentence.setM(sqlMp);
        sqlMp.put("status",CardItem.SALES);
        sqlMp.put("isUp",BaseEntity.YES);
        sqlMp.put("isDel",BaseEntity.NO);
        sqlMp.put("commonId",vo.getCommonId());
        sqlSentence.setSqlSentence("select * from card_item where  id=#{m.commonId} and  status=#{m.status} and isUp=#{m.isUp} and isDel=#{m.isDel}");
        CardItem cardItem=commonService.selectOne(CardItemMapper.class,sqlSentence);
        if(cardItem==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡包:找不到该卡项,请确保卡项状态必须是销售中并且是上架状态,卡项标识:"+vo.getCommonId());
        }
        if(cardItem.getTotal()==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡包:"+cardItem.getName()+",没有销售价格,请填写销售单价在进行创建订单");
        }
        User user=commonService.selectOneByKey(UserMapper.class,userId);
        if(user==null){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡包:处理促销操作:未找到该用户信息");
        }
        sqlSentence.setSqlSentence("select * from card_equity where  cardItemId=#{m.commonId}  and isDel=#{m.isDel}");
        List<CardEquity> cardEquities=commonService.selectList(CardEquityMapper.class,sqlSentence);
        if(cardEquities==null || cardEquities.size()==0){
            throw new PlatTipsException(PlatformCode.ERROR_TIPS,"卡包:"+cardItem.getName()+",没有找到权益信息!");
        }
        BigDecimal sumCardBagMoney =BigDecimal.ZERO;
        BigDecimal userNum =null;
        if(vo.getCardItemInfoIds()!=null && vo.getCardItemInfoIds().size()>0){
            for(CardItemInfo info:vo.getCardItemInfoIds()){
                userNum=new BigDecimal(info.getUserNum());
                sqlMp.clear();
                sqlMp.put("id",info.getId());
                sqlSentence.setSqlSentence("select * from card_item_info where  id =#{m.id} and isDel=0  ");
                CardItemInfo cardItemInfo = commonService.selectOne(CardItemInfoMapper.class, sqlSentence);
                if(cardItemInfo!=null){
                    BigDecimal shareMoney=cardItemInfo.getShareMoney()==null?cardItemInfo.getEveryShareMoney():cardItemInfo.getShareMoney();
                    sumCardBagMoney=sumCardBagMoney.add(shareMoney.multiply(userNum));
                }
            }
        }
        sqlMp.clear();
        sqlMp.put("isDel", BaseEntity.NO);
        //限制会员等级购买
        sqlMp.put("foreignKey",cardItem.getId());
        checkMemberLevel(sqlSentence, user, OrderItemConstants.TYPE_CARD,commonService);
 
        JSONObject param = new JSONObject();
        param.put("commonId",cardItem.getId());
        param.put("userId",userId);
        param.put("shopId",map.get("shopId"));
        //卡项/促销/项目购买限制
        LimitPlatformUtil.limitMethod(commonService,param.toJSONString());
 
        //商品实付金额=卡项的销售价格*购买数量*折扣
        actualTotal = BigDecimal.ZERO;
 
        //商品订单总额=项目销售价*购买数量
        sonTotalPrice=sumCardBagMoney;
 
        //优惠金额=商品的销售单价*数量-用户实付金额;
        discountTotal=discountTotal.add(sumCardBagMoney.multiply(buyNum).subtract(actualTotal));
 
        vo.setSingle(0);//单次疗程数量
        vo.setUsedTotal(vo.getSingle()*vo.getBuyNum());//总次疗程次数,单次疗程次数*购买的总量
        vo.setOriPrice(sumCardBagMoney);//原价,项目的销售价
        vo.setCurPrice(sumCardBagMoney);//现价
 
        vo.setSpecs("[]");//规格
        vo.setGoodsNo(cardItem.getCode());//卡项编号
        vo.setGoodsName(cardItem.getName());//卡项名称
        vo.setGoodsImage("");//卡项主图
 
        Map<String,BigDecimal> moneyMap=new HashMap<>();
        moneyMap.put("actualTotal",actualTotal);
        moneyMap.put("sonTotalPrice",sonTotalPrice);
        moneyMap.put("discountTotal",discountTotal);
        return moneyMap;
    }
 
 
    /**
     * 开单-创建卡项的二级子订单
     * @param ordersId    总订单标识
     * @param vo
     * @param
     */
    public static void createOrderItemSonCard(String ordersId, OrderItem vo, CardItemInfo cardItemInfo,Integer type,CommonService commonService) {
//        for(CardItemInfo cardItemInfo:cardItemInfos){
            if(cardItemInfo.getPrice()==null){
                throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"开单:卡项组合项目价格为空,组合项标识:"+cardItemInfo.getId());
            }
            if(cardItemInfo.getMaxNum()==null && cardItemInfo.getEveryDrawNum()==null){
                throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"开单:卡项组合项目最大次数或者每次划扣的扣减次数为空,组合项标识:"+cardItemInfo.getId());
            }
            BigDecimal total = cardItemInfo.getPrice().multiply(new BigDecimal(cardItemInfo.getMaxNum() ==null ?cardItemInfo.getEveryDrawNum():cardItemInfo.getMaxNum()));
            OrderItemSon orderItemSon=new OrderItemSon();
            orderItemSon.setType(cardItemInfo.getCommonType());
            orderItemSon.setGoodsNo(cardItemInfo.getCommonCode());
            orderItemSon.setGoodsId(cardItemInfo.getCommonId());
            orderItemSon.setGoodsName(cardItemInfo.getCommonName());
            orderItemSon.setGoodsImage("");//暂定为空
            orderItemSon.setSpecs(cardItemInfo.getSpecJson());
            orderItemSon.setTotal(total);
            orderItemSon.setActualTotal(total);
            orderItemSon.setOriPrice(cardItemInfo.getShareMoney()==null?cardItemInfo.getEveryShareMoney():cardItemInfo.getShareMoney());
            orderItemSon.setCurPrice(cardItemInfo.getShareMoney()==null?cardItemInfo.getEveryShareMoney():cardItemInfo.getShareMoney());
            orderItemSon.setUsedTotal(0);//暂定为0
            orderItemSon.setUsedOne(0);//暂定为0
            orderItemSon.setBuyNum(OrderItemConstants.CARD_BAG.equals(vo.getType())?cardItemInfo.getUserNum():cardItemInfo.getMaxNum() ==null ?cardItemInfo.getEveryDrawNum():cardItemInfo.getMaxNum());
            orderItemSon.setOrderId(ordersId);
            orderItemSon.setOrderItemId(vo.getId());
            orderItemSon.setCardItemInfoId(cardItemInfo.getId());
            orderItemSon.setCardEquityId(cardItemInfo.getCardEquityId());
            orderItemSon.setIsCardBag(type==0?0:1);
            commonService.insert(OrderItemSonMapper.class,orderItemSon);
//        }
    }
}