using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace IStation.DAL
{
///
/// 附加属性映射
///
public partial class SpecialPropertyMapping : CorpDAL
{
///
///
///
public override ConnectionConfig ConnectionConfig
{
get { return ConfigHelper.DefaultConnectionConfig; }
}
///
/// 通过 CatalogID 获取
///
public List GetByCatalogID(long CorpID, long CatalogID)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && x.CatalogID == CatalogID).ToList();
}
}
///
/// 通过 CatalogIds 获取
///
public List GetByCatalogIds(long CorpID, List CatalogIds)
{
if (CatalogIds == null || CatalogIds.Count() < 1)
return default;
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.CorpID == CorpID && CatalogIds.Contains(x.CatalogID)).ToList();
}
}
///
/// 通过 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();
}
}
///
/// 通过 CatalogID 和 PropertyID 获取
///
public Entity.SpecialPropertyMapping GetByCatalogIDAndPropertyID(long CorpID, long CatalogID, long PropertyID)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.First(x => x.CorpID == CorpID && x.CatalogID == CatalogID && x.PropertyID == PropertyID);
}
}
///
/// 设置
///
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.CatalogID == item.CatalogID && x.PropertyID == item.PropertyID).Count() > 0;
if (bol)//已存在
{
if (!item.Selected)//未选择
{
var result = db.Deleteable()
.Where(x => x.CorpID == item.CorpID && x.CatalogID == item.CatalogID && x.PropertyID == item.PropertyID).ExecuteCommandHasChange();
if (!result)
{
db.RollbackTran();
return false;
}
}
}
else//不存在
{
if (item.Selected)//选择
{
var entity = new Entity.SpecialPropertyMapping();
entity.CorpID = item.CorpID;
entity.CatalogID = item.CatalogID;
entity.PropertyID = item.PropertyID;
var id = db.Insertable(entity).ExecuteReturnSnowflakeId();
if (id < 1)
{
db.RollbackTran();
return false;
}
}
}
}
db.CommitTran();
return true;
}
catch
{
db.RollbackTran();
throw;
}
}
}
}
}