示例#1
0
 public async Task <APIResult> Gets()
 {
     return(new APIResult()
     {
         Result = 0,
         Data = await productQueries.Gets(string.Empty, LanguageId, DateTime.Now)
     });
 }
        public override async Task <int> HandleCommand(ProcessCommand request, CancellationToken cancellationToken)
        {
            var rs = 0;

            //Get statistic for this logic
            var products = await productQueries.Gets();

            var uoms = await uoMQueries.Gets();

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        farmerBuyingCalendarRepository.JoinTransaction(conn, trans);
                        farmerBuyingCalendarQueries.JoinTransaction(conn, trans);
                        farmerRetailerOrderItemRepository.JoinTransaction(conn, trans);
                        retailerOrderQueries.JoinTransaction(conn, trans);
                        retailerOrderRepository.JoinTransaction(conn, trans);

                        var order = await retailerOrderQueries.Get(request.Processing.OrderId);

                        if (order == null)
                        {
                            throw new BusinessException("Order.Retailer.Order.NotExisted");
                        }

                        order.StatusId = (int)RetailerOrderStatuses.FarmerOrdered;
                        order          = UpdateBuild(order, request.LoginSession);
                        if ((await retailerOrderRepository.Update(order)) != 0)
                        {
                            return(rs = -1);
                        }

                        foreach (var item in request.Processing.Items)
                        {
                            if (item.Plannings == null)
                            {
                                continue;
                            }

                            var orderItem = order.Items.FirstOrDefault(i => i.Id == item.OrderItemId);

                            if (orderItem == null)
                            {
                                throw new BusinessException("OrderItem.NotExisted");
                            }

                            // wrong logic
                            if (!products.Any(p => p.Id == orderItem.ProductId) || !uoms.Any(u => u.Id == orderItem.UoMId))
                            {
                                return(rs = -1);
                            }

                            orderItem.StatusId = (int)RetailerOrderStatuses.FarmerOrdered;
                            if ((await retailerOrderRepository.UpdateItem(orderItem)) != 0)
                            {
                                return(rs = -1);
                            }

                            foreach (var planning in item.Plannings)
                            {
                                // Get farmer planning and check it existing
                                var checkingOrder = (await farmerBuyingCalendarQueries.Gets($"o.buying_date = '{order.BuyingDate.Date.ToString("yyyyMMdd")}' AND o.farmer_id = {planning.FarmerId} AND o.is_ordered = 0")).FirstOrDefault();
                                if (checkingOrder == null)
                                {
                                    //No existed

                                    //Create buying calendar
                                    var planningCode = await farmerBuyingCalendarQueries.GenarateCode();

                                    var addOrder = CreateBuild(new UI.Models.FarmerBuyingCalendar()
                                    {
                                        IsExpired  = order.BuyingDate <= DateTime.Now ? true : false,
                                        IsOrdered  = false,
                                        Code       = planningCode,
                                        Name       = planningCode,
                                        BuyingDate = order.BuyingDate,
                                        FarmerId   = planning.FarmerId
                                    }, request.LoginSession);
                                    var planningId = await farmerBuyingCalendarRepository.Add(addOrder);

                                    //Create item
                                    var planningItem = new FarmerBuyingCalendarItem()
                                    {
                                        ProductId              = orderItem.ProductId,
                                        UoMId                  = orderItem.UoMId,
                                        Quantity               = planning.Quantity,
                                        AdapQuantity           = planning.Quantity,
                                        FarmerBuyingCalendarId = planningId
                                    };
                                    var planningItemId = await farmerBuyingCalendarRepository.AddItem(planningItem);

                                    //Add mapping
                                    await farmerRetailerOrderItemRepository.Add(new UI.Models.FarmerRetailerOrderItems()
                                    {
                                        IsPlanning          = true,
                                        FarmerId            = planning.FarmerId,
                                        FarmerOrderId       = planningId,
                                        FarmerOrderItemId   = planningItemId,
                                        RetailerId          = order.RetailerId,
                                        RetailerOrderId     = order.Id,
                                        RetailerOrderItemId = orderItem.Id,
                                        ProductId           = orderItem.ProductId,
                                        Quantity            = planning.Quantity,
                                        UoMId = orderItem.UoMId
                                    });
                                }
                                else
                                {
                                    //Existed

                                    // Update information for Order
                                    checkingOrder = UpdateBuild(checkingOrder, request.LoginSession);
                                    if (await farmerBuyingCalendarRepository.Update(checkingOrder) != 0)
                                    {
                                        return(rs = -1);
                                    }

                                    //Get the item and update quantity for item
                                    var checkingOrderItem = checkingOrder.Items.FirstOrDefault(i => i.ProductId == orderItem.ProductId && i.UoMId == orderItem.UoMId);
                                    if (checkingOrderItem == null)
                                    {
                                        //Create item
                                        var planningItem = new FarmerBuyingCalendarItem()
                                        {
                                            ProductId              = orderItem.ProductId,
                                            UoMId                  = orderItem.UoMId,
                                            Quantity               = planning.Quantity,
                                            AdapQuantity           = planning.Quantity,
                                            FarmerBuyingCalendarId = checkingOrder.Id
                                        };
                                        var planningItemId = await farmerBuyingCalendarRepository.AddItem(planningItem);

                                        //Add mapping
                                        await farmerRetailerOrderItemRepository.Add(new UI.Models.FarmerRetailerOrderItems()
                                        {
                                            IsPlanning          = true,
                                            FarmerId            = planning.FarmerId,
                                            FarmerOrderId       = checkingOrder.Id,
                                            FarmerOrderItemId   = planningItemId,
                                            RetailerId          = order.RetailerId,
                                            RetailerOrderId     = order.Id,
                                            RetailerOrderItemId = orderItem.Id,
                                            ProductId           = orderItem.ProductId,
                                            Quantity            = planning.Quantity,
                                            UoMId = orderItem.UoMId
                                        });
                                    }
                                    else
                                    {
                                        checkingOrderItem.Quantity += planning.Quantity;
                                        if (await farmerBuyingCalendarRepository.UpdateItem(checkingOrderItem) != 0)
                                        {
                                            return(rs = -1);
                                        }

                                        //Add mapping
                                        await farmerRetailerOrderItemRepository.Add(new UI.Models.FarmerRetailerOrderItems()
                                        {
                                            IsPlanning          = true,
                                            FarmerId            = planning.FarmerId,
                                            FarmerOrderId       = checkingOrder.Id,
                                            FarmerOrderItemId   = checkingOrderItem.Id,
                                            RetailerId          = order.RetailerId,
                                            RetailerOrderId     = order.Id,
                                            RetailerOrderItemId = orderItem.Id,
                                            ProductId           = orderItem.ProductId,
                                            Quantity            = planning.Quantity,
                                            UoMId = orderItem.UoMId
                                        });
                                    }
                                }
                            }
                        }
                    }
                    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(AddCommand request, CancellationToken cancellationToken)
        {
            var rs = -1;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        productRepository.JoinTransaction(conn, trans);
                        productQueries.JoinTransaction(conn, trans);

                        if (request.Product == null)
                        {
                            throw new BusinessException("AddWrongInformation");
                        }

                        var product = (await productQueries.Gets($"p.code = '{request.Product.Code}' and p.is_deleted = 0")).FirstOrDefault();
                        if (product != null)
                        {
                            throw new BusinessException("Product.ExistedCode");
                        }

                        if (request.Product.ImageData?.Length > Constant.MaxImageLength)
                        {
                            throw new BusinessException("Image.OutOfLength");
                        }
                        //With ImageData < 100byte. This is a link image. With Image > 100byte, It can a real imageData.
                        if (request.Product.ImageData?.Length > 200)
                        {
                            string type = CommonHelper.GetImageType(System.Text.Encoding.ASCII.GetBytes(request.Product.ImageData));
                            if (!CommonHelper.IsImageType(type))
                            {
                                throw new BusinessException("Image.WrongType");
                            }
                            string Base64StringData = request.Product.ImageData.Substring(request.Product.ImageData.IndexOf(",") + 1);
                            string fileName         = Guid.NewGuid().ToString().Replace("-", "");
                            request.Product.ImageURL = CommonHelper.SaveImage($"{GlobalConfiguration.ProductImagePath}/{DateTime.Now.ToString("yyyyMM")}/", fileName, type, Base64StringData);
                        }

                        request.Product.Code = await productQueries.GenarateCode();

                        request.Product = CreateBuild(request.Product, request.LoginSession);
                        var productId = await productRepository.Add(request.Product);

                        // languages
                        foreach (var item in request.Product.Languages)
                        {
                            item.ProductId = productId;
                            await productRepository.AddOrUpdateLanguage(item);
                        }

                        // prices
                        foreach (var item in request.Product.Prices)
                        {
                            item.ProductId = productId;
                            await productRepository.AddPrice(item);
                        }

                        rs = 0;
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try
                            {
                                trans.Rollback();
                            }
                            catch { }
                            CommonHelper.DeleteImage(request.Product.ImageURL);
                        }
                    }
                }
            }

            return(rs);
        }
