Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }