using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace IStation.DAL
{
///
///
///
public partial class SpecialPropertyValue : CorpDAL
{
///
///
///
public override ConnectionConfig ConnectionConfig
{
get { return ConfigHelper.DefaultConnectionConfig; }
}
///
/// 通过 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==IStation.ObjectType.Station&&x.ObjectID==ObjectID).ToList();
}
}
///
/// 通过 Object 和 PropertyID 获取
///
public Entity.SpecialPropertyValue GetObjectAndPropertyID(long CorpID, string ObjectType, long ObjectID, long PropertyID)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.First(x => x.CorpID == CorpID && x.ObjectType == IStation.ObjectType.Station && x.ObjectID == ObjectID&&x.PropertyID== PropertyID);
}
}
///
/// 通过 PropertyID 获取
///
public List GetByPropertyID(long CorpID, long PropertyID)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.PropertyID==PropertyID).ToList();
}
}
///
/// 通过 PropertyIds 获取
///
public List GetByPropertyIds(long CorpID, List PropertyIds)
{
if (PropertyIds == null || PropertyIds.Count < 1)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && PropertyIds.Contains(x.PropertyID)).ToList();
}
}
///
/// 设置
///
public bool Set(List list)
{
if (list == null || list.Count() < 1)
return false;
var corpIds = list.Select(x => x.CorpID).Distinct().ToList();
if (corpIds.Count != 1)
return false;
using (var db = new SqlSugarClient(ConnectionConfig))
{
try
{
db.BeginTran();
foreach (var item in list)
{
var bol = db.Queryable()
.Where(x => x.CorpID == item.CorpID && x.ObjectType == item.ObjectType && x.ObjectID==item.ObjectID && x.PropertyID == item.PropertyID).Count() > 0;
if (bol)//已存在
{
var result = db.Updateable()
.SetColumns(x => x.PropertyValue== item.PropertyValue)
.Where(x => x.CorpID == item.CorpID&&x.ObjectType==item.ObjectType&&x.ObjectID==item.ObjectID && x.PropertyID == item.PropertyID)
.ExecuteCommand() > 0;
if (!result)
{
db.RollbackTran();
return false;
}
}
else//不存在
{
var entity = new Entity.SpecialPropertyValue();
entity.CorpID = item.CorpID;
entity.ObjectType = item.ObjectType;
entity.ObjectID = item.ObjectID;
entity.PropertyID = item.PropertyID;
entity.PropertyValue = item.PropertyValue;
var id = db.Insertable(entity).ExecuteReturnSnowflakeId();
if (id < 1)
{
db.RollbackTran();
return false;
}
}
}
db.CommitTran();
return true;
}
catch
{
db.RollbackTran();
throw;
}
}
}
}
}