quanwei
2025-11-21 1db9a4130699636cabe7e0c9f7f15d004aadada0
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
<?php
 
namespace app\api\service\order\settled;
 
use app\api\model\order\Order as OrderModel;
use app\api\model\order\OrderProduct;
use app\api\model\order\OrderAddress as OrderAddress;
use app\common\enum\order\OrderPayTypeEnum;
use app\common\enum\order\OrderSourceEnum;
use app\common\enum\order\OrderTypeEnum;
use app\common\model\settings\Setting as SettingModel;
use app\api\service\points\PointsDeductService;
use app\api\model\store\Store as StoreModel;
use app\api\service\user\UserService;
use app\common\enum\settings\DeliveryTypeEnum;
use app\common\library\helper;
use app\common\service\delivery\ExpressService;
use app\common\service\BaseService;
use app\common\service\product\factory\ProductFactory;
use app\api\service\fullreduce\FullDeductService;
use app\common\model\plus\table\Record as RecordModel; //by lyzflash
use app\common\model\order\OrderTrade as OrderTradeModel;
use app\common\model\branch\ActivityOrder as BranchActivityOrderModel;
use app\common\model\user\PointsLog as PointsLogModel;
 
/**
 * 订单结算服务基类
 */
class BranchActivitySettledService extends BaseService
{
    /* $model OrderModel 订单模型 */
    public $model;
 
    // 当前应用id
    protected $app_id;
 
    protected $user;
 
    // 订单结算商品列表
    protected $supplierData = [];
 
    protected $params;
 
    /**
     * 订单结算的规则
     * 主商品默认规则
     */
    protected $settledRule = [
        'is_coupon' => true,        // 优惠券抵扣
        'is_use_points' => true,        // 是否使用积分抵扣
        'force_points' => false,     // 强制使用积分,积分兑换
        'is_user_grade' => true,     // 会员等级折扣
        'is_agent' => true,     // 商品是否开启分销,最终还是支付成功后判断分销活动是否开启
        'is_reduce' => true, //是否满减
    ];
 
    /**
     * 订单结算数据
     */
    protected $commonOrderData = [];
    /**
     * 订单结算数据
     */
    protected $orderData = [];
    /**
     * 订单来源
     */
    protected $orderSource;
 
    /**
     * 构造函数
     */
    public function __construct($user, $supplierData, $params)
    {
        $this->model = new OrderModel;
        $this->app_id = OrderModel::$app_id;
        $this->user = $user;
        $this->supplierData = $supplierData;
        $this->params = $params;
        $this->orderSource = [
            'source' => OrderSourceEnum::BRANCHACTIVITY,
            'activity_id' => 0
        ];
    }
 
