public async Task <CreatePrivateOrderCommandResponse> Handle(CreatePrivateOrderCommand command) { var appSetting = _applicationSettingRepository.AsQuery().FirstOrDefault(); if (appSetting == null) { throw new Exception(); } var customer = _personRepository.AsQuery().OfType <Customer>().SingleOrDefault(p => p.UserId == command.UserId); if (customer == null) { throw new DomainException("مشتری یافت نشد"); } await _orderDomainService.CheckCustomerRequestOrderDuration(customer, appSetting); var customerAddress = customer.CustomerAddresses.SingleOrDefault(p => p.Id == command.CustomerAddressId); if (customerAddress == null) { throw new DomainException("ادرس مشتری یافت نشد"); } var shop = _personRepository.AsQuery().OfType <Shop>().SingleOrDefault(p => p.Id == command.ShopId); if (shop == null) { throw new DomainException("فروشگاه وارد شده یافت نشد"); } var orderAddress = new OrderAddress(customerAddress.AddressText, customerAddress.PhoneNumber, customerAddress.CityId, customerAddress.CityName, customerAddress.Geography); var privateOrder = new PrivateOrder(customer, orderAddress, command.Description, DateTime.Now.AddSeconds(appSetting.OrderExpireOpenTime), shop) { OrderItems = new List <OrderItem>() }; if (command.OrderItems.Any(p => p.IsPercentDiscount)) { if (_factorDomainService.HavePercentDiscountToday(customer)) { throw new DomainException("شما مجاز به انتخاب یک کالای دارای تخفیف درصدی در هر روز می باشید"); } } decimal totalPrice = 0; foreach (var orderItem in command.OrderItems) { var product = _productRepository.Find(orderItem.ProductId); if (product == null) { throw new DomainException("کالای انتخابی یافت نشد"); } var orderProduct = new OrderProduct(product.Id, product.Name, product.Price, product.MainImage, product.Brand.Id, product.Brand.Name); OrderItemDiscountBase orderItemDiscount = null; if (orderItem.IsPercentDiscount) { if (product.ProductDiscount != null) { if (product.ProductDiscount is ProductPercentDiscount productPercentDiscount) { if (productPercentDiscount.HasDiscountValid()) { var haveRemainOrderCount = _percentDiscountDomainService.HaveRemainOrderCount(productPercentDiscount.DiscountId, customer); if (haveRemainOrderCount) { orderItemDiscount = new OrderItemPercentDiscount(Guid.NewGuid(), productPercentDiscount.DiscountId, productPercentDiscount.Title, productPercentDiscount.FromDate, productPercentDiscount.ToDate, productPercentDiscount.Percent, productPercentDiscount.FromTime, productPercentDiscount.ToTime); _percentDiscountDomainService.LowOfNumberRemainOrderCount(productPercentDiscount.DiscountId, customer); } } } } } privateOrder.OrderItems.Add(new OrderItem(Guid.NewGuid(), orderItem.Quantity, orderItem.Description, orderProduct, orderItemDiscount)); totalPrice = totalPrice + (product.Price * orderItem.Quantity); } if (privateOrder.OrderItems.Count(item => item.Discount != null) > 1) { throw new DomainException("شما مجاز به انتخاب یک کالای دارای تخفیف درصدی در هر روز می باشید"); } _applicationSettingDomainService.CheckMinimumBuy(totalPrice); _orderDomainService.CalcOrderPercentDiscount(privateOrder); _privateOrderRepository.Add(privateOrder); DomainEventDispatcher.Raise(new CreateShopOrderLogEvent(shop, privateOrder)); _context.SaveChanges(); await _fcmNotification.SendToIds(shop.GetPushTokens(), "سفارش جدید", $"یک سفارش ثبت شد", NotificationType.OrderAdd, AppType.Shop, NotificationSound.Shopper); SendNotificationToBoss(privateOrder, shop, customer); return(new CreatePrivateOrderCommandResponse(privateOrder.Id, appSetting.OrderExpireOpenTime)); }