コード例 #1
0
 public async Task <APIResult> Get(long orderId)
 {
     return(new APIResult()
     {
         Result = 0,
         Data = await farmerOrderQueries.Get(orderId)
     });
 }
コード例 #2
0
        public override async Task <int> HandleCommand(UpdateStatusCommand request, CancellationToken cancellationToken)
        {
            if (request.OrderId == 0)
            {
                return(-1);
            }

            if (!Enum.IsDefined(typeof(FarmerOrderStatuses), request.StatusId))
            {
                return(-1); //wrong status
            }

            var order = await farmerOrderQueries.Get(request.OrderId);

            if (order != null)
            {
                if (request.StatusId == (int)FarmerOrderStatuses.Canceled)
                {
                    //warning: condition of cancel FarmerOrder
                    if (order.BuyingDate > DateTime.Now.Date)
                    {
                        throw new BusinessException("Order.Farmer.ErrorCanceledCondition");
                    }
                }
                else
                {
                    if (order.StatusId != request.StatusId - 1)
                    {
                        throw new BusinessException("Order.WrongStep");
                    }
                }

                order          = UpdateBuild(order, request.LoginSession);
                order.StatusId = request.StatusId;
                return(await farmerOrderRepository.Update(order));
            }

            return(-1);
        }
コード例 #3
0
        public override async Task <int> HandleCommand(UpdateInCollectorCommand request, CancellationToken cancellationToken)
        {
            if (request.Order == null || request.Order.Id == 0 ||
                request.Order.Items == null || request.Order.Items.Count == 0)
            {
                return(-1);
            }

            var rs = 0;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    string tranId = Guid.NewGuid().ToString().Replace("-", "");
                    try
                    {
                        farmerOrderRepository.JoinTransaction(conn, trans);
                        farmerOrderQueries.JoinTransaction(conn, trans);
                        farmerRetailerOrderItemQueries.JoinTransaction(conn, trans);

                        var order = await farmerOrderQueries.Get(request.Order.Id);

                        if (order.FarmerId != request.Order.FarmerId)
                        {
                            return(rs = -1); //fake api
                        }

                        if (!Enum.IsDefined(typeof(FarmerOrderStatuses), request.Order.StatusId))
                        {
                            return(rs = -1); //wrong status
                        }

                        var status = (FarmerOrderStatuses)request.Order.StatusId;
                        if (status != FarmerOrderStatuses.Canceled && status != FarmerOrderStatuses.Completed)
                        {
                            return(rs = -1);
                        }

                        var frOrders = await farmerRetailerOrderItemQueries.GetByBC(order.Id);

                        decimal totalAmount = 0;
                        foreach (var item in request.Order.Items)
                        {
                            var orderItem = order.Items.First(i => i.Id == item.Id);
                            orderItem.StatusId           = (int)status;
                            orderItem.DeliveriedQuantity = status == FarmerOrderStatuses.Canceled ? 0 : item.DeliveriedQuantity;
                            await farmerOrderRepository.UpdateItem(item);

                            totalAmount += orderItem.Price * item.DeliveriedQuantity;

                            if (status == FarmerOrderStatuses.Completed)
                            {
                                await WebHelper.HttpPost <object>(GlobalConfiguration.APIGateWayURI, AppUrl.SetCollectionInventory, new {
                                    Direction = 1,
                                    request.Order.CollectionId,
                                    TraceCode = GetMockTraceCode(),
                                    item.ProductId,
                                    item.UoMId,
                                    Quantity     = item.DeliveriedQuantity,
                                    CommandStyle = (int)CommandStyles.Transaction,
                                    CommandId    = tranId
                                }, request.LoginSession.AccessToken);

                                var mappingItem = frOrders.FirstOrDefault(i => i.IsPlanning == false && i.FarmerOrderItemId == item.Id);
                                if (mappingItem != null)
                                {
                                    await mediator.Send(new UpdateItemStatusCommand(
                                                            mappingItem.RetailerOrderId,
                                                            mappingItem.RetailerOrderItemId,
                                                            (int)RetailerOrderStatuses.InConllections
                                                            )
                                    {
                                        CommandId    = tranId,
                                        LoginSession = request.LoginSession,
                                        CommandStyle = (int)CommandStyles.Transaction
                                    }
                                                        );
                                }
                            }
                        }

                        request.Order          = UpdateBuild(request.Order, request.LoginSession);
                        request.Order.StatusId = (int)status;
                        request.Order.FarmerBuyingCalendarId = order.FarmerBuyingCalendarId;
                        request.Order.TotalAmount            = status == FarmerOrderStatuses.Canceled ? 0 : totalAmount;
                        if (await farmerOrderRepository.Update(request.Order) != 0)
                        {
                            throw new Exception("Update Order failed");
                        }
                    }
                    catch (Exception ex)
                    {
                        rs = -1;
                        throw ex;
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            // commit
                            try
                            {
                                await WebHelper.HttpPost <object>(GlobalConfiguration.APIGateWayURI, AppUrl.SetCollectionInventory, new
                                {
                                    CommandStyle = (int)CommandStyles.CommitTransaction,
                                    CommandId    = tranId
                                }, request.LoginSession.AccessToken);

                                await mediator.Send(new UpdateItemStatusCommand(0, 0, 0)
                                {
                                    CommandId    = tranId,
                                    LoginSession = request.LoginSession,
                                    CommandStyle = (int)CommandStyles.CommitTransaction
                                });

                                trans.Commit();
                            }
                            catch (Exception ex) {
                                trans.Rollback();
                                throw ex;
                            }
                        }
                        else
                        {
                            try
                            {
                                // rollback
                                await WebHelper.HttpPost <object>(GlobalConfiguration.APIGateWayURI, AppUrl.SetCollectionInventory, new
                                {
                                    CommandStyle = (int)CommandStyles.RollbackTransaction,
                                    CommandId    = tranId
                                }, request.LoginSession.AccessToken);
                            }
                            catch { }
                            try
                            {
                                // rollback
                                await mediator.Send(new UpdateItemStatusCommand(0, 0, 0)
                                {
                                    CommandId    = tranId,
                                    LoginSession = request.LoginSession,
                                    CommandStyle = (int)CommandStyles.RollbackTransaction
                                });
                            }
                            catch { }
                            try
                            {
                                trans.Rollback();
                            }
                            catch {  }
                        }
                    }
                }
            }

            return(rs);
        }