    /**
     * 订单确认-结算台
     */
    public function settlement()
    {
        // 验证商品状态, 是否允许购买
        $this->validateProductList();
        $orderTotalNum = 0;
        $orderTotalPrice = 0;
        $orderPayPrice = 0;
        $expressPrice = 0;
        $totalPointsMoney = 0;
        $totalPoints = 0;
        $totalProductReduce = 0;
        $totalServicePrice = 0;
        $this->commonOrderData = $this->getCommonOrderData();
        // 供应商
        foreach ($this->supplierData as &$supplier) {
            // 整理订单数据
            $this->orderData = $this->getOrderData($supplier['shop_supplier_id'],$supplier['productList']);
            // 订单商品总数量
            $orderTotalNum += helper::getArrayColumnSum($supplier['productList'], 'total_num');
            // 设置订单商品总服务费用
            $totalServicePrice += $this->setProductService($supplier['productList']);
            //总服务费用
            $this->orderData['total_service_price'] = $totalServicePrice;
            // 设置订单商品总金额(不含优惠折扣)
            $this->setOrderTotalPrice($supplier['productList']);
            $orderTotalPrice += $this->orderData['order_total_price'];
            // 计算积分商城抵扣
            $this->setOrderPoints($supplier['productList'], $this->settledRule['force_points']);
            // 计算订单商品的实际付款金额
            $this->setOrderProductPayPrice($supplier['productList']);
 
            // 设置默认配送方式
            if(!isset($this->params['supplier'])){
                $deliveryType=SettingModel::getItem('store')['delivery_type'];
                $delivery=$this->params['delivery'];
                if(in_array($delivery,$deliveryType)){
                    $delivery=$delivery;
                }else if(in_array("10",$deliveryType)){
                    $delivery=10;
                }else if(in_array("40",$deliveryType)){
                    $delivery=40;
                }else{
                    $delivery = $deliveryType[0];
                }
                
                $this->orderData['delivery'] = $supplier['productList'][0]['is_virtual'] == 1 || $supplier['productList'][0]['is_virtual'] == 2 ? 30 : $delivery;
            }else{
                if($supplier['productList'][0]['is_virtual'] == 1 || $supplier['productList'][0]['is_virtual'] == 2){
                    $this->orderData['delivery'] = 30;
                }else {
                    $this->orderData['delivery'] = $this->params['supplier'][$supplier['shop_supplier_id']]['delivery'];
                }
                $this->orderData['store_id'] = $this->params['supplier'][$supplier['shop_supplier_id']]['store_id'];
            }
         
            // 处理配送方式
            if ($this->orderData['delivery'] == DeliveryTypeEnum::EXPRESS) {
                $this->setOrderExpress($supplier['productList']);
                $expressPrice += $this->orderData['express_price'];
            } elseif ($this->orderData['delivery'] == DeliveryTypeEnum::EXTRACT) {
                $this->orderData['store_id'] > 0 && $this->orderData['extract_store'] = StoreModel::detail($this->params['supplier'][$supplier['shop_supplier_id']]['store_id']);
            } elseif ($this->orderData['delivery'] == DeliveryTypeEnum::STORESS) {
                 $this->orderData['extract_store_id'] = isset($this->params['delivery_store']) ? $this->params['delivery_store'] : $this->orderData['store_id'];
            }
 
            // 计算订单最终金额
            $this->setOrderPayPrice($supplier['productList']);
            $orderPayPrice += $this->orderData['order_pay_price'];
            $supplier['orderData'] = $this->orderData;
            //核销数据
            $verify_data = $this->orderData["verify_data"];
            //预约的数据
            $booking_data = empty($this->params["booking_data"]) ? [] : $this->params["booking_data"];
        }
        //最终价格
        $orderPayPrice = $this->setOrderFinalPrice();
        // 计算完成后再计算积分可抵扣
        foreach ($this->supplierData as &$supplier) {
            $this->orderData = $supplier['orderData'];
            // 计算积分抵扣
            $this->setOrderPoints($supplier['productList'], $this->settledRule['force_points']);
            $totalPointsMoney += $this->orderData['points_money'];
            $totalPoints += $this->orderData['points_num'];
            // 商品总价 - 积分抵扣
            foreach ($supplier['productList'] as &$product) {
                $value = $product['total_pay_price'];
                // 减去积分抵扣金额
                if ($this->orderData['is_allow_points'] && $this->commonOrderData['is_use_points'] && !$this->settledRule['force_points']) {
                    $value = helper::bcsub($product['total_pay_price'], $product['points_money']);
                }
                $product['total_pay_price'] = helper::number2($value);
            }
            // 计算订单最终金额
            $this->setOrderPayPrice($supplier['productList']);
            $supplier['orderData'] = $this->orderData;
        }
        $this->commonOrderData['is_use_points'] && $orderPayPrice = $orderPayPrice - $totalPointsMoney;
        // 计算订单积分赠送数量
        $this->setOrderPointsBonus();
 
        //订单数据
        $this->commonOrderData = array_merge([
            'order_total_num' => $orderTotalNum,        // 商品总数量
            'order_total_price' => helper::number2($orderTotalPrice),        // 商品总价
            'order_pay_price' => helper::number2($orderPayPrice),        // 商品总价,最终支付
            'total_service_price' => helper::number2($totalServicePrice),  // 商品总服务费
            'coupon_list' => [],
            'coupon_id_sys' =>'',
            'coupon_money_sys' => 0,
            'points_money' => $totalPointsMoney,
            'points_num' => $totalPoints,
            'is_real_use_points' => $this->params['is_use_points'],
            'product_reduce_money' => $totalProductReduce,
            'verify_data' => $verify_data,
            'booking_data' => $booking_data,
            'last_extract' => UserService::getLastExtract($this->user['user_id']),
            // 房间id
            'room_id' => isset($this->params['room_id']) && $this->params['room_id'] > 0 ? $this->params['room_id'] : 0,
            // 计次卡
            'counting_id' =>  0,
            'user_counting_data' => [],
            'activity_id' => $this->params['activity_id'], //活动id
        ], $this->commonOrderData, $this->settledRule);
 
        // 返回订单数据
        return [
            'supplierList' => $this->supplierData,
            'orderData' => $this->commonOrderData
        ];
    }
 
