Exemplo n.º 1
0
 protected override Task ExecuteAsync(CancellationToken stoppingToken)
 {
     return(_busClient.SubscribeAsync <NoticeMessage <LotteryTicketed> >(async(message) =>
     {
         try
         {
             _logger.LogInformation("Received ticketing message: {0} {1} Content:{2}", message.LdpMerchanerId, message.LdpOrderId, message.Content);
             IOrderingApplicationService orderingApplicationService = _iocResolver.GetRequiredService <IOrderingApplicationService>();
             IUnitOfWorkManager unitOfWorkManager = _iocResolver.GetRequiredService <IUnitOfWorkManager>();
             using (var uow = unitOfWorkManager.Begin())
             {
                 if (message.Content.TicketingType == LotteryTicketingTypes.Success)
                 {
                     var order = await orderingApplicationService.TicketedAsync(message.LdpOrderId, message.LdpMerchanerId, message.Content.TicketedNumber, message.Content.TicketedTime, message.Content.TicketedOdds);
                     if (order != null)
                     {
                         await _schedulerManager.EnqueueAsync <ILotteryAwardingScheduler, AwardingScheduleArgs>(new AwardingScheduleArgs
                         {
                             LdpOrderId = message.LdpOrderId,
                             LdpMerchanerId = message.LdpMerchanerId,
                             LvpOrderId = message.Content.LvpOrderId,
                             LvpMerchanerId = message.Content.LvpMerchanerId
                         }, delay: order.ExpectedBonusTime - DateTime.Now);
                     }
                 }
                 else
                 {
                     await orderingApplicationService.RejectedAsync(message.LdpOrderId);
                 }
                 uow.Complete();
                 return new Ack();
             }
         }
         catch (Exception ex)
         {
             _logger.LogInformation(ex, "Received ticketing message: {0} {1} Content:{2}", message.LdpMerchanerId, message.LdpOrderId, message.Content);
         }
         return new Nack();
     }, context =>
     {
         context.UseSubscribeConfiguration(configuration =>
         {
             configuration.OnDeclaredExchange(exchange =>
             {
                 exchange.WithName("Baibaocp.LotteryNoticing")
                 .WithDurability(true)
                 .WithAutoDelete(false)
                 .WithType(ExchangeType.Topic);
             });
             configuration.FromDeclaredQueue(queue =>
             {
                 queue.WithName("LotteryOrdering.Tickets")
                 .WithAutoDelete(false)
                 .WithDurability(true);
             });
             configuration.Consume(consume =>
             {
                 consume.WithRoutingKey("LotteryOrdering.Ticketed.#");
             });
         });
     }, stoppingToken));
 }
        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            return(_busClient.SubscribeAsync <LvpOrderMessage>(async(message) =>
            {
                try
                {
                    ILotteryMerchanterApplicationService lotteryMerchanterApplicationService = _iocResolver.GetRequiredService <ILotteryMerchanterApplicationService>();

                    IUnitOfWorkManager unitOfWorkManager = _iocResolver.GetRequiredService <IUnitOfWorkManager>();
                    using (var uow = unitOfWorkManager.Begin())
                    {
                        /* 此处必须保证投注渠道已经开通相应的彩种和出票渠道*/
                        string ldpVenderId = await lotteryMerchanterApplicationService.FindLdpMerchanterIdAsync(message.LvpVenderId, message.LotteryId);
                        if (string.IsNullOrEmpty(ldpVenderId))
                        {
                            _logger.LogError("当前投注渠道{0}不支持该彩种{1}", message.LvpVenderId, message.LotteryId);
                            return new Nack();
                        }
                        IOrderingApplicationService orderingApplicationService = _iocResolver.GetRequiredService <IOrderingApplicationService>();
                        (LotteryMerchanteOrder order, TimeSpan? delay) = await orderingApplicationService.CreateAsync(message.LvpOrderId, message.LvpUserId, message.LvpVenderId, message.LotteryId, message.LotteryPlayId, message.IssueNumber, message.InvestCode, message.InvestType, message.InvestCount, message.InvestTimes, message.InvestAmount);

                        /* 需要延期投注的票加入计划任务,否则直接分票投注*/
                        if (delay.HasValue)
                        {
                            ISchedulerManager schedulerManager = _iocResolver.GetRequiredService <ISchedulerManager>();
                            await schedulerManager.EnqueueAsync <ILotteryOrderingScheduler, OrderingScheduleArgs>(new OrderingScheduleArgs
                            {
                                LdpOrderId = order.Id,
                                LdpMerchanerId = ldpVenderId,
                                Message = message
                            }, SchedulerPriority.High, delay);
                        }
                        else
                        {
                            await _dispatchOrderingMessageService.PublishAsync(order.Id, ldpVenderId, message);
                        }
                        uow.Complete();
                        return new Ack();
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error of the ordering :{0}", message.LvpOrderId);
                }
                return new Nack();
            }, context =>
            {
                context.UseSubscribeConfiguration(configuration =>
                {
                    configuration.OnDeclaredExchange(exchange =>
                    {
                        exchange.WithName("Baibaocp.LotteryOrdering")
                        .WithDurability(true)
                        .WithAutoDelete(false)
                        .WithType(ExchangeType.Topic);
                    });
                    configuration.FromDeclaredQueue(queue =>
                    {
                        queue.WithName("LotteryOrdering.Orders")
                        .WithAutoDelete(false)
                        .WithDurability(true);
                    });
                    configuration.Consume(consume =>
                    {
                        consume.WithRoutingKey("LotteryOrdering.Accepted.#");
#if DEBUG
                        consume.WithPrefetchCount(1);
#endif
                    });
                });
            }, stoppingToken));
        }