public async Task <IActionResult> GetAll() { var items = await _productForSubscriptionRepository.AllIncluding(x => x.Product).ToListAsync(); var results = Mapper.Map <IEnumerable <ProductForSubscriptionViewModel> >(items); return(Ok(results)); }
public async Task <IActionResult> GetByMonthRange(int startYear, int startMonth, int endYear, int endMonth) { var start = startYear * 100 + startMonth; var end = endYear * 100 + endMonth; var products = await _productForSubscriptionRepository.AllIncluding(x => x.Product).ToListAsync(); var items = await _subscriptionMonthPromotionRepository.AllIncluding(x => x.SubscriptionMonthPromotionBonuses) .Where(x => x.Year * 100 + x.Month >= start && x.Year * 100 + x.Month <= end).ToListAsync(); var ids = items.Select(x => x.Id).ToList(); await _subscriptionMonthPromotionBonusDateRepository.All.Where(x => ids.Contains(x.SubscriptionMonthPromotionBonus.SubscriptionMonthPromotionId)).LoadAsync(); foreach (var promotion in items) { var product = products.Single(x => x.Id == promotion.ProductForSubscriptionId); var sb = new StringBuilder(product.Product.Name).AppendFormat("连续订购").AppendFormat(promotion.PromotionType.ToString()).AppendFormat("赠送:"); var index = 0; foreach (var bonus in promotion.SubscriptionMonthPromotionBonuses) { var bonusProduct = products.Single(x => x.Id == bonus.ProductForSubscriptionId); var totalCount = bonus.SubscriptionMonthPromotionBonusDates.Aggregate(0, (p, c) => p + c.DayBonusCount); if (index > 0) { sb.AppendFormat("/n/r"); } sb.AppendFormat(bonusProduct.Product.Name).AppendFormat("共") .AppendFormat(totalCount.ToString()).AppendFormat("个, 派送日期:"); foreach (var date in bonus.SubscriptionMonthPromotionBonusDates) { sb.AppendFormat(" ").AppendFormat(date.Date.ToString("yyyy-MM-dd")).AppendFormat(":") .AppendFormat(date.DayBonusCount.ToString()).AppendFormat("个;"); } index++; } } var results = Mapper.Map <IEnumerable <SubscriptionMonthPromotionViewModel> >(items); return(Ok(results)); }
public async Task <List <SubscriptionOrderDayCountErrorViewModel> > ValidateDayCountAsync(int milkmanId, List <SubscriptionOrderAddViewModel> orderVms) { // dates foreach (var orderVm in orderVms) { foreach (var orderDate in orderVm.SubscriptionOrderDates) { orderDate.Date = orderDate.Date.ToLocalTime(); } foreach (var modifiedBonusDate in orderVm.SubscriptionOrderModifiedBonusDates) { modifiedBonusDate.Date = modifiedBonusDate.Date.ToLocalTime(); } } var vmOrderDates = orderVms.SelectMany(x => x.SubscriptionOrderDates).Select(x => x.Date).ToList(); var vmModifiedBonusOrders = orderVms.SelectMany(x => x.SubscriptionOrderModifiedBonusDates).ToList(); var vmModifiedBonusDates = vmModifiedBonusOrders.Select(x => x.Date) .Distinct().ToList(); var vmOrderAndModifiedDates = vmOrderDates.Concat(vmModifiedBonusDates).ToList(); var vmPromotionDateIds = orderVms.SelectMany(x => x.SubscriptionOrderBonusDates) .Select(x => x.SubscriptionMonthPromotionBonusDateId).Distinct().ToList(); var promotionOrdersForVm = await _subscriptionMonthPromotionBonusDateRepository.AllIncluding(x => x.SubscriptionMonthPromotionBonus) .Where(x => vmPromotionDateIds.Contains(x.Id)).ToListAsync(); var vmPromotionDates = promotionOrdersForVm.Select(x => x.Date).ToList(); var allDates = vmOrderAndModifiedDates.Concat(vmPromotionDates).Distinct().OrderBy(x => x).ToList(); // db models var dbOrders = await _subscriptionOrderDateRepository.AllIncluding(x => x.SubscriptionOrder) .Where(x => x.SubscriptionOrder.MilkmanId == milkmanId && allDates.Contains(x.Date)).ToListAsync(); var dbPromotionOrders = await _subscriptionOrderBonusDateRepository.GetWithGrandPromotionInformation(milkmanId, allDates); var dbModifiedBonusOrders = await _subscriptionOrderModifiedBonusDateRepository.All .Where(x => x.SubscriptionOrder.MilkmanId == milkmanId && allDates.Contains(x.Date)).ToListAsync(); // products var vmOrderProductSnapshotIds = orderVms.Select(x => x.SubscriptionProductSnapshotId).Distinct().ToList(); var vmPromotionProductIds = promotionOrdersForVm .Select(x => x.SubscriptionMonthPromotionBonus.ProductForSubscriptionId).ToList(); var vmModifiedBonusProductSnapshotIds = vmModifiedBonusOrders.Select(x => x.SubscriptionProductSnapshotId).ToList(); var dbOrderProductSnapshotIds = dbOrders.Select(x => x.SubscriptionOrder.SubscriptionProductSnapshotId).ToList(); var dbModifiedBonusProductSnapshotIds = dbModifiedBonusOrders.Select(x => x.SubscriptionProductSnapshotId).ToList(); var dbPromotionProductIds = dbPromotionOrders .Select(x => x.SubscriptionMonthPromotionBonus.ProductForSubscriptionId).ToList(); var productSnapshotIds = vmOrderProductSnapshotIds.Concat(vmModifiedBonusProductSnapshotIds) .Concat(dbOrderProductSnapshotIds).Concat(dbModifiedBonusProductSnapshotIds).Distinct().ToList(); var productSnapshots = await _subscriptionProductSnapshotRepository.All .Where(x => productSnapshotIds.Contains(x.Id)).ToListAsync(); var productIds = vmPromotionProductIds.Concat(dbPromotionProductIds).Distinct().ToList(); var allProductIds = productIds.Concat(productSnapshots.Select(x => x.ProductForSubscriptionId)).Distinct().ToList(); var allProducts = await _productForSubscriptionRepository.AllIncluding(x => x.Product).Where(x => allProductIds.Contains(x.Id)) .ToListAsync(); var todayStr = DateTime.Now.Date.ToString("yyyy-MM-dd"); var todayProductSnapshots = await _subscriptionProductSnapshotRepository.All .Where(x => x.SubscriptionDay.Date == todayStr).ToListAsync(); // validation by date then by product var errors = new List <SubscriptionOrderDayCountErrorViewModel>(); foreach (var date in allDates) { foreach (var productId in allProductIds) { int count; var vmDatePromotionOrders = promotionOrdersForVm.Where(x => x.Date == date && x.SubscriptionMonthPromotionBonus.ProductForSubscriptionId == productId).ToList(); var dbDatePromotionOrders = dbPromotionOrders.Where(x => x.Date == date && x.SubscriptionMonthPromotionBonus.ProductForSubscriptionId == productId).ToList(); count = vmDatePromotionOrders.Sum(x => x.DayBonusCount) + dbDatePromotionOrders.Sum(x => x.DayBonusCount); var todayProductSnapshot = todayProductSnapshots.SingleOrDefault(x => x.ProductForSubscriptionId == productId); if (todayProductSnapshot != null) // 可能有order 和 modifiedBonusOrder { var todayProductSnapshotId = todayProductSnapshot.Id; var vmDateOrders = orderVms.Where(x => x.SubscriptionProductSnapshotId == todayProductSnapshotId) .SelectMany(x => x.SubscriptionOrderDates).Where(x => x.Date == date).ToList(); foreach (var vmDateOrder in vmDateOrders) { var parentOrder = orderVms.Single(x => x.SubscriptionOrderDates.Contains(vmDateOrder)); count += parentOrder.PresetDayCount + parentOrder.PresetDayGift; } var vmDateModifiedOrders = vmModifiedBonusOrders.Where(x => x.Date == date && x.SubscriptionProductSnapshotId == todayProductSnapshotId).ToList(); count += vmDateModifiedOrders.Sum(x => x.DayCount); } var productSnapshotIdsForDb = productSnapshots.Where(x => x.ProductForSubscriptionId == productId).Select(x => x.Id).ToList(); if (productSnapshotIdsForDb.Any()) { var dbDateOrders = dbOrders.Where(x => x.Date == date && productSnapshotIdsForDb.Contains(x.SubscriptionOrder.SubscriptionProductSnapshotId)).ToList(); count += dbDateOrders.Sum(x => x.SubscriptionOrder.PresetDayCount + x.SubscriptionOrder.PresetDayGift); var dbDateModifiedBonusOrders = dbModifiedBonusOrders.Where(x => x.Date == date && productSnapshotIdsForDb.Contains(x.SubscriptionProductSnapshotId)).ToList(); count += dbDateModifiedBonusOrders.Sum(x => x.DayCount); } if (count < 0) { var product = allProducts.Single(x => x.Id == productId); errors.Add(new SubscriptionOrderDayCountErrorViewModel { Date = date, DayCount = count, ProductForSubscriptionId = productId, ProductName = product.Product.Name }); } } } return(errors); }