using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Chloe; using DB.Dto; using DB.Entity; using Tool; namespace DB.Service { public class AlarmLogService { public void Insert(string msg, string showTime) { using (var dbContext = DbFactory.GetContext) { var entity= dbContext.Query() .Where(x => x.Msg == msg) .Where(x => x.ShowTime == showTime) .OrderByDesc(x => x.ID).FirstOrDefault(); if (entity == null) { var count = dbContext.Query() .Where(x => x.Msg == msg).Count(); AlarmLog log = new AlarmLog { Msg = msg, ShowTime = showTime, ID = SnowflakeFactory.NewId, Count = count+1 }; dbContext.Insert(log); } } } public void Insert(Dictionary dic,int alarm) { using (var dbContext = DbFactory.GetContext) { foreach (KeyValuePair keyValuePair in dic) { string msg = keyValuePair.Key; var showTime = keyValuePair.Value.ToFullString(); var entity = dbContext.Query() .Where(x => x.Msg == msg) .Where(x=>x.Alarm == alarm) .Where(x => x.ShowTime == showTime) .OrderByDesc(x => x.ID).FirstOrDefault(); if (entity == null) { var count = dbContext.Query() .Where(x => x.Msg == msg && x.Alarm==alarm).Count(); AlarmLog log = new AlarmLog { Alarm = alarm, Msg = msg, ShowTime = showTime, ID = SnowflakeFactory.NewId, Count = count + 1 }; dbContext.Insert(log); } } } } public PagedList GetPagedList(int pageIndex, int pageSize, string key,int alarm) { PagingResult pagePagingResult; using (var dbContext = DbFactory.GetContext) { pagePagingResult = dbContext.Query() .WhereIf(alarm>0,x=>x.Alarm==alarm) .WhereIf(!string.IsNullOrEmpty(key), x => x.Msg.Contains(key)) .OrderByDesc(x => x.ID) .Paging(pageIndex, pageSize); } var ls = new List(pagePagingResult.DataList.Count); foreach (var alarmLog in pagePagingResult.DataList) { ls.Add(new AlarmLogView() { Alarm = alarmLog.Alarm==1?"警告":"提醒", ID = alarmLog.ID, Count = alarmLog.Count, Msg = alarmLog.Msg, ShowTime = alarmLog.ShowTime }); } PagedList paged = new PagedList(ls, pagePagingResult.Totals.ToInt(), pageIndex, pageSize); return paged; } } }