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 MonitorMonthRecord
{
#region 最近一条记录
///
/// 获取最近一条记录
///
public Model.MonitorMonthRecordPure GetLastRecord(long CorpID, long MonitorPointID, long SignalID)
{
var redisHelper = new MonitorRecordCacheHelper();
var record = redisHelper.GetLastMonthRecord(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.GetLastMonthRecord(CorpID, MonitorPointID, SignalIds);
return records;
}
///
/// 获取最近一条记录
///
public List GetLastRecord(long CorpID, Dictionary dict)
{
var redisHelper = new MonitorRecordCacheHelper();
var records = redisHelper.GetLastMonthRecord(CorpID, dict);
return records;
}
///
/// 获取最近一条记录
///
public List GetLastRecord(long CorpID, long MonitorPointID)
{
var redisHelper = new MonitorRecordCacheHelper();
var records = redisHelper.GetLastMonthRecord(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.GetLastMonthRecord(CorpID, MonitorPointIds);
return records;
}
///
/// 获取最近一条正常记录
///
public Model.MonitorMonthRecordPure GetLastNormalRecord(long CorpID, long MonitorPointID, long SignalID)
{
var redisHelper = new MonitorRecordCacheHelper();
var record = redisHelper.GetLastNormalMonthRecord(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.GetLastNormalMonthRecord(CorpID, dict);
return records;
}
///
/// 获取最近一条正常记录
///
public List GetLastNormalRecord(long CorpID, long MonitorPointID)
{
var redisHelper = new MonitorRecordCacheHelper();
var records = redisHelper.GetLastNormalMonthRecord(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.GetLastNormalMonthRecord(CorpID, MonitorPointIds);
return records;
}
#endregion
#region 历史记录
///
/// 通过 MonitorPointID 获取
///
public List GetByMonitorPointID(long CorpID, long MonitorPointID)
{
var dal = new DAL.MonitorMonthRecord();
var entity_list = dal.GetByMonitorPointID(CorpID,MonitorPointID);
var model_list = Entity2Models(entity_list);
return model_list?.OrderBy(x => x.DataYear).ThenBy(x => x.DataMonth).ThenBy(x => x.DataTime).ToList();
}
///
/// 通过 MonitorPointID 获取某年的数据
///
public List GetByMonitorPointIDOfYear(long CorpID, long MonitorPointID, int Year)
{
var dal = new DAL.MonitorMonthRecord();
var entity_list = dal.GetByMonitorPointIDOfYear(CorpID, MonitorPointID, Year);
var model_list = Entity2Models(entity_list);
return model_list?.OrderBy(x=>x.DataYear).ThenBy(x=>x.DataMonth).ThenBy(x=>x.DataTime).ToList();
}
///
/// 通过 MonitorPointID 获取某月的数据
///
public List GetByMonitorPointIDOfMonth(long CorpID, long MonitorPointID, int Year, int Month)
{
var dal = new DAL.MonitorMonthRecord();
var entity_list = dal.GetByMonitorPointIDOfMonth(CorpID, MonitorPointID, Year, Month);
var model_list = Entity2Models(entity_list);
return model_list?.OrderBy(x => x.DataYear).ThenBy(x => x.DataMonth).ThenBy(x => x.DataTime).ToList();
}
///
/// 通过 MonitorPointID 获取年区间内的数据
///
public List GetByMonitorPointIDOfYearRange(long CorpID, long MonitorPointID, int StartYear, int EndYear)
{
var dal = new DAL.MonitorMonthRecord();
var entity_list = dal.GetByMonitorPointIDOfYearRange(CorpID, MonitorPointID, StartYear, EndYear);
var model_list = Entity2Models(entity_list);
return model_list?.OrderBy(x => x.DataYear).ThenBy(x => x.DataMonth).ThenBy(x => x.DataTime).ToList();
}
///
/// 通过 MonitorPointID 获取月区间内的数据
///
public List GetByMonitorPointIDOfMonthRange(long CorpID, long MonitorPointID, int StartYear, int StartMonth, int EndYear, int EndMonth)
{
var dal = new DAL.MonitorMonthRecord();
var entity_list = dal.GetByMonitorPointIDOfMonthRange(CorpID, MonitorPointID, StartYear, StartMonth, EndYear, EndMonth);
var model_list = Entity2Models(entity_list);
return model_list?.OrderBy(x => x.DataYear).ThenBy(x => x.DataMonth).ThenBy(x => x.DataTime).ToList();
}
///
/// 通过 SignalID 获取某月的数据
///
public Model.MonitorMonthRecord GetBySignalIDOfMonth(long CorpID, long MonitorPointID, long SignalID, int DataYear, int DataMonth)
{
var dal = new DAL.MonitorMonthRecord();
var entity = dal.GetBySignalIDOfMonth(CorpID, MonitorPointID, SignalID, DataYear, DataMonth);
var model = Entity2Model(entity);
return model;
}
///
/// 通过 SignalID 获取月区间内的数据
///
public List GetBySignalIDOfMonthRange(long CorpID, long MonitorPointID, long SignalID, int StartYear, int StartMonth, int EndYear, int EndMonth)
{
var dal = new DAL.MonitorMonthRecord();
var entityList = dal.GetBySignalIDOfMonthRange(CorpID, MonitorPointID, SignalID, StartYear, StartMonth, EndYear, EndMonth);
var modelList = Entity2Models(entityList);
return modelList;
}
///
/// 通过 SignalID 获取月区间内的基础内容数据
///
public List GetBasicContentBySignalIDOfMonthRange(long CorpID, long MonitorPointID,long SignalID, int StartYear, int StartMonth, int EndYear, int EndMonth)
{
var dal = new DAL.MonitorMonthRecord();
var entity_list = dal.GetBySignalIDOfMonthRange(CorpID, MonitorPointID, SignalID, StartYear, StartMonth, EndYear, EndMonth);
var model_list = Entity2BasicContents(entity_list);
return model_list;
}
#endregion
#region Insert
///
/// 插入一条
///
public bool Insert(Model.MonitorMonthRecordPure model)
{
if (model == null)
return default;
var dal = new DAL.MonitorMonthRecord();
var entity = Model2Entity(new Model.MonitorMonthRecord(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.MonitorMonthRecord();
var entity_list = Model2Entities(list.Select(x => new Model.MonitorMonthRecord(x)).ToList()).ToList();
var bol = dal.Inserts(entity_list);
return bol;
}
///
/// 插入最近一条记录
///
public bool InsertLastRecord(Model.MonitorMonthRecordPure model)
{
if (model == null)
return default;
var dal = new DAL.MonitorMonthRecord();
var entity = Model2Entity(new Model.MonitorMonthRecord(model));
var bol = dal.Insert(entity) > 0;
if (bol)
{
var redisHelper = new MonitorRecordCacheHelper();
redisHelper.SetLastMonthRecord(model);
}
return bol;
}
///
/// 插入最近多条记录
///
public bool InsertsLastRecord(List list)
{
if (list == null || list.Count() < 1)
return default;
var dal = new DAL.MonitorMonthRecord();
var entity_list = Model2Entities(list.Select(x => new Model.MonitorMonthRecord(x)).ToList());
var bol = dal.Inserts(entity_list);
if (bol)
{
var redisHelper = new MonitorRecordCacheHelper();
redisHelper.SetLastMonthRecord(list);
}
return bol;
}
///
/// 补录一条
///
public bool InsertSupplement(Model.MonitorMonthRecordPure model)
{
if (model == null)
return default;
var dal = new DAL.MonitorMonthRecord();
var entity = Model2Entity(new Model.MonitorMonthRecord(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.MonitorMonthRecord();
var entity_list = Model2Entities(list.Select(x=>new Model.MonitorMonthRecord(x)).ToList()).ToList();
var bol = dal.Inserts(entity_list);
return bol;
}
///
/// 重录一条
///
public bool InsertAgain(Model.MonitorMonthRecordPure model)
{
if (model == null)
return false;
var dal = new DAL.MonitorMonthRecord();
var result = dal.DeleteBySignalIDOfMonth(model.CorpID, model.MonitorPointID, model.SignalID, model.DataYear,model.DataMonth);
if (!result)
{
LogHelper.Info($"重录一条监测月记录中,CorpID:{model.CorpID},MonitorPointID:{model.MonitorPointID},SignalID:{model.SignalID},DataYear:{model.DataYear},DataMonth:{model.DataMonth},未检索到已存在记录!");
}
var entity = Model2Entity(new Model.MonitorMonthRecord(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.MonitorMonthRecord();
foreach (var group in groupList)
{
var orderList = group.OrderBy(x => x.DataYear).ThenBy(x => x.DataMonth).ToList();
var first = orderList.First();
var startYear = first.DataYear;
var startMonth=first.DataMonth;
var last = orderList.Last();
var endYear = last.DataYear;
var endMonth=last.DataMonth;
var result = dal.DeleteBySignalIDOfMonthRange(group.Key.CorpID, group.Key.MonitorPointID, group.Key.SignalID, startYear,startMonth,endYear,endMonth);
if (!result)
{
LogHelper.Info($"重录多条监测月记录中,CorpID:{group.Key.CorpID},MonitorPointID:{group.Key.MonitorPointID},SignalID:{group.Key.SignalID},StartYear:{startYear},StartMonth:{startMonth},EndYear:{endYear},EndMonth:{endMonth},未检索到已存在记录!");
}
}
var entityList = Model2Entities(list.Select(x => new Model.MonitorMonthRecord(x)).ToList());
var bol = dal.Inserts(entityList);
return bol;
}
#endregion
#region Update
///
/// 更新一条
///
public bool Update(Model.MonitorMonthRecord model)
{
if (model == null)
return default;
var dal = new DAL.MonitorMonthRecord();
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.MonitorMonthRecord();
var entityList = Model2Entities(list);
var bol = dal.Updates(entityList);
return bol;
}
#endregion
}
}