quanwei
7 days ago 30563323a53b0d0260c97d08a9e8bd4cc8227a95
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
<?php
 
namespace app\api\model\order;
 
use app\api\model\product\Product as ProductModel;
use app\api\service\order\paysuccess\type\MasterPaySuccessService;
use app\api\service\order\PaymentService;
use app\api\model\settings\Setting as SettingModel;
use app\common\enum\order\OrderPayTypeEnum;
use app\common\enum\order\OrderSourceEnum;
use app\common\enum\order\OrderTypeEnum;
use app\common\enum\order\OrderPayStatusEnum;
use app\common\enum\order\OrderStatusEnum;
use app\common\exception\BaseException;
use app\common\service\order\OrderCompleteService;
use app\common\enum\settings\DeliveryTypeEnum;
use app\common\library\helper;
use app\common\model\order\Order as OrderModel;
use app\api\service\order\checkpay\CheckPayFactory;
use app\common\service\product\factory\ProductFactory;
use app\common\model\plus\coupon\UserCoupon as UserCouponModel;
use app\common\model\app\AppWx as AppWxModel; //by lyzflash
use app\common\model\plus\table\Record as RecordModel; //by lyzflash
use app\common\model\order\OrderTrade as OrderTradeModel;
use app\common\service\order\OrderRefundService;
use app\common\model\supplier\Balance as supplierBalanceModel;
use app\common\model\supplier\Supplier as SupplierModel;
 
/**
 * 普通订单模型
 */
