Files
zszq-trs/YLErpDAL/Modules/SystemModule/SysFunctionService.cs
T
2024-05-09 14:06:26 +08:00

179 lines
6.4 KiB
C#

using BaseOUDAL;
using System.Text;
using System.Xml;
namespace YLErp.Modules.SystemModule
{
/// <summary>
/// 系统权限数据服务
/// </summary>
public class SysFunctionService : BaseService<ErpBaseContext>
{
public SysFunctionService(OptUserInfo optUser) : base(optUser)
{
}
/// <summary>
/// 来自SystemController.SyncFunctionRight
/// </summary>
/// <param name="functionRightXmlFilePath"></param>
public string SyncFunctionRight(string functionRightXmlFilePath)
{
var doc = new XmlDocument();
var sbMsg = new StringBuilder();
doc.Load(functionRightXmlFilePath);
var pNodes = doc.DocumentElement.ChildNodes;
if (pNodes.Count < 1)
{
throw new ServiceException("error count");
}
var funcDic = DbContext.Functions.ToList()
.ToDictionary(n => n.ParentName.Trim().Trim('-') + "-" + n.Name.Trim());
foreach (var item in funcDic.Values)
{
item.Sort = -9999;
}
var psort = 0;
foreach (XmlNode xn in pNodes)
{
if (xn.NodeType != XmlNodeType.Element || xn.Name != "FunctionParent")
{
continue;
}
var pxe = (XmlElement)xn;
var parentName = pxe.GetAttribute("Name").TrimToEmpty();
var parentNote = pxe.GetAttribute("Note").TrimToEmpty();
var type = pxe.GetAttribute("Type").TrimToEmpty();
var title = pxe.GetAttribute("Title").TrimToEmpty();
if (string.IsNullOrEmpty(parentName))
{
throw new ServiceException("FunctionParent Name不能为空");
}
//父节点
if (!funcDic.TryGetValue("-" + parentName, out var pzFunc))
{
//没有父节点
var pFunc = new Function
{
Name = parentName,
Note = parentNote,
Sort = psort++,
Type = type,
ParentName = "-"
};
DbContext.Functions.Add(pFunc);
sbMsg.AppendFormat("增加权限 {0}", parentName);
}
else
{
pzFunc.Sort = psort++;
if (!string.IsNullOrWhiteSpace(parentNote) && pzFunc.Note != parentNote)
{
pzFunc.Note = parentNote;
sbMsg.AppendFormat("备注改变 {0}:{1}", parentName, parentNote);
}
if (pzFunc.Type != type)
{
pzFunc.Type = type;
sbMsg.AppendFormat("权限类型描述改变 {0}:{1}", parentName, type);
}
if (!string.IsNullOrWhiteSpace(title) && pzFunc.Title != title)
{
pzFunc.Title = title;
sbMsg.AppendFormat("Title改变 {0}:{1}", parentName, title);
}
}
var childSort = 0;
foreach (XmlNode cnode in pxe.ChildNodes)
{
if (cnode.NodeType != XmlNodeType.Element || cnode.Name != "FunctionSub")
{
continue;
}
var cxe = (XmlElement)cnode;
var cName = cxe.GetAttribute("Name").TrimToEmpty();
var cNote = cxe.GetAttribute("Note").TrimToEmpty();
var ctype = cxe.GetAttribute("Type").TrimToEmpty();
var ctitle = cxe.GetAttribute("Title").TrimToEmpty();
if (string.IsNullOrEmpty(cName))
{
throw new ServiceException("FunctionSub Name不能为空");
}
if (!funcDic.TryGetValue(parentName + "-" + cName, out var cFunc))
{
//没有节点
var pFunc = new Function
{
Name = cName,
Sort = childSort++,
Note = cNote,
ParentName = parentName,
Type = ctype
};
DbContext.Functions.Add(pFunc);
sbMsg.AppendFormat("增加子权限 {0}-{1}", parentName, cName);
}
else
{
cFunc.Sort = childSort++;
if (!string.IsNullOrWhiteSpace(cNote) && cFunc.Note != cNote)
{
cFunc.Note = cNote;
sbMsg.AppendFormat("备注改变 {0}:{1}", cName, cNote);
}
if (cFunc.Type != ctype)
{
cFunc.Type = ctype;
sbMsg.AppendFormat("权限类型描述改变 {0}:{1}", cName, ctype);
}
if (!string.IsNullOrWhiteSpace(ctitle) && cFunc.Title != ctitle)
{
cFunc.Title = ctitle;
sbMsg.AppendFormat("Title改变 {0}:{1}", cName, ctype);
}
}
}
}
DbContext.SaveChanges();
#region xml删除多余权限
foreach (var item in funcDic.Values)
{
if (item.Sort < 0)
{
DbContext.Functions.Remove(item);
if (item.ParentName.Replace("-", "").Trim() == "")
{
sbMsg.AppendFormat("删除权限 {0}", item.Name);
}
else
{
sbMsg.AppendFormat("删除子权限 {0}-{1}", item.ParentName, item.Name);
}
}
}
DbContext.SaveChanges();
#endregion
sbMsg.AppendFormat("同步权限成功!");
return sbMsg.ToString();
}
}
}