using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
namespace IStation.DAL
{
///
/// 测点映射
///
public partial class MonitorPointMapping : CorpDAL_Sorter
{
///
///
///
public override ConnectionConfig ConnectionConfig
{
get { return ConfigHelper.DefaultConnectionConfig; }
}
///
/// 通过 MonitorPointID 获取
///
public List GetByMonitorPointID(long CorpID, long MonitorPointID)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID).ToList();
}
}
///
/// 通过 MonitorPointIds 获取
///
public List GetByMonitorPointIds(long CorpID, List MonitorPointIds)
{
if (MonitorPointIds == null || MonitorPointIds.Count < 1)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && MonitorPointIds.Contains(x.MonitorPointID)).ToList();
}
}
///
/// 通过 ObjectType 和 ObjectID 获取
///
public List GetByObjectTypeAndObjectID(long CorpID, string ObjectType, long ObjectID)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.ObjectType == ObjectType && x.ObjectID == ObjectID).ToList();
}
}
///
/// 更新 AccordParas
///
public bool UpdateAccordParas(long CorpID, long ID, string AccordParas)
{
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
{
return db.Updateable()
.SetColumns(x => x.AccordParas == AccordParas)
.Where(x => x.CorpID == CorpID && x.ID == ID)
.ExecuteCommand() > 0;
}
}
///
/// 通过 MonitorPointID 删除
///
public bool DeleteByMonitorPointID(long CorpID, long MonitorPointID)
{
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
{
return db.Deleteable()
.Where(x => x.CorpID == CorpID && x.MonitorPointID == MonitorPointID)
.ExecuteCommand() > 0;
}
}
///
/// 通过 ObjectType 和 ObjectID 删除
///
public bool DeleteByObjectTypeAndObjectID(long CorpID, string ObjectType, long ObjectID)
{
using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
{
return db.Deleteable()
.Where(x => x.CorpID == CorpID && x.ObjectType == ObjectType && x.ObjectID == ObjectID)
.ExecuteCommand() > 0;
}
}
///
/// 通过 ObjectType 和 ObjectID 设置
///
public bool SetOfObject(long CorpID, string ObjectType, long ObjectID, List list)
{
if (list == null || list.Count() < 1)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Deleteable()
.Where(x => x.CorpID == CorpID && x.ObjectType == ObjectType && x.ObjectID == ObjectID)
.ExecuteCommand() > 0;
}
}
using (var db = new SqlSugarClient(ConnectionConfig))
{
try
{
db.BeginTran();
var add_list = list.Where(x => x.ID < 1).ToList();
var update_list = list.Where(x => x.ID > 0).ToList();
var ids_exist = update_list.Select(x => x.ID).Distinct().ToList();
db.Deleteable()
.Where(x => x.CorpID == CorpID && x.ObjectType == ObjectType && x.ObjectID == ObjectID && !ids_exist.Contains(x.ID))
.ExecuteCommand();
if (add_list.Count > 0)
{
var result = db.Insertable(add_list).ExecuteReturnSnowflakeIdList().Count > 0;
if (!result)
{
db.RollbackTran();
return default;
}
}
if (update_list.Count > 0)
{
var result = db.Updateable(update_list).ExecuteCommand() > 0;
if (!result)
{
db.RollbackTran();
return default;
}
}
db.CommitTran();
return true;
}
catch
{
db.RollbackTran();
throw;
}
}
}
}
}