username@email.com
2024-10-29 3f91a6737fc06b45461ce11eae5660cbbf766f7e
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
/***********************************************************************
 *            Project: baifenBinfa
 *        ProjectName: 百分兵法管理系统                               
 *                Web: http://chuanyin.com                     
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 202403/02   
 *        Description: 暂无
 ***********************************************************************/
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreCms.Net.Caching.AutoMate.RedisCache;
using CoreCms.Net.Configuration;
using CoreCms.Net.IRepository;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.IServices;
using CoreCms.Net.Loging;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.ViewModels.Basics;
using CoreCms.Net.Model.ViewModels.DTO;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Utility.Extensions;
using CoreCms.Net.Utility.Helper;
using CoreCms.Net.WeChat.Service.Options;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;
using SqlSugar;
 
 
namespace CoreCms.Net.Services
{
    /// <summary>
    /// 支付单表 接口实现
    /// </summary>
    public class CoreCmsBillPaymentsServices : BaseServices<CoreCmsBillPayments>, ICoreCmsBillPaymentsServices
    {
        private readonly ICoreCmsBillPaymentsRepository _dal;
        private readonly IUnitOfWork _unitOfWork;
        private readonly IServiceProvider _serviceProvider;
 
 
        private readonly ICoreCmsSettingServices _settingServices;
        private readonly ICoreCmsUserBalanceServices _userBalanceServices;
        private readonly ICoreCmsFormSubmitServices _formSubmitServices;
        private readonly IHttpContextAccessor _httpContextAccessor;
        //private IWeChatPayServices _weChatPayServices;
        private readonly ICoreCmsPaymentsServices _paymentsServices;
        private readonly ICoreCmsOrderItemServices _orderItemServices;
        private readonly ICoreCmsServicesServices _servicesServices;
        private readonly ICoreCmsUserServicesOrderServices _userServicesOrderServices;
        private readonly ICoreCmsUserWeChatInfoServices _userWeChatInfoServices;
        private readonly ICoreCmsTopUpTypeServices _topUpTypeServices;
        private readonly ICoreCmsUserPointLogServices _userPointLogServices;
        private readonly WeChatOptions _weChatOptions;
        private readonly IRedisOperationRepository _redisOperationRepository;
 
 
 
 
        public CoreCmsBillPaymentsServices(IUnitOfWork unitOfWork
            , ICoreCmsBillPaymentsRepository dal
            , ICoreCmsSettingServices settingServices
            , IHttpContextAccessor httpContextAccessor
            , ICoreCmsUserBalanceServices userBalanceServices
            , ICoreCmsFormSubmitServices formSubmitServices
            //, IWeChatPayServices weChatPayServices
            , ICoreCmsPaymentsServices paymentsServices
            , ICoreCmsOrderItemServices orderItemServices
            , IServiceProvider serviceProvider, ICoreCmsServicesServices servicesServices
            , ICoreCmsUserServicesOrderServices userServicesOrderServices
            , ICoreCmsUserWeChatInfoServices userWeChatInfoServices
            , IOptions<WeChatOptions> weChatOptions, ICoreCmsTopUpTypeServices topUpTypeServices, ICoreCmsUserPointLogServices userPointLogServices, IRedisOperationRepository redisOperationRepository)
        {
            this._dal = dal;
            base.BaseDal = dal;
            _unitOfWork = unitOfWork;
 
            _httpContextAccessor = httpContextAccessor;
            _settingServices = settingServices;
            _userBalanceServices = userBalanceServices;
            _formSubmitServices = formSubmitServices;
            //_weChatPayServices = weChatPayServices;
            _formSubmitServices = formSubmitServices;
            _paymentsServices = paymentsServices;
            _orderItemServices = orderItemServices;
            _serviceProvider = serviceProvider;
            _servicesServices = servicesServices;
            _userServicesOrderServices = userServicesOrderServices;
            _userWeChatInfoServices = userWeChatInfoServices;
            _topUpTypeServices = topUpTypeServices;
            _userPointLogServices = userPointLogServices;
            _redisOperationRepository = redisOperationRepository;
            _weChatOptions = weChatOptions.Value;
        }
 
        #region 生成支付单的时候,格式化支付单明细
 
