using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
namespace IStation.DAL
{
///
///
///
public class MonitorRealRecord : CorpDAL
{
///
///
///
public override ConnectionConfig ConnectionConfig
{
get { return ConfigHelper.MonitorRecordConnectionConfig; }
}
#region 通过 MonitorPointID 获取
///
/// 通过 MonitorPointID 获取某天的全部数据
///
public List GetByMonitorPointIDOfDay(long CorpID, long MonitorPointID, DateTime Day)
{
if (CorpID < 1)
return default;
if (MonitorPointID < 1)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.DataTime >= Day.Date && x.DataTime < Day.Date.AddDays(1))
.OrderBy(x => x.DataTime, OrderByType.Asc).ToList();
}
}
///
/// 通过 MonitorPointID 获取某天固定数量的数据
///
public List GetFixedByMonitorPointIDOfDay(long CorpID, long MonitorPointID, DateTime Day, int Fixed)
{
if (CorpID < 1)
return default;
if (MonitorPointID < 1)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
var list = db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.DataTime >= Day.Date && x.DataTime < Day.Date.AddDays(1))
.OrderBy(x => x.DataTime, OrderByType.Asc).ToList();
var group_list = list.GroupBy(x => x.MonitorPointID).ToList();
var fixed_list = group_list.SelectMany(x => GetFixedList(x.ToList(), Fixed)).OrderBy(x => x.DataTime).ToList();
return fixed_list;
}
}
///
/// 通过 MonitorPointID 获取日期区间内的数据
///
public List GetByMonitorPointIDOfDayRange(long CorpID, long MonitorPointID, DateTime StartDay, DateTime EndDay)
{
if (StartDay.Date > EndDay.Date)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.DataTime >= StartDay.Date && x.DataTime < EndDay.Date.AddDays(1))
.OrderBy(x => x.DataTime, OrderByType.Asc).ToList();
}
}
///
/// 通过 MonitorPointID 获取日期区间内固定数量的数据
///
public List GetFixedByMonitorPointIDOfDayRange(long CorpID, long MonitorPointID, DateTime StartDay, DateTime EndDay, int Fixed)
{
if (StartDay.Date > EndDay.Date)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
var list = db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.DataTime >= StartDay.Date && x.DataTime < EndDay.Date.AddDays(1))
.OrderBy(x => x.DataTime, OrderByType.Asc).ToList();
var group_list = list.GroupBy(x => x.MonitorPointID).ToList();
var fixed_list = group_list.SelectMany(x => GetFixedList(x.ToList(), Fixed)).OrderBy(x => x.DataTime).ToList();
return fixed_list;
}
}
///
/// 通过 MonitorPointID 获取时间区间内的数据
///
public List GetByMonitorPointIDOfTimeRange(long CorpID, long MonitorPointID, DateTime StartTime, DateTime EndTime)
{
if (StartTime > EndTime)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.DataTime >= StartTime && x.DataTime <= EndTime)
.OrderBy(x => x.DataTime, OrderByType.Asc).ToList();
}
}
///
/// 通过 MonitorPointID 获取时间区间内固定数量的数据
///
public List GetFixedByMonitorPointIDOfTimeRange(long CorpID, long MonitorPointID, DateTime StartTime, DateTime EndTime, int Fixed)
{
if (StartTime > EndTime)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
var list = db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.DataTime >= StartTime && x.DataTime <= EndTime)
.OrderBy(x => x.DataTime, OrderByType.Asc).ToList();
var group_list = list.GroupBy(x => x.MonitorPointID).ToList();
var fixed_list = group_list.SelectMany(x => GetFixedList(x.ToList(), Fixed)).OrderBy(x => x.DataTime).ToList();
return fixed_list;
}
}
#endregion
#region 通过 SignalID 获取
///
/// 通过 SignalID 获取某一时间的数据
///
public Entity.MonitorRealRecord GetBySignalIDOfDataTime(long CorpID, long MonitorPointID, long SignalID, DateTime DataTime)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x=>x.DataTime> DataTime.AddDays(-1)&& x.DataTime<=DataTime)
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.SignalID == SignalID)
.OrderBy(x => x.DataTime, OrderByType.Desc).First();
}
}
///
/// 通过 SignalID 获取某天的全部数据
///
public List GetBySignalIDOfDay(long CorpID, long MonitorPointID, long SignalID, DateTime Day)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.SignalID == SignalID && x.DataTime >= Day.Date && x.DataTime < Day.AddDays(1).Date)
.OrderBy(x => x.DataTime, OrderByType.Asc).ToList();
}
}
///
/// 通过 SignalIds 获取某天的全部数据
///
public List GetBySignalIdsOfDay(long CorpID, long MonitorPointID, IEnumerable SignalIds, DateTime Day)
{
if (SignalIds == null || SignalIds.Count() < 1)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x=>x.DataTime>=Day.Date&&x.DataTime x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && SignalIds.Contains(x.SignalID))
.OrderBy(x => x.DataTime, OrderByType.Asc).ToList();
}
}
///
/// 通过 SignalID 获取某天固定数量的数据
///
public List GetFixedBySignalIDOfDay(long CorpID, long MonitorPointID, long SignalID, DateTime Day, int Fixed)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
var list = db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.SignalID == SignalID && x.DataTime >= Day.Date && x.DataTime < Day.AddDays(1).Date)
.OrderBy(x => x.DataTime, OrderByType.Asc).ToList();
return GetFixedList(list, Fixed);
}
}
///
/// 通过 SignalID 获取某天的第一条数据
///
public Entity.MonitorRealRecord GetFirstBySignalIDOfDay(long CorpID, long MonitorPointID, long SignalID, DateTime Day)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.SignalID == SignalID && x.DataTime >= Day.Date && x.DataTime < Day.AddDays(1).Date)
.OrderBy(x => x.DataTime, OrderByType.Asc).First();
}
}
///
/// 通过 SignalID 获取某天的最后一条数据
///
public Entity.MonitorRealRecord GetLastBySignalIDOfDay(long CorpID, long MonitorPointID, long SignalID, DateTime Day)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.SignalID == SignalID && x.DataTime >= Day.Date && x.DataTime < Day.AddDays(1).Date)
.OrderBy(x => x.DataTime, OrderByType.Desc).First();
}
}
///
/// 通过 SignalID 获取日期区间内的数据
///
public List GetBySignalIDOfDayRange(long CorpID, long MonitorPointID, long SignalID, DateTime StartDay, DateTime EndDay)
{
if (StartDay.Date > EndDay.Date)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.SignalID == SignalID && x.DataTime >= StartDay.Date && x.DataTime < EndDay.AddDays(1).Date)
.OrderBy(x => x.DataTime, OrderByType.Asc).ToList();
}
}
///
/// 通过 SignalID 获取日期区间内固定数量的数据
///
public List GetFixedBySignalIDOfDayRange(long CorpID, long MonitorPointID, long SignalID, DateTime StartDay, DateTime EndDay, int Fixed)
{
if (StartDay.Date > EndDay.Date)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
var list = db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.SignalID == SignalID && x.DataTime >= StartDay.Date && x.DataTime < EndDay.AddDays(1).Date)
.OrderBy(x => x.DataTime, OrderByType.Asc).ToList();
return GetFixedList(list, Fixed);
}
}
///
/// 通过 SignalID 获取日期区间内的第一条数据
///
public Entity.MonitorRealRecord GetFirstBySignalIDOfDayRange(long CorpID, long MonitorPointID, long SignalID, DateTime StartDay, DateTime EndDay)
{
if (StartDay.Date > EndDay.Date)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.SignalID == SignalID && x.DataTime >= StartDay.Date && x.DataTime < EndDay.AddDays(1).Date)
.OrderBy(x => x.DataTime, OrderByType.Asc).First();
}
}
///
/// 通过 SignalID 获取日期区间内的最后一条数据
///
public Entity.MonitorRealRecord GetLastBySignalIDOfDayRange(long CorpID, long MonitorPointID, long SignalID, DateTime StartDay, DateTime EndDay)
{
if (StartDay.Date > EndDay.Date)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.SignalID == SignalID && x.DataTime >= StartDay.Date && x.DataTime < EndDay.AddDays(1).Date)
.OrderBy(x => x.DataTime, OrderByType.Desc).First();
}
}
///
/// 通过 SignalID 获取时间区间内的数据
///
public List GetBySignalIDOfTimeRange(long CorpID, long MonitorPointID, long SignalID, DateTime StartTime, DateTime EndTime)
{
if (StartTime > EndTime)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.SignalID == SignalID && x.DataTime >= StartTime && x.DataTime <= EndTime)
.OrderBy(x => x.DataTime, OrderByType.Asc).ToList();
}
}
///
/// 通过 SignalID 获取时间区间内固定数量的数据
///
public List GetFixedBySignalIDOfTimeRange(long CorpID, long MonitorPointID, long SignalID, int Fixed, DateTime StartTime, DateTime EndTime)
{
if (StartTime > EndTime)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
var list = db.Queryable()
.Where(x=>x.DataTime>=StartTime&&x.DataTime<=EndTime)
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.SignalID == SignalID)
.OrderBy(x => x.DataTime, OrderByType.Asc).ToList();
return GetFixedList(list, Fixed);
}
}
///
/// 通过 SignalID 获取时间区间内的第一条数据
///
public Entity.MonitorRealRecord GetFirstBySignalIDOfTimeRange(long CorpID, long MonitorPointID, long SignalID, DateTime StartTime, DateTime EndTime)
{
if (StartTime > EndTime)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.SignalID == SignalID && x.DataTime >= StartTime && x.DataTime <= EndTime)
.OrderBy(x => x.DataTime, OrderByType.Asc).First();
}
}
///
/// 通过 SignalID 获取时间区间内的最后一条数据
///
public Entity.MonitorRealRecord GetLastBySignalIDOfTimeRange(long CorpID, long MonitorPointID, long SignalID, DateTime StartTime, DateTime EndTime)
{
if (StartTime > EndTime)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.SignalID == SignalID && x.DataTime >= StartTime && x.DataTime <= EndTime)
.OrderBy(x => x.DataTime, OrderByType.Desc).First();
}
}
#endregion
#region Method
//返回固定数量的列表(包含首位和末位、均匀分布)
private static List GetFixedList(List s, int n)
{
if (s == null || s.Count < 1)
return s;
if (n <= 0)
return null;
if (n == 1)
return new List() { s.First() };
if (s.Count <= n)
return s;
var r = new List();
float step = (s.Count - 1f) / (n - 1f);
for (int i = 0; i < n; i++)
{
r.Add(s[(int)Math.Round(step * i)]);
}
return r;
}
#endregion
#region Update
///
/// 更新
///
public override bool Update(Entity.MonitorRealRecord entity)
{
if (entity == null)
return false;
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
{
return db.Updateable(entity).IgnoreColumns(x => x.DataTime).Where(x=>x.ID==entity.ID).ExecuteCommand() > 0;
}
}
///
/// 批量更新
///
public override bool Updates(List list)
{
if (list == null || list.Count < 1)
return false;
var corpIds = list.Select(x => x.CorpID).Distinct().ToList();
if (corpIds.Count() != 1 || corpIds[0] < 1)
return false;
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
{
return db.Updateable(list).IgnoreColumns(x => x.DataTime).WhereColumns(it => new { it.ID }).ExecuteCommand() > 0;
}
}
#endregion
#region Delete
///
/// 通过 SignalID 删除数据时间下的数据
///
public bool DeleteBySignalIDOfDataTime(long CorpID,long MonitorPointID,long SignalID,DateTime DataTime)
{
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
{
return db.Deleteable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID ==MonitorPointID&&x.SignalID==SignalID)
.Where(x=>x.DataTime>DataTime.AddSeconds(-1)&&x.DataTime 0;
}
}
///
/// 通过 SignalID 删除时间区间下的数据
///
public bool DeleteBySignalIDOfTimeRange(long CorpID, long MonitorPointID, long SignalID, DateTime StartTime, DateTime EndTime)
{
if (EndTime < StartTime)
return false;
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
{
return db.Deleteable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID && x.SignalID == SignalID)
.Where(x => x.DataTime > StartTime.AddSeconds(-1) && x.DataTime < EndTime.AddSeconds(1))
.ExecuteCommand() > 0;
}
}
#endregion
}
}