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);
        }
예제 #2
0
        public override async Task <int> HandleCommand(AddCommand request, CancellationToken cancellationToken)
        {
            if (request.Order == null || request.Order.FarmerId == 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);

                        IEnumerable <FarmerRetailerOrderItems> farmerRetailerItems = null;

                        if (request.Order.FarmerBuyingCalendarId == 0)
                        {
                            request.Order.FarmerBuyingCalendarId = null;
                        }

                        // update status for buying calendar
                        if (request.Order.FarmerBuyingCalendarId != null)
                        {
                            farmerBuyingCalendarQueries.JoinTransaction(conn, trans);
                            farmerRetailerOrderItemQueries.JoinTransaction(conn, trans);
                            var buyingCalendar = await farmerBuyingCalendarQueries.Get(request.Order.FarmerBuyingCalendarId.Value);

                            if (buyingCalendar != null)
                            {
                                farmerBuyingCalendarRepository.JoinTransaction(conn, trans);
                                buyingCalendar           = UpdateBuild(buyingCalendar, request.LoginSession);
                                buyingCalendar.IsOrdered = true;
                                var rsUpdate = await farmerBuyingCalendarRepository.Update(buyingCalendar);

                                if (rs != 0)
                                {
                                    throw new Exception("Update Buying calendar failed");
                                }
                                farmerRetailerItems = await farmerRetailerOrderItemQueries.GetByBC(buyingCalendar.Id);
                            }
                        }

                        request.Order.Code = await farmerOrderQueries.GenarateCode();

                        request.Order          = CreateBuild(request.Order, request.LoginSession);
                        request.Order.StatusId = (int)UI.FarmerOrderStatuses.BeginOrder;
                        request.Order.Id       = await farmerOrderRepository.Add(request.Order);

                        decimal totalAmount = 0;
                        foreach (var item in request.Order.Items)
                        {
                            var prod = (await WebHelper.HttpGet <IEnumerable <ProductViewModel> >(GlobalConfiguration.APIGateWayURI, $"{AppUrl.GetProduct}?productId={item.ProductId}", request.LoginSession.AccessToken)).FirstOrDefault(p => p.CurrentUoM == item.UoMId);
                            if (prod == null)
                            {
                                throw new Exception("Product doesn't existed");
                            }

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

                            // Mapping From FarmerRetailer BuyingCalendar to FarmerRetailer Order
                            if (farmerRetailerItems != null)
                            {
                                var FRItems       = farmerRetailerItems.Where(i => i.ProductId == item.ProductId && i.UoMId == item.UoMId);
                                int totalQuantity = item.DeliveriedQuantity;
                                foreach (var FRItem in FRItems)
                                {
                                    await farmerRetailerOrderItemRepository.Add(new UI.Models.FarmerRetailerOrderItems()
                                    {
                                        IsPlanning          = false,
                                        FarmerId            = FRItem.FarmerId,
                                        FarmerOrderId       = FRItem.FarmerOrderId,
                                        FarmerOrderItemId   = FRItem.FarmerOrderItemId,
                                        RetailerId          = FRItem.RetailerId,
                                        RetailerOrderId     = FRItem.RetailerOrderId,
                                        RetailerOrderItemId = FRItem.RetailerOrderItemId,
                                        ProductId           = item.ProductId,
                                        Quantity            = (totalQuantity < FRItem.Quantity) ? totalQuantity : FRItem.Quantity,
                                        UoMId = item.UoMId
                                    });

                                    totalQuantity = (totalQuantity - FRItem.Quantity < 0) ? 0 : totalQuantity - FRItem.Quantity;
                                }
                            }

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

                        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(0);
        }
예제 #3
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);
        }