/// <summary> /// Перерасчитывает статистику для пользователя /// </summary> /// <param name="userId"></param> public static void ComputeUserStatistics(Guid userId) { BCT.Execute(d => { var bulletins = d.BulletinDb.Bulletins.Where(q => q.UserId == userId).ToArray(); foreach (var bulletin in bulletins) { var bulletinViews = d.BulletinDb.BulletinInstances.Count(q => q.BulletinId == bulletin.Id); bulletin.Views = bulletinViews; bulletin.StateEnum = bulletin.StateEnum; } d.SaveChanges(); var accesses = d.BulletinDb.Accesses.Where(q => q.UserId == userId && q.StateEnum != FessooFramework.Objects.Data.DefaultState.Disable).ToArray(); var totalViews = accesses.Sum(q => q.Views); var totalMessages = accesses.Sum(q => q.Messages); var totalCalls = accesses.Sum(q => q.Calls); var user = d.BulletinDb.UserStatistics.FirstOrDefault(q => q.UserId == userId); if (user == null) { user = new BulletinHub.Models.UserStatistics(); user.StateEnum = FessooFramework.Objects.Data.DefaultState.Created; } user.TotalViews = totalViews; user.TotalMessages = totalMessages; user.TotalCalls = totalCalls; d.SaveChanges(); }); }
/// <summary> /// Перерасчитывает статистику для пользователя /// </summary> /// <param name="userId"></param> public static bool ComputeUserStatistics(string userLogin) { var result = false; BCT.Execute(d => { var user = d.MainDb.UserAccesses.FirstOrDefault(q => q.Login == userLogin); if (user == null) { return; } var userId = user.Id; var bulletins = d.BulletinDb.Bulletins.Where(q => q.UserId == userId).ToArray(); var bulletinIds = bulletins.Select(q => q.Id).ToArray(); foreach (var bulletin in bulletins) { var bulletinViews = d.BulletinDb.BulletinInstances.Where(q => q.BulletinId == bulletin.Id).ToArray().Sum(q => q.Views); bulletin.Views = bulletinViews; bulletin.StateEnum = bulletin.StateEnum; } d.SaveChanges(); var accesses = d.BulletinDb.Accesses.Where(q => q.UserId == userId && q.State != (int)FessooFramework.Objects.Data.DefaultState.Created).ToArray(); var totalViews = accesses.Sum(q => q.Views); var totalMessages = accesses.Sum(q => q.Messages); var totalCalls = accesses.Sum(q => q.Calls); var totalBulletins = bulletins.Count(); var totalInstances = d.BulletinDb.BulletinInstances.Count(q => q.Url != null && q.ActivationDate != null && bulletinIds.Any(qq => qq == q.BulletinId)); var userStat = d.BulletinDb.UserStatistics.FirstOrDefault(q => q.UserId == userId); if (userStat == null) { userStat = new BulletinHub.Models.UserStatistics(); userStat.UserId = userId; } userStat.TotalViews = totalViews; userStat.TotalMessages = totalMessages; userStat.TotalCalls = totalCalls; userStat.TotalProducts = totalBulletins; userStat.TotalInstances = totalInstances; userStat.StateEnum = FessooFramework.Objects.Data.DefaultState.Enable; d.SaveChanges(); }); return(result); }