        /// <summary>
        /// 生成支付单的时候,格式化支付单明细
        /// </summary>
        /// <param name="orderId">订单编号</param>
        /// <param name="type"></param>
        /// <param name="params"></param>
        /// <returns></returns>
        public async Task<WebApiCallBack> FormatPaymentRel(string orderId, int type, JObject @params)
        {
            using var container = _serviceProvider.CreateScope();
            var orderServices = container.ServiceProvider.GetService<ICoreCmsOrderServices>();
 
            var jm = new WebApiCallBack();
 
            var dto = new CheckPayDTO();
 
            //订单
            if (type == (int)GlobalEnumVars.BillPaymentsType.Common
                || type == (int)GlobalEnumVars.BillPaymentsType.PinTuan
                || type == (int)GlobalEnumVars.BillPaymentsType.Group
                || type == (int)GlobalEnumVars.BillPaymentsType.Seckill
                || type == (int)GlobalEnumVars.BillPaymentsType.Bargain
                || type == (int)GlobalEnumVars.BillPaymentsType.Giveaway
                || type == (int)GlobalEnumVars.BillPaymentsType.Solitaire
                )
            {
                //如果是订单生成支付单的话,取第一条订单的店铺id,后面的所有订单都要保证是此店铺的id
                var orderModel = await orderServices.QueryByClauseAsync(p =>
                    p.orderId == orderId && p.payStatus == (int)GlobalEnumVars.OrderPayStatus.No &&
                    p.status == (int)GlobalEnumVars.OrderStatus.Normal);
                if (orderModel != null)
                {
                    dto.rel.Add(new Rel()
                    {
                        sourceId = orderId,
                        money = orderModel.orderAmount
                    });
                    dto.money += orderModel.orderAmount;
                }
                else
                {
                    jm.status = false;
                    jm.msg = "订单号:" + orderId + "没有找到,或不是未支付状态";
                    return jm;
                }
                jm.status = true;
                jm.data = dto;
 
            }
            //充值
            else if (type == (int)GlobalEnumVars.BillPaymentsType.Recharge)
            {
                var typeId = Convert.ToInt16(orderId);
                var typeModel = await _topUpTypeServices.QueryByClauseAsync(p => p.id == typeId, true);
 
                if (typeModel != null)
                {
                    dto.rel.Add(new Rel()
                    {
                        sourceId = orderId,
                        money = typeModel.defaultMoney
                    });
                    dto.money += typeModel.defaultMoney;
                }
                else
                {
                    jm.status = false;
                    jm.msg = "请输入正确的充值金额";
                    return jm;
                }
 
                jm.status = true;
                jm.data = dto;
            }
            //表单
            else if (type == (int)GlobalEnumVars.BillPaymentsType.FormPay || type == (int)GlobalEnumVars.BillPaymentsType.FormOrder)
            {
                dto.money = 0;
                var intId = orderId.ObjectToInt(0);
                if (intId <= 0)
                {
                    jm.status = false;
                    jm.msg = "表单:" + intId + "没有找到,或不是未支付状态";
                    return jm;
                }
 
 
                var formInfo = await _formSubmitServices.QueryByClauseAsync(p => p.id == intId && p.payStatus == false);
                if (formInfo != null)
                {
                    dto.rel.Add(new Rel()
                    {
                        sourceId = intId.ToString(),
                        money = formInfo.money
                    });
                    dto.money += formInfo.money;
                }
                else
                {
                    jm.status = false;
                    jm.msg = "表单:" + intId + "没有找到,或不是未支付状态";
                    return jm;
                }
                jm.status = true;
                jm.data = dto;
            }
            else if (type == (int)GlobalEnumVars.BillPaymentsType.ServiceOrder)
            {
                dto.money = 0;
 
                var order = await _userServicesOrderServices.QueryByClauseAsync(p => p.serviceOrderId == orderId);
 
                var dt = DateTime.Now;
                var where = PredicateBuilder.True<CoreCmsServices>();
                @where = @where.And(p => p.status == (int)GlobalEnumVars.ServicesStatus.Shelve);
                @where = @where.And(p => p.amount > 0);
                @where = @where.And(p => p.startTime < dt && p.endTime > dt);
                @where = @where.And(p => p.id == order.servicesId);
 
                var serviceInfo = await _servicesServices.QueryByClauseAsync(@where);
                if (serviceInfo != null)
                {
                    dto.rel.Add(new Rel()
                    {
                        sourceId = orderId,
                        money = serviceInfo.money
                    });
                    dto.money += serviceInfo.money;
                }
                else
                {
                    jm.status = false;
                    jm.msg = "服务订单:" + orderId + "没有找到,或不是有效状态";
                    return jm;
                }
 
                jm.status = true;
                jm.data = dto;
            }
 
            else if (false)
            {
                //todo 其他业务逻辑
            }
            else
            {
                jm.status = false;
                jm.msg = GlobalErrorCodeVars.Code10054;
                jm.data = 10054;
                return jm;
            }
 
            return jm;
        }
 
 
        #endregion
 