class Order extends OrderModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'update_time'
    ];
 
    /**
     * 订单支付事件
     */
    public function onPay($params, $user)
    {
        $orderID = explode(',', $params['order_id']);
        if (count($orderID) == 1) {
            $order = self::getUserOrderDetail($params['order_id'], $user['user_id']);
            // 判断订单状态
            $checkPay = CheckPayFactory::getFactory($order['order_source']);
 
            if (!$checkPay->checkOrderStatus($order)) {
                $this->error = $checkPay->getError();
                return false;
            }
        }
        return true;
    }
 
    /**
     *付呗重新发起支付时需要更换order_no by lyzflash
     */
    public function changeOrderNo($payType = OrderPayTypeEnum::WECHAT)
    {
        if ($payType == OrderPayTypeEnum::WECHAT) {
            $wxConfig = AppWxModel::getAppWxCache($this['app_id']);
            if ($wxConfig["apitype"]) { //付呗支付
                $order_no = $this->orderNo();
                if ($this->save(['order_no' => $order_no])) {
                    return $order_no;
                }
 
            }
        }
        return false;
    }
 
    /**
     * 用户中心订单列表
     */
    public function getList($user_id, $type = 'all', $params)
    {
        // 筛选条件
        $filter = [];
        // 订单数据类型
        switch ($type) {
            case 'all':
                break;
            case 'payment';
                $filter['pay_status'] = OrderPayStatusEnum::PENDING;
                $filter['order_status'] = 10;
                break;
            case 'delivery';
                $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;
                $filter['delivery_status'] = 10;
                $filter['order_status'] = 10;
                break;
            case 'received';
                $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;
                $filter['delivery_status'] = 20;
                $filter['receipt_status'] = 10;
                $filter['order_status'] = 10;
                break;
            case 'comment';
                $filter['is_comment'] = 0;
                $filter['order_status'] = 30;
                break;
        }
        $model = new self;
        if(isset($params['shop_supplier_id'])&&$params['shop_supplier_id']){
            $model = $model->where('shop_supplier_id','=',$params['shop_supplier_id']);
        }else if(isset($params['store_id'])&&$params['store_id']){
            $model = $model->where('extract_store_id','=',$params['store_id']);
        }else if(isset($params['clerk_id'])&&$params['clerk_id']){
            //只查找门店配送订单
            $model = $model->where('extract_clerk_id','=',$params['clerk_id'])->where('delivery_type','=','40');
        }else{
            // 用户查询
            $model = $model->where('user_id', '=', $user_id);
        }
        $list = $model->with(['product.image', 'supplier','extractClerk', 'verify', 'advance'])
            ->where($filter)
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc'])
            ->paginate($params);
        foreach ($list as &$item) {
            if ($item['pay_status']['value'] == 10 && $item['order_status']['value'] != 20 && $item['pay_end_time'] != 0) {
                $item['pay_end_time_format'] = $this->formatPayEndTime($item['pay_end_time'] - time());
            } else {
                $item['pay_end_time_format'] = '';
            }
        }
        return $list;
    }
 
    /**
     * 用户中心拼团订单列表
     */
    public function getAssembleList($user_id, $params)
    {
        // 筛选条件
        $filter = [];
        // 订单数据类型
        switch ($params['type']) {
            case '10'://所有订单
                break;
            case '20';//待支付订单
                $filter['pay_status'] = OrderPayStatusEnum::PENDING;
                $filter['order_status'] = 10;
                break;
            case '30';//拼团中
                $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;
                $filter['assemble_status'] = 10;
                $filter['order_status'] = 10;
                break;
            case '40';//拼团成功
                $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;
                $filter['assemble_status'] = 20;
                break;
            case '50';//拼团失败
                $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;
                $filter['assemble_status'] = 30;
                break;
        }
        $list = $this->with(['product.image', 'supplier', 'advance'])
            ->where('user_id', '=', $user_id)
            ->where('order_source', '=', OrderSourceEnum::ASSEMBLE)
            ->where($filter)
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc'])
            ->paginate($params);
        foreach ($list as &$item) {
            if ($item['pay_status']['value'] == 10 && $item['order_status']['value'] != 20 && $item['pay_end_time'] != 0) {
                $item['pay_end_time_format'] = $this->formatPayEndTime($item['pay_end_time'] - time());
            } else {
                $item['pay_end_time_format'] = '';
            }
        }
        return $list;
    }
 
    /**
     * 确认收货
     */
    public function receipt()
    {
        // 验证订单是否合法
        // 条件1: 订单必须已发货
        // 条件2: 订单必须未收货
        if ($this['delivery_status']['value'] != 20 || $this['receipt_status']['value'] != 10) {
            $this->error = '该订单不合法';
            return false;
        }
        return $this->transaction(function () {
            // 更新订单状态
            $status = $this->save([
                'receipt_status' => 20,
                'receipt_time' => time(),
                'order_status' => 30
            ]);
            // 执行订单完成后的操作
            $OrderCompleteService = new OrderCompleteService(OrderTypeEnum::MASTER);
            $OrderCompleteService->complete([$this], static::$app_id);
            return $status;
        });
    }
 
    /**
     * 立即购买:获取订单商品列表
     */
    public static function getOrderProductListByNow($params)
    {
        // 商品详情
        $product = ProductModel::detail($params['product_id']);
        // 商品sku信息
        $product['product_sku'] = ProductModel::getProductSku($product, $params['product_sku_id']);
        // 自定义表单数据id,直接读取最新的那条记录 by lyzflash
        $product['table_record_id'] = RecordModel::getLastRecordId($params['user_id'], 'product_' . $params['product_id'] . "_" . $product['product_sku']['spec_sku_id']);
        // 商品列表
        $productList = [$product->hidden(['category', 'content', 'image', 'sku'])];
        foreach ($productList as &$item) {
            // 商品单价
            $item['product_price'] = $item['product_sku']['product_price'];
            $item['settlement_price'] = $item['product_sku']['settlement_price'];
            // 商品购买数量
            $item['total_num'] = $params['product_num'];
            $item['spec_sku_id'] = $item['product_sku']['spec_sku_id'];
            // 商品购买总金额
            $item['total_price'] = helper::bcmul($item['product_price'], $params['product_num']);
            // 商品总结算价
            $item['total_settlement_price'] = helper::bcmul($item['settlement_price'], $params['product_num']);
        }
        $supplierData[] = [
            'shop_supplier_id' => $product['shop_supplier_id'],
            'supplier' => $product['supplier'],
            'productList' => $productList
        ];
        unset($product['supplier']);
        return $supplierData;
    }
 
    /**
     * 获取订单总数
     */
    public function getCount($user, $type = 'all', $params = [])
    {
        // 筛选条件
        $filter = [];
        if (isset($params['shop_supplier_id']) && $params['shop_supplier_id']){
            $filter['shop_supplier_id'] = $params['shop_supplier_id'];
        } else if (isset($params['store_id']) && $params['store_id']){
            $filter['extract_store_id'] = $params['store_id'];
        } else if (isset($params['clerk_id']) && $params['clerk_id']){
            //只查找门店配送订单
            $filter['extract_clerk_id'] = $params['clerk_id'];
            $filter['delivery_type'] = 40;
        } else {
            // 用户查询
            if ($user === false) {
                return false;
            }
            $filter['user_id'] = $user['user_id'];
        }
        // 订单数据类型
        switch ($type) {
            case 'all':
                break;
            case 'payment';
                $filter['pay_status'] = OrderPayStatusEnum::PENDING;
                break;
            case 'delivery';
                $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;
                $filter['delivery_status'] = 10;
                $filter['order_status'] = 10;
                break;    
            case 'received';
                $filter['pay_status'] = OrderPayStatusEnum::SUCCESS;
                $filter['delivery_status'] = 20;
                $filter['receipt_status'] = 10;
                break;
            case 'comment';
                $filter['order_status'] = 30;
                $filter['is_comment'] = 0;
                break;
        }
        return $this->where('order_status', '<>', 20)
            ->where($filter)
            ->where('is_delete', '=', 0)
            ->count();
    }
 
    /**
     * 取消订单
     */
    public function cancel($user)
    {
        if ($this['delivery_status']['value'] == 20) {
            $this->error = '已发货订单不可取消';
            return false;
        }
        //进行中的拼团订单不能取消
        if($this['order_source'] == OrderSourceEnum::ASSEMBLE){
            if($this['assemble_status'] == 10){
                $this->error = '订单正在拼团,到期后如果订单未拼团成功将自动退款';
                return false;
            }
        }
        // 订单取消事件
        return $this->transaction(function () use ($user) {
            // 订单是否已支付
            $isPay = $this['pay_status']['value'] == OrderPayStatusEnum::SUCCESS;
            // 未付款的订单
            if ($isPay == false) {
                //主商品退回库存
                ProductFactory::getFactory($this['order_source'])->backProductStock($this['product'], $isPay);
                // 回退用户优惠券
                $this['coupon_id'] > 0 && UserCouponModel::setIsUse($this['coupon_id'], false);
                $this['coupon_id_sys'] > 0 && UserCouponModel::setIsUse($this['coupon_id_sys'], false);
                // 回退用户积分
                $describe = "订单取消:{$this['order_no']}";
                $this['points_num'] > 0 && $user->setIncPoints($this['points_num'], $describe);
                //判断是否为预售订单
                if ($this['order_source'] == OrderSourceEnum::ADVANCE) {
                    if ($this['advance']['money_return'] == 1) {//预售订单退定金
                        if ((new OrderRefundService)->execute($this['advance'])) {
                            // 更新订单状态
                            $this['advance']->save([
                                'is_refund' => 1,
                                'refund_money' => $this['advance']['pay_price'],
                            ]);
                        }
                    }
                    $this['advance']->save(['order_status' => 20]);
                }
            }
            // 更新订单状态
            return $this->save(['order_status' => $isPay ? OrderStatusEnum::APPLY_CANCEL : OrderStatusEnum::CANCELLED]);
        });
    }
 
    /**
     * 订单详情
     */
    public static function getUserOrderDetail($order_id, $user_id)
    {
        $model = new static();
        $where = ['order_id' => $order_id, 'user_id' => $user_id];
        if(strlen($order_id)>=14){ //按照微信支付的 预下单填入的out_trade_no
            $where = ['trade_no' => $order_id, 'user_id' => $user_id];
            $order = $model->where($where)->with(['product' => ['image', 'refund'], 'address', 'express', 'extractStore','extractClerk', 'supplier', 'advance'])->find();
            if(!$order){
                $where = ['ins_order_sn' => $order_id, 'user_id' => $user_id];
                $order = $model->where($where)->with(['product' => ['image', 'refund'], 'address', 'express', 'extractStore','extractClerk', 'supplier', 'advance'])->find();
            }
        }else{
            $order = $model->where($where)->with(['product' => ['image', 'refund'], 'address', 'express', 'extractStore','extractClerk', 'supplier', 'advance'])->find();
        }
        if (empty($order)) {
            throw new BaseException(['msg' => '订单不存在']);
        }
        return $order;
    }
 
    /**
     * 供应商查看订单详情
     */
    public static function getSupplierOrderDetail($order_id, $shop_supplier_id)
    {
        $model = new static();
        $order = $model->where(['order_id' => $order_id, 'shop_supplier_id' => $shop_supplier_id])->with(['product' => ['image', 'refund'], 'address', 'express','extractStore','extractClerk','supplier', 'advance'])->find();
        if (empty($order)) {
            throw new BaseException(['msg' => '订单不存在']);
        }
        return $order;
    }
    /**
     * 余额支付标记订单已支付
     */
    public function onPaymentByBalance($orderNo, $data=[])
    {
        // 获取订单详情
        $PaySuccess = new MasterPaySuccessService($orderNo, $data);
        // 发起余额支付
        $status = $PaySuccess->onPaySuccess(OrderPayTypeEnum::BALANCE);
        if (!$status) {
            $this->error = $PaySuccess->getError();
        }
        return $status;
    }
 
    /**
     * 构建微信支付请求
     */
    protected static function onPaymentByWechat($user, $order_no, $pay_source, $online_money, $multiple, $shop_supplier_id=0)
    {
        return PaymentService::wechat(
            $user,
            $order_no,
            OrderTypeEnum::MASTER,
            $pay_source,
            $online_money,
            $multiple,
            $shop_supplier_id
        );
    }
 
    /**
     * 构建支付宝请求
     */
    protected static function onPaymentByAlipay($user, $order_no, $pay_source, $online_money, $multiple)
    {
        return PaymentService::alipay(
            $user,
            $order_no,
            OrderTypeEnum::MASTER,
            $pay_source,
            $online_money,
            $multiple
        );
    }
    /**
     * 待支付订单详情
     */
    public static function getPayDetail($orderNo)
    {
        $model = new static();
        return $model->where(['trade_no' => $orderNo, 'pay_status' => 10, 'is_delete' => 0])->with(['product', 'user', 'advance'])->find();
    }
 
    /**
     * 构建支付请求的参数
     */
    public static function onOrderPayment($user, $order_no, $payType, $pay_source, $online_money, $multiple, $shop_supplier_id=0)
    {
        //如果来源是h5,首次不处理,payH5再处理
//        if ($pay_source == 'h5') {
//            return [];
//        }
        if ($payType == OrderPayTypeEnum::WECHAT) {
            return self::onPaymentByWechat($user, $order_no, $pay_source, $online_money, $multiple, $shop_supplier_id);
        }
        if ($payType == OrderPayTypeEnum::ALIPAY) {
            return self::onPaymentByAlipay($user, $order_no, $pay_source, $online_money, $multiple);
        }
        return [];
    }
 
    /**
     * 判断当前订单是否允许核销
     */
    public function checkExtractOrder(&$order)
    {
        if (
            $order['pay_status']['value'] == OrderPayStatusEnum::SUCCESS
            && (($order['delivery_type']['value'] == DeliveryTypeEnum::EXTRACT || DeliveryTypeEnum::NO_EXPRESS) || $order['delivery_type']['value'] == DeliveryTypeEnum::STORESS)
            && ($order['delivery_status']['value'] == 10 || $order['delivery_status']['value'] == 20)
        ) {
            return true;
        }
        $this->setError('该订单不能被核销');
        return false;
    }
 
    /**
     * 当前订单是否允许申请售后
     */
    public function isAllowRefund()
    {
        // 必须是已发货的订单
        if ($this['delivery_status']['value'] != 20) {
            return false;
        }
        // 允许申请售后期限(天)
        $refundDays = SettingModel::getItem('trade')['order']['refund_days'];
        // 不允许售后
        if ($refundDays == 0) {
            return false;
        }
        // 当前时间超出允许申请售后期限
        if (
            $this['receipt_status'] == 20
            && time() > ($this['receipt_time'] + ((int)$refundDays * 86400))
        ) {
            return false;
        }
        return true;
    }
 
    /**
     * 获取活动订单
     * 已付款,未取消
     */
    public static function getPlusOrderNum($user_id, $product_id, $order_source)
    {
        $model = new static();
        return $model->alias('order')->where('order.user_id', '=', $user_id)
            ->join('order_product', 'order_product.order_id = order.order_id', 'left')
            ->where('order_product.product_source_id', '=', $product_id)
            ->where('order.pay_status', '=', 20)
            ->where('order.order_source', '=', $order_source)
            ->where('order.order_status', '<>', 20)
            ->sum('total_num');
    }
 
    /**
     * 累计成交笔数
     */
    public static function getTotalPayOrder($shop_supplier_id)
    {
        //累积成交笔数
        return (new static())->where('shop_supplier_id', '=', $shop_supplier_id)
            ->where('pay_status', '=', 20)
            ->where('order_status', 'in', [10,30])
            ->count();
    }
 
    public static function getTodayPayOrder($shop_supplier_id)
    {
        //开始
        $beginToday = mktime(0,0,0,date('m'),date('d'),date('Y'));
        //结束
        $endToday = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        //今日成交笔数
        return (new static())->where('shop_supplier_id', '=', $shop_supplier_id)
            ->where('pay_status', '=', 20)
            ->where('order_status', 'in', [10,30])
            ->whereBetweenTime('create_time', $beginToday, $endToday)
            ->count();
    }
    
    /**
     * 累计成交笔数
     */
    public static function getTotalPayOrderByStore($store_id,$clerk_id=''){
        //累积成交笔数
        $orderModel=new static();
        if(!empty($clerk_id)){
            //只查门店配送的订单
            $orderModel=$orderModel->where('extract_clerk_id', '=', $clerk_id)->where('delivery_type', '=', 40);
        }
        return $orderModel->where('extract_store_id', '=', $store_id)
            ->where('pay_status', '=', 20)
            ->where('order_status', 'in', [10,30])
            ->count();
    }
    
    public static function getTodayPayOrderByStore($store_id,$clerk_id=''){
        //开始
        $beginToday = mktime(0,0,0,date('m'),date('d'),date('Y'));
        //结束
        $endToday = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        //今日成交笔数
        $orderModel=new static();
        if(!empty($clerk_id)){
            //只查门店配送的订单
            $orderModel=$orderModel->where('extract_clerk_id', '=', $clerk_id)->where('delivery_type', '=', 40);
        }
        return $orderModel->where('extract_store_id', '=', $store_id)
            ->where('pay_status', '=', 20)
            ->where('order_status', 'in', [10,30])
            ->whereBetweenTime('create_time', $beginToday, $endToday)
            ->count();
    }
    
    /**
     * 设置错误信息
     */
    protected function setError($error)
    {
        empty($this->error) && $this->error = $error;
    }
 
    /**
     * 是否存在错误
     */
    public function hasError()
    {
        return !empty($this->error);
    }
     /**
     * 获取直播订单
     */
    public function getLiveOrder($params)
    {
        $model = $this;
        if(isset($params['room_id'])&&$params['room_id']){
            $model = $model->where('room_id','=',$params['room_id']);
        }
        if(isset($params['pay_status'])&&$params['pay_status']){
            $model = $model->where('pay_status','=',$params['pay_status']);
        }
        return $model->with(['product.image'])
            ->where('shop_supplier_id', '=', $params['shop_supplier_id'])
            ->where('room_id', '>', 0)
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc'])
            ->paginate($params);
    }
 
    /**
     * 主订单购买的数量
     * 未取消的订单
     */
    public static function getHasBuyOrderNum($user_id, $product_id)
    {
        $model = new static();
        return $model->alias('order')->where('order.user_id', '=', $user_id)
            ->join('order_product', 'order_product.order_id = order.order_id', 'left')
            ->where('order_product.product_id', '=', $product_id)
            ->where('order.order_source', '=', OrderSourceEnum::MASTER)
            ->where('order.order_status', '<>', 20)
            ->sum('total_num');
    }
 
    /**
     * 获取订单信息
     */
    public static function orderInfo($order_id, $user)
    {
        $orderID = explode(',', $order_id);
        $multiple = 0;
        if (count($orderID) > 0) {
            $payPrice = OrderModel::where('order_id', 'in', $orderID)
                ->where('user_id', '=', $user['user_id'])
                ->where('pay_status', '=', 10)
                ->sum('pay_price');
            $multiple = 1;
        } else {
            $payDetail = OrderModel::where('order_id', '=', $orderID)
                ->where('pay_status', '=', 10)
                ->find();
            $payPrice = $payDetail['pay_price'];
        }
        $payInfo['order_id'] = $orderID;
        $payInfo['pay_price'] = $payPrice;
        $payInfo['multiple'] = $multiple;
        $payInfo['app_id'] = self::$app_id;
        return $payInfo;
    }
 
    /**
     * 创建订单支付信息
     */
    public function OrderPay($user, $params)
    {
        $payType = $params['payType'];
        $payment = '';
        $online_money = 0;
        $orderID = explode(',', $params['order_id']);
        $order_no = $this->orderNo();
        $multiple = 0;
        $orderInfo = "";
        $shop_supplier_id = 0;
        //商户是否独立收款 by yj 2024.3.12
       // $is_independent = SettingModel::getIndependentOpen();
        if (count($orderID) > 1) {
 
            $order_list= OrderModel::where('order_id', 'in', $orderID)
                ->where('user_id', '=', $user['user_id'])
                ->where('pay_status', '=', 10)
                ->select();
            foreach ($order_list as $val){
                $is_independent = SupplierModel::getIndependentOpen($val["shop_supplier_id"]);
                if(!empty($is_independent) && $is_independent == 1){
                    $this->error = '不支持多个商户的订单同时支付';
                    return false;
                }
            }
 
            $payPrice = OrderModel::where('order_id', 'in', $orderID)
                ->where('user_id', '=', $user['user_id'])
                ->where('pay_status', '=', 10)
                ->sum('pay_price');
            OrderTradeModel::where('order_id', 'in', $orderID)
                ->where('pay_status', '=', 10)
                ->update(['balance' => 0, 'online_money' => 0, 'out_trade_no' => $order_no]);
            $multiple = 1;
        } else {
            $orderInfo = OrderModel::where('order_id', 'in', $orderID)
                ->where('pay_status', '=', 10)
                ->find();
            $orderInfo->save(['balance' => 0, 'online_money' => 0, 'trade_no' => $order_no]);
            $payPrice = $orderInfo['pay_price'];
            //获取订单的商户id by yj 2024.3.12
            $shop_supplier_id = $orderInfo["shop_supplier_id"];
            $is_independent = SupplierModel::getIndependentOpen($shop_supplier_id);
        }
        // 余额支付标记余额抵扣
        if ($params['use_balance'] == 1) {
 
            if(!empty($is_independent) && $is_independent == 1){
                //获取用户的商户余额
                $where =[
                  "user_id"=>$user['user_id'],
                  "shop_supplier_id"=>$shop_supplier_id,
                ];
                $supplier_user_balance = supplierBalanceModel::detail($where);
                $user_balance = empty($supplier_user_balance) ? 0 : $supplier_user_balance["balance"];
            }else{
                $user_balance = $user['balance'];
            }
 
            if ($user_balance >= $payPrice) {
                $payType = 10;
                if ($multiple == 1) {
                    OrderTradeModel::where('order_id', 'in', $orderID)
                        ->where('pay_status', '=', 10)
                        ->update(['balance' => $payPrice]);
                } else {
                    $orderInfo->save(['balance' => $payPrice]);
                }
                $data['multiple'] = $multiple;
                $this->onPaymentByBalance($order_no, $data);
            } else {
                if ($payType <= 10) {
                    $this->error = '用户余额不足';
                    return false;
                }
                $online_money = round($payPrice - $user_balance, 2);
                if ($multiple == 1) {
                    OrderTradeModel::where('order_id', 'in', $orderID)
                        ->where('pay_status', '=', 10)
                        ->update(['balance' => $user_balance, 'online_money' => $online_money]);
                } else {
                    $orderInfo->save(['balance' => $user_balance, 'online_money' => $online_money]);
                }
            }
        } else {
            if ($payType <= 10) {
                $this->error = '请选择支付方式';
                return false;
            }
            $online_money = $payPrice;
            if ($multiple == 1) {
                OrderTradeModel::where('order_id', 'in', $orderID)
                    ->where('pay_status', '=', 10)
                    ->update(['online_money' => $online_money]);
            } else {
                $orderInfo->save(['online_money' => $online_money]);
            }
        }
        $online_money > 0 && $payment = self::onOrderPayment($user, $order_no, $payType, $params['pay_source'], $online_money, $multiple, $shop_supplier_id);
 
        $result['order_id'] = $params['order_id'];
        $result['payType'] = $payType;
        $result['payment'] = $payment;
        $result['order_no'] = $order_no;
        $result['pay_price'] = $payPrice;
        $result['use_balance'] = $params['use_balance'];
        return $result;
    }
 
    /**
     * 获取尾款订单数据
     */
    public static function getFinalDetail($order_id)
    {
        $detail = self::detail($order_id);
        $detail['product'][0]['product_image'] = $detail['product'][0]['image']['file_path'];
        $detail['product'][0]['reduce_money'] = $detail['advance']['reduce_money'];
        $supplierData[] = [
            'shop_supplier_id' => $detail['shop_supplier_id'],
            'supplier' => $detail['supplier'],
            'productList' => $detail['product'],
            'order' => $detail
        ];
        return $supplierData;
    }
 
    private function formatPayEndTime($leftTime)
    {
        if ($leftTime <= 0) {
            return '';
        }
        $str = '';
        $day = floor($leftTime / 86400);
        $hour = floor(($leftTime - $day * 86400) / 3600);
        $min = floor((($leftTime - $day * 86400) - $hour * 3600) / 60);
 
        if ($day > 0) $str .= $day . '天';
        if ($hour > 0) $str .= $hour . '小时';
        if ($min > 0) $str .= $min . '分钟';
        return $str;
    }
 
    /**
     * 微信发货
     */
    public function wxDelivery()
    {
        if ($this['pay_source'] != 'wx'
            || $this['delivery_status']['value'] != 20
            || $this['order_status']['value'] != 10) {
            $this->error = '订单状态错误';
            return false;
        }
        if ($this['wx_delivery_status'] != 10) {
            $this->error = '订单已发货';
            return false;
        }
        $this->startTrans();
        try {
            // 订单同步到微信
            $result = $this->sendWxExpress($this['express_id'], $this['express_no']);
            if (!$result) {
                $this->error = '发货失败';
                return false;
            }
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
}