示例#1
0
        private FullSuggestionModel _CreateSuggestionsModel(string woeid, int languageId, string userId)
        {
            FullSuggestionModel output = new FullSuggestionModel();

            SuggestionsResult suggestionResult = this._suggestionService.GetSuggestion(woeid, languageId, userId);

            if (suggestionResult != null)
            {
                Dictionary <int, ItemType> itemTypes = this._dataStore.GetAllItemTypes().ToDictionary(k => k.Id);

                foreach (string key in suggestionResult.CommonSuggestions.Keys)
                {
                    output.CommonSuggestedItems.Add(key, suggestionResult.CommonSuggestions[key].Select(x => new SuggestionModel {
                        Name = x
                    }).ToList());
                }

                var itemsToUpdateShowTimes = new List <int>();

                foreach (string actionTypeName in suggestionResult.Suggestions.Keys)
                {
                    // Create HTML id from action type name.
                    string strongActionTypeName = actionTypeName.RemoveSpecialSymbols().ToLower();
                    if (!output.SuggestedItems.Keys.Contains(strongActionTypeName))
                    {
                        output.SuggestedItems.Add(strongActionTypeName, new List <SuggestionModel>());
                    }

                    var itemsCollection = suggestionResult.Suggestions[actionTypeName].SuggestionItems
                                          .Where(x => x.Item.ActionTypeId.HasValue)
                                          .Take(18); // <<<<< ===== TAKE ONLY 18 .

                    // Look through items to build model.
                    foreach (var item in itemsCollection)
                    {
                        string imagePath = String.Empty;
                        if (!string.IsNullOrEmpty(item.Item.DefaultImageUri))
                        {
                            imagePath = item.Item.DefaultImageUri;
                        }
                        else
                        {
                            var images = this._dataStore.GetImagesByItemId(item.ItemId.Value);
                            if (images.Any())
                            {
                                imagePath = images.First().ImageUrl;
                            }
                            else
                            {
                                imagePath = @"/img/default_wear/veshalka.png";
                            }
                        }

                        string   itemTypeFilter = "0";
                        ItemType itemType       = null;
                        if (item.Item.ItemTypeId.HasValue && itemTypes.TryGetValue(item.Item.ItemTypeId.Value, out itemType) && itemType.EnumType.HasValue)
                        {
                            itemTypeFilter = itemType.EnumType.Value.ToString();
                        }

                        output.SuggestedItems[strongActionTypeName].Add(
                            new SuggestionModel
                        {
                            ActionType             = actionTypeName,
                            ImageUrl               = imagePath,
                            ReferrerUrl            = item.Item.Referrer,
                            Price                  = (item.Item.Price ?? 0).ToString() + " " + ContentHelpers.GetLocalCurrencyName(item.Item.Currency),
                            Name                   = item.Item.Name,
                            Description            = item.Item.Description,
                            MadeBy                 = item.Item.MadeBy,
                            ProvideBy              = item.Item.ProvideBy ?? string.Empty,
                            Language               = CurrentLang.Code,
                            Gender                 = item.Item.Gender,
                            Season                 = item.Item.Season.ToString(),
                            WaterProtectionPercent =
                                item.Item.WaterProtectionPercent.HasValue
                                        ? item.Item.WaterProtectionPercent.Value
                                        : 0,
                            IceProtection =
                                item.Item.IceProtectionPercent.HasValue
                                        ? item.Item.IceProtectionPercent.Value
                                        : false,
                            ArmoringPercent =
                                item.Item.ArmoringPercent.HasValue
                                        ? item.Item.ArmoringPercent.Value
                                        : 0,
                            MinAge = item.Item.MinAge.HasValue ? item.Item.MinAge.Value : 0,
                            MaxAge = item.Item.MaxAge.HasValue ? item.Item.MaxAge.Value : 0,
                            SunProtectionPercent =
                                item.Item.SunProtectionPercent.HasValue
                                        ? item.Item.SunProtectionPercent.Value
                                        : 0,
                            ItemType   = itemTypeFilter,
                            IsWardrobe = item.Item.IsWardrobe
                        });
                    }

                    itemsToUpdateShowTimes.AddRange(itemsCollection.Where(x => x.ItemId.HasValue).Select(x => x.ItemId.Value));

                    if (string.IsNullOrEmpty(output.ForecastDescription))
                    {
                        output.ForecastDescription = suggestionResult.Suggestions[actionTypeName].FullDescription;
                    }
                }

                _dataStore.UpdateShowedTimes(itemsToUpdateShowTimes);
            }

            return(output);
        }