using Yw.DAL.PostgreSql;
namespace IStation.DAL
{
///
/// 业务清单
///
public partial class LogicTree : BaseDAL_TreeSorter
{
///
///
///
public override ConnectionConfig ConnectionConfig
{
get { return ConfigHelper.DefaultConnectionConfig; }
}
///
/// 通过 PolicyID 获取
///
public List GetByPolicyID(long PolicyID)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
return db.Queryable()
.Where(x => x.PolicyID == PolicyID)
.ToList();
}
}
///
/// 保存
///
public bool Save(long PolicyID, List treeList)
{
using (var db = new SqlSugarClient(ConnectionConfig))
{
try
{
if (treeList == null || treeList.Count < 1)
{
return db.Deleteable()
.Where(x => x.PolicyID == PolicyID)
.ExecuteCommandHasChange();
}
db.BeginTran();
var idList = new List();
#region 迭代函数
long Append(Entity.LogicTreeSaveTree tree, List parentIds, int sortCode)
{
if (parentIds == null)
parentIds = new List();
sortCode++;
//构造
var entity = new Entity.LogicTree()
{
ID = tree.ID,
PolicyID = PolicyID,
ParentIds = TreeParentIdsHelper.ToString(parentIds),
LogicType = tree.LogicType,
LogicID = tree.LogicID,
SortCode = sortCode
};
if (entity.ID < 1)
{
entity.ID = db.Insertable(entity).ExecuteReturnSnowflakeId();
if (entity.ID < 1)
{
return default;
}
}
else
{
var bol = db.Updateable(entity).ExecuteCommandHasChange();
if (!bol)
{
return default;
}
}
idList.Add(entity.ID);
if (tree.Children != null && tree.Children.Count > 0)
{
var childParentIds = parentIds.Append(entity.ID).ToList();
var childSortCode = 0;
foreach (var child in tree.Children)
{
var id = Append(child, childParentIds, childSortCode);
if (id < 1)
return default;
}
}
return entity.ID;
}
#endregion
#region 遍历处理
foreach (var tree in treeList)
{
var id = Append(tree, null, 0);
if (id < 1)
{
db.RollbackTran();
return false;
}
}
#endregion
#region 清理目录
if (idList.Count > 0)
{
db.Deleteable()
.Where(x => x.PolicyID == PolicyID && !idList.Contains(x.ID))
.ExecuteCommandHasChange();
}
else
{
db.Deleteable()
.Where(x => x.PolicyID == PolicyID)
.ExecuteCommandHasChange();
}
#endregion
db.CommitTran();
return true;
}
catch
{
db.RollbackTran();
throw;
}
}
}
}
}