    /**
     * 整理订单数据(结算台初始化),公共部分
     */
    private function getCommonOrderData()
    {
        // 积分设置
        $pointsSetting = SettingModel::getItem('points');
        return [
            // 默认地址
            'address' => $this->user['address_default'],
            // 是否存在收货地址
            'exist_address' => $this->user['address_id'] > 0,
            // 是否允许使用积分抵扣
            'is_allow_points' => true,
            // 是否使用积分抵扣
            'is_use_points' => $this->params['is_use_points'],
            // 是否使用计次卡
            'counting_id' => isset($this->params['counting_id']) ? $this->params['counting_id'] : '',
            // 支付方式
            'pay_type' => isset($this->params['pay_type']) ? $this->params['pay_type'] : OrderPayTypeEnum::WECHAT,
            'pay_source' => isset($this->params['pay_source']) ? $this->params['pay_source'] : '',
            // 系统设置
            'setting' => [
                'points_name' => $pointsSetting['points_name'],      // 积分名称
            ],
        ];
    }
 
 
    /**
     * 验证订单商品的状态
     */
    public function validateProductList()
    {
        foreach ($this->supplierData as $supplier) {
            foreach ($supplier['productList'] as $product) {
                // 判断商品是否下架
                if ($product['product_status']['value'] != 10) {
                    $this->error = "很抱歉,商品 [{$product['product_name']}] 已下架";
                    return false;
                }
                // 判断商品库存
                if ($product['total_num'] > $product['product_sku']['stock_num']) {
                    $this->error = "很抱歉,商品 [{$product['product_name']}] 库存不足";
                    return false;
                }
                // 判断是否超过限购数量
                if($product['limit_num'] > 0){
                    $hasNum = OrderModel::getHasBuyOrderNum($this->user['user_id'], $product['product_id']);
                    if($hasNum + $product['total_num'] > $product['limit_num']){
                        $this->error = "很抱歉,购买超过此商品最大限购数量";
                        return false;
                    }
                }
            }
        }
        return true;
    }
 
    /**
     * 创建新订单
     */
    public function createOrder($order)
    {
        // 表单验证
        if (!$this->validateOrderForm($order, $this->params)) {
            return false;
        }
        $order_arr = [];
        // 创建新的订单
        foreach ($order['supplierList'] as $supplier) {
            if ($supplier['orderData']['delivery'] == DeliveryTypeEnum::EXTRACT) {
                if (empty($supplier['orderData']['extract_store'])) {
                    $this->error = '请先选择自提门店';
                    return false;
                }
            }
            $this->model = new OrderModel;
            $this->model->transaction(function () use ($order, $supplier) {
                // 创建订单事件
                $this->createOrderEvent($order['orderData'], $supplier);
            });
            array_push($order_arr, $this->model);
        }
        if (count($order_arr) > 1) {
            $orderNo = $this->model->orderNo();
            $trade_list = [];
            foreach ($order_arr as $order) {
                $trade_model = new OrderTradeModel;
                $trade_list[] = [
                    'out_trade_no' => $orderNo,
                    'order_id' => $order['order_id'],
                    'app_id' => $this->app_id
                ];
                $trade_model->saveAll($trade_list);
            }
        }
        $order_id = helper::getArrayColumn($order_arr, 'order_id');
        return $order_id;
    }
 
    /**
     * 设置订单的商品总金额(不含优惠折扣)
     */
    private function setOrderTotalPrice($productList)
    {
        // 订单商品的总金额(不含优惠券折扣)
        $this->orderData['order_total_price'] = helper::number2(helper::getArrayColumnSum($productList, 'total_price'));
    }
 
    /**
     * 计算订单商品的实际付款金额
     */
    private function setOrderProductPayPrice($productList)
    {
        // 商品总价 - 优惠抵扣
        foreach ($productList as &$product) {
            // 减去优惠券抵扣金额
            $value = helper::bcsub($product['total_price'], $product['coupon_money']);
            // 减去积分抵扣金额
            if ($this->orderData['is_allow_points'] && $this->commonOrderData['is_use_points'] && !$this->settledRule['force_points']) {
                $value = helper::bcsub($value, $product['points_money']);
            }
            $product['total_pay_price'] = helper::number2($value);
        }
 
        return true;
    }
 