        #region 生成支付单的时候,格式化支付单明细
 
        /// <summary>
        /// 生成支付单的时候,格式化支付单明细
        /// </summary>
        /// <param name="sourceStr"></param>
        /// <param name="type"></param>
        /// <param name="params"></param>
        /// <returns></returns>
        public async Task<WebApiCallBack> BatchFormatPaymentRel(string[] sourceStr, int type, JObject @params)
        {
            using var container = _serviceProvider.CreateScope();
            var orderServices = container.ServiceProvider.GetService<ICoreCmsOrderServices>();
 
            var jm = new WebApiCallBack();
            var dto = new CheckPayDTO();
 
            //订单
            if (type == (int)GlobalEnumVars.BillPaymentsType.Common
                || type == (int)GlobalEnumVars.BillPaymentsType.PinTuan
                || type == (int)GlobalEnumVars.BillPaymentsType.Group
                || type == (int)GlobalEnumVars.BillPaymentsType.Seckill
                || type == (int)GlobalEnumVars.BillPaymentsType.Bargain
                || type == (int)GlobalEnumVars.BillPaymentsType.Giveaway
                || type == (int)GlobalEnumVars.BillPaymentsType.Solitaire
               )
            {
                //如果是订单生成支付单的话,取第一条订单的店铺id,后面的所有订单都要保证是此店铺的id
                foreach (var item in sourceStr)
                {
                    var orderModel = await orderServices.QueryByClauseAsync(p =>
                         p.orderId == item && p.payStatus == (int)GlobalEnumVars.OrderPayStatus.No &&
                         p.status == (int)GlobalEnumVars.OrderStatus.Normal);
                    if (orderModel != null)
                    {
                        dto.rel.Add(new Rel()
                        {
                            sourceId = item,
                            money = orderModel.orderAmount
                        });
                        dto.money += orderModel.orderAmount;
                    }
                    else
                    {
                        jm.status = false;
                        jm.msg = "订单号:" + item + "没有找到,或不是未支付状态";
                        return jm;
                    }
                }
                jm.status = true;
                jm.data = dto;
            }
            //充值
            else if (type == (int)GlobalEnumVars.BillPaymentsType.Recharge)
            {
                foreach (var item in sourceStr)
                {
                    var typeId = Convert.ToInt16(item);
                    var typeModel = await _topUpTypeServices.QueryByClauseAsync(p => p.id == typeId, true);
 
                    if (typeModel != null)
                    {
                        dto.rel.Add(new Rel()
                        {
                            sourceId = item,
                            money = typeModel.defaultMoney
                        });
                        dto.money += typeModel.defaultMoney;
                    }
                    else
                    {
                        jm.status = false;
                        jm.msg = "请输入正确的充值金额";
                        return jm;
                    }
 
                }
                jm.status = true;
                jm.data = dto;
            }
            //表单
            else if (type == (int)GlobalEnumVars.BillPaymentsType.FormPay || type == (int)GlobalEnumVars.BillPaymentsType.FormOrder)
            {
                dto.money = 0;
                var intIds = CommonHelper.StringArrAyToIntArray(sourceStr);
 
                foreach (var item in intIds)
                {
                    var formInfo = await _formSubmitServices.QueryByClauseAsync(p => p.id == item && p.payStatus == false);
                    if (formInfo != null)
                    {
                        dto.rel.Add(new Rel()
                        {
                            sourceId = item.ToString(),
                            money = formInfo.money
                        });
                        dto.money += formInfo.money;
                    }
                    else
                    {
                        jm.status = false;
                        jm.msg = "表单:" + item + "没有找到,或不是未支付状态";
                        return jm;
                    }
 
                }
                jm.status = true;
                jm.data = dto;
            }
            else if (type == (int)GlobalEnumVars.BillPaymentsType.ServiceOrder)
            {
                dto.money = 0;
 
                foreach (var item in sourceStr)
                {
 
                    var order = await _userServicesOrderServices.QueryByClauseAsync(p => p.serviceOrderId == item);
 
                    var dt = DateTime.Now;
                    var where = PredicateBuilder.True<CoreCmsServices>();
                    @where = @where.And(p => p.status == (int)GlobalEnumVars.ServicesStatus.Shelve);
                    @where = @where.And(p => p.amount > 0);
                    @where = @where.And(p => p.startTime < dt && p.endTime > dt);
                    @where = @where.And(p => p.id == order.servicesId);
 
                    var serviceInfo = await _servicesServices.QueryByClauseAsync(@where);
                    if (serviceInfo != null)
                    {
                        dto.rel.Add(new Rel()
                        {
                            sourceId = item,
                            money = serviceInfo.money
                        });
                        dto.money += serviceInfo.money;
                    }
                    else
                    {
                        jm.status = false;
                        jm.msg = "服务订单:" + item + "没有找到,或不是有效状态";
                        return jm;
                    }
 
                }
                jm.status = true;
                jm.data = dto;
            }
 
            else if (false)
            {
                //todo 其他业务逻辑
            }
            else
            {
                jm.status = false;
                jm.msg = GlobalErrorCodeVars.Code10054;
                jm.data = 10054;
                return jm;
            }
 
 
 
 
 
            return jm;
        }
 
 
        #endregion
 
 
        #region 支付,先生成支付单,然后去支付
        /// <summary>
        /// 支付,先生成支付单,然后去支付
        /// </summary>
        /// <param name="sourceStr">来源,一般是订单号或者用户id</param>
        /// <param name="paymentCode">支付方式</param>
        /// <param name="userId">用户序列</param>
        /// <param name="type">订单/充值/服务订单</param>
        /// <param name="params">支付的时候用到的参数,如果是微信支付的话,这里可以传trade_type=>'JSAPI'(小程序支付),或者'MWEB'(h5支付),当是JSPI的时候,可以不传其他参数了,默认就可以,默认的这个值就是JSAPI,如果是MWEB的话,需要传wap_url(网站url地址)参数和wap_name(网站名称)参数,其他支付方式需要传什么参数这个以后再说</param>
        /// <returns></returns>
        public async Task<WebApiCallBack> Pay(string sourceStr, string paymentCode, int userId, int type, JObject @params)
        {
            using var container = _serviceProvider.CreateScope();
 
            var weChatPayServices = container.ServiceProvider.GetService<IWeChatPayServices>();
            var aliPayServices = container.ServiceProvider.GetService<IAliPayServices>();
            var balancePayServices = container.ServiceProvider.GetService<IBalancePayServices>();
            var offlinePayServices = container.ServiceProvider.GetService<IOfflinePayServices>();
 
            var jm = new WebApiCallBack();
 
            //如果支付类型为余额充值,那么资源ID就是用户ID
            //if (type == (int)GlobalEnumVars.BillPaymentsType.Recharge)
            //{
            //    sourceStr = userId.ToString();
            //}
            //判断支付方式是否开启
            var paymentInfo = await _paymentsServices.QueryByClauseAsync(p => p.code == paymentCode && p.isEnable == true);
            if (paymentInfo == null)
            {
                jm.data = jm.code = 10050;
                jm.msg = GlobalErrorCodeVars.Code10050;
                return jm;
            }
            //如果是公众号支付,并且没有登陆或者没有open_id的话,报错
            var res = await CheckOpenId(paymentCode, @params);
            if (res.status == false)
            {
                return res;
            }
 
            //生成支付单,只是单纯的生成了支付单
            var result = await ToAdd(sourceStr, paymentCode, userId, type, @params);
            if (result.status == false)
            {
                return result;
            }
 
            var billPayments = result.data as CoreCmsBillPayments;
 
            if (billPayments.money < 0)
            {
                jm.msg = "支付金额异常!";
                return jm;
            }
 
            //根据支付方式返回支付配置
            //微信支付
            if (paymentCode == GlobalEnumVars.PaymentsTypes.wechatpay.ToString())
            {
                jm = await weChatPayServices.PubPay(billPayments);
            }
            //支付宝支付
            else if (paymentCode == GlobalEnumVars.PaymentsTypes.alipay.ToString())
            {
                jm = await aliPayServices.PubPay(billPayments);
 
            }
            //余额支付
            else if (paymentCode == GlobalEnumVars.PaymentsTypes.balancepay.ToString())
            {
                jm = await balancePayServices.PubPay(billPayments);
 
            }
            //线下支付
            else if (paymentCode == GlobalEnumVars.PaymentsTypes.offline.ToString())
            {
                jm = offlinePayServices.PubPay(billPayments);
 
            }
 
            return jm;
        }
 
 
        #endregion
 