示例#4
0
        public override async Task <int> HandleCommand(ProcessInRetailerOrderCommand request, CancellationToken cancellationToken)
        {
            var rs = 0;

            //Get statistic for this logic
            var products = await productQueries.Gets();

            var uoms = await uoMQueries.Gets();

            using (var conn = DALHelper.GetConnection())
            {
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        farmerBuyingCalendarRepository.JoinTransaction(conn, trans);
                        farmerBuyingCalendarQueries.JoinTransaction(conn, trans);
                        farmerRetailerOrderItemRepository.JoinTransaction(conn, trans);

                        foreach (var order in request.BuyingCalendars)
                        {
                            // wrong logic
                            if (order.FarmerId == 0)
                            {
                                return(rs = -1);
                            }

                            // Add new
                            if (order.Id == 0)
                            {
                                foreach (var item in order.Items)
                                {
                                    // wrong logic
                                    if (!products.Any(p => p.Id == item.ProductId) || !uoms.Any(u => u.Id == item.UoMId))
                                    {
                                        return(rs = -1);
                                    }

                                    // Get farmer order and check it existing
                                    var checkingOrder = (await farmerBuyingCalendarQueries.Gets($"i.product_id = {item.Id} AND i.uom_id == {item.UoMId} AND o.buying_date = '{order.BuyingDate.Date}' AND o.farmer_id = {order.FarmerId} AND o.is_ordered = 0")).FirstOrDefault();
                                    if (checkingOrder == null)
                                    {
                                        //No existed

                                        //Create buying calendar
                                        order.IsExpired = order.BuyingDate <= DateTime.Now ? true : false;
                                        order.IsOrdered = false;
                                        var addOrder = CreateBuild(order, request.LoginSession);
                                        var orderId  = await farmerBuyingCalendarRepository.Add(addOrder);

                                        //Create item
                                        item.FarmerBuyingCalendarId = orderId;
                                        var orderItemId = await farmerBuyingCalendarRepository.AddItem(item);

                                        //Add mapping
                                        foreach (var FRitem in item.FarmerRetailerOrderItems)
                                        {
                                            await farmerRetailerOrderItemRepository.Add(new UI.Models.FarmerRetailerOrderItems()
                                            {
                                                FarmerId            = order.FarmerId,
                                                FarmerOrderId       = orderId,
                                                FarmerOrderItemId   = orderItemId,
                                                RetailerId          = FRitem.RetailerId,
                                                RetailerOrderId     = FRitem.RetailerOrderId,
                                                RetailerOrderItemId = FRitem.RetailerOrderItemId,
                                                ProductId           = item.ProductId,
                                                Quantity            = FRitem.Quantity,
                                                UoMId = item.UoMId
                                            });
                                        }
                                    }
                                    else
                                    {
                                        //Existed

                                        // Update information for Order
                                        var updateOrder = UpdateBuild(order, request.LoginSession);
                                        if (await farmerBuyingCalendarRepository.Update(updateOrder) != 0)
                                        {
                                            return(rs = -1);
                                        }

                                        //Get the item and update quantity for item
                                        var checkingOrderItem = updateOrder.Items.First(i => i.ProductId == item.ProductId && item.UoMId == item.UoMId);
                                        checkingOrderItem.Quantity += item.Quantity;
                                        if (await farmerBuyingCalendarRepository.UpdateItem(item) != 0)
                                        {
                                            return(rs = -1);
                                        }

                                        // Add mapping
                                        foreach (var FRitem in item.FarmerRetailerOrderItems)
                                        {
                                            await farmerRetailerOrderItemRepository.Add(new UI.Models.FarmerRetailerOrderItems()
                                            {
                                                FarmerId            = order.FarmerId,
                                                FarmerOrderId       = updateOrder.Id,
                                                FarmerOrderItemId   = checkingOrderItem.Id,
                                                RetailerId          = FRitem.RetailerId,
                                                RetailerOrderId     = FRitem.RetailerOrderId,
                                                RetailerOrderItemId = FRitem.RetailerOrderItemId,
                                                ProductId           = item.ProductId,
                                                Quantity            = FRitem.Quantity,
                                                UoMId = item.UoMId
                                            });
                                        }
                                    }
                                }
                            }
                            else //Modify
                            {
                                // Get farmer order and check it existing
                                var checkingOrder = await farmerBuyingCalendarQueries.Get(order.Id);

                                if (checkingOrder == null)
                                {
                                    return(rs = -1);
                                }

                                checkingOrder = UpdateBuild(checkingOrder, request.LoginSession);
                                if (await farmerBuyingCalendarRepository.Update(checkingOrder) != 0)
                                {
                                    return(rs = -1);
                                }

                                foreach (var item in order.Items)
                                {
                                    // wrong logic
                                    if (!products.Any(p => p.Id == item.ProductId) || !uoms.Any(u => u.Id == item.UoMId))
                                    {
                                        return(rs = -1);
                                    }

                                    // New item
                                    if (item.Id == 0)
                                    {
                                        var checkingOrderItem = checkingOrder.Items.FirstOrDefault(i => i.ProductId == item.ProductId && i.UoMId == item.UoMId);

                                        if (checkingOrderItem == null)
                                        {
                                            //Create item
                                            item.FarmerBuyingCalendarId = checkingOrder.Id;
                                            var orderItemId = await farmerBuyingCalendarRepository.AddItem(item);

                                            checkingOrder.Items.Add(item); //Add for next checking

                                            //Add mapping
                                            foreach (var FRitem in item.FarmerRetailerOrderItems)
                                            {
                                                await farmerRetailerOrderItemRepository.Add(new UI.Models.FarmerRetailerOrderItems()
                                                {
                                                    FarmerId            = order.FarmerId,
                                                    FarmerOrderId       = checkingOrder.Id,
                                                    FarmerOrderItemId   = orderItemId,
                                                    RetailerId          = FRitem.RetailerId,
                                                    RetailerOrderId     = FRitem.RetailerOrderId,
                                                    RetailerOrderItemId = FRitem.RetailerOrderItemId,
                                                    ProductId           = item.ProductId,
                                                    Quantity            = FRitem.Quantity,
                                                    UoMId = item.UoMId
                                                });
                                            }
                                        }
                                        else //New item but product and UoM is same as another item in this order
                                        {
                                            checkingOrderItem.Quantity += item.Quantity;
                                            await farmerBuyingCalendarRepository.UpdateItem(item);

                                            // Add mapping
                                            foreach (var FRitem in item.FarmerRetailerOrderItems)
                                            {
                                                await farmerRetailerOrderItemRepository.Add(new UI.Models.FarmerRetailerOrderItems()
                                                {
                                                    FarmerId            = order.FarmerId,
                                                    FarmerOrderId       = checkingOrderItem.Id,
                                                    FarmerOrderItemId   = checkingOrderItem.Id,
                                                    RetailerId          = FRitem.RetailerId,
                                                    RetailerOrderId     = FRitem.RetailerOrderId,
                                                    RetailerOrderItemId = FRitem.RetailerOrderItemId,
                                                    ProductId           = item.ProductId,
                                                    Quantity            = FRitem.Quantity,
                                                    UoMId = item.UoMId
                                                });
                                            }
                                        }
                                    }
                                    else //Modify
                                    {
                                        var checkingOrderItem = checkingOrder.Items.FirstOrDefault(i => i.ProductId == item.ProductId && i.UoMId == item.UoMId);

                                        if (checkingOrderItem == null)
                                        {
                                            return(rs = -1);
                                        }

                                        //Update Item
                                        checkingOrderItem.Quantity = item.Quantity;
                                        if (await farmerBuyingCalendarRepository.UpdateItem(checkingOrderItem) != 0)
                                        {
                                            return(rs = -1);
                                        }

                                        //Delete all mapping with this item
                                        await farmerRetailerOrderItemRepository.Delete($"`farmer_item_id` = {checkingOrderItem.Id}");

                                        //Add mapping again
                                        foreach (var FRitem in item.FarmerRetailerOrderItems)
                                        {
                                            await farmerRetailerOrderItemRepository.Add(new UI.Models.FarmerRetailerOrderItems()
                                            {
                                                FarmerId            = order.FarmerId,
                                                FarmerOrderId       = checkingOrderItem.Id,
                                                FarmerOrderItemId   = checkingOrderItem.Id,
                                                RetailerId          = FRitem.RetailerId,
                                                RetailerOrderId     = FRitem.RetailerOrderId,
                                                RetailerOrderItemId = FRitem.RetailerOrderItemId,
                                                ProductId           = item.ProductId,
                                                Quantity            = FRitem.Quantity,
                                                UoMId = item.UoMId
                                            });
                                        }
                                    }
                                }
                            }
                        }

                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        rs = -1;
                        throw ex;
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try
                            {
                                trans.Rollback();
                            }
                            catch { }
                        }
                    }
                }
            }

            return(0);
        }