    /**
     * 整理订单数据(结算台初始化)
     */
    private function getOrderData($shop_supplier_id,$productList=[])
    {
        // 系统支持的配送方式 (后台设置)
        $deliveryType = SettingModel::getItem('store')['delivery_type'];
        
        // 积分设置
        $pointsSetting = SettingModel::getItem('points');
 
        if(isset($this->params['supplier'])){
            $delivery = $this->params['supplier'][$shop_supplier_id]['delivery'];
        }else{
            if(in_array("10",$deliveryType)){
                $delivery=10;
            }else if(in_array("40",$deliveryType)){
                $delivery=40;
            }else{
                $delivery = $deliveryType[0];
            }
        }
 
        if(!empty($productList) && $productList[0]['is_virtual'] == 1){
            //虚拟商品核销 by yj
            $verify_data = [
                'is_verify' => $productList[0]['is_verify'] && empty($productList[0]['virtual_auto']) ? 1 : 0, //必须是手动发货并且支持核销
                'verify_type' => $productList[0]['verify_type'],
                'verify_day' => $productList[0]['verify_day'],
                'verify_store_ids' => $productList[0]['verify_store_ids'],
                'verify_store_force' => 0 //预留,是否必须选择核销门店
            ];
            if ($verify_data['verify_type'] == 20) {
                $verify_data['verify_start_time'] = date('Y-m-d', $productList[0]['verify_start_time']);
                $verify_data['verify_end_time'] = date('Y-m-d', $productList[0]['verify_end_time']);
            }
        }else{
            $verify_data = [];
        }
 
        //通过收货地址获取最近的门店
        if(!empty($this->user['address_default'])){
            $address = $this->user['address_default'];
            $store_list = (new storeModel())->getList(false,$address["longitude"],$address["latitude"],false,$shop_supplier_id);
            if(!empty($store_list)){
                $sell_store_id = $store_list[0]["store_id"];
            }
        }
 
        return [
            //虚拟商品核销数据
            'verify_data' => $verify_data,
            // 配送类型
            'delivery' => $delivery,
            // 默认地址
            'address' => $this->user['address_default'],
            // 是否存在收货地址
            'exist_address' => $this->user['address_id'] > 0,
            // 配送费用
            'express_price' => 0.00,
            // 当前用户收货城市是否存在配送规则中
            'intra_region' => true,
            // 自提门店信息
            'extract_store' => [],
            // 是否允许使用积分抵扣
            'is_allow_points' => false,
            // 是否使用积分抵扣
            'is_use_points' => $this->params['is_use_points'],
            // 支付方式
            'pay_type' => isset($this->params['pay_type']) ? $this->params['pay_type'] : OrderPayTypeEnum::WECHAT,
            // 系统设置
            'setting' => [
                'delivery' => $deliveryType,     // 支持的配送方式
                'points_name' => $pointsSetting['points_name'],      // 积分名称
            ],
            // 记忆的自提联系方式
            //'last_extract' => UserService::getLastExtract($this->user['user_id']),
            'deliverySetting' => $deliveryType,
            //门店id
            'store_id' => 0,
            //优惠券id
            'coupon_id' => 0,
            //优惠金额
            'coupon_money'=>0,
            //总服务费用金额
            'total_service_price'=>0,
            // 通过收货地址获取最近的门店出货
            'sell_store_id' => empty($sell_store_id) ? 0 : $sell_store_id,
        ];
    }
 
    /**
     * 订单配送-快递配送
     */
    private function setOrderExpress($productList)
    {
        // 设置默认数据:配送费用
        helper::setDataAttribute($productList, [
            'express_price' => 0,
        ], true);
        // 当前用户收货城市id
        $cityId = $this->user['address_default'] ? $this->user['address_default']['city_id'] : null;
 
        /*if( $this->user['user_id'] == 8743){
            print_r("a".$cityId);exit;
        }*/
       /* if(empty($cityId)){
            $this->error = "请先完善您的收货地址再提交订单";
            return false;
        }*/
 
        // 初始化配送服务类
        $ExpressService = new ExpressService(
            $this->app_id,
            $cityId,
            $productList,
            OrderTypeEnum::MASTER,
            $this->user
        );
 
        // 获取不支持当前城市配送的商品
        $notInRuleProduct = $ExpressService->getNotInRuleProduct();
 
        // 验证商品是否在配送范围
        $this->orderData['intra_region'] = ($notInRuleProduct === false);
 
        if (!$this->orderData['intra_region']) {
            $notInRuleProductName = $notInRuleProduct['product_name'];
            $this->error = "很抱歉,您的收货地址不在商品 [{$notInRuleProductName}] 的配送范围内";
            return false;
        } else {
            // 计算配送金额
            $ExpressService->setExpressPrice();
        }
 
        // 订单总运费金额
        $this->orderData['express_price'] = helper::number2($ExpressService->getTotalFreight());
        return true;
    }
 
    /**
     * 设置订单的实际支付金额(含配送费+安装费)
     */
    private function setOrderPayPrice($productList)
    {
        // 订单金额(含优惠折扣)
        $this->orderData['order_price'] = helper::number2(helper::getArrayColumnSum($productList, 'total_pay_price'));
        // 订单实付款金额(订单金额 + 运费)
        $this->orderData['order_pay_price'] = helper::number2(helper::bcadd($this->orderData['order_price'], $this->orderData['express_price']));
        // 订单实付款金额(订单金额 + 服务费)
        $this->orderData['order_pay_price'] = helper::number2(helper::bcadd($this->orderData['order_pay_price'], $this->orderData['total_service_price']));
    }
 
    /**
     * 表单验证 (订单提交)
     */
    private function validateOrderForm(&$order)
    {
        //如果是积分兑换,判断用户积分是否足够
        if ($this->settledRule['force_points']) {
            //因为积分会过期问题 所以现在读取现在有的积分
            $points = ( new PointsLogModel())->getUserPoints($this->user['user_id']);
            if ($points < $order['orderData']['points_num']) {
                $this->error = '用户积分不足,无法使用积分兑换';
                return false;
            }
        }
        return true;
    }
 
