Exemplo n.º 1
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            //Logger.LogError("Exec order database init service!");
            await Dapper.ExecuteAsync(
                @"
SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
SET FOREIGN_KEY_CHECKS=0;
CREATE TABLE IF NOT EXISTS `Order` (
  `OrderCode` varchar(255) DEFAULT NULL,
  `UserId` int(11) DEFAULT NULL,
  `PayCode` varchar(50) DEFAULT NULL,
  `Amount` decimal(10,2) DEFAULT NULL,
  `PayStatus` tinyint(4) DEFAULT NULL,
  `OrderStatus` tinyint(4) DEFAULT NULL,
  `CreatedOn` datetime DEFAULT NULL,
  `CompletedTime` datetime DEFAULT NULL,
  `Result` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `OrderDetail` (
  `OrderCode` varchar(255) DEFAULT NULL,
  `GoodsId` int(11) DEFAULT NULL,
  `Count` int(11) DEFAULT NULL,
  `Price` decimal(10,2) DEFAULT NULL,
  `CreatedOn` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
            ");
        }
        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
                });
            }
        }
Exemplo n.º 3
0
        public async Task <(bool Succeed, string ErrorMessage)> Register(RegisterView userInfo)
        {
            if (await Dapper.ExecuteScalarAsync <int>("select count(1) from Users where email=@Email",
                                                      new { userInfo.Email }) > 0)
            {
                return(false, "Email address already exists");
            }
            var ok = await Dapper.ExecuteAsync(
                "insert into Users(email,password,nickname) values(@Email,@Password,@NickName);", new
            {
                userInfo.Email,
                Password = BCryptor.Encrypt(userInfo.Password),
                userInfo.NickName
            }) > 0;

            return(ok ? (true, "") : (false, "Registration failed"));
        }
Exemplo n.º 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"
                });
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// update order status
        /// </summary>
        /// <param name="orderCode">order uid</param>
        /// <param name="status">order status</param>
        /// <returns></returns>
        public async Task <ResponseResult <bool> > UpdateOrderStatus(string orderCode, OrderStatus status, string orderResult = "")
        {
            try
            {
                var order = await Dapper.QueryFirstOrDefaultAsync <NewOrderBase>(
                    "select OrderCode,OrderStatus,PayStatus from `Order` where OrderCode=@orderCode", new { orderCode });

                if (order.OrderStatus == status)
                {
                    //log
                    Logger.LogError($"order code is :{orderCode},updated status is the same to the old status.");
                    return(new ResponseResult <bool>
                    {
                        Result = false,
                        Error = $"operation not permitted.",
                        Success = false
                    });
                }

                if (order.OrderStatus == OrderStatus.Delete) //deleted order cann't be handle
                {
                    //log
                    Logger.LogError($"order code is :{orderCode},deleted order cann't be handled.");
                    return(new ResponseResult <bool>
                    {
                        Result = false,
                        Error = $"operation not permitted.",
                        Success = false
                    });
                }
                if (order.OrderStatus == OrderStatus.Failed) //failed order can only be delete
                {
                    if (status != OrderStatus.Delete)
                    {
                        //log
                        Logger.LogError($"order code is :{orderCode},failed order can only be deleted.");
                        return(new ResponseResult <bool>
                        {
                            Result = false,
                            Error = $"operation not permitted.",
                            Success = false
                        });
                    }
                }

                if (order.OrderStatus == OrderStatus.Cancel) //cancelled order can only be deleted
                {
                    if (status != OrderStatus.Delete)
                    {
                        //log
                        Logger.LogError($"order code is :{orderCode},cancelled order can only be deleted.");
                        return(new ResponseResult <bool>
                        {
                            Result = false,
                            Error = $"operation not permitted.",
                            Success = false
                        });
                    }
                }

                if (order.OrderStatus == OrderStatus.Submmit) //submmitted order can be cancelled or failed.
                {
                    if (status != OrderStatus.Cancel && status != OrderStatus.Failed)
                    {
                        //log
                        Logger.LogError($"order code is :{orderCode},submmitted order can only be cancelled or failed.");
                        return(new ResponseResult <bool>
                        {
                            Result = false,
                            Error = $"operation not permitted.",
                            Success = false
                        });
                    }
                }

                if (order.OrderStatus == OrderStatus.Complete) //completed order can only be deleted
                {
                    if (status != OrderStatus.Delete)
                    {
                        //log
                        Logger.LogError($"order code is :{orderCode},completed order can only be deleted.");
                        return(new ResponseResult <bool>
                        {
                            Result = false,
                            Error = $"operation not permitted.",
                            Success = false
                        });
                    }
                }

                var result = await Dapper.ExecuteAsync(
                    "update `Order` set OrderStatus=@status,Result=@orderResult where OrderCode=@orderCode",
                    new { status, orderResult, orderCode });

                if (result == 1)
                {
                    return(new ResponseResult <bool>
                    {
                        Result = true,
                        Error = "",
                        Success = true
                    });
                }
                else
                {
                    //log
                    Logger.LogError($"order code is :{orderCode},order status was not changed.");
                    return(new ResponseResult <bool>
                    {
                        Result = false,
                        Error = $"operation failed.",
                        Success = false
                    });
                }
            }
            catch (Exception e)
            {
                Logger.LogError(e, $"order code is :{orderCode},order status changed has error.");
                return(new ResponseResult <bool>
                {
                    Result = false,
                    Error = $"operation has error.",
                    Success = false
                });
            }
        }