Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public async Task<List<ProductCombinationsModel>> GetProductAvailableCategoryAttributeCombinations(SearchAttributesModel searchModel)
- {
- List<ProductAttributeDetail> productAttributeDetails = new List<ProductAttributeDetail>();
- //if (searchModel.SelectedCategoryAttributeValueIds != null)
- //{
- // productAttributeDetails = _productAttributeDetailRepository
- // .Table.Where(x => x.ProductAttribute.ProductId == searchModel.ProductId && searchModel.SelectedCategoryAttributeValueIds.Contains(x.CategoryAttributeValueId)).SelectMany(x => x.ProductAttribute.ProductAttributeDetail).ToList();
- // productAttributeDetails = _productAttributeRepository
- // .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();
- //}
- //else
- //{
- productAttributeDetails = _productAttributeDetailRepository
- .Table.Where(x => x.ProductAttribute.ProductId == searchModel.ProductId).SelectMany(x => x.ProductAttribute.ProductAttributeDetail).ToList();
- //}
- var groupedResult = productAttributeDetails.GroupBy(x => x.CategoryAttributeId);
- var list = groupedResult.ToList();
- List<ProductCombinationsModel> data = new List<ProductCombinationsModel>();
- foreach(var result in groupedResult)
- {
- ProductCombinationsModel model = new ProductCombinationsModel();
- model.ProductId = searchModel.ProductId;
- model.CategoryAttributeId = result.Key;
- model.CategoryAttributeName = _categoryAttributeRepository.GetNameById(model.CategoryAttributeId);
- var valueIds = result.Select(x => x.CategoryAttributeValueId).Distinct();
- var otherValues = searchModel.SelectedCategoryAttributeValueIds != null
- ? searchModel.SelectedCategoryAttributeValueIds.Where(x => !valueIds.Contains(x))
- : null;
- if (searchModel.SelectedCategoryAttributeValueIds != null && otherValues != null)
- {
- 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())
- .Select(x => x.CategoryAttributeValue).Distinct().ProjectTo<CategoryAttributeValueViewModel>().ToList();
- foreach (var attributeValue in model.CategoryAttributeValues)
- {
- if (searchModel.SelectedCategoryAttributeValueIds.Contains(attributeValue.Id.Value))
- {
- attributeValue.Selected = true;
- }
- }
- }
- else
- {
- model.CategoryAttributeValues = _categoryAttributeValueRepository.Table.Where(x => valueIds.Contains(x.Id)).ProjectTo<CategoryAttributeValueViewModel>().ToList();
- }
- data.Add(model);
- }
- foreach(var model in data)
- {
- var categoryId = model.CategoryAttributeId;
- var categoryValueId = model.CategoryAttributeValues.FirstOrDefault(x => x.Selected);
- if (categoryValueId == null) break;
- var productAttributeId = _productAttributeDetailRepository.GetMany(x => x.CategoryAttributeId == categoryId && x.CategoryAttributeValueId == categoryValueId.Id).Select(x => x.ProductAttributeId).ToList();
- foreach (var id in productAttributeId)
- {
- var productAtttibute = _productAttributeRepository.Get(x => x.Id == id);
- if (productAtttibute.ProductId == searchModel.ProductId)
- {
- var quantity = productAtttibute.Quantity;
- 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);
- if (quantity - items == 0)
- {
- var detail = productAtttibute.ProductAttributeDetail;
- if (detail.Count == 1)
- {
- var deleteModel = model.CategoryAttributeValues.First(x => x.Id == detail.FirstOrDefault().CategoryAttributeValueId);
- Debug.WriteLine(id);
- foreach (var tt in data)
- {
- tt.CategoryAttributeValues.Remove(deleteModel);
- }
- }
- }
- }
- }
- }
- return data;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement