namespace Yw.DAL
{
///
/// 运行实时记录
///
public class RunRealRecord
{
///
///
///
public ConnectionConfig ConnectionConfig
{
get { return ConfigHelper.RecordConnectionConfig; }
}
///
/// 通过 Day 获取
///
public List GetOfDay(DateTime Day)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.DataTime >= Day.Date && x.DataTime < Day.Date.AddDays(1))
.OrderBy(x => x.DataTime).ToList();
}
}
///
/// 通过 ObjectType 和 ObjectID 获取
///
public List GetByObjectTypeAndObjectID(string ObjectType, long ObjectID)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.ObjectType == ObjectType && x.ObjectID == ObjectID)
.OrderBy(x => x.DataTime).ToList();
}
}
///
/// 通过 ObjectType 和 ObjectID 获取某日的数据
///
public List GetByObjectTypeAndObjectIDOfDay(string ObjectType, long ObjectID, DateTime Day)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.DataTime >= Day.Date && x.DataTime < Day.Date.AddDays(1))
.Where(x => x.ObjectType == ObjectType && x.ObjectID == ObjectID)
.OrderBy(x => x.DataTime).ToList();
}
}
///
/// 通过 ObjectType 和 ObjectID 获取某日的运行数据
///
public List GetRunByObjectTypeAndObjectIDOfDay(string ObjectType, long ObjectID, DateTime Day)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.DataTime >= Day.Date && x.DataTime < Day.Date.AddDays(1))
.Where(x => x.ObjectType == ObjectType && x.ObjectID == ObjectID)
.Where(x => x.RSa == RunStatus.Run)
.OrderBy(x => x.DataTime).ToList();
}
}
///
/// 通过 ObjectType 和 ObjectID 获取日期区间内的数据
///
public List GetByObjectTypeAndObjectIDOfDayRange(string ObjectType, long ObjectID, DateTime StartDay, DateTime EndDay)
{
if (StartDay.Date > EndDay.Date)
{
return default;
}
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.DataTime >= StartDay.Date && x.DataTime < EndDay.Date.AddDays(1))
.Where(x => x.ObjectType == ObjectType && x.ObjectID == ObjectID)
.OrderBy(x => x.DataTime)
.ToList();
}
}
///
/// 通过 ObjectType 和 ObjectID 获取日期区间内的运行数据
///
public List GetRunByObjectTypeAndObjectIDOfDayRange(string ObjectType, long ObjectID, DateTime StartDay, DateTime EndDay)
{
if (StartDay.Date > EndDay.Date)
{
return default;
}
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.DataTime >= StartDay.Date && x.DataTime < EndDay.Date.AddDays(1))
.Where(x => x.ObjectType == ObjectType && x.ObjectID == ObjectID)
.Where(x => x.RSa == RunStatus.Run)
.OrderBy(x => x.DataTime)
.ToList();
}
}
///
/// 通过 ObjectType 和 ObjectID 获取时间区间内的数据
///
public List GetByObjectTypeAndObjectIDOfTimeRange(string ObjectType, long ObjectID, DateTime StartTime, DateTime EndTime)
{
if (StartTime > EndTime)
{
return default;
}
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.DataTime > StartTime.AddSeconds(-1) && x.DataTime < EndTime.AddSeconds(1))
.Where(x => x.ObjectType == ObjectType && x.ObjectID == ObjectID)
.OrderBy(x => x.DataTime)
.ToList();
}
}
///
/// 通过 ObjectType 和 ObjectID 获取时间区间内的运行数据
///
public List GetRunByObjectTypeAndObjectIDOfTimeRange(string ObjectType, long ObjectID, DateTime StartTime, DateTime EndTime)
{
if (StartTime > EndTime)
{
return default;
}
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.DataTime > StartTime.AddSeconds(-1) && x.DataTime < EndTime.AddSeconds(1))
.Where(x => x.ObjectType == ObjectType && x.ObjectID == ObjectID)
.Where(x => x.RSa == RunStatus.Run)
.OrderBy(x => x.DataTime)
.ToList();
}
}
#region InsertOrUpdate
///
/// 插入
///
public bool InsertOrUpdate(Entity.RunRealRecord entity)
{
if (entity == null)
return false;
using (var db = new SqlSugarClient(ConnectionConfig))
{
var sql = CreateSql();
var paras = CreateParameters(entity);
return db.Ado.ExecuteCommand(sql, paras) > 0;
}
}
///
/// 批量插入
///
public bool InsertOrUpdate(List list)
{
if (list == null || list.Count < 1)
return false;
using (var db = new SqlSugarClient(ConnectionConfig))
{
try
{
db.Ado.BeginTran();
var sql = CreateSql();
foreach (var item in list)
{
var paras = CreateParameters(item);
var bol = db.Ado.ExecuteCommand(sql, paras) > 0;
if (!bol)
{
db.Ado.RollbackTran();
return false;
}
}
db.Ado.CommitTran();
return true;
}
catch
{
db.Ado.RollbackTran();
throw;
}
}
}
//创建sql
private string CreateSql()
{
var sb = new StringBuilder();
sb.AppendLine("INSERT INTO run_real_record");
sb.AppendLine($" VALUES(@objecttype,@objectid,@datatime,@rsa,@continueruntime,@totalruntime,@boottimes,@analystatus,@analyinfo)");
sb.AppendLine("ON CONFLICT(objecttype, objectid, datatime) DO UPDATE");
sb.AppendLine("SET rsa = excluded.rsa,");
sb.AppendLine("continueruntime = excluded.continueruntime,");
sb.AppendLine("totalruntime = excluded.totalruntime,");
sb.AppendLine("boottimes = excluded.boottimes,");
sb.AppendLine("analystatus = excluded.analystatus,");
sb.AppendLine("analyinfo = excluded.analyinfo;");
return sb.ToString();
}
//创建参数
private List CreateParameters(Entity.RunRealRecord entity)
{
var list = new List();
list.Add(new SugarParameter("@objecttype", entity.ObjectType));
list.Add(new SugarParameter("@objectid", entity.ObjectID));
list.Add(new SugarParameter("@datatime", entity.DataTime));
list.Add(new SugarParameter("@rsa", entity.RSa));
list.Add(new SugarParameter("@continueruntime", entity.ContinueRunTime));
list.Add(new SugarParameter("@totalruntime", entity.TotalRunTime));
list.Add(new SugarParameter("@boottimes", entity.BootTimes));
list.Add(new SugarParameter("@analystatus", entity.AnalyStatus));
list.Add(new SugarParameter("@analyinfo", entity.AnalyInfo));
return list;
}
#endregion
}
}