You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CaiQie/DB/Service/AlarmLogService.cs

123 lines
3.6 KiB
C#

3 weeks ago
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<AlarmLog>()
.Where(x => x.Msg == msg)
.Where(x => x.ShowTime == showTime)
.OrderByDesc(x => x.ID).FirstOrDefault();
if (entity == null)
{
var count = dbContext.Query<AlarmLog>()
.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<string, DateTime> dic,int alarm)
{
using (var dbContext = DbFactory.GetContext)
{
foreach (KeyValuePair<string, DateTime> keyValuePair in dic)
{
string msg = keyValuePair.Key;
var showTime = keyValuePair.Value.ToFullString();
var entity = dbContext.Query<AlarmLog>()
.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<AlarmLog>()
.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<AlarmLogView> GetPagedList(int pageIndex, int pageSize, string key,int alarm)
{
PagingResult<AlarmLog> pagePagingResult;
using (var dbContext = DbFactory.GetContext)
{
pagePagingResult = dbContext.Query<AlarmLog>()
.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<AlarmLogView>(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<AlarmLogView> paged = new PagedList<AlarmLogView>(ls, pagePagingResult.Totals.ToInt(), pageIndex, pageSize);
return paged;
}
}
}