        #region 如果是公众号支付,并且没有登陆或者没有open_id的话,报错
        /// <summary>
        /// 如果是公众号支付,并且没有登陆或者没有open_id的话,报错
        /// </summary>
        /// <param name="paymentCode"></param>
        /// <param name="params"></param>
        /// <returns></returns>
        private async Task<WebApiCallBack> CheckOpenId(string paymentCode, JObject jobj)
        {
            var jm = new WebApiCallBack { status = true };
 
            //当只有微信支付的时候,才判断
            if (paymentCode != "wechatpay") return jm;
 
            if (jobj != null)
            {
                //当只有公众号支付的时候,才判断
                if (jobj.ContainsKey("trade_type") && jobj["trade_type"].ObjectToString() == "JSAPI_OFFICIAL") return jm;
                if (jobj.ContainsKey("openid") && jobj["openid"].ObjectToString() != "") return jm;
 
                //到这里基本上就说明
                if (!jobj.ContainsKey("url"))
                {
                    jm.data = 10067;
                    jm.code = 10067;
                    jm.msg = GlobalErrorCodeVars.Code10067;
                    return jm;
                }
                var allConfigs = await _settingServices.GetConfigDictionaries();
                var wxOfficialAppid = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.WxOfficialAppid);
                var redirectUrl = CommonHelper.UrlEncode(jobj["url"].ObjectToString());
 
                jm.status = false;
                jm.data = 10006;
 
                jm.msg = $"https://open.weixin.qq.com/connect/oauth2/authorize?appid={_weChatOptions.WeiXinAppId}&redirect_uri={redirectUrl}&response_type={"code"}&scope={3}&state={"corecms"}{"&connect_redirect=1"}#wechat_redirect";
            }
            return jm;
        }
 