コード例 #4
0
        public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken)
        {
            if (request.Order == null || request.Order.Id == 0 ||
                request.Order.Items == null || request.Order.Items.Count == 0)
            {
                return(-1);
            }

            var rs = 0;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction(System.Data.IsolationLevel.ReadCommitted))
                {
                    try
                    {
                        farmerOrderRepository.JoinTransaction(conn, trans);
                        farmerOrderQueries.JoinTransaction(conn, trans);
                        productQueries.JoinTransaction(conn, trans);
                        var order = await farmerOrderQueries.Get(request.Order.Id);

                        if (order.FarmerId != request.Order.FarmerId)
                        {
                            return(rs = -1); //fake api
                        }

                        #warning Hoang Uncompleted
                        //check business update farmer order here
                        bool isOk = false;
                        if (isOk)
                        {
                            return(rs = -1); //Time out
                        }

                        decimal totalAmount = 0;
                        await farmerOrderRepository.DeleteItems(request.Order.Id);

                        foreach (var item in request.Order.Items)
                        {
                            var prod = (await productQueries.Get(item.ProductId)).FirstOrDefault(p => p.CurrentUoM == item.UoMId);
                            if (prod == null)
                            {
                                throw new Exception("Product doesn't existed");
                            }

                            item.Id                 = request.Order.Id;
                            item.StatusId           = (int)FarmerOrderStatuses.BeginOrder;
                            item.DeliveriedQuantity = 0;
                            item.Price              = prod.BuyingCurrentPrice;
                            await farmerOrderRepository.AddItem(item);

                            totalAmount += prod.BuyingCurrentPrice * item.OrderedQuantity;
                        }

                        request.Order          = UpdateBuild(request.Order, request.LoginSession);
                        request.Order.StatusId = (int)FarmerOrderStatuses.BeginOrder;
                        request.Order.FarmerBuyingCalendarId = order.FarmerBuyingCalendarId;
                        request.Order.TotalAmount            = totalAmount;
                        if (await farmerOrderRepository.Update(request.Order) != 0)
                        {
                            throw new Exception("Update Order failed");
                        }
                    }
                    catch (Exception ex)
                    {
                        rs = -1;
                        throw ex;
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try
                            {
                                trans.Rollback();
                            }
                            catch { }
                        }
                    }
                }
            }

            return(rs);
        }