using Bootstrap.DataAccess; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; namespace Bootstrap.Admin.Controllers.Api { /// /// /// [Route("api/[controller]")] public class NotificationsController : Controller { /// /// /// /// [HttpGet] public Notifications Get() { var ret = new Notifications(); if (!User.IsInRole("Administrators")) return ret; // New Users var user = UserHelper.RetrieveNewUsers(); ret.Users = user.Take(6).ToList(); ret.Users.AsParallel().ForAll(n => { var ts = DateTime.Now - n.RegisterTime; if (ts.TotalMinutes < 5) n.Period = "刚刚"; else if (ts.Days > 0) n.Period = string.Format("{0}天", ts.Days); else if (ts.Hours > 0) n.Period = string.Format("{0}小时", ts.Hours); else if (ts.Minutes > 0) n.Period = string.Format("{0}分钟", ts.Minutes); }); ret.NewUsersCount = user.Count(); // Tasks var task = TaskHelper.RetrieveTasks(); ret.Tasks = task.Take(6).ToList(); ret.TasksCount = task.Count(); //Message var message = MessageHelper.RetrieveMessagesHeader(User.Identity.Name); ret.Messages = message.Take(6).ToList(); ret.Messages.AsParallel().ForAll(m => m.FromIcon = Url.Content(m.FromIcon)); ret.MessagesCount = message.Count(); //Apps var apps = ExceptionHelper.RetrieveExceptions().Where(n => n.ExceptionType != "Longbow.Data.DBAccessException"); ret.Apps = apps.Take(6).ToList(); ret.Apps.AsParallel().ForAll(n => { n.ExceptionType = n.ExceptionType.Split('.').Last(); var ts = DateTime.Now - n.LogTime; if (ts.TotalMinutes < 5) n.Period = "刚刚"; else if (ts.Days > 0) n.Period = string.Format("{0}天", ts.Days); else if (ts.Hours > 0) n.Period = string.Format("{0}小时", ts.Hours); else if (ts.Minutes > 0) n.Period = string.Format("{0}分钟", ts.Minutes); }); ret.AppExceptionsCount = apps.Count(); //Dbs var dbs = ExceptionHelper.RetrieveExceptions().Where(n => n.ExceptionType == "Longbow.Data.DBAccessException"); ret.Dbs = dbs.Take(6).ToList(); ret.Dbs.AsParallel().ForAll(n => { var ts = DateTime.Now - n.LogTime; if (ts.TotalMinutes < 5) n.Period = "刚刚"; else if (ts.Days > 0) n.Period = string.Format("{0}天", ts.Days); else if (ts.Hours > 0) n.Period = string.Format("{0}小时", ts.Hours); else if (ts.Minutes > 0) n.Period = string.Format("{0}分钟", ts.Minutes); }); ret.DbExceptionsCount = dbs.Count(); return ret; } /// /// /// /// /// [HttpGet("{id}")] public object Get(string id) { var ret = new object(); if (id == "newusers") ret = UserHelper.RetrieveNewUsers().ToList(); return ret; } public class Notifications { public Notifications() { Users = new List(); Apps = new List(); Dbs = new List(); Tasks = new List(); Messages = new List(); } /// /// /// public List Users { get; set; } /// /// /// public List Apps { get; set; } /// /// /// public List Dbs { get; set; } /// /// /// public List Tasks { get; set; } /// /// /// public List Messages { get; set; } /// /// 获得/设置 消息数量 /// public int MessagesCount { get; set; } /// /// 获得/设置 新用户数量 /// public int NewUsersCount { get; set; } /// /// 获取/设置 任务数量 /// public int TasksCount { get; set; } /// /// 获取/设置 应用程序错误数量 /// public int AppExceptionsCount { get; set; } /// /// 获取/设置 数据库错误数量 /// public int DbExceptionsCount { get; set; } } } }