Advertisement
Guest User

Some function

a guest
Oct 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.41 KB | None | 0 0
  1. public async Task<List<ProductCombinationsModel>> GetProductAvailableCategoryAttributeCombinations(SearchAttributesModel searchModel)
  2.         {
  3.             List<ProductAttributeDetail> productAttributeDetails = new List<ProductAttributeDetail>();
  4.  
  5.             //if (searchModel.SelectedCategoryAttributeValueIds != null)
  6.             //{
  7.             //    productAttributeDetails = _productAttributeDetailRepository
  8.             //        .Table.Where(x => x.ProductAttribute.ProductId == searchModel.ProductId && searchModel.SelectedCategoryAttributeValueIds.Contains(x.CategoryAttributeValueId)).SelectMany(x => x.ProductAttribute.ProductAttributeDetail).ToList();
  9.  
  10.             //    productAttributeDetails = _productAttributeRepository
  11.             //        .Table.Where(x => x.ProductId == searchModel.ProductId && x.ProductAttributeDetail.Count(pad => searchModel.SelectedCategoryAttributeValueIds.Contains(pad.CategoryAttributeValueId)) >= searchModel.SelectedCategoryAttributeValueIds.Count - 1).SelectMany(x => x.ProductAttributeDetail).ToList();
  12.             //}
  13.             //else
  14.             //{
  15.                 productAttributeDetails = _productAttributeDetailRepository
  16.                     .Table.Where(x => x.ProductAttribute.ProductId == searchModel.ProductId).SelectMany(x => x.ProductAttribute.ProductAttributeDetail).ToList();
  17.             //}
  18.  
  19.  
  20.             var groupedResult = productAttributeDetails.GroupBy(x => x.CategoryAttributeId);
  21.  
  22.             var list = groupedResult.ToList();
  23.  
  24.  
  25.             List<ProductCombinationsModel> data = new List<ProductCombinationsModel>();
  26.             foreach(var result in groupedResult)
  27.             {
  28.                 ProductCombinationsModel model = new ProductCombinationsModel();
  29.  
  30.                 model.ProductId = searchModel.ProductId;
  31.                 model.CategoryAttributeId = result.Key;
  32.                 model.CategoryAttributeName = _categoryAttributeRepository.GetNameById(model.CategoryAttributeId);
  33.  
  34.                 var valueIds = result.Select(x => x.CategoryAttributeValueId).Distinct();
  35.                 var otherValues = searchModel.SelectedCategoryAttributeValueIds != null
  36.                     ? searchModel.SelectedCategoryAttributeValueIds.Where(x => !valueIds.Contains(x))
  37.                     : null;
  38.  
  39.                 if (searchModel.SelectedCategoryAttributeValueIds != null && otherValues != null)
  40.                 {
  41.                     model.CategoryAttributeValues = _productAttributeDetailRepository.Table.Where(x => x.CategoryAttributeId == model.CategoryAttributeId && x.ProductAttribute.ProductId == searchModel.ProductId && x.ProductAttribute.ProductAttributeDetail.Count(pad => otherValues.Contains(pad.CategoryAttributeValueId)) == otherValues.Count())
  42.                         .Select(x => x.CategoryAttributeValue).Distinct().ProjectTo<CategoryAttributeValueViewModel>().ToList();
  43.  
  44.                     foreach (var attributeValue in model.CategoryAttributeValues)
  45.                     {
  46.                         if (searchModel.SelectedCategoryAttributeValueIds.Contains(attributeValue.Id.Value))
  47.                         {
  48.                             attributeValue.Selected = true;
  49.                         }
  50.                     }
  51.                 }
  52.                 else
  53.                 {
  54.                     model.CategoryAttributeValues = _categoryAttributeValueRepository.Table.Where(x => valueIds.Contains(x.Id)).ProjectTo<CategoryAttributeValueViewModel>().ToList();
  55.                 }
  56.  
  57.                 data.Add(model);
  58.             }
  59.  
  60.             foreach(var model in data)
  61.             {
  62.                 var categoryId = model.CategoryAttributeId;
  63.                 var categoryValueId = model.CategoryAttributeValues.FirstOrDefault(x => x.Selected);
  64.  
  65.                 if (categoryValueId == null) break;
  66.  
  67.                 var productAttributeId = _productAttributeDetailRepository.GetMany(x => x.CategoryAttributeId == categoryId && x.CategoryAttributeValueId == categoryValueId.Id).Select(x => x.ProductAttributeId).ToList();
  68.                 foreach (var id in productAttributeId)
  69.                 {
  70.                     var productAtttibute = _productAttributeRepository.Get(x => x.Id == id);
  71.                     if (productAtttibute.ProductId == searchModel.ProductId)
  72.                     {
  73.                         var quantity = productAtttibute.Quantity;
  74.                         var items = await _orderItemRepository.CountAsync(x => x.ProductAttributeId == id && x.RentalStartDateUtc <= searchModel.ToDate && x.ProductId == searchModel.ProductId || x.ProductAttributeId == id && x.RentalEndDateUtc >= searchModel.FromDate && x.ProductId == searchModel.ProductId);
  75.  
  76.                         if (quantity - items == 0)
  77.                         {
  78.                             var detail = productAtttibute.ProductAttributeDetail;
  79.                             if (detail.Count == 1)
  80.                             {
  81.                                 var deleteModel = model.CategoryAttributeValues.First(x => x.Id == detail.FirstOrDefault().CategoryAttributeValueId);
  82.                                 Debug.WriteLine(id);
  83.                                 foreach (var tt in data)
  84.                                 {
  85.                                     tt.CategoryAttributeValues.Remove(deleteModel);
  86.                                 }
  87.                             }
  88.                         }
  89.                     }
  90.                 }
  91.             }
  92.  
  93.             return data;
  94.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement