FunctionDal.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Cksoft.Data;
  5. using DllUfpEntity;
  6. using System.Linq;
  7. using DllUfpEntity.Dto;
  8. using Cksoft.Unity.Log4NetConfig;
  9. using Cksoft.Data.Repository;
  10. namespace DllUfpDal
  11. {
  12. public class FunctionDal
  13. {
  14. public IDatabase Currdb;
  15. public FunctionDal(IDatabase db)
  16. {
  17. this.Currdb = db;
  18. }
  19. /// <summary>
  20. /// 获取菜单列表的根菜单
  21. /// </summary>
  22. /// <returns></returns>
  23. public IEnumerable<EapFunction> GetRoots(int sysId, string errorinfo)
  24. {
  25. var roots = Currdb.FindListForCondition<EapFunction>($" and (a.ParentId is null or a.ParentId=0) and systemid={sysId} and a.Hidden=-1 order by rank", ref errorinfo);
  26. string sql = "select distinct ParentID from Function";
  27. var parentIds = Currdb.FindList<string>(sql).ToList();
  28. foreach (var item in roots)
  29. {
  30. if (!parentIds.Contains(item.ID.ToString()))
  31. {
  32. item.IsLeaf = true;
  33. }
  34. else
  35. {
  36. item.IsLeaf = false;
  37. }
  38. }
  39. return roots;
  40. }
  41. /// <summary>
  42. /// 根据上级Id获取子菜单
  43. /// </summary>
  44. /// <param name="parentId"></param>
  45. /// <returns></returns>
  46. public IEnumerable<EapFunction> GetSubFunctions(int sysId, int parentId, string errorinfo)
  47. {
  48. var funcs = Currdb.FindListForCondition<EapFunction>($" and a.systemid={sysId} and a.ParentId={parentId} and a.Hidden=-1 order by rank", ref errorinfo);
  49. var parentIds = Currdb.FindListForCondition<EapFunction>(string.Empty, ref errorinfo).Select(c => c.ParentID).Distinct();
  50. foreach (var item in funcs)
  51. {
  52. if (!parentIds.Contains(item.ID))
  53. {
  54. item.IsLeaf = true;
  55. }
  56. else
  57. {
  58. item.IsLeaf = false;
  59. }
  60. }
  61. return funcs;
  62. }
  63. public EapFunction GetFunction(int id)
  64. {
  65. return Currdb.FindEntity<EapFunction>(id);
  66. }
  67. public int Insert(EapFunction function, string usercode)
  68. {
  69. string sql = string.Format($"insert into Function(FCode,FName,Url,Remark,ParentId,RecCode,RecTime,ModCode,ModTime,Icon,Rank,SystemId) values " +
  70. $"('{function.FCode}','{function.FName}','{function.Url}','{function.Remark}','{function.ParentID}','{function.RecCode}'," +
  71. $"'{function.RecTime.ToString("yyyy-MM-dd")}','{function.ModCode}'," +
  72. $"'{function.ModTime.ToString("yyyy-MM-dd")}','{function.Icon}','{function.Rank}','{function.SystemId}');");
  73. sql += "select @@identity;";
  74. return Convert.ToInt32(Currdb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  75. }
  76. public int Update(EapFunction function, string usercode)
  77. {
  78. return Currdb.UpdateFor<EapFunction>(function, usercode);
  79. }
  80. public int Delete(int Id)
  81. {
  82. int total = Currdb.DeleteFor<EapFunction>(Id);
  83. ChildrenDelete(Id, ref total);
  84. return total;
  85. /*return Currdb.DeleteFor<EapFunction>(Id);*/
  86. }
  87. public void ChildrenDelete(int id,ref int total)
  88. {
  89. var fun = Currdb.FindList<EapFunction>($@"select * from function where ParentID={id}").ToList();
  90. if (fun != null)
  91. {
  92. foreach (var item in fun)
  93. {
  94. total=Currdb.DeleteFor<EapFunction>(item.ID);
  95. ChildrenDelete(item.ID, ref total);
  96. }
  97. }
  98. }
  99. /// <summary>
  100. /// 递归获取菜单树结构
  101. /// </summary>
  102. /// <param name="func"></param>
  103. /// <param name="errorinfo"></param>
  104. /// <returns></returns>
  105. public FuncTree GetSubFunctionsByParent(int sysId, FuncTree func, string errorinfo)
  106. {
  107. IEnumerable<EapFunction> subs = Currdb.FindListForCondition<EapFunction>($" and a.SystemId={sysId} and a.ParentId={func.Key} order by rank", ref errorinfo);
  108. if (subs != null && subs.Count() > 0)
  109. {
  110. func.Children = subs.Select(c => new FuncTree() { Key = c.ID.ToString(), Title = c.FName, IsLeaf = false }).ToList();
  111. for (var i = 0; i < func.Children.Count(); i++)
  112. {
  113. GetSubFunctionsByParent(sysId, func.Children[i], errorinfo);
  114. }
  115. }
  116. else
  117. {
  118. func.IsLeaf = true;
  119. }
  120. return func;
  121. }
  122. /// <summary>
  123. /// 获取菜单树
  124. /// </summary>
  125. /// <returns></returns>
  126. public IEnumerable<FuncTree> GetFuncTreesAll(int sysId)
  127. {
  128. string errorinfo = string.Empty;
  129. var roots = this.GetRoots(sysId, errorinfo)?.OrderBy(c => c.Rank).Select(c => new FuncTree()
  130. {
  131. Key = c.ID.ToString(),
  132. Title = c.FName,
  133. IsLeaf = false,
  134. Children = null
  135. }).ToList();
  136. for (var i = 0; i < roots.Count(); i++)
  137. {
  138. this.GetSubFunctionsByParent(sysId, roots[i], errorinfo);
  139. }
  140. return roots;
  141. }
  142. public int SetFunction(IEnumerable<int> funcIds, int roleId, string usercode)
  143. {
  144. if (funcIds == null || funcIds.Count() <= 0)
  145. {
  146. return 1;
  147. }
  148. foreach (var item in funcIds)
  149. {
  150. var res = Currdb.InsertFor<RoleFunc>(new RoleFunc()
  151. {
  152. FuncId = item,
  153. RoleId = roleId,
  154. RecTime = DateTime.Now,
  155. ModTime = DateTime.Now,
  156. ModCode = usercode,
  157. RecCode = usercode,
  158. Type = 0
  159. }, usercode);
  160. if (res < 0)
  161. return -1;
  162. }
  163. return 1;
  164. }
  165. public int DeleteRoleFuncs(IEnumerable<int> funcIds, int roleId)
  166. {
  167. if (funcIds == null || funcIds.Count() <= 0)
  168. return 1;
  169. foreach (var item in funcIds)
  170. {
  171. var res = Currdb.DeleteForCondition<RoleFunc>($" and a.funcid={item} and a.roleid={roleId} and a.Type=0");
  172. if (res < 0)
  173. {
  174. return -1;
  175. }
  176. }
  177. return 1;
  178. }
  179. public IEnumerable<int> GetAuthedFunIds(int roleId, int type)
  180. {
  181. string error = string.Empty;
  182. var ids = Currdb.FindListForCondition<RoleFunc>($" and a.type={type} and a.roleid={roleId} and " +
  183. $"a.funcid not in (select distinct parentid from function)", ref error).Select(c => c.FuncId);
  184. return ids;
  185. }
  186. public IEnumerable<int> GetAuthedBtns(int roleId)
  187. {
  188. string error = string.Empty;
  189. var ids = Currdb.FindListForCondition<RoleFunc>($" and a.type=1 and a.roleid={roleId} ", ref error).Select(c => c.FuncId);
  190. return ids;
  191. }
  192. public IEnumerable<int> GetFuncIds(string filter)
  193. {
  194. string error = string.Empty;
  195. var ids = Currdb.FindListForCondition<RoleFunc>(filter, ref error).Select(c => c.FuncId);
  196. return ids;
  197. }
  198. /// <summary>
  199. /// 根据登录用户获取菜单
  200. /// </summary>
  201. /// <param name="usercode"></param>
  202. /// <returns></returns>
  203. public IEnumerable<Menu> GetMenusByUserCode(int sysId, string usercode)
  204. {
  205. var funcs = GetFunctions(sysId, usercode);
  206. IList<Menu> menuRoots = funcs.Where(c => c.ParentID == 0).OrderBy(c => c.Rank).Select(c => new Menu
  207. {
  208. Level = 1,
  209. Id = c.ID,
  210. Open = false,
  211. Title = c.FName,
  212. Url = c.Url,
  213. Icon = c.Icon,
  214. }).ToList();
  215. for (var i = 0; i < menuRoots.Count; i++)
  216. {
  217. this.GetMenuByParent(menuRoots[i], funcs);
  218. }
  219. return menuRoots;
  220. }
  221. public Menu GetMenuByParent(Menu menu, IEnumerable<EapFunction> functions)
  222. {
  223. var subs = functions.Where(c => c.ParentID == menu.Id)?.OrderBy(c => c.Rank).Select(c => new Menu()
  224. {
  225. Level = menu.Level + 1,
  226. Id = c.ID,
  227. Title = c.FName,
  228. Url = c.Url,
  229. Icon = c.Icon
  230. }).ToList();
  231. if (subs != null && subs.Count() > 0)
  232. {
  233. menu.Children = subs;
  234. for (var j = 0; j < subs.Count; j++)
  235. {
  236. this.GetMenuByParent(subs[j], functions);
  237. }
  238. }
  239. return menu;
  240. }
  241. /// <summary>
  242. /// 根据用户工号获取所有有权限的菜单
  243. /// </summary>
  244. /// <param name="usercode"></param>
  245. /// <returns></returns>
  246. public IEnumerable<EapFunction> GetFunctions(int sysId, string usercode)
  247. {
  248. var sql = @"select c.ID,c.FCode,c.Fname,c.Url,c.ParentID,c.Icon,c.Rank,c.SystemId,c.ExternalLink from rolefunc a
  249. inner join role b on a.roleid=b.id
  250. inner join function c on a.funcid=c.id "
  251. + $" where a.Type=0 and c.hidden=-1 and c.systemid={sysId} and c.parentid<>-1";
  252. sql += $" and a.roleid in "
  253. + $"(select d.roleid from staffrole d inner join staff e on d.staffCode = e.fcode where e.fcode='{usercode}') ";
  254. sql += $" group by c.ID,c.FCode,c.Fname,c.Url,c.ParentID,c.Icon,c.Rank,c.SystemId order by rank asc ";
  255. var str = $"select * from staff where fcode='{usercode}'";
  256. var staff = Currdb.FindList<Staff>(str).FirstOrDefault();
  257. if (staff.IsSA == 1)
  258. {
  259. sql = $"select * from function where systemid={sysId} and hidden=-1 and parentid<>-1 order by rank asc";
  260. }
  261. var funcs = Currdb.FindList<EapFunction>(sql);
  262. return funcs;
  263. }
  264. /// <summary>
  265. /// 根据登录用户获取菜单
  266. /// </summary>
  267. /// <param name="usercode"></param>
  268. /// <returns></returns>
  269. public IEnumerable<AlainMenu> GetMenusByUserCodeForAlain(int sysId, string usercode)
  270. {
  271. var funcs = GetFunctions(sysId, usercode);
  272. IList<AlainMenu> menuRoots = funcs.Where(c => c.ParentID == 0).OrderBy(c => c.Rank).Select(c => new AlainMenu
  273. {
  274. Id = c.ID,
  275. Link = c.Url,
  276. Text = c.FName,
  277. ShortcutRoot = false,
  278. Target = c.ExternalLink == 1 ? "_blank" : "_self",
  279. Icon = new AlainIcon() { Type = "icon", Value = c.Icon }
  280. }).ToList();
  281. for (var i = 0; i < menuRoots.Count; i++)
  282. {
  283. this.GetMenuByParentForAlain(menuRoots[i], funcs);
  284. }
  285. return menuRoots;
  286. }
  287. public AlainMenu GetMenuByParentForAlain(AlainMenu menu, IEnumerable<EapFunction> functions)
  288. {
  289. var subs = functions.Where(c => c.ParentID == menu.Id)?.OrderBy(c => c.Rank).Select(c => new AlainMenu()
  290. {
  291. Id = c.ID,
  292. Link = c.Url,
  293. Text = c.FName,
  294. Target = c.ExternalLink == 1 ? "_blank" : "_self",
  295. ShortcutRoot = false,
  296. Icon = string.IsNullOrEmpty(c.Icon) ? null : new AlainIcon { Type = "icon", Value = c.Icon }
  297. }).ToList();
  298. if (subs != null && subs.Count() > 0)
  299. {
  300. menu.Children = subs;
  301. for (var j = 0; j < subs.Count; j++)
  302. {
  303. this.GetMenuByParentForAlain(subs[j], functions);
  304. }
  305. }
  306. return menu;
  307. }
  308. public Cksoft.Unity.EapResponse GetMean(int id)
  309. {
  310. var funcs = Currdb.FindList<EapFunction>($@"select * FROM function where id <>{id}");
  311. IList<AlainMenu> menuRoots = funcs.Where(c => c.ParentID == 0).OrderBy(c => c.Rank).Select(c => new AlainMenu
  312. {
  313. Id = c.ID,
  314. Link = c.Url,
  315. Text = c.FName,
  316. ShortcutRoot = false,
  317. Target = c.ExternalLink == 1 ? "_blank" : "_self",
  318. Icon = new AlainIcon() { Type = "icon", Value = c.Icon }
  319. }).ToList();
  320. for (var i = 0; i < menuRoots.Count; i++)
  321. {
  322. this.GetMenuByParentForAlain(menuRoots[i], funcs);
  323. }
  324. return new Cksoft.Unity.EapResponse()
  325. {
  326. Code = 1,
  327. Data = menuRoots
  328. };
  329. }
  330. /// <summary>
  331. /// 判断当前用户是否有访问改菜单的权限
  332. /// </summary>
  333. /// <param name="usercode"></param>
  334. /// <param name="url"></param>
  335. /// <returns></returns>
  336. public int IsAuthorized(int sysId, string usercode, string url)
  337. {
  338. var staff = Currdb.FindList<Staff>($"select * from staff where fcode='{usercode}'").FirstOrDefault();
  339. if (staff.IsSA == 1)
  340. return 1;
  341. string sql = string.Format("select *,c.FName as FuncName from roleFunc a " +
  342. "inner join staffrole b on a.roleid=b.roleid " +
  343. "inner join function c on a.funcid=c.id " +
  344. $"where 1=1 and c.systemid='{sysId}' and b.staffcode='{usercode}' and c.url='{url}'");
  345. var roleFuncs = Currdb.FindList<RoleFunc>(sql);
  346. if (roleFuncs == null || roleFuncs.Count() <= 0)
  347. {
  348. return -1;
  349. }
  350. // var parent = this.GetFunction(roleFuncs.First().ParentID);
  351. // Log(parent, roleFuncs.First(), usercode);
  352. return 1;
  353. }
  354. private void Log(EapFunction parent, RoleFunc roleFunc, string userCode)
  355. {
  356. // 写日志
  357. var log = new DllEapEntity.Log()
  358. {
  359. Platform = 1,
  360. Category = "用户访问",
  361. LogLevel = "Info",
  362. Type = 1,
  363. Module = parent.FName,
  364. Action = roleFunc.FuncName,
  365. Message = $"访问【{roleFunc.FuncName}】页面",
  366. RecCode = userCode,
  367. RecTime = DateTime.Now
  368. };
  369. using (IDatabase db = DbFactory.Base("eap"))
  370. {
  371. db.InsertFor(log, userCode);
  372. }
  373. }
  374. public int GetSysIdByRoleId(int roleId)
  375. {
  376. string sql = $"select systemid from role where id='{roleId}'";
  377. return Convert.ToInt32(Currdb.FindList<string>(sql).FirstOrDefault());
  378. }
  379. }
  380. }