using System;
using System.Collections.Generic;
using System.Text;
using Cksoft.Data;
using DllUfpEntity;
using System.Linq;
using DllUfpEntity.Dto;
using Cksoft.Unity.Log4NetConfig;
using Cksoft.Data.Repository;
namespace DllUfpDal
{
public class FunctionDal
{
public IDatabase Currdb;
public FunctionDal(IDatabase db)
{
this.Currdb = db;
}
///
/// 获取菜单列表的根菜单
///
///
public IEnumerable GetRoots(int sysId, string errorinfo)
{
var roots = Currdb.FindListForCondition($" and (a.ParentId is null or a.ParentId=0) and systemid={sysId} and a.Hidden=-1 order by rank", ref errorinfo);
string sql = "select distinct ParentID from Function";
var parentIds = Currdb.FindList(sql).ToList();
foreach (var item in roots)
{
if (!parentIds.Contains(item.ID.ToString()))
{
item.IsLeaf = true;
}
else
{
item.IsLeaf = false;
}
}
return roots;
}
///
/// 根据上级Id获取子菜单
///
///
///
public IEnumerable GetSubFunctions(int sysId, int parentId, string errorinfo)
{
var funcs = Currdb.FindListForCondition($" and a.systemid={sysId} and a.ParentId={parentId} and a.Hidden=-1 order by rank", ref errorinfo);
var parentIds = Currdb.FindListForCondition(string.Empty, ref errorinfo).Select(c => c.ParentID).Distinct();
foreach (var item in funcs)
{
if (!parentIds.Contains(item.ID))
{
item.IsLeaf = true;
}
else
{
item.IsLeaf = false;
}
}
return funcs;
}
public EapFunction GetFunction(int id)
{
return Currdb.FindEntity(id);
}
public int Insert(EapFunction function, string usercode)
{
string sql = string.Format($"insert into Function(FCode,FName,Url,Remark,ParentId,RecCode,RecTime,ModCode,ModTime,Icon,Rank,SystemId) values " +
$"('{function.FCode}','{function.FName}','{function.Url}','{function.Remark}','{function.ParentID}','{function.RecCode}'," +
$"'{function.RecTime.ToString("yyyy-MM-dd")}','{function.ModCode}'," +
$"'{function.ModTime.ToString("yyyy-MM-dd")}','{function.Icon}','{function.Rank}','{function.SystemId}');");
sql += "select @@identity;";
return Convert.ToInt32(Currdb.FindList(sql).FirstOrDefault() ?? "-1");
}
public int Update(EapFunction function, string usercode)
{
return Currdb.UpdateFor(function, usercode);
}
public int Delete(int Id)
{
int total = Currdb.DeleteFor(Id);
ChildrenDelete(Id, ref total);
return total;
/*return Currdb.DeleteFor(Id);*/
}
public void ChildrenDelete(int id,ref int total)
{
var fun = Currdb.FindList($@"select * from function where ParentID={id}").ToList();
if (fun != null)
{
foreach (var item in fun)
{
total=Currdb.DeleteFor(item.ID);
ChildrenDelete(item.ID, ref total);
}
}
}
///
/// 递归获取菜单树结构
///
///
///
///
public FuncTree GetSubFunctionsByParent(int sysId, FuncTree func, string errorinfo)
{
IEnumerable subs = Currdb.FindListForCondition($" and a.SystemId={sysId} and a.ParentId={func.Key} order by rank", ref errorinfo);
if (subs != null && subs.Count() > 0)
{
func.Children = subs.Select(c => new FuncTree() { Key = c.ID.ToString(), Title = c.FName, IsLeaf = false }).ToList();
for (var i = 0; i < func.Children.Count(); i++)
{
GetSubFunctionsByParent(sysId, func.Children[i], errorinfo);
}
}
else
{
func.IsLeaf = true;
}
return func;
}
///
/// 获取菜单树
///
///
public IEnumerable GetFuncTreesAll(int sysId)
{
string errorinfo = string.Empty;
var roots = this.GetRoots(sysId, errorinfo)?.OrderBy(c => c.Rank).Select(c => new FuncTree()
{
Key = c.ID.ToString(),
Title = c.FName,
IsLeaf = false,
Children = null
}).ToList();
for (var i = 0; i < roots.Count(); i++)
{
this.GetSubFunctionsByParent(sysId, roots[i], errorinfo);
}
return roots;
}
public int SetFunction(IEnumerable funcIds, int roleId, string usercode)
{
if (funcIds == null || funcIds.Count() <= 0)
{
return 1;
}
foreach (var item in funcIds)
{
var res = Currdb.InsertFor(new RoleFunc()
{
FuncId = item,
RoleId = roleId,
RecTime = DateTime.Now,
ModTime = DateTime.Now,
ModCode = usercode,
RecCode = usercode,
Type = 0
}, usercode);
if (res < 0)
return -1;
}
return 1;
}
public int DeleteRoleFuncs(IEnumerable funcIds, int roleId)
{
if (funcIds == null || funcIds.Count() <= 0)
return 1;
foreach (var item in funcIds)
{
var res = Currdb.DeleteForCondition($" and a.funcid={item} and a.roleid={roleId} and a.Type=0");
if (res < 0)
{
return -1;
}
}
return 1;
}
public IEnumerable GetAuthedFunIds(int roleId, int type)
{
string error = string.Empty;
var ids = Currdb.FindListForCondition($" and a.type={type} and a.roleid={roleId} and " +
$"a.funcid not in (select distinct parentid from function)", ref error).Select(c => c.FuncId);
return ids;
}
public IEnumerable GetAuthedBtns(int roleId)
{
string error = string.Empty;
var ids = Currdb.FindListForCondition($" and a.type=1 and a.roleid={roleId} ", ref error).Select(c => c.FuncId);
return ids;
}
public IEnumerable GetFuncIds(string filter)
{
string error = string.Empty;
var ids = Currdb.FindListForCondition(filter, ref error).Select(c => c.FuncId);
return ids;
}
///
/// 根据登录用户获取菜单
///
///
///
public IEnumerable