private IList <ScoreStore> CalculateScore(IList <Store> stores, EnumSkrType type, User user) { ScoreRatio scoreRatio = GetScoreRatio(type); return(stores.Select(s => CalculateScore(s, scoreRatio, user)) .Where(s => s.FinalScore > 0) .OrderByDescending(s => s.FinalScore) .ToList()); }
private void SearchAndRecommendStores(IDialogContext context, EnumSkrType type) { User user = context.UserData.GetValue <User>("User"); IList <ScoreStore> stores = _storeService.GetRandomList(user, type); context.UserData.SetValue("_stores", stores); RecommendStore(context); }
private ScoreRatio GetScoreRatio(EnumSkrType type) { ScoreRatio result = new ScoreRatio(); switch (type) { case EnumSkrType.Random: result.CurrentStoreScoreRatio = 1; result.CurrentStoreScoreWithOthersRatio = 5; result.DistanceScoreRatio = 3; result.TenDayNoEatScoreRatio = 5; result.PriceRatio = 2; result.NotAlwaysEatScoreRatio = 4; break; case EnumSkrType.NotEatenYetRandom: result.CurrentStoreScoreRatio = 1; result.CurrentStoreScoreWithOthersRatio = 5; result.DistanceScoreRatio = 3; result.TenDayNoEatScoreRatio = 5; result.PriceRatio = 2; result.NotAlwaysEatScoreRatio = 5; break; case EnumSkrType.SimpleFilter: result.CurrentStoreScoreRatio = 5; result.CurrentStoreScoreWithOthersRatio = 1; result.DistanceScoreRatio = 3; result.TenDayNoEatScoreRatio = 3; result.PriceRatio = 2; result.NotAlwaysEatScoreRatio = 1; break; case EnumSkrType.Favorite: result.CurrentStoreScoreRatio = 5; result.CurrentStoreScoreWithOthersRatio = 1; result.DistanceScoreRatio = 1; result.TenDayNoEatScoreRatio = 1; result.PriceRatio = 1; result.NotAlwaysEatScoreRatio = 1; break; default: result.CurrentStoreScoreRatio = 1; result.CurrentStoreScoreWithOthersRatio = 1; result.DistanceScoreRatio = 1; result.TenDayNoEatScoreRatio = 1; result.PriceRatio = 1; result.NotAlwaysEatScoreRatio = 1; break; } return(result); }
public IList <ScoreStore> GetRandomList(User user, EnumSkrType type) { IList <Store> stores = _storeDao.GetList(user, type); stores = _recordDao.GetPositiveRecordNum(stores); IList <ScoreStore> scoreStores = CalculateScore(stores, EnumSkrType.Random, user); IList <ScoreStore> result = new List <ScoreStore>(); int totalFinalScore = 0; scoreStores.ToList() .ForEach(s => { int count = Convert.ToInt32(s.FinalScore); totalFinalScore += count; }); scoreStores.ToList() .ForEach(s => { int count = Convert.ToInt32(s.FinalScore / totalFinalScore * 500); IList <ScoreStore> sameScoreStores = Enumerable.Repeat(s, count).ToList(); result = result.Concat(sameScoreStores).ToList(); }); // Shuffle list Random random = new Random(); int index = scoreStores.Count; while (index > 1) { index--; int swapIndex = random.Next(index + 1); ScoreStore value = scoreStores[swapIndex]; scoreStores[swapIndex] = scoreStores[index]; scoreStores[index] = value; } return(result); }
public IList <Store> GetList(User user, EnumSkrType type) { DynamicParameters parameters = new DynamicParameters(); StringBuilder fromScript = new StringBuilder(); fromScript.Append("select s.*, ss.SCORE as SCORE from SKR_STORE_M as s"); fromScript.Append(" left join SKR_SCORE as ss on s.STORE_SEQ_NO = ss.STORE_SEQ_NO and ss.USER_SEQ_NO = @userId"); parameters.Add("@userId", user.Id); StringBuilder whereScript = new StringBuilder(); // Not in hate list whereScript.Append(" and s.STORE_SEQ_NO in (" + "select st.STORE_SEQ_NO from SKR_STORETYPE_M as st" + " where st.TYPE_SEQ_NO not in (select h.TYPE_SEQ_NO from SKR_TYPE_HATELIST_M as h where h.USER_SEQ_NO = @hUserId))"); parameters.Add("@hUserId", user.Id); // Exclude stores had be eaten if (type == EnumSkrType.NotEatenYetRandom) { whereScript.Append(" and s.STORE_SEQ_NO not in (" + "select r.STORE_SEQ_NO from SKR_RECORD_M as r" + " where r.USER_SEQ_NO = @rUserId)"); parameters.Add("@rUserId", user.Id); } string sql = $"{fromScript.ToString()} where 1=1 {whereScript.ToString()}"; IList <Store> result = new List <Store>(); using (DbFixture) { try { IEnumerable <dynamic> dataRows = DbFixture.Db.Connection.Query(sql, parameters); if (dataRows != null && dataRows.Count() > 0) { result = dataRows.ToList() .Select(r => { IDictionary <string, object> row = r as IDictionary <string, object>; return(new Store() { Id = row["STORE_SEQ_NO"] as string, Name = row["STORE_NAME"] as string, Price = Convert.ToDouble(row["STORE_PRICE"]), Address = row["STORE_ADDRESS"] as string, Latitude = Convert.ToDouble(row["LOCATION_LAT"]), Longitude = Convert.ToDouble(row["LOCATION_LONG"]), Distance = Convert.ToDouble(row["DISTANCE"]), Seat = row["SEAT"] as string, Url = row["STORE_URL"] as string, CreateDate = row["CRE_DTE"] as DateTime?, CreateUser = row["CRE_USR"] as string, Score = Convert.ToDouble(row["SCORE"]), StoreUrl = row["STORE_URL"] as string, PictureUrl = row["PICTURE_URL"] as string, PictureType = row["PICTURE_TYPE"] as string }); }) .ToList(); } } catch (Exception exception) { Logger.Write(EnumLogCategory.Error, exception.ToString()); } } return(result); }