        #endregion
 
        #region 生成支付单,只是单纯的生成了支付单
 
        /// <summary>
        /// 生成支付单,只是单纯的生成了支付单
        /// </summary>
        /// <param name="sourceStr">资源id字段</param>
        /// <param name="paymentCode">支付方式</param>
        /// <param name="userId">支付用户id</param>
        /// <param name="type">支付类型</param>
        /// <param name="params">参数</param>
        /// <returns></returns>
        private async Task<WebApiCallBack> ToAdd(string sourceStr, string paymentCode, int userId = 0, int type = (int)GlobalEnumVars.BillPaymentsType.Common, JObject @params = null)
        {
            var jm = new WebApiCallBack();
 
            //判断支付方式
            var paymentInfo = await _paymentsServices.QueryByClauseAsync(p => p.code == paymentCode && p.isEnable == true);
            if (paymentInfo == null)
            {
                jm.data = jm.code = 10050;
                jm.msg = GlobalErrorCodeVars.Code10050;
                return jm;
            }
 
            var paymentRelData = new CheckPayDTO();
 
            var sourceStrArr = sourceStr.Split(",");
            if (sourceStrArr.Length > 1)
            {
                var paymentRel = await BatchFormatPaymentRel(sourceStrArr, type, @params);
                if (paymentRel.status == false)
                {
                    return paymentRel;
                }
                paymentRelData = paymentRel.data as CheckPayDTO;
            }
            else
            {
                var paymentRel = await FormatPaymentRel(sourceStr, type, @params);
                if (paymentRel.status == false)
                {
                    return paymentRel;
                }
                paymentRelData = paymentRel.data as CheckPayDTO;
            }
 
            var billPayments = new CoreCmsBillPayments();
            billPayments.paymentId = CommonHelper.GetSerialNumberType((int)GlobalEnumVars.SerialNumberType.支付单编号);
            billPayments.sourceId = sourceStr;
            billPayments.money = paymentRelData.money;
            billPayments.userId = userId;
            billPayments.type = type;
            billPayments.status = (int)GlobalEnumVars.BillPaymentsStatus.NoPay;
            billPayments.paymentCode = paymentCode;
            billPayments.ip = _httpContextAccessor.HttpContext?.Connection.RemoteIpAddress != null ? _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString() : "127.0.0.1";
            billPayments.parameters = @params != null ? JsonConvert.SerializeObject(@params) : "";
            billPayments.createTime = DateTime.Now;
 
            await _dal.InsertAsync(billPayments);
 
            //判断支付单金额是否为0,如果为0,直接支付成功,
            if (billPayments.money == 0)
            {
                //更新订单信息
                await ToUpdate(billPayments.paymentId, (int)GlobalEnumVars.BillPaymentsStatus.Payed, billPayments.paymentCode, billPayments.money, "金额为0,自动支付成功", "");
 
                jm.data = jm.code = 10059;
                jm.msg = GlobalErrorCodeVars.Code10059;
                jm.otherData = new
                {
                    sourceStrArr,
                    sourceStr,
                    paymentRelData
                };
                return jm;
            }
            //取支付标题,就不往数据库里存了吧
            billPayments.payTitle = await PayTitle(billPayments);
 
            jm.status = true;
            jm.data = billPayments;
 
            return jm;
        }
 
 
        #endregion
 