    /**
     * 创建订单事件
     */
    private function createOrderEvent($commomOrder, $supplier)
    {
        // 新增订单记录
        $status = $this->add($commomOrder, $supplier);
        if ($supplier['orderData']['delivery'] == DeliveryTypeEnum::EXPRESS || $supplier['orderData']['delivery'] == DeliveryTypeEnum::STORESS) {
            // 记录收货地址
            $this->saveOrderAddress($commomOrder['address'], $status);
        } elseif ($supplier['orderData']['delivery'] == DeliveryTypeEnum::EXTRACT) {
            // 记录自提信息
            if(!empty($this->params['linkman']) && !empty($this->params['phone'])){
                $this->saveOrderExtract($this->params['linkman'], $this->params['phone']);
            }else{
                $this->saveOrderExtract($commomOrder['address']['name'], $commomOrder['address']['phone']);
            }
        }
        //如果虚拟商品支持核销,保存核销信息 by yj
        if ($supplier['productList'][0]['is_verify'] == 1) {
            $this->saveOrderVerify($supplier['productList'][0]);
        }
        // 保存订单商品信息
        $this->saveOrderProduct($supplier, $status, $commomOrder);
 
        // 更新商品库存 (针对下单减库存的商品)
        if(!empty($commomOrder["extract_store_id"])){
            //自提门店
            $sell_store_id = $commomOrder["extract_store_id"];
        }elseif(!empty($commomOrder["sell_store_id"])){
            //没有自提门店,判断出货门店
            $sell_store_id = $supplier['orderData']["sell_store_id"];
        }else{
            $sell_store_id = 0;
        }
        // 减库存
        ProductFactory::getFactory($this->orderSource['source'])->updateProductStock($supplier['productList']);
 
        // 积分兑换扣除用户积分
        if ($commomOrder['force_points']) {
            $describe = "用户积分兑换消费:{$this->model['order_no']}";
            $this->user->setIncPoints(-$commomOrder['points_num'], $describe,0,true,$status);
        } else {
            // 积分抵扣情况下扣除用户积分
            if ($commomOrder['is_allow_points'] && $commomOrder['is_real_use_points'] && $commomOrder['points_num'] > 0) {
                $describe = "用户消费:{$this->model['order_no']}";
                $this->user->setIncPoints(-$commomOrder['points_num'], $describe,0,true,$status);
            }
        }
        return $status;
    }
 
    /**
     * 新增订单记录
     */
    private function add($commomOrder, $supplier)
    {
        $order = $supplier['orderData'];
        //没有门店id且物流为快递配送
        // $sell_store_id = $order['sell_store_id'];
        // if(empty($sell_store_id) && $supplier['orderData']['delivery'] == 10){
        //     //如没有发货门店,获取一个收货门店
        //     $sell_store_id = storeModel::getDeliverStore($supplier["shop_supplier_id"]);
        // }
        // 订单数据
        $data = [
            'user_id' => $this->user['user_id'],
            'order_no' => $this->model->orderNo(),
            'trade_no' => $this->model->orderNo(),
            'total_price' => $order['order_total_price'],
            'order_price' => $order['order_price'],
            'coupon_id' => 0,
            'coupon_money' => $supplier['orderData']['coupon_money'],
            'coupon_id_sys'=>0,
            'coupon_money_sys'=>0,
            'points_money' => $commomOrder['is_real_use_points'] == 1?$supplier['orderData']['points_money']:0,
            'points_num' => $commomOrder['is_real_use_points'] == 1?$supplier['orderData']['points_num']:0,
            'pay_price' => $order['order_pay_price'],
            'delivery_type' => $supplier['orderData']['delivery'],
            'pay_type' => $commomOrder['pay_type'],
            'pay_source' => empty($commomOrder['pay_source']) ? 'wx' : $commomOrder['pay_source'],
            'buyer_remark' => $this->params['supplier'][$supplier['shop_supplier_id']]['remark'],
            'order_source' => $this->orderSource['source'],
            'points_bonus' => $supplier['orderData']['points_bonus'],
            'is_agent' => $this->settledRule['is_agent']? 1:0,
            'shop_supplier_id' => $supplier['shop_supplier_id'],
            'supplier_money' => $order['supplier_money'],
            'sys_money' => $order['sys_money'],
            'app_id' => $this->app_id,
            'room_id' => $commomOrder['room_id'],
            'virtual_auto' => $supplier['productList'][0]['virtual_auto'],
            'is_verify' => isset($supplier['productList'][0]['is_verify'])?$supplier['productList'][0]['is_verify']:0, //是否支持核销 by yj
            'is_virtual' => $supplier['productList'][0]['is_virtual'], //是否是服务商品 by yj
            'product_reduce_money' => 0,
            'user_counting_id' => empty($commomOrder['user_counting_data']) ? 0 : $commomOrder['user_counting_data']["order_id"],
            'total_service_price' => empty($order['total_service_price']) ? 0 : $order['total_service_price'],
            // 'sell_store_id' => empty($sell_store_id) ? 0 : $sell_store_id,
            'activity_id' => $commomOrder['activity_id'], //分会活动的id
        ];
 
        if ($supplier['orderData']['delivery'] == DeliveryTypeEnum::EXPRESS) {
            $data['express_price'] = $order['express_price'];
        } elseif ($supplier['orderData']['delivery'] == DeliveryTypeEnum::EXTRACT) {
            $data['extract_store_id'] = $order['extract_store']['store_id'];
        }elseif ($supplier['orderData']['delivery'] == DeliveryTypeEnum::STORESS) {
            //配送门店
            $data['delivery_store'] = isset($this->params['delivery_store']) ? $this->params['delivery_store'] : $order['extract_store_id'];
            $data['extract_store_id'] = isset($this->params['delivery_store']) ? $this->params['delivery_store'] : $order['extract_store_id'];
        }
 
        //随主订单配置
        $config = SettingModel::getItem('trade');
        $closeDays = $config['order']['close_days'];
        $closeDays != 0 && $data['pay_end_time'] = time() + ((int)$closeDays * 86400);
        // 保存订单记录
        $this->model->save($data);
        $order_id = $this->model['order_id'];
        $activity_order = new BranchActivityOrderModel();
        $activity_order->save(['order_id'=>$order_id,'activity_id'=>$commomOrder['activity_id'], 'app_id' => $this->app_id, 'shop_supplier_id' => $supplier['shop_supplier_id']]);
        return $order_id;
    }
 
