AccessRanggeQuery.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using Sugar.Enties;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Linq.Expressions;
  9. namespace WebAPIBase.NetCore.BusinessCore
  10. {
  11. public sealed class AccessRanggeQuery : DbContext<SystemUser>
  12. {
  13. private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
  14. public string BuildAccessRangeString(string usercode, string operationCode, string classname)
  15. {
  16. SystemClassItem systemClassItem = SystemClassDescription.GetSystemClassItem(classname);
  17. return BuildAccessRangeString(usercode, operationCode, systemClassItem.TableName, systemClassItem.KeyColumnName, systemClassItem.TypeColumnName, systemClassItem.CreateUserColumnName, systemClassItem.UnitColumnName, systemClassItem.ProjectColumnName);
  18. }
  19. public string BuildAccessRangeString(string usercode, string operationCode, string tableName, string keyColumnName, string typeColumnName, string createUserColumnName, string UnitColumnName, string ProjectColumnName)
  20. {
  21. return BuildAccessTypeString(usercode, operationCode, tableName, keyColumnName, typeColumnName, createUserColumnName, UnitColumnName, ProjectColumnName);
  22. }
  23. private string BuildAccessTypeString(string usercode, string operationCode, string tableName, string keyColumnName, string typeColumnName, string createUserColumnName, string UnitColumnName, string ProjectColumnName)
  24. {
  25. string text = "";
  26. FdcUser fdcUser = new FdcUser(usercode);
  27. bool flag = false;
  28. if (typeColumnName != "")
  29. {
  30. List<FdcGroupUnitRight> groupUnitRight = fdcUser.GetGroupUnitRight(operationCode);
  31. if (groupUnitRight.Count > 0)
  32. {
  33. flag = true;
  34. }
  35. foreach (FdcGroupUnitRight item in groupUnitRight)
  36. {
  37. if (item.IsPerson == 1)
  38. {
  39. if (createUserColumnName != "")
  40. {
  41. if (UnitColumnName != "")
  42. {
  43. text += string.Format(" or (dbo.GetSystemGroupFullID({0}.{1}) like '{2}%' and dbo.GetUnitFullCode({0}.{3}) like ('{4}%') and {0}.{5}='{6}')", tableName, typeColumnName, item.group.FullId, UnitColumnName, item.unit.FullCode, createUserColumnName, usercode);
  44. }
  45. else if (ProjectColumnName != "")
  46. {
  47. string projectCodesByParentUnit = GetProjectCodesByParentUnit(item.unit.UnitCode);
  48. if (projectCodesByParentUnit != string.Empty)
  49. {
  50. text += string.Format(" or (dbo.GetSystemGroupFullID({0}.{1}) like '{2}%' and {0}.{3} in ({4}) and {0}.{5}='{6}')", tableName, typeColumnName, item.group.FullId, ProjectColumnName, projectCodesByParentUnit, createUserColumnName, usercode);
  51. }
  52. }
  53. else
  54. {
  55. text += string.Format(" or (dbo.GetSystemGroupFullID({0}.{1}) like '{2}%' and {0}.{3}='{4}')", tableName, typeColumnName, item.group.FullId, createUserColumnName, usercode);
  56. }
  57. }
  58. else
  59. {
  60. text += string.Format(" or dbo.GetSystemGroupFullID({0}.{1}) like '{2}%'", tableName, typeColumnName, item.group.FullId, createUserColumnName, usercode);
  61. }
  62. }
  63. else if (UnitColumnName != "")
  64. {
  65. text += string.Format(" or (dbo.GetSystemGroupFullID({0}.{1}) like '{2}%' and dbo.GetUnitFullCode({0}.{3}) like ('{4}%'))", tableName, typeColumnName, item.group.FullId, UnitColumnName, item.unit.FullCode);
  66. }
  67. else if (ProjectColumnName != "")
  68. {
  69. string projectCodesByParentUnit2 = GetProjectCodesByParentUnit(item.unit.UnitCode);
  70. if (projectCodesByParentUnit2 != string.Empty)
  71. {
  72. text += string.Format(" or (dbo.GetSystemGroupFullID({0}.{1}) like '{2}%' and {0}.{3} in ({4}))", tableName, typeColumnName, item.group.FullId, ProjectColumnName, projectCodesByParentUnit2);
  73. }
  74. }
  75. else
  76. {
  77. text += string.Format(" or dbo.GetSystemGroupFullID({0}.{1}) like '{2}%'", tableName, typeColumnName, item.group.FullId, UnitColumnName, item.unit.FullCode);
  78. }
  79. }
  80. }
  81. else if (UnitColumnName != "" || ProjectColumnName != "")
  82. {
  83. List<FdcUnitRight> unitRight = fdcUser.GetUnitRight(operationCode);
  84. if (unitRight.Count > 0)
  85. {
  86. flag = true;
  87. }
  88. foreach (FdcUnitRight item2 in unitRight)
  89. {
  90. if (item2.IsPerson == 1)
  91. {
  92. if (!(createUserColumnName != ""))
  93. {
  94. continue;
  95. }
  96. if (UnitColumnName != "")
  97. {
  98. text += string.Format(" or dbo.GetUnitFullCode({0}.{1}) like ('{2}%') and {0}.{3}='{4}'", tableName, UnitColumnName, item2.unit.FullCode, createUserColumnName, usercode);
  99. }
  100. else if (ProjectColumnName != "")
  101. {
  102. string projectCodesByParentUnit3 = GetProjectCodesByParentUnit(item2.unit.UnitCode);
  103. if (projectCodesByParentUnit3 != string.Empty)
  104. {
  105. text += string.Format(" or {0}.{1} in ({2}) and {0}.{3}='{4}'", tableName, ProjectColumnName, projectCodesByParentUnit3, createUserColumnName, usercode);
  106. }
  107. }
  108. else
  109. {
  110. text += $" or {tableName}.{createUserColumnName}='{usercode}'";
  111. }
  112. }
  113. else if (UnitColumnName != "")
  114. {
  115. text += $" or dbo.GetUnitFullCode({tableName}.{UnitColumnName}) like ('{item2.unit.FullCode}%')";
  116. }
  117. else
  118. {
  119. string projectCodesByParentUnit4 = GetProjectCodesByParentUnit(item2.unit.UnitCode);
  120. if (projectCodesByParentUnit4 != string.Empty)
  121. {
  122. text += $" or {tableName}.{ProjectColumnName} in ({projectCodesByParentUnit4})";
  123. }
  124. }
  125. }
  126. }
  127. else if (createUserColumnName != "")
  128. {
  129. flag = true;
  130. text += $" or {tableName}.{createUserColumnName}='{usercode}'";
  131. }
  132. if (text != "")
  133. {
  134. text = text.Substring(4, text.Length - 4);
  135. }
  136. if (flag && text != "")
  137. {
  138. return $"({text})";
  139. }
  140. return "(1=2)";
  141. }
  142. private string GetProjectCodesByParentUnit(string unitCode)
  143. {
  144. string text = string.Empty;
  145. try
  146. {
  147. string format = "select unitcode from Unit u where exists(select * from project where projectcode=u.unitcode) or exists(select * from saproject where pid=u.unitcode) and FullCode like '%{0}%' and UnitType='项目'";
  148. format = string.Format(format, unitCode);
  149. var dt = Db.Ado.GetDataTable(format);
  150. foreach (DataRow row in dt.Rows)
  151. {
  152. text += string.Format("'{0}',", row["unitcode"].ToString().Replace("'", "''"));
  153. }
  154. if (text != string.Empty)
  155. {
  156. text = text.Remove(text.Length - 1);
  157. text = "'All',''," + text;
  158. }
  159. }
  160. catch (Exception exp)
  161. {
  162. text = string.Empty;
  163. logger.Error(exp);
  164. throw;
  165. }
  166. return text;
  167. }
  168. private string GetUnitCodesByUser(string UserCode)
  169. {
  170. string text = "";
  171. DataTable dataTable = new DataTable();
  172. dataTable.Columns.Add("UnitCode");
  173. string queryString = string.Format("select s.UnitCode from userrole ur, station s where ur.StationCode = s.StationCode and ur.UserCode = '{0}'", UserCode.Replace("'", "''"));
  174. var dt = Db.Ado.GetDataTable(queryString);
  175. foreach (DataRow row in dt.Rows)
  176. {
  177. EntityData unitAllChildAndSelf = GetUnitAllChildAndSelf(row["UnitCode"].ToString());
  178. foreach (DataRow row2 in unitAllChildAndSelf.CurrentTable.Rows)
  179. {
  180. if (dataTable.Select("UnitCode = '" + row2["UnitCode"].ToString() + "'").Length == 0)
  181. {
  182. DataRow dataRow3 = dataTable.NewRow();
  183. dataRow3["UnitCode"] = row2["UnitCode"].ToString();
  184. dataTable.Rows.Add(dataRow3);
  185. }
  186. }
  187. }
  188. foreach (DataRow row3 in dataTable.Rows)
  189. {
  190. if (text != "")
  191. {
  192. text += ",";
  193. }
  194. text = text + "'" + row3["UnitCode"].ToString() + "'";
  195. }
  196. return text;
  197. }
  198. public EntityData GetUnitAllChildAndSelf(string parentCode)
  199. {
  200. try
  201. {
  202. string text = "";
  203. EntityData unitByCode = GetUnitByCode(parentCode);
  204. text = ((!unitByCode.HasRecord()) ? "null" : unitByCode.GetString("FullCode"));
  205. unitByCode.Dispose();
  206. UnitStrategyBuilder unitStrategyBuilder = new UnitStrategyBuilder();
  207. unitStrategyBuilder.AddStrategy(new Strategy(UnitStrategyName.UnderUnitCode, text));
  208. string str = unitStrategyBuilder.BuildQueryChildCountString();
  209. str += unitStrategyBuilder.GetDefaultOrder();
  210. logger.Info($"【GetUnitAllChildAndSelf】sql:{str}");
  211. var result = Db.Ado.GetDataSetAll(str);
  212. return (EntityData)result;
  213. }
  214. catch (Exception ex)
  215. {
  216. throw ex;
  217. }
  218. }
  219. public EntityData GetUnitByCode(string code)
  220. {
  221. try
  222. {
  223. return SelectbyPrimaryKey(code, "Unit");
  224. }
  225. catch (Exception ex)
  226. {
  227. throw ex;
  228. }
  229. }
  230. public EntityData SelectbyPrimaryKey(object keyvalues, string entityName)
  231. {
  232. try
  233. {
  234. EntityData entitydata = null;
  235. SqlStruct sqlStruct = SqlManager.GetSqlStruct(entityName, "Select");
  236. if (sqlStruct.SqlString.Length != 0)
  237. {
  238. if (1 != sqlStruct.ParamsList.Length)
  239. throw new ApplicationException("参数列表不匹配");
  240. entitydata = (EntityData)Db.Ado.GetDataSetAll(sqlStruct.SqlString, sqlStruct.ParamsList);
  241. }
  242. return entitydata;
  243. }
  244. catch (Exception ex)
  245. {
  246. throw ex;
  247. }
  248. }
  249. public static string BuildStationCodeString(string stationCodes)
  250. {
  251. string text = "";
  252. string[] array = stationCodes.Split(',');
  253. foreach (string str in array)
  254. {
  255. if (text != "")
  256. {
  257. text += ",";
  258. }
  259. text = text + "'" + str + "'";
  260. }
  261. return text;
  262. }
  263. public string BuildContractAccessRangeString(string userCode, string systemgroupcodes)
  264. {
  265. return BuildAccessRangeString(userCode, systemgroupcodes, "Contract");
  266. }
  267. }
  268. }