Ejemplo n.º 1
0
        /// <summary>
        /// 添加或更新网站访问量
        /// </summary>
        /// <param name="day">日期</param>
        public async Task <bool> AddOrUpdate()
        {
            var day     = DateTime.Now;
            var date    = day.Date;
            var endDate = date.AddDays(1);
            var number  = await _context.PageViews.CountAsync(x => x.CreateTime > date && x.CreateTime < endDate);

            if (await _context.SiteViews.AnyAsync(x => x.Day == date))
            {
                var view = await _context.SiteViews.FirstOrDefaultAsync(x => x.Day == date);

                if (view != null)
                {
                    view.Number = number;
                }
            }
            else
            {
                var view = new SiteViewEntity
                {
                    Day    = date,
                    Number = number
                };
                view.InitEntity();
                _context.SiteViews.Add(view);
            }
            await _context.SaveChangesAsync();

            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 添加或更新网站访问量
        /// </summary>
        /// <param name="day">日期</param>
        public async Task <bool> AddOrUpdate(DateTime day)
        {
            using (var scope = _dbContextScopeFactory.Create())
            {
                var db      = scope.DbContexts.Get <JuCheapContext>();
                var date    = day.Date;
                var endDate = date.AddDays(1);
                var number  = await db.PageViews.CountAsync(x => x.CreateDateTime > date && x.CreateDateTime < endDate);

                if (await db.SiteViews.AnyAsync(x => x.Day == date))
                {
                    var view = await db.SiteViews.FirstOrDefaultAsync(x => x.Day == date);

                    if (view != null)
                    {
                        view.Number = number;
                    }
                }
                else
                {
                    var view = new SiteViewEntity
                    {
                        Day    = date,
                        Number = number
                    };
                    view.Create();
                    db.SiteViews.Add(view);
                }
                await scope.SaveChangesAsync();

                return(true);
            }
        }