        #region 支付成功后,更新支付单状态
 
        /// <summary>
        /// 支付成功后,更新支付单状态
        /// </summary>
        /// <param name="paymentId"></param>
        /// <param name="paymentCode"></param>
        /// <param name="money"></param>
        /// <param name="status"></param>
        /// <param name="payedMsg"></param>
        /// <param name="tradeNo"></param>
        public async Task<WebApiCallBack> ToUpdate(string paymentId, int status, string paymentCode, decimal money, string payedMsg = "", string tradeNo = "")
        {
            using var container = _serviceProvider.CreateScope();
 
            var orderServices = container.ServiceProvider.GetService<ICoreCmsOrderServices>();
 
            var jm = new WebApiCallBack();
 
            var billPaymentInfo = await _dal.QueryByClauseAsync(p =>
                p.paymentId == paymentId && p.money == money &&
                p.status != (int)GlobalEnumVars.BillPaymentsStatus.Payed);
            if (billPaymentInfo == null)
            {
                NLogUtil.WriteAll(LogLevel.Trace, LogType.Order, "支付成功后,更新支付单状态", "没有找到此未支付的支付单号");
                jm.msg = "没有找到此未支付的支付单号";
                return jm;
            }
 
            billPaymentInfo.status = status;
            billPaymentInfo.paymentCode = paymentCode;
            billPaymentInfo.payedMsg = payedMsg;
            billPaymentInfo.tradeNo = tradeNo;
            billPaymentInfo.updateTime = DateTime.Now;
 
            await _dal.UpdateAsync(billPaymentInfo);
            if (status == (int)GlobalEnumVars.BillPaymentsStatus.Payed)
            {
                if (billPaymentInfo.type == (int)GlobalEnumVars.BillPaymentsType.Common
                    || billPaymentInfo.type == (int)GlobalEnumVars.BillPaymentsType.PinTuan
                    || billPaymentInfo.type == (int)GlobalEnumVars.BillPaymentsType.Group
                    || billPaymentInfo.type == (int)GlobalEnumVars.BillPaymentsType.Seckill
                    || billPaymentInfo.type == (int)GlobalEnumVars.BillPaymentsType.Bargain
                    || billPaymentInfo.type == (int)GlobalEnumVars.BillPaymentsType.Giveaway
                    || billPaymentInfo.type == (int)GlobalEnumVars.BillPaymentsType.Solitaire
                   )
                {
                    //如果是订单类型,做支付后处理
                    await orderServices.Pay(billPaymentInfo.sourceId, paymentCode, billPaymentInfo);
                }
                else if (billPaymentInfo.type == (int)GlobalEnumVars.BillPaymentsType.Recharge)
                {
                    //给用户做充值
                    var topUpTypeId = billPaymentInfo.sourceId.ObjectToInt(0);
 
                    var topUpTypeModel = await _topUpTypeServices.QueryByClauseAsync(p => p.id == topUpTypeId, true);
                    if (topUpTypeModel != null)
                    {
                        var topUpMoney = topUpTypeModel.defaultMoney + topUpTypeModel.giftMoney;
                        await _userBalanceServices.Change(billPaymentInfo.userId, (int)GlobalEnumVars.UserBalanceSourceTypes.Recharge, topUpMoney, billPaymentInfo.paymentId);
 
                        if (topUpTypeModel.giftPoint > 0)
                        {
                            await _userPointLogServices.SetPoint(billPaymentInfo.userId, topUpTypeModel.giftPoint,
                                (int)GlobalEnumVars.UserPointSourceTypes.PointTypeTopUp, "充值赠送积分");
                        }
                    }
                }
                else if (billPaymentInfo.type == (int)GlobalEnumVars.BillPaymentsType.ServiceOrder)
                {
                    //给用户做增加购买关系和生成券操作
                    await _userServicesOrderServices.CreateUserServicesTickets(billPaymentInfo.sourceId, billPaymentInfo.paymentId);
                }
                else if (billPaymentInfo.type == (int)GlobalEnumVars.BillPaymentsType.FormOrder || billPaymentInfo.type == (int)GlobalEnumVars.BillPaymentsType.FormPay)
                {
                    //form表单支付
                    var id = billPaymentInfo.sourceId.ObjectToInt(0);
                    await _formSubmitServices.Pay(id);
                }
                else
                {
                    //::todo 其他业务逻辑
                }
 
                //微信发货信息管理API发货
                await _redisOperationRepository.ListLeftPushAsync(RedisMessageQueueKey.WeChatPayShipping, billPaymentInfo.paymentId);
 
            }
            jm.status = true;
            jm.data = paymentId;
            jm.msg = "支付成功";
 
            return jm;
        }
 
