BtnFuncDal.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using Cksoft.Data;
  2. using DllUfpEntity;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. namespace DllUfpDal
  8. {
  9. public class BtnFuncDal
  10. {
  11. public IDatabase CurrDb;
  12. public BtnFuncDal(IDatabase db)
  13. {
  14. this.CurrDb = db;
  15. }
  16. public IEnumerable<BtnFunc> Get(int start, int length, string order, string sort, string filter, string errorinfo)
  17. {
  18. var btnfuncs = CurrDb.FindListForCondition<BtnFunc>($" {filter} order by {sort} {order} limit {start - 1},{length}", ref errorinfo);
  19. return btnfuncs;
  20. }
  21. public int GetCount(string filter)
  22. {
  23. string sql = $"select count(1) from BtnFunc a where 1=1 {filter}";
  24. return Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "0");
  25. }
  26. public BtnFunc Get(string id)
  27. {
  28. return CurrDb.FindEntityFor<BtnFunc>(id);
  29. }
  30. /// <summary>
  31. /// 添加角色并返回角色Id
  32. /// </summary>
  33. /// <param name="role"></param>
  34. /// <param name="userCode"></param>
  35. /// <returns></returns>
  36. public int Add(BtnFunc func, string userCode)
  37. {
  38. func.RecCode = func.ModCode = userCode;
  39. func.RecTime = func.ModTime = DateTime.Now;
  40. string sql = $"insert into BtnFunc (FCode,FName,ModuleName,SystemId,Remark,RecCode,RecTime,ModCode,ModTime,FuncName) " +
  41. $"values ('{func.FCode}','{func.FName}','{func.ModuleName}','{func.SystemId}','{func.Remark}','{func.RecCode}','{func.RecTime.Value.ToString("yyyy-MM-dd")}'," +
  42. $"'{func.ModCode}','{func.ModTime.Value.ToString("yyyy-MM-dd")}','{func.FuncName}');";
  43. sql += "select @@identity;";
  44. var id = Convert.ToInt32(CurrDb.FindList<string>(sql).FirstOrDefault() ?? "-1");
  45. return id;
  46. }
  47. public int Update(BtnFunc func, string userCode)
  48. {
  49. return CurrDb.UpdateFor(func, userCode);
  50. }
  51. public IEnumerable<RoleFunc> getRoleFunc(int id)
  52. {
  53. string sql = $"select * from RoleFunc where FuncId='{id}' and type=2";
  54. return CurrDb.FindList<RoleFunc>(sql);
  55. }
  56. public int Delete(int id, ref string msg)
  57. {
  58. string sql = $"delete from RoleFunc where FuncId='{id}' and type=2";
  59. var res = CurrDb.ExecuteBySql(sql);
  60. if (res < 0)
  61. {
  62. msg = "删除角色功能时失败";
  63. return -1;
  64. }
  65. res = CurrDb.DeleteFor<BtnFunc>(id);
  66. if (res < 0)
  67. {
  68. msg = "删除功能主表时失败";
  69. return -1;
  70. }
  71. return 1;
  72. }
  73. public int SetBtnFunction(IEnumerable<int> funcIds, int roleId, string usercode)
  74. {
  75. if (funcIds == null || funcIds.Count() <= 0)
  76. {
  77. return 1;
  78. }
  79. foreach (var item in funcIds)
  80. {
  81. var res = CurrDb.InsertFor<RoleFunc>(new RoleFunc()
  82. {
  83. FuncId = item,
  84. RoleId = roleId,
  85. RecTime = DateTime.Now,
  86. ModTime = DateTime.Now,
  87. ModCode = usercode,
  88. RecCode = usercode,
  89. Type = 1
  90. }, usercode);
  91. if (res < 0)
  92. return -1;
  93. }
  94. return 1;
  95. }
  96. public IEnumerable<RoleFunc> getRoleFuncByRoleId( int roleId)
  97. {
  98. var sql = $"select * from RoleFunc where roleid={roleId} and type=1";
  99. return CurrDb.FindList<RoleFunc>(sql);
  100. }
  101. public int DeleteRoleFuncs(IEnumerable<int> funcIds, int roleId)
  102. {
  103. var sql = $"delete from RoleFunc where roleid={roleId} and type=1";
  104. if (CurrDb.ExecuteBySql(sql) < 0)
  105. return -1;
  106. return 1;
  107. }
  108. public bool IsPermitted(string userCode, string btnFunc, ref string errorinfo)
  109. {
  110. var staffSql = $"select * from staff where fcode='{userCode}'";
  111. var staff = CurrDb.FindList<Staff>(staffSql).FirstOrDefault();
  112. if (staff == null)
  113. {
  114. errorinfo = "用户不存在或已被删除";
  115. return false;
  116. }
  117. if (staff.IsSA == 1)
  118. {
  119. return true;
  120. }
  121. string sql = $@"select a.* from roleFunc a
  122. left join btnfunc b on a.funcid=b.id
  123. left join role c on a.roleid=c.id
  124. left join staffrole d on c.id=d.roleid
  125. where 1=1 and a.Type=1 and d.staffcode='{userCode}' and b.fCode='{btnFunc}'";
  126. var entity = CurrDb.FindList<RoleFunc>(sql).FirstOrDefault();
  127. if (entity == null)
  128. {
  129. errorinfo = "当前功能未授权,请授权后再试";
  130. return false;
  131. }
  132. return true;
  133. }
  134. /// <summary>
  135. /// 判断请求的接口是否有权限
  136. /// </summary>
  137. /// <param name="userCode">用户工号</param>
  138. /// <param name="moduleName">模块名称(控制器名称)</param>
  139. /// <param name="btnFunc">action名称</param>
  140. /// <param name="errorinfo">错误信息</param>
  141. /// <returns></returns>
  142. public bool IsPermitted(string userCode, string moduleName, string btnFunc, ref string errorinfo)
  143. {
  144. if (string.IsNullOrEmpty(userCode))
  145. return true;
  146. var sql = $"select * from staff where fcode='{userCode}'";
  147. var staff = CurrDb.FindList<Staff>(sql).FirstOrDefault();
  148. if (staff == null || staff.IsSA == 1)
  149. return true;
  150. var strsql = $"select * from btnfunc where funcName='{btnFunc}' and modulename='{moduleName}'";
  151. var btn = CurrDb.FindList<BtnFunc>(strsql).FirstOrDefault();
  152. if (btn == null)
  153. return true;
  154. sql = $@"select a.* from roleFunc a
  155. left join btnfunc b on a.funcid=b.id
  156. left join role c on a.roleid=c.id
  157. left join staffrole d on c.id=d.roleid
  158. where 1=1 and a.Type=1 and d.staffcode='{userCode}' and b.funcName='{btnFunc}' and b.modulename='{moduleName}'";
  159. var entity = CurrDb.FindList<RoleFunc>(sql).FirstOrDefault();
  160. if (entity == null)
  161. {
  162. errorinfo = "当前功能未授权,请授权后再试";
  163. return false;
  164. }
  165. return true;
  166. }
  167. }
  168. }