Esempio n. 1
0
 private void WeeklyRewardTask()
 {
     using (var scope = _scopeFactory.CreateScope())
     {
         var db = scope.ServiceProvider.GetRequiredService <BlogContext>();
         // today should be first day of new week
         var firstday = DateTime.Today.AddDays(-7);
         var rankings = GetRankingSinceDate(db, firstday, firstday.AddDays(6), HistoryRanking.Type.RankWeekly);
         db.HistoryRankings.RemoveRange(db.HistoryRankings.Where(h => h.RankType == HistoryRanking.Type.RankWeekly && firstday <= h.RankDate));
         db.HistoryRankings.AddRange(rankings);
         db.SaveChanges();
         ExpUtil expUtil = scope.ServiceProvider.GetService <ExpUtil>();
         expUtil.addRankExp(rankings, WeeklyReward, "周榜奖励");
     }
 }
Esempio n. 2
0
 private void MonthlyRewardTask()
 {
     using (var scope = _scopeFactory.CreateScope())
     {
         var db = scope.ServiceProvider.GetRequiredService <BlogContext>();
         // today should be first day of new month
         DateTime firstday = DateTime.Today.AddMonths(-1);
         DateTime lastDay  = new DateTime(firstday.Year, firstday.Month, DateTime.DaysInMonth(firstday.Year, firstday.Month));
         var      rankings = GetRankingSinceDate(db, firstday, lastDay, HistoryRanking.Type.RankMonthly);
         db.HistoryRankings.RemoveRange(db.HistoryRankings.Where(h => h.RankType == HistoryRanking.Type.RankMonthly && DbFunctions.DiffMonths(h.RankDate, firstday) == 0));
         db.HistoryRankings.AddRange(rankings);
         db.SaveChanges();
         // Remove any key at rankdate as we just updated.
         IDictionary <DateTime, MonthRanking> cache = _cache.Get <System.Collections.Concurrent.ConcurrentDictionary <DateTime, MonthRanking> >("~MonthRanking");
         if (cache != null && cache.ContainsKey(firstday))
         {
             cache.Remove(firstday);
         }
         ExpUtil expUtil = scope.ServiceProvider.GetService <ExpUtil>();
         expUtil.addRankExp(rankings, MonthlyReward, "月榜奖励");
     }
 }
Esempio n. 3
0
        private void DailyRewardTask()
        {
            using (var scope = _scopeFactory.CreateScope())
            {
                var db       = scope.ServiceProvider.GetRequiredService <BlogContext>();
                var rankings = db.Blogs.Where(b => DbFunctions.DiffMinutes(b.BlogDate, DateTime.Now) < 1440 && b.isApproved == true && !NoRankCategories.Contains(b.CategoryID))
                               .Join(db.BlogRatings.GroupBy(r => r.BlogID).Select(g => new { blogId = g.Key, rating = g.Sum(r => r.value) })
                                     , b => b.BlogID, r => r.blogId, (b, r) => new
                {
                    blog      = b,
                    Rating    = r.rating,
                    PostCount = db.Posts.Count(p => p.IdType == ItemType.Blog && p.ItemId == b.BlogID)
                })
                               .OrderByDescending(r => r.Rating)
                               .ThenByDescending(r => r.blog.BlogDate)
                               .Take(RankSize)
                               .ToArray()
                               .Select(rank => new HistoryRanking
                {
                    BlogID    = rank.blog.BlogID,
                    RankDate  = DateTime.Now.Date,
                    Rating    = rank.Rating,
                    Author    = rank.blog.Author,
                    BlogDate  = rank.blog.BlogDate,
                    BlogThumb = BlogHelper.firstImgPath(rank.blog, true),
                    BlogTitle = rank.blog.BlogTitle,
                    BlogVisit = rank.blog.BlogVisit,
                    PostCount = rank.PostCount,
                    RankType  = HistoryRanking.Type.RankDaily,
                });

                db.HistoryRankings.AddRange(rankings);
                db.SaveChanges();
                ExpUtil expUtil = scope.ServiceProvider.GetService <ExpUtil>();
                expUtil.addRankExp(rankings, DailyReward, "日榜奖励");
            }
        }