    /**
     * 记录收货地址
     */
    private function saveOrderAddress($address, $order_id)
    {
        $model = new OrderAddress();
        if ($address['region_id'] == 0 && !empty($address['district'])) {
            $address['detail'] = $address['district'] . ' ' . $address['detail'];
        }
        return $model->save([
            'order_id' => $order_id,
            'user_id' => $this->user['user_id'],
            'app_id' => $this->app_id,
            'name' => $address['name'],
            'phone' => $address['phone'],
            'province_id' => $address['province_id'],
            'city_id' => $address['city_id'],
            'region_id' => $address['region_id'],
            'detail' => $address['detail'],
            // 增加坐标和门牌用于openLocation by lyzflash
            'longitude' => $address['longitude'],
            'latitude' => $address['latitude'],
            'house_number' => $address['house_number']
        ]);
    }
 
    /**
     * 保存上门自提联系人
     */
    private function saveOrderExtract($linkman, $phone)
    {
        // 记忆上门自提联系人(缓存),用于下次自动填写
        UserService::setLastExtract($this->model['user_id'], trim($linkman), trim($phone));
        // 保存上门自提联系人(数据库)
        return $this->model->extract()->save([
            'linkman' => trim($linkman),
            'phone' => trim($phone),
            'user_id' => $this->model['user_id'],
            'app_id' => $this->app_id,
        ]);
    }
 
    /**
     * 保存订单商品信息
     */
    private function saveOrderProduct($supplier, $status, $commomOrder)
    {
        // 获取出货门店 by yj 2024.1.24
        // if(!empty($commomOrder["extract_store_id"])){
        //     //自提门店
        //     $sell_store_id = $commomOrder["extract_store_id"];
        // }elseif(!empty($commomOrder["sell_store_id"])){
        //     //没有自提门店,判断是不是扫门店码
        //     $sell_store_id = $commomOrder["sell_store_id"];
        // }else{
        //     //如没有发货门店,获取一个收货门店
        //     $sell_store_id = storeModel::getDeliverStore($supplier["shop_supplier_id"]);
        // }
        // 订单商品列表
        $productList = [];
        foreach ($supplier['productList'] as $product) {
            $item = [
                'order_id' => $status,
                'user_id' => $this->user['user_id'],
                'app_id' => $this->app_id,
                'product_id' => $product['product_id'],
                'product_name' => $product['product_name'],
                'image_id' => empty($product['product_sku']['image_id']) ? $product['image'][0]['image_id'] : $product['product_sku']['image_id'],
                'deduct_stock_type' => $product['deduct_stock_type'],
                'spec_type' => $product['spec_type'],
                'spec_sku_id' => $product['product_sku']['spec_sku_id'],
                'product_sku_id' => $product['product_sku']['product_sku_id'],
                'product_attr' => $product['product_sku']['product_attr'],
                'content' => $product['content'],
                'product_no' => $product['product_sku']['product_no'],
                'product_price' => $product['product_sku']['product_price'],
                'line_price' => $product['product_sku']['line_price'],
                'product_weight' => empty($product['product_sku']['product_weight']) ? 0 : $product['product_sku']['product_weight'],
                'is_user_grade' => (int)$product['is_user_grade'],
                'grade_ratio' => $product['grade_ratio']??0,
                'grade_product_price' => isset($product['grade_product_price'])?$product['grade_product_price']:0,
                'grade_total_money' => $product['grade_total_money']??0,
                'coupon_money' => isset($product['coupon_money'])?$product['coupon_money']:0,
                'points_money' => isset($product['points_money']) && $commomOrder['is_real_use_points']?$product['points_money']:0,
                'points_num' => isset($product['points_num']) && $commomOrder['is_real_use_points']?$product['points_num']:0,
                'points_bonus' => isset($product['points_bonus'])?$product['points_bonus']:0,
                'total_num' => $product['total_num'],
                'total_price' => $product['total_price'],
                'total_pay_price' => $product['total_pay_price'],
                'supplier_money' => $product['supplier_money'],
                'is_agent' => $product['is_agent'],
                'is_enable_team' => $product['is_enable_team'],
                'is_ind_agent' => $product['is_ind_agent'],
                'agent_money_type' => $product['agent_money_type'],
                'first_money' => $product['first_money'],
                'second_money' => $product['second_money'],
                'third_money' => $product['third_money'],
                'fullreduce_money' => isset($product['fullreduce_money'])?$product['fullreduce_money']:0,
                'virtual_content' => $product['virtual_content'],
                'is_alone_team' => $product['is_alone_team'],
                'alone_team_equity' => $product['alone_team_equity'],
                'product_reduce_money' => $product['product_reduce_money'],
                'is_verify' => $product['is_verify'],
                'verify_type' => $product['verify_type'],
                'verify_day' => $product['verify_day'],
                'service_name' => !empty($product['service_name'])?$product['service_name']:'',
                'service_price' => !empty($product['service_price'])?$product['service_price']:0,
            ];
 
            // 记录订单商品来源id
            $item['product_source_id'] = isset($product['product_source_id']) ? $product['product_source_id'] : 0;
            // 记录订单商品sku来源id
            $item['sku_source_id'] = isset($product['sku_source_id']) ? $product['sku_source_id'] : 0;
            // 记录拼团类的商品来源id
            $item['bill_source_id'] = isset($product['bill_source_id']) ? $product['bill_source_id'] : 0;
            // 自定义表单数据id,直接读取最新的那条记录 by lyzflash
            $item['table_record_id'] = RecordModel::getLastRecordId($this->user['user_id'], 'product_' . $product['product_id'] . '_' . $product['product_sku']['spec_sku_id']);
            $productList[] = $item;
        }
 
        $model = new OrderProduct();
        return $model->saveAll($productList);
    }
 
