コード例 #1
0
        [CapSubscribe("callback-stock-update")] //correspond the callbackName of publisher
        public async Task AcceptUpdateStockResult(ResponseResult <OrderPublish> result)
        {
            Dapper.BeginTransaction();
            try
            {
                if (!result.Success)
                {
                    var updateResult =
                        await UpdateOrderStatus(result.Result.OrderCode, OrderStatus.Failed, result.Error);

                    if (updateResult.Result)
                    {
                        //send email
                        await CapBus.PublishAsync("route.order.email",
                                                  new OrderUser
                        {
                            OrderCode   = result.Result.OrderCode,
                            UserId      = result.Result.UserId,
                            OrderStatus = OrderStatus.Failed
                        });

                        Dapper.CommitTransaction();
                    }
                }
            }
            catch (Exception e)
            {
                //log e.message
                Logger.LogError(e, "accepting update stock result has error");
                Dapper.RollbackTransaction();
            }
        }
コード例 #2
0
        public async Task <ResponseResult <OrderPublish> > UpdateStock(OrderPublish order)
        {
            Dapper.BeginTransaction();
            try
            {
                foreach (var goodsInfo in order.GoodsInfos)
                {
                    var tempGoods = goodsInfo;
                    var result    = await Dapper.ExecuteAsync(
                        $"update `Goods` set Stock=Stock-{tempGoods.Count} where Id={tempGoods.GoodsId} and Stock>={tempGoods.Count};");

                    if (result != 1)
                    {
                        Dapper.RollbackTransaction();
                        return(new ResponseResult <OrderPublish> {
                            Error = "stock is low", Result = order, Success = false
                        });
                    }
                }
                Dapper.CommitTransaction();
                return(new ResponseResult <OrderPublish> {
                    Error = "", Result = order, Success = true
                });
            }
            catch (Exception e)
            {
                Logger.LogError(e, "update stock has error");
                Dapper.RollbackTransaction();
                return(new ResponseResult <OrderPublish> {
                    Error = "update stock has error", Result = order, Success = false
                });
            }
        }
コード例 #3
0
        /// <summary>
        /// cancel order.
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="orderCode"></param>
        /// <returns></returns>
        public async Task <ResponseResult <bool> > CancelOrder(int userId, string orderCode)
        {
            Dapper.BeginTransaction();
            try
            {
                var updateResult = await UpdateOrderStatus(orderCode, OrderStatus.Cancel, "cancel order.");

                if (updateResult.Result)
                {
                    //send email
                    await CapBus.PublishAsync("route.order.email",
                                              new OrderUser
                    {
                        OrderCode   = orderCode,
                        UserId      = userId,
                        OrderStatus = OrderStatus.Cancel
                    });

                    Dapper.CommitTransaction();
                }

                return(updateResult);
            }
            catch (Exception e)
            {
                //log e.message
                Logger.LogError(e, "cancel order has error");
                Dapper.RollbackTransaction();
                return(new ResponseResult <bool>
                {
                    Error = "cancel order failed.",
                    Success = false,
                    Result = false
                });
            }
        }
コード例 #4
0
        /// <summary>
        /// submmit order
        /// </summary>
        /// <param name="order">order info</param>
        /// <returns></returns>
        public async Task <ResponseResult <NewOrderResult> > Submmit(NewOrderAdd order)
        {
            var orderCode      = Guid.NewGuid().ToString("N");
            var dateNow        = DateTime.Now;
            var strDateNow     = dateNow.ToString("yyyy-MM-dd HH:mm:ss");
            var lstGoodsDetail = order.GoodsInfos.Select(i =>
                                                         $"insert into `OrderDetail` (OrderCode,GoodsId,Count,Price,CreatedOn) values('{orderCode}',{i.GoodsId},{i.Count},{i.Price},'{strDateNow}')");

            Dapper.BeginTransaction();
            try
            {
                await Dapper.ExecuteAsync(
                    "insert into `Order` (OrderCode,UserId,PayCode,Amount,PayStatus,OrderStatus,CreatedOn,CompletedTime) values(@OrderCode,@UserId,@PayCode,@Amount,@PayStatus,@OrderStatus,@CreatedOn,@CompletedTime);" +
                    string.Join(";", lstGoodsDetail),
                    new
                {
                    OrderCode = orderCode,
                    order.UserId,
                    PayCode       = string.Empty,
                    Amount        = order.GoodsInfos.Sum(i => i.Price *i.Count),
                    PayStatus     = (int)PayStatus.UnComplete,
                    OrderStatus   = (int)OrderStatus.Submmit,
                    CreatedOn     = strDateNow,
                    CompletedTime = new DateTime(1999, 1, 1, 0, 0, 0)
                });

                //publish message to goods service.
                await CapBus.PublishAsync("route.order.submmit", new OrderPublish
                {
                    UserId     = order.UserId,
                    OrderCode  = orderCode,
                    GoodsInfos = order.GoodsInfos.Select(i => new GoodsInfoBase {
                        Count = i.Count, GoodsId = i.GoodsId
                    })
                                 .ToList()
                }, "callback-stock-update");

                //publish message to send email.
                await CapBus.PublishAsync("route.order.email",
                                          new OrderUser { OrderCode = orderCode, UserId = order.UserId, OrderStatus = OrderStatus.Submmit });

                Dapper.CommitTransaction();
                return(new ResponseResult <NewOrderResult>
                {
                    Success = true,
                    Result = new NewOrderResult {
                        CreatedOn = dateNow, OrderCode = orderCode
                    },
                    Error = ""
                });
            }
            catch (Exception e)
            {
                //log e.message
                Logger.LogError(e, "submmit order has error");
                Dapper.RollbackTransaction();
                return(new ResponseResult <NewOrderResult>
                {
                    Success = false,
                    Result = null,
                    Error = "submmit order has error"
                });
            }
        }