using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IStation.RedisCache;
namespace IStation.Service
{
///
/// 日监测记录
///
public partial class MonitorDayRecord
{
#region 最近一次记录
///
/// 获取最近一条记录
///
public Model.MonitorDayRecordPure GetLastRecord(long CorpID, long MonitorPointID, long SignalID)
{
var redisHelper = new MonitorRecordCacheHelper();
var record = redisHelper.GetLastDayRecord(CorpID, MonitorPointID, SignalID);
return record;
}
///
/// 获取最近一条记录
///
public List GetLastRecord(long CorpID, long MonitorPointID, IEnumerable SignalIds)
{
if (SignalIds == null || SignalIds.Count() < 1)
return default;
var redisHelper = new MonitorRecordCacheHelper();
var records = redisHelper.GetLastDayRecord(CorpID, MonitorPointID, SignalIds);
return records;
}
///
/// 获取最近一条记录
///
public List GetLastRecord(long CorpID, Dictionary dict)
{
var redisHelper = new MonitorRecordCacheHelper();
var records = redisHelper.GetLastDayRecord(CorpID, dict);
return records;
}
///
/// 获取最近一条记录
///
public List GetLastRecord(long CorpID, long MonitorPointID)
{
var redisHelper = new MonitorRecordCacheHelper();
var records = redisHelper.GetLastDayRecord(CorpID, MonitorPointID);
return records;
}
///
/// 获取最近一条记录
///
public List GetLastRecord(long CorpID, IEnumerable MonitorPointIds)
{
if (MonitorPointIds == null || MonitorPointIds.Count() < 1)
return default;
var redisHelper = new MonitorRecordCacheHelper();
var records = redisHelper.GetLastDayRecord(CorpID, MonitorPointIds);
return records;
}
///
/// 获取最近一条正常记录
///
public Model.MonitorDayRecordPure GetLastNormalRecord(long CorpID, long MonitorPointID, long SignalID)
{
var redisHelper = new MonitorRecordCacheHelper();
var record = redisHelper.GetLastNormalDayRecord(CorpID, MonitorPointID, SignalID);
return record;
}
///
/// 获取最近一条正常记录
///
public List GetLastNormalRecord(long CorpID, Dictionary dict)
{
if (dict == null || dict.Count < 1)
return default;
var redisHelper = new MonitorRecordCacheHelper();
var records = redisHelper.GetLastNormalDayRecord(CorpID, dict);
return records;
}
///
/// 获取最近一条正常记录
///
public List GetLastNormalRecord(long CorpID, long MonitorPointID)
{
var redisHelper = new MonitorRecordCacheHelper();
var records = redisHelper.GetLastNormalDayRecord(CorpID, MonitorPointID);
return records;
}
///
/// 获取最近一条正常记录
///
public List GetLastNormalRecord(long CorpID, IEnumerable MonitorPointIds)
{
if (MonitorPointIds == null || MonitorPointIds.Count() < 1)
return default;
var redisHelper = new MonitorRecordCacheHelper();
var records = redisHelper.GetLastNormalDayRecord(CorpID, MonitorPointIds);
return records;
}
#endregion
#region 历史记录
///
/// 通过 MonitorPointID 获取
///
public List GetByMonitorPointID(long CorpID, long MonitorPointID)
{
var dal=new DAL.MonitorDayRecord();
var entity_list = dal.GetByMonitorPointID(CorpID, MonitorPointID);
var model_list = Entity2Models(entity_list);
return model_list?.OrderBy(x => x.DataDay).ThenBy(x => x.DataTime).ToList();
}
///
/// 通过 MonitorPointID 获取某天的数据
///
public List GetByMonitorPointIDOfDay(long CorpID, long MonitorPointID, DateTime Day)
{
var dal = new DAL.MonitorDayRecord();
var entity_list = dal.GetByMonitorPointIDOfDay(CorpID, MonitorPointID, Day.Date);
var model_list = Entity2Models(entity_list);
return model_list?.OrderBy(x => x.DataDay).ThenBy(x => x.DataTime).ToList();
}
///
/// 通过 MonitorPointID 获取日期区间内的数据
///
public List GetByMonitorPointIDOfDayRange(long CorpID, long MonitorPointID, DateTime StartDay, DateTime EndDay)
{
if (StartDay.Date > EndDay.Date)
return default;
var dal = new DAL.MonitorDayRecord();
var entity_list = dal.GetByMonitorPointIDOfDayRange(CorpID, MonitorPointID, StartDay.Date, EndDay.Date);
var model_list = Entity2Models(entity_list);
return model_list?.OrderBy(x => x.DataDay).ThenBy(x => x.DataTime).ToList();
}
///
/// 通过 SignalID 获取某天的数据
///
public Model.MonitorDayRecord GetBySignalIDOfDay(long CorpID, long MonitorPointID, long SignalID, DateTime Day)
{
var dal = new DAL.MonitorDayRecord();
var entity = dal.GetBySignalIDOfDay(CorpID, MonitorPointID,SignalID, Day.Date);
var model = Entity2Model(entity);
return model;
}
///
/// 通过 SignalID 获取日期区间内的数据
///
public List GetBySignalIDOfDayRange(long CorpID, long MonitorPointID, long SignalID, DateTime StartDay, DateTime EndDay)
{
if (StartDay.Date > EndDay.Date)
return default;
var dal = new DAL.MonitorDayRecord();
var entityList = dal.GetBySignalIDOfDayRange(CorpID, MonitorPointID, SignalID, StartDay.Date, EndDay.Date);
var modelList = Entity2Models(entityList);
return modelList;
}
///
/// 通过 SignalID 获取日期区间内的基础内容数据
///
public List GetBasicContentBySignalIDOfDayRange(long CorpID, long MonitorPointID,long SignalID, DateTime StartDay, DateTime EndDay)
{
if (StartDay.Date > EndDay.Date)
return default;
var dal = new DAL.MonitorDayRecord();
var entity_list = dal.GetBySignalIDOfDayRange(CorpID, MonitorPointID,SignalID ,StartDay.Date, EndDay.Date);
var model_list = Entity2BasicContents(entity_list);
return model_list;
}
#endregion
#region Insert
///
/// 插入一条
///
public bool Insert(Model.MonitorDayRecordPure model)
{
if (model == null)
return default;
var dal = new DAL.MonitorDayRecord();
var entity = Model2Entity(new Model.MonitorDayRecord(model));
var id = dal.Insert(entity);
return id > 0;
}
///
/// 插入多条
///
public bool Inserts(List list)
{
if (list == null || list.Count() < 1)
return default;
var dal = new DAL.MonitorDayRecord();
var entity_list = Model2Entities(list.Select(x => new Model.MonitorDayRecord(x)).ToList());
var bol = dal.Inserts(entity_list);
return bol;
}
///
/// 插入最近一条记录
///
public bool InsertLastRecord(Model.MonitorDayRecordPure model)
{
if (model == null)
return default;
var dal = new DAL.MonitorDayRecord();
var entity = Model2Entity(new Model.MonitorDayRecord(model));
var bol = dal.Insert(entity) > 0;
if (bol)
{
var redisHelper = new MonitorRecordCacheHelper();
redisHelper.SetLastDayRecord(model);
}
return bol;
}
///
/// 插入最近多条记录
///
public bool InsertsLastRecord(List list)
{
if (list == null || list.Count() < 1)
return default;
var dal = new DAL.MonitorDayRecord();
var entity_list = Model2Entities(list.Select(x => new Model.MonitorDayRecord(x)).ToList());
var bol = dal.Inserts(entity_list);
if (bol)
{
var redisHelper = new MonitorRecordCacheHelper();
redisHelper.SetLastDayRecord(list);
}
return bol;
}
///
/// 补录一条
///
public bool InsertSupplement(Model.MonitorDayRecordPure model)
{
if (model == null)
return default;
var dal = new DAL.MonitorDayRecord();
var entity = Model2Entity(new Model.MonitorDayRecord(model));
var id = dal.Insert(entity);
return id>0;
}
///
/// 补录多条
///
public bool InsertsSupplement(List list)
{
if (list == null || list.Count() < 1)
return default;
var dal = new DAL.MonitorDayRecord();
var entity_list = Model2Entities(list.Select(x=>new Model.MonitorDayRecord(x)).ToList());
var bol = dal.Inserts(entity_list);
return bol;
}
///
/// 重录一条
///
public bool InsertAgain(Model.MonitorDayRecordPure model)
{
if (model == null)
return false;
var dal = new DAL.MonitorDayRecord();
var result = dal.DeleteBySignalIDOfDay(model.CorpID, model.MonitorPointID, model.SignalID, model.DataDay);
if (!result)
{
LogHelper.Info($"重录一条监测日记录中,CorpID:{model.CorpID},MonitorPointID:{model.MonitorPointID},SignalID:{model.SignalID},DataDay:{model.DataDay:yyyy-MM-dd},未检索到已存在记录!");
}
var entity = Model2Entity(new Model.MonitorDayRecord(model));
var id = dal.Insert(entity);
return id > 0;
}
///
/// 重录多条
///
public bool InsertsAgain(List list)
{
if (list == null || list.Count < 1)
return false;
var corpIds = list.Select(x => x.CorpID).Distinct().ToList();
if (corpIds.Count > 1)
{
return default;
}
var groupList = list.GroupBy(x => new { x.CorpID, x.MonitorPointID, x.SignalID }).ToList();
var dal = new DAL.MonitorDayRecord();
foreach (var group in groupList)
{
var startDay = group.Min(x => x.DataDay);
var endDay = group.Max(x => x.DataDay);
var result = dal.DeleteBySignalIDOfDayRange(group.Key.CorpID, group.Key.MonitorPointID, group.Key.SignalID, startDay, endDay);
if (!result)
{
LogHelper.Info($"重录多条监测日记录中,CorpID:{group.Key.CorpID},MonitorPointID:{group.Key.MonitorPointID},SignalID:{group.Key.SignalID},StartDay:{startDay:yyyy-MM-dd}, EndDay:{endDay:yyyy-MM-dd},未检索到已存在记录!");
}
}
var entityList = Model2Entities(list.Select(x => new Model.MonitorDayRecord(x)).ToList());
var bol = dal.Inserts(entityList);
return bol;
}
#endregion
#region Update
///
/// 更新一条
///
public bool Update(Model.MonitorDayRecord model)
{
if (model == null)
return default;
var dal = new DAL.MonitorDayRecord();
var entity = Model2Entity(model);
var bol = dal.Update(entity);
return bol;
}
///
/// 批量更新
///
public bool Updates(List list)
{
if (list == null || list.Count < 1)
return default;
var dal = new DAL.MonitorDayRecord();
var entityList = Model2Entities(list);
var bol = dal.Updates(entityList);
return bol;
}
#endregion
}
}