    /**
     * 计算订单可用积分抵扣
     * $force_points true:积分商城 false:普通商品
     */
    private function setOrderPoints($productList, $force_points = true)
    {
        $this->orderData['points_money'] = 0;
        // 积分抵扣总数量
        $this->orderData['points_num'] = 0;
        // 允许积分抵扣
        $this->orderData['is_allow_points'] = false;
        // 积分商城兑换
        if (isset($this->settledRule['force_points']) && $this->settledRule['force_points'] && $force_points) {
            // 积分抵扣金额,商品价格-兑换金额
            $this->orderData['points_money'] = $productList[0]['points_money'];
            // 积分抵扣总数量
            $this->orderData['points_num'] = $productList[0]['points_num'];
            // 允许积分抵扣
            $this->orderData['is_allow_points'] = true;
 
            //因为积分会过期问题 所以现在读取现在有的积分
            $points = ( new PointsLogModel())->getUserPoints($this->user['user_id']);
            if ($points < $productList[0]['points_num']) {
                $this->error = '用户积分不足,无法使用积分兑换';
                return false;
            }
/*            if ($this->user['points'] < $productList[0]['points_num']) {
                $this->error = '积分不足,去多赚点积分吧!';
                return false;
            }*/
            return true;
        }
        if($force_points){
            return true;
        }
        // 积分设置
        $setting = SettingModel::getItem('points');
        // 条件:后台开启下单使用积分抵扣
        if (!$setting['is_shopping_discount']) {
            return false;
        }
        // 条件:订单金额满足[?]元
        if (helper::bccomp($setting['discount']['full_order_price'], $this->orderData['order_total_price']) === 1) {
            return false;
        }
        // 计算订单商品最多可抵扣的积分数量
        $this->setOrderProductMaxPointsNum($productList);
        // 订单最多可抵扣的积分总数量
        $maxPointsNumCount = helper::getArrayColumnSum($productList, 'max_points_num');
        // 实际可抵扣的积分数量
        $actualPointsNum = min($maxPointsNumCount, $this->user['points']);
        if ($actualPointsNum < 1) {
            $this->orderData['points_money'] = 0;
            // 积分抵扣总数量
            $this->orderData['points_num'] = 0;
            // 允许积分抵扣
            $this->orderData['is_allow_points'] = true;
            return false;
        }
        // 计算订单商品实际抵扣的积分数量和金额
        $ProductDeduct = new PointsDeductService($productList);
        $ProductDeduct->setProductPoints($maxPointsNumCount, $actualPointsNum);
        // 积分抵扣总金额
        $orderPointsMoney = helper::getArrayColumnSum($productList, 'points_money');
        $this->orderData['points_money'] = helper::number2($orderPointsMoney);
        // 积分抵扣总数量
        $this->orderData['points_num'] = $actualPointsNum;
        // 允许积分抵扣
        $this->orderData['is_allow_points'] = true;
        return true;
    }
 
