Exemple #1
0
        public async Task <AcceptOrderSuggestionCommandResponse> Handle(AcceptOrderSuggestionCommand command)
        {
            var setting = _applicationSettingRepository.AsQuery().FirstOrDefault();

            var orderSuggestion = await _repository.AsQuery().SingleOrDefaultAsync(p => p.Id == command.OrderSuggestionId);

            if (orderSuggestion == null)
            {
                throw new DomainException("پیشنهاد سفارش یافت نشد");
            }
            var order = _orderRepository.AsQuery().SingleOrDefault(p => p.Id == orderSuggestion.OrderId);

            if (order == null)
            {
                throw new DomainException("سفارش یافت نشد");
            }

            if (DateTime.Now > orderSuggestion.CreationTime.AddMinutes(setting.OrderSuggestionExpireTime))
            {
                throw new DomainException("زمان تایید پیشنهاد پایان یافته است");
            }
            var selectedOrderSuggestionItems =
                orderSuggestion.OrderSuggestionItems.Where(p => command.AcceptOrderSuggestionItem.Any(i => i.OrderSuggestionItemId == p.Id))
                .ToList();
            long factorId = _seqRepository.GetNextSequenceValue(SqNames.FactorIdSequence);

            List <FactorRaw> factorRaws = new List <FactorRaw>();

            var havePercentDiscountToday = _orderDomainService.CheckOrderPercentDiscountToday(order.Customer, orderSuggestion.OrderId);

            foreach (var selectedOrderSuggestionItem in selectedOrderSuggestionItems)
            {
                switch (selectedOrderSuggestionItem)
                {
                case AlternativeProductSuggestionItem alternativeProductSuggestionItem:
                {
                    var itemQuantity = command.AcceptOrderSuggestionItem.SingleOrDefault(p =>
                                                                                         p.OrderSuggestionItemId == selectedOrderSuggestionItem.Id);
                    var quantity = itemQuantity?.Quantity ?? alternativeProductSuggestionItem.Quantity;
                    var product  = _productRepository.Find(alternativeProductSuggestionItem
                                                           .AlternativeProductSuggestion.ProductId);
                    if (product == null)
                    {
                        throw new DomainException("محصول جایگزین یافت نشد");
                    }
                    var factorRaw = new FactorRaw(product.Id, quantity,
                                                  alternativeProductSuggestionItem.Description,
                                                  alternativeProductSuggestionItem.Price, product.Name,
                                                  alternativeProductSuggestionItem.AlternativeProductSuggestion
                                                  .ProductImage, product.Brand.Id, product.Brand.Name, null);
                    factorRaws.Add(factorRaw);
                    break;
                }

                case HasProductSuggestionItem hasProductSuggestionItem:
                {
                    var itemQuantity = command.AcceptOrderSuggestionItem.SingleOrDefault(p =>
                                                                                         p.OrderSuggestionItemId == selectedOrderSuggestionItem.Id);
                    var       quantity = itemQuantity?.Quantity ?? hasProductSuggestionItem.Quantity;
                    var       product  = _productRepository.Find(hasProductSuggestionItem.OrderItem.OrderProduct.ProductId);
                    FactorRaw factorRaw;
                    if (havePercentDiscountToday)
                    {
                        if (hasProductSuggestionItem.OrderItem.Discount != null)
                        {
                            if (hasProductSuggestionItem.OrderItem.Discount is OrderItemPercentDiscount
                                orderItemPercentDiscount)
                            {
                                if (orderItemPercentDiscount.HasDiscountValid())
                                {
                                    factorRaw = new FactorRaw(product.Id, quantity,
                                                              hasProductSuggestionItem.Description,
                                                              hasProductSuggestionItem.Price, product.Name,
                                                              hasProductSuggestionItem.OrderItem.OrderProduct.ProductImage,
                                                              product.Brand.Id, product.Brand.Name,
                                                              new FactorRawPercentDiscount(Guid.NewGuid(),
                                                                                           orderItemPercentDiscount.DiscountId,
                                                                                           orderItemPercentDiscount.DiscountTitle,
                                                                                           orderItemPercentDiscount.FromDate, orderItemPercentDiscount.ToDate,
                                                                                           orderItemPercentDiscount.Percent, orderItemPercentDiscount.FromTime,
                                                                                           orderItemPercentDiscount.ToTime));
                                }
                                else
                                {
                                    factorRaw = new FactorRaw(product.Id, quantity,
                                                              hasProductSuggestionItem.Description,
                                                              hasProductSuggestionItem.Price, product.Name,
                                                              hasProductSuggestionItem.OrderItem.OrderProduct.ProductImage,
                                                              product.Brand.Id,
                                                              product.Brand.Name, null);
                                }
                            }
                            else
                            {
                                factorRaw = new FactorRaw(product.Id, quantity,
                                                          hasProductSuggestionItem.Description,
                                                          hasProductSuggestionItem.Price, product.Name,
                                                          hasProductSuggestionItem.OrderItem.OrderProduct.ProductImage,
                                                          product.Brand.Id,
                                                          product.Brand.Name, null);
                            }
                        }
                        else
                        {
                            factorRaw = new FactorRaw(product.Id, quantity,
                                                      hasProductSuggestionItem.Description,
                                                      hasProductSuggestionItem.Price, product.Name,
                                                      hasProductSuggestionItem.OrderItem.OrderProduct.ProductImage, product.Brand.Id,
                                                      product.Brand.Name, null);
                        }
                    }
                    else
                    {
                        factorRaw = new FactorRaw(product.Id, quantity,
                                                  hasProductSuggestionItem.Description,
                                                  hasProductSuggestionItem.Price, product.Name,
                                                  hasProductSuggestionItem.OrderItem.OrderProduct.ProductImage, product.Brand.Id,
                                                  product.Brand.Name, null);
                    }
                    factorRaws.Add(factorRaw);
                    break;
                }
                }
            }
            DomainEventDispatcher.Raise(new TheOrderStatusWentToAcceptEvent(order));
            DomainEventDispatcher.Raise(new CreateFactorEvent(factorId, order.Id, orderSuggestion.Id,
                                                              CalcTotalPrice(factorRaws), orderSuggestion.Discount,
                                                              factorRaws, orderSuggestion.Shop, order.Customer, order, orderSuggestion.ShippingTime));
            orderSuggestion.OrderSuggestionStatus = OrderSuggestionStatus.Accept;
            return(new AcceptOrderSuggestionCommandResponse(factorId));
        }