        #endregion
 
 
        #region 获取支付单详情
        /// <summary>
        /// 获取支付单详情
        /// </summary>
        /// <returns></returns>
        public async Task<WebApiCallBack> GetInfo(string paymentId, int userId = 0)
        {
            var jm = new WebApiCallBack();
 
            if (string.IsNullOrEmpty(paymentId))
            {
                jm.msg = GlobalErrorCodeVars.Code13100;
                return jm;
            }
            var where = PredicateBuilder.True<CoreCmsBillPayments>();
            where = where.And(p => p.paymentId == paymentId);
            if (userId > 0)
            {
                where = where.And(p => p.userId == userId);
            }
            var billPayments = await _dal.QueryByClauseAsync(where);
            if (billPayments == null)
            {
                jm.msg = "没有找到此支付记录";
                jm.data = jm.code = 10002;
                return jm;
            }
 
            jm.status = true;
            jm.data = billPayments;
            return jm;
        }
        #endregion
 
        //扩展方法==========================================================================================
 
        #region 扩展方法
        private async Task<string> PayTitle(CoreCmsBillPayments entity)
        {
 
            var res = string.Empty;
            if (entity.type == (int)GlobalEnumVars.BillPaymentsType.Common
                || entity.type == (int)GlobalEnumVars.BillPaymentsType.PinTuan
                || entity.type == (int)GlobalEnumVars.BillPaymentsType.Group
                || entity.type == (int)GlobalEnumVars.BillPaymentsType.Seckill
                || entity.type == (int)GlobalEnumVars.BillPaymentsType.Bargain
                || entity.type == (int)GlobalEnumVars.BillPaymentsType.Giveaway
                || entity.type == (int)GlobalEnumVars.BillPaymentsType.Solitaire
              )
            {
                var orderItem = await _orderItemServices.QueryByClauseAsync(p => p.orderId == entity.sourceId);
                if (orderItem != null)
                {
                    res = orderItem.name;
                }
            }
            else if (entity.type == (int)GlobalEnumVars.BillPaymentsType.Recharge)
            {
                res = "账户充值";
            }
            else if (entity.type == (int)GlobalEnumVars.BillPaymentsType.FormPay)
            {
            }
            else if (entity.type == (int)GlobalEnumVars.BillPaymentsType.FormOrder)
            {
            }
            else if (entity.type == (int)GlobalEnumVars.BillPaymentsType.ServiceOrder)
            {
            }
            else
            {
            }
 
            if (string.IsNullOrEmpty(res))
            {
                var allConfigs = await _settingServices.GetConfigDictionaries();
                res = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShopName);  //店铺名称
            }
            return res;
        }
 
        #endregion
 
        #region 卖家直接支付操作
        /// <summary>
        /// 卖家直接支付操作
        /// </summary>
        /// <param name="orderId">订单编号</param>
        /// <param name="type">支付类型</param>
        /// <param name="paymentCode">支付类型编码</param>
        /// <returns></returns>
        public async Task<WebApiCallBack> ToPay(string orderId, int type, string paymentCode)
        {
            using (var container = _serviceProvider.CreateScope())
            {
                var orderServices = container.ServiceProvider.GetService<ICoreCmsOrderServices>();
 
                var jm = new WebApiCallBack();
 
                //查支付人id
                var userId = 0;
                if (type == (int)GlobalEnumVars.BillPaymentsType.Common
                    || type == (int)GlobalEnumVars.BillPaymentsType.PinTuan
                    || type == (int)GlobalEnumVars.BillPaymentsType.Group
                    || type == (int)GlobalEnumVars.BillPaymentsType.Seckill
                    || type == (int)GlobalEnumVars.BillPaymentsType.Bargain
                    || type == (int)GlobalEnumVars.BillPaymentsType.Giveaway
                    || type == (int)GlobalEnumVars.BillPaymentsType.Solitaire
                   )
                {
                    var orderInfo = await orderServices.QueryByIdAsync(orderId);
                    if (orderInfo == null)
                    {
                        jm.code = 10000;
                        jm.msg = GlobalErrorCodeVars.Code10000;
                        return jm;
                    }
 
                    userId = orderInfo.userId;
                    type = orderInfo.orderType;
                }
 
                //::todo 校验支付方式是否存在
                //生成支付单
                var result = await ToAdd(orderId, paymentCode, userId, type);
                if (!result.status)
                {
                    return result;
                }
                var data = result.data as CoreCmsBillPayments;
                //支付单支付
                jm = await ToUpdate(data.paymentId, (int)GlobalEnumVars.BillPaymentsStatus.Payed,
                    data.paymentCode, data.money, "后台手动支付");
                return jm;
            }
        }
        #endregion
 
 
        #region 支付单7天统计
        /// <summary>
        /// 支付单7天统计
        /// </summary>
        /// <returns></returns>
        public async Task<List<StatisticsOut>> Statistics()
        {
            return await _dal.Statistics();
        }
        #endregion
 
 
        #region 重写根据条件查询分页数据
        /// <summary>
        ///     重写根据条件查询分页数据
        /// </summary>
        /// <param name="predicate">判断集合</param>
        /// <param name="orderByType">排序方式</param>
        /// <param name="pageIndex">当前页面索引</param>
        /// <param name="pageSize">分布大小</param>
        /// <param name="orderByExpression"></param>
        /// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
        /// <returns></returns>
        public async Task<IPageList<CoreCmsBillPayments>> QueryPageAsync(Expression<Func<CoreCmsBillPayments, bool>> predicate,
            Expression<Func<CoreCmsBillPayments, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
            int pageSize = 20, bool blUseNoLock = false)
        {
            return await _dal.QueryPageAsync(predicate, orderByExpression, orderByType, pageIndex, pageSize, blUseNoLock);
        }
        #endregion
 
    }
}