    /**
     * 计算订单商品最多可抵扣的积分数量
     */
    private function setOrderProductMaxPointsNum($productList)
    {
        // 积分设置
        $setting = SettingModel::getItem('points');
        foreach ($productList as &$product) {
            // 积分兑换
            if ($this->settledRule['force_points']) {
                $product['max_points_num'] = $product['points_num'];
            } else {
                // 商品不允许积分抵扣
                if (!$product['is_points_discount']) continue;
                // 积分抵扣比例
                $deductionRatio = helper::bcdiv($setting['discount']['max_money_ratio'], 100);
                // 最多可抵扣的金额
                $maxPointsMoney = helper::bcmul($product['total_pay_price'], $deductionRatio);
                // 最多可抵扣的积分数量
                $product['max_points_num'] = helper::bcdiv($maxPointsMoney, $setting['discount']['discount_ratio'], 0);
                // 如果超过商品最大抵扣数量
                if($product['max_points_discount'] > 0 && $product['max_points_num'] > $product['max_points_discount'] * $product['total_num']){
                    $product['max_points_num'] = $product['max_points_discount'] * $product['total_num'];
                }
            }
        }
        return true;
    }
 
 
    /**
     * 计算订单积分赠送数量
     */
    private function setOrderPointsBonus()
    {
        // 初始化商品积分赠送数量
        foreach ($this->supplierData as &$supplier){
            foreach ($supplier['productList'] as $product){
                $product['points_bonus'] = 0;
            }
            $supplier['orderData']['points_bonus'] = 0;
        }
        // 积分设置
        $setting = SettingModel::getItem('points');
        // 条件:后台开启开启购物送积分
        if (!$setting['is_shopping_gift']) {
            return false;
        }
        // 设置商品积分赠送数量
        foreach ($this->supplierData as &$supplier) {
            foreach ($supplier['productList'] as &$product) {
                // 积分赠送比例
                $ratio = $setting['gift_ratio'] / 100;
                // 计算抵扣积分数量
                $product['points_bonus'] = !$product['is_points_gift'] ? 0 : helper::bcmul($product['total_pay_price'], $ratio, 0);
            }
            //  订单积分赠送数量
            $supplier['orderData']['points_bonus'] = helper::getArrayColumnSum($supplier['productList'], 'points_bonus');
        }
 
        return true;
    }
 
 
    /**
     * 设置订单商品服务费用
     */
    private function setProductService($productList)
    {
        // 计算服务费用
        $servicePrice = 0;
        foreach ($productList as $product) {
            if (!empty($product['service_price']) && $product['service_price'] > 0) {
                $servicePrice += $product['service_price'] * $product['total_num'];
            }
        }
        return $servicePrice;
    }
 
    /**
     * 设置订单满减抵扣信息
     */
    private function setOrderFullreduceMoney($reduce, $productList)
    {
        // 计算订单商品满减抵扣金额
        $productListTemp = helper::getArrayColumns($productList, ['total_price']);
        $service = new FullDeductService;
        $completed = $service->setProductFullreduceMoney($productListTemp, $reduce['reduced_price']);
        if($completed){
            // 分配订单商品优惠券抵扣金额
            foreach ($productList as $key => &$product) {
                $product['fullreduce_money'] = $completed[$key]['fullreduce_money'] / 100;
            }
        }
        return true;
    }
 
    /**
     * 获取所有支付价格
     */
    private function setOrderFinalPrice()
    {
 
        foreach ($this->supplierData as &$supplier){
            //商户独立抽成
            $config = SettingModel::getSupplierCommissionRate($supplier["shop_supplier_id"]);
            $sys_percent = intval($config['commission_rate']);
            $supplier_percent = 100 - $sys_percent;
            // 供应商结算金额,包括运费
            $supplier['orderData']['supplier_money'] = helper::number2($supplier['orderData']['order_price'] * $supplier_percent/100 + $supplier['orderData']['express_price']);
            // 平台分佣金额
            $supplier['orderData']['sys_money'] = helper::number2($supplier['orderData']['order_price'] * $sys_percent/100);
            // 产品价格
            // 结算金额不包括运费
            foreach ($supplier['productList'] as &$product){
 
                $product['supplier_money'] = helper::number2($product['total_pay_price'] * $supplier_percent/100);
                $product['sys_money'] = helper::number2($product['total_pay_price'] * $sys_percent/100);
            }
        }
        $price = 0;
        foreach ($this->supplierData as &$supplier){
            $price += $supplier['orderData']['order_pay_price'];
        }
 
        return $price;
    }
 
    /**
     * 保存虚拟商品核销信息
     */
    private function saveOrderVerify($product)
    {
        return $this->model->verify()->save([
            'verify_type' => $product['verify_type'],
            'verify_day' => $product['verify_day'],
            'verify_start_time' => $product['verify_start_time'],
            'verify_end_time' => $product['verify_end_time'],
            'verify_store_ids' => implode(',', $product['verify_store_ids']),
            'app_id' => $product['app_id'],
        ]);
    }
 
}