BaseServices.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. using Autofac;
  7. using Bowin.Common;
  8. using EMIS.Entities;
  9. using System.Linq.Expressions;
  10. using System.Data.Entity;
  11. using EMIS.DataLogic.Common;
  12. using Bowin.Common.Linq.Entity;
  13. using EMIS.DataLogic.Common.AdministrativeOrgan;
  14. using EMIS.ViewModel;
  15. using EMIS.DataLogic.SystemDAL;
  16. using EMIS.Utility.FormValidate;
  17. using EMIS.Utility;
  18. namespace EMIS.CommonLogic
  19. {
  20. public class BaseServices : IBaseServices
  21. {
  22. public EMIS.DataLogic.UnitOfWork UnitOfWork { get; set; }
  23. public EMIS.DataLogic.HRUnitOfWork HRUnitOfWork { get; set; }
  24. public EMIS.DataLogic.LogUnitOfWork LogUnitOfWork { get; set; }
  25. private DataRangeDAL DataRangeDAL { get; set; }
  26. public BaseServices()
  27. {
  28. DataRangeDAL = AutofacHelper.RequestLifetimeScope.Resolve<DataRangeDAL>();
  29. }
  30. protected Expression<Func<T, bool>> GetDataRageExpression<T>(Expression<Func<T, Guid?>> userColumn)
  31. {
  32. ParameterExpression paramter;
  33. var userColumnExp = userColumn;
  34. paramter = userColumnExp.Parameters[0];
  35. SYS_DataRange dataRange;
  36. var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current;
  37. var curUserID = curUser.UserID;
  38. if (curUser.IsStudent)
  39. {
  40. dataRange = SYS_DataRange.Teacher;
  41. }
  42. var menuNo = HttpHelper.GetMenuNo();
  43. if (string.IsNullOrEmpty(menuNo))
  44. {
  45. menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU");
  46. }
  47. dataRange = DataRangeDAL.GetRoleDataRange(curUser.RoleID, menuNo);
  48. IQueryable<Guid?> userIDQueryable;
  49. switch (dataRange)
  50. {
  51. case SYS_DataRange.All:
  52. default:
  53. return (x => true);
  54. case SYS_DataRange.Campus:
  55. userIDQueryable = UnitOfWork.CF_StaffManageCampus.Where(x => x.UserID == curUserID)
  56. .SelectMany(x => x.CF_Campus.CF_College).SelectMany(x => x.CF_Staff).Select(x => (Guid?)x.UserID);
  57. break;
  58. case SYS_DataRange.College:
  59. userIDQueryable = UnitOfWork.CF_StaffManageCollege.Where(x => x.UserID == curUserID)
  60. .SelectMany(x => x.CF_College.CF_Staff).Select(x => (Guid?)x.UserID);
  61. break;
  62. case SYS_DataRange.Department:
  63. userIDQueryable = UnitOfWork.CF_StaffManageDepartment.Where(x => x.UserID == curUserID)
  64. .SelectMany(x => x.CF_Department.CF_Staff).Select(x => (Guid?)x.UserID);
  65. break;
  66. case SYS_DataRange.Assistant:
  67. return (x => false);
  68. case SYS_DataRange.Teacher:
  69. return Expression.Lambda<Func<T, bool>>(Expression.Equal(userColumnExp.Body, Expression.Constant(curUserID, typeof(Guid?))), paramter);
  70. }
  71. return Expression.Lambda<Func<T, bool>>(Expression.Call(typeof(Queryable), "Contains", new Type[] { typeof(Guid?) }, userIDQueryable.Expression, userColumnExp.Body), paramter);
  72. }
  73. protected Expression<Func<T, bool>> GetDataRageExpressionByCollege<T>(Expression<Func<T, Guid?>> collegeColumn)
  74. {
  75. ParameterExpression paramter;
  76. var collegeColumnExp = collegeColumn;
  77. paramter = collegeColumnExp.Parameters[0];
  78. SYS_DataRange dataRange;
  79. var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current;
  80. var curUserID = curUser.UserID;
  81. if (curUser.IsStudent)
  82. {
  83. dataRange = SYS_DataRange.Teacher;
  84. }
  85. var menuNo = HttpHelper.GetMenuNo();
  86. if (string.IsNullOrEmpty(menuNo))
  87. {
  88. menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU");
  89. }
  90. dataRange = DataRangeDAL.GetRoleDataRange(curUser.RoleID, menuNo);
  91. IQueryable<Guid?> collegeIDQueryable;
  92. switch (dataRange)
  93. {
  94. case SYS_DataRange.All:
  95. default:
  96. return (x => true);
  97. case SYS_DataRange.Campus:
  98. collegeIDQueryable = UnitOfWork.CF_StaffManageCampus.Where(x => x.UserID == curUserID)
  99. .SelectMany(x => x.CF_Campus.CF_College).Select(x => (Guid?)x.CollegeID);
  100. break;
  101. case SYS_DataRange.College:
  102. collegeIDQueryable = UnitOfWork.CF_StaffManageCollege.Where(x => x.UserID == curUserID)
  103. .Select(x => (Guid?)x.CollegeID);
  104. break;
  105. case SYS_DataRange.Department:
  106. collegeIDQueryable = UnitOfWork.CF_StaffManageDepartment.Where(x => x.UserID == curUserID)
  107. .Select(x => (Guid?)x.CF_Department.CollegeID);
  108. break;
  109. case SYS_DataRange.Assistant:
  110. return (x => false);
  111. case SYS_DataRange.Teacher:
  112. return Expression.Lambda<Func<T, bool>>(Expression.Equal(collegeColumnExp.Body, Expression.Constant(curUser.CollegeID, typeof(Guid?))), paramter);
  113. }
  114. return Expression.Lambda<Func<T, bool>>(Expression.Call(typeof(Queryable), "Contains", new Type[] { typeof(Guid?) }, collegeIDQueryable.Expression, collegeColumnExp.Body), paramter);
  115. }
  116. protected IQueryable<T> GetQueryByAssistant<T>(IQueryable<T> query, Expression<Func<T, Guid?>> classmajorColumn = null, IQueryable<T> defaultQuery = null)
  117. {
  118. ExpressionFactory<T> factory = new ExpressionFactory<T>(query);
  119. var outerExp = classmajorColumn;
  120. if (classmajorColumn == null)
  121. {
  122. ParameterExpression paramter = Expression.Parameter(typeof(T));
  123. outerExp = Expression.Lambda<Func<T, Guid?>>(Expression.Convert(Expression.Property(paramter, "ClassmajorID"), typeof(Guid?)), paramter);
  124. }
  125. var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current;
  126. var curUserID = curUser.UserID;
  127. if (curUser.IsStudent)
  128. {
  129. return query;
  130. }
  131. var menuNo = HttpHelper.GetMenuNo();
  132. if (string.IsNullOrEmpty(menuNo))
  133. {
  134. menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU");
  135. }
  136. SYS_DataRange dataRange = DataRangeDAL.GetRoleDataRange(curUser.RoleID, menuNo);
  137. if (dataRange == SYS_DataRange.Assistant)
  138. {
  139. var queryWithClassmajor = query.Join(
  140. UnitOfWork.CF_Classmajor.AsQueryable().Where(x => x.UserID == curUserID),
  141. outerExp,
  142. (x => x.ClassmajorID),
  143. ((x, y) => x));
  144. return queryWithClassmajor;
  145. }
  146. else
  147. {
  148. if (defaultQuery != null)
  149. {
  150. return defaultQuery;
  151. }
  152. else
  153. {
  154. return query;
  155. }
  156. }
  157. }
  158. protected IQueryable<T> GetQueryByDataRange<T>(IQueryable<T> query, Expression<Func<T, Guid?>> userColumn = null)
  159. {
  160. ExpressionFactory<T> factory = new ExpressionFactory<T>(query);
  161. var outerExp = userColumn;
  162. if (userColumn == null)
  163. {
  164. ParameterExpression paramter = Expression.Parameter(typeof(T));
  165. outerExp = Expression.Lambda<Func<T, Guid?>>(Expression.Property(paramter, "CreateUserID"), paramter);
  166. }
  167. var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current;
  168. var curUserID = curUser.UserID;
  169. if (curUser.IsStudent)
  170. {
  171. return query;
  172. }
  173. var menuNo = HttpHelper.GetMenuNo();
  174. if (string.IsNullOrEmpty(menuNo))
  175. {
  176. menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU");
  177. }
  178. SYS_DataRange dataRange = DataRangeDAL.GetRoleDataRange(curUser.RoleID, menuNo);
  179. var queryWithUserAndDepartment = query
  180. .Join(UnitOfWork.CF_Staff.AsQueryable(), outerExp, (x => x.UserID), ((x, y) => new { result = x, user = y }));
  181. switch (dataRange)
  182. {
  183. case SYS_DataRange.All:
  184. default:
  185. return query;
  186. case SYS_DataRange.Campus:
  187. return query
  188. .Join(
  189. UnitOfWork.CF_Campus.SelectMany(x => x.CF_StaffManageCampus)
  190. .Where(x => x.UserID == curUser.UserID)
  191. .SelectMany(x => x.CF_Campus.CF_College.SelectMany(w => w.CF_Staff)),
  192. outerExp, (x => x.UserID), ((x, y) => x));
  193. case SYS_DataRange.College:
  194. return query
  195. .Join(
  196. UnitOfWork.CF_College.SelectMany(x => x.CF_StaffManageCollege)
  197. .Where(x => x.UserID == curUser.UserID)
  198. .SelectMany(x => x.CF_College.CF_Staff),
  199. outerExp, (x => x.UserID), ((x, y) => x));
  200. case SYS_DataRange.Department:
  201. return query
  202. .Join(
  203. UnitOfWork.CF_Department.SelectMany(x => x.CF_StaffManageDepartment)
  204. .Where(x => x.UserID == curUser.UserID)
  205. .SelectMany(x => x.CF_Department.CF_Staff),
  206. outerExp, (x => x.UserID), ((x, y) => x));
  207. case SYS_DataRange.Assistant:
  208. return query.Where(x => false);
  209. case SYS_DataRange.Teacher:
  210. return query
  211. .Join(
  212. UnitOfWork.CF_Staff.Where(x => x.UserID == curUser.UserID),
  213. outerExp, (x => x.UserID), ((x, y) => x));
  214. }
  215. }
  216. protected IQueryable<T> GetQueryByDataRangeByCampus<T>(IQueryable<T> query, Expression<Func<T, Guid?>> campusColumn = null)
  217. {
  218. ExpressionFactory<T> factory = new ExpressionFactory<T>(query);
  219. var outerExp = campusColumn;
  220. if (campusColumn == null)
  221. {
  222. ParameterExpression paramter = Expression.Parameter(typeof(T));
  223. outerExp = Expression.Lambda<Func<T, Guid?>>(Expression.Convert(Expression.Property(paramter, "CampusID"), typeof(Guid?)), paramter);
  224. }
  225. var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current;
  226. var curUserID = curUser.UserID;
  227. if (curUser.IsStudent)
  228. {
  229. return query;
  230. }
  231. var menuNo = HttpHelper.GetMenuNo();
  232. if (string.IsNullOrEmpty(menuNo))
  233. {
  234. menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU");
  235. }
  236. SYS_DataRange dataRange = DataRangeDAL.GetRoleDataRange(curUser.RoleID, menuNo);
  237. var queryWithCampus = query
  238. .Join(UnitOfWork.CF_Campus.AsQueryable(), outerExp, (x => x.CampusID), ((x, y) => new { result = x, campus = y }));
  239. switch (dataRange)
  240. {
  241. case SYS_DataRange.All:
  242. default:
  243. return query;
  244. case SYS_DataRange.Campus:
  245. return queryWithCampus
  246. .Where(x => x.campus.CF_StaffManageCampus.Any(w => w.UserID == curUserID))
  247. .Select(x => x.result);
  248. case SYS_DataRange.College:
  249. return queryWithCampus
  250. .Where(x => x.campus.CF_College.SelectMany(w => w.CF_StaffManageCollege).Any(w => w.UserID == curUserID))
  251. .Select(x => x.result);
  252. case SYS_DataRange.Department:
  253. return queryWithCampus
  254. .Where(x => x.campus.CF_College.SelectMany(w => w.CF_Department).SelectMany(w => w.CF_StaffManageDepartment)
  255. .Any(w => w.UserID == curUserID))
  256. .Select(x => x.result);
  257. case SYS_DataRange.Assistant:
  258. return query.Where(x => false);
  259. case SYS_DataRange.Teacher:
  260. return queryWithCampus
  261. .Where(x => x.campus.CF_College.SelectMany(w => w.CF_Staff).Any(w => w.UserID == curUserID))
  262. .Select(x => x.result);
  263. }
  264. }
  265. protected IQueryable<T> GetQueryByDataRangeByCollege<T>(IQueryable<T> query, Expression<Func<T, Guid?>> collegeColumn = null)
  266. {
  267. ExpressionFactory<T> factory = new ExpressionFactory<T>(query);
  268. var outerExp = collegeColumn;
  269. if (collegeColumn == null)
  270. {
  271. ParameterExpression paramter = Expression.Parameter(typeof(T));
  272. outerExp = Expression.Lambda<Func<T, Guid?>>(Expression.Convert(Expression.Property(paramter, "CollegeID"), typeof(Guid?)), paramter);
  273. }
  274. var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current;
  275. var curUserID = curUser.UserID;
  276. if (curUser.IsStudent)
  277. {
  278. return query;
  279. }
  280. var menuNo = HttpHelper.GetMenuNo();
  281. if (string.IsNullOrEmpty(menuNo))
  282. {
  283. menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU");
  284. }
  285. SYS_DataRange dataRange = DataRangeDAL.GetRoleDataRange(curUser.RoleID, menuNo);
  286. var queryWithCollege = query
  287. .Join(UnitOfWork.CF_College.AsQueryable(), outerExp, (x => x.CollegeID), ((x, y) => new { result = x, college = y }));
  288. switch (dataRange)
  289. {
  290. case SYS_DataRange.All:
  291. default:
  292. return query;
  293. case SYS_DataRange.Campus:
  294. return queryWithCollege
  295. .Where(x => x.college.CF_Campus.CF_StaffManageCampus.Any(w => w.UserID == curUserID))
  296. .Select(x => x.result);
  297. case SYS_DataRange.College:
  298. return queryWithCollege
  299. .Where(x => x.college.CF_StaffManageCollege.Any(w => w.UserID == curUserID))
  300. .Select(x => x.result);
  301. case SYS_DataRange.Department:
  302. return queryWithCollege
  303. .Where(x => x.college.CF_Department.SelectMany(w => w.CF_StaffManageDepartment).Any(w => w.UserID == curUserID))
  304. .Select(x => x.result);
  305. case SYS_DataRange.Assistant:
  306. return query.Where(x => false);
  307. case SYS_DataRange.Teacher:
  308. return queryWithCollege
  309. .Where(x => x.college.CF_Staff.Any(w => w.UserID == curUserID))
  310. .Select(x => x.result);
  311. }
  312. }
  313. protected IQueryable<T> GetQueryByDataRangeByDepartment<T>(IQueryable<T> query, Expression<Func<T, Guid?>> departmentColumn = null)
  314. {
  315. ExpressionFactory<T> factory = new ExpressionFactory<T>(query);
  316. var outerExp = departmentColumn;
  317. if (departmentColumn == null)
  318. {
  319. ParameterExpression paramter = Expression.Parameter(typeof(T));
  320. outerExp = Expression.Lambda<Func<T, Guid?>>(Expression.Convert(Expression.Property(paramter, "DepartmentID"), typeof(Guid?)), paramter);
  321. }
  322. var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current;
  323. var curUserID = curUser.UserID;
  324. if (curUser.IsStudent)
  325. {
  326. return query;
  327. }
  328. var menuNo = HttpHelper.GetMenuNo();
  329. if (string.IsNullOrEmpty(menuNo))
  330. {
  331. menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU");
  332. }
  333. SYS_DataRange dataRange = DataRangeDAL.GetRoleDataRange(curUser.RoleID, menuNo);
  334. var queryWithDepartment = query
  335. .Join(UnitOfWork.CF_Department.AsQueryable(), outerExp, (x => x.DepartmentID), ((x, y) => new { result = x, department = y }));
  336. switch (dataRange)
  337. {
  338. case SYS_DataRange.All:
  339. default:
  340. return query;
  341. case SYS_DataRange.Campus:
  342. return queryWithDepartment
  343. .Where(x => x.department.CF_College.CF_Campus.CF_StaffManageCampus.Any(w => w.UserID == curUserID))
  344. .Select(x => x.result);
  345. case SYS_DataRange.College:
  346. return queryWithDepartment
  347. .Where(x => x.department.CF_College.CF_StaffManageCollege.Any(w => w.UserID == curUserID))
  348. .Select(x => x.result);
  349. case SYS_DataRange.Department:
  350. return queryWithDepartment
  351. .Where(x => x.department.CF_StaffManageDepartment.Any(w => w.UserID == curUserID))
  352. .Select(x => x.result);
  353. case SYS_DataRange.Assistant:
  354. return query.Where(x => false);
  355. case SYS_DataRange.Teacher:
  356. return queryWithDepartment
  357. .Where(x => x.department.CF_Staff.Any(w => w.UserID == curUserID))
  358. .Select(x => x.result);
  359. }
  360. }
  361. protected bool IsUserInDataRange<T>(List<Guid> formIDList, Guid userID, Expression<Func<T, Guid?>> userColumn = null) where T : class
  362. {
  363. var outerExp = userColumn;
  364. ParameterExpression paramter = Expression.Parameter(typeof(T));
  365. if (userColumn == null)
  366. {
  367. outerExp = Expression.Lambda<Func<T, Guid?>>(Expression.Convert(Expression.Property(paramter, "CreateUserID"), typeof(Guid?)), paramter);
  368. }
  369. var containMethodInfo = formIDList.GetType().GetMethod("Contains");
  370. var keyExp = Expression.Lambda<Func<T, bool>>(
  371. Expression.Call(Expression.Constant(formIDList), containMethodInfo, Expression.Property(paramter, TableKeyDictionary.GetKeyName<T>())),
  372. paramter
  373. );
  374. var menuNo = HttpHelper.GetMenuNo();
  375. if (string.IsNullOrEmpty(menuNo))
  376. {
  377. menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU");
  378. }
  379. SYS_DataRange dataRange = DataRangeDAL.GetUserDataRange(userID, menuNo);
  380. switch (dataRange)
  381. {
  382. case SYS_DataRange.All:
  383. default:
  384. return true;
  385. case SYS_DataRange.Campus:
  386. return UnitOfWork.Set<T>().Where(keyExp)
  387. .Join(
  388. UnitOfWork.CF_Campus.SelectMany(x => x.CF_StaffManageCampus).Select(x => x.CF_Staff)
  389. .Where(x => x.UserID == userID),
  390. outerExp, (x => x.UserID), ((x, y) => x))
  391. .Count() == formIDList.Count;
  392. case SYS_DataRange.College:
  393. return UnitOfWork.Set<T>().Where(keyExp)
  394. .Join(
  395. UnitOfWork.CF_College.SelectMany(x => x.CF_StaffManageCollege).Select(x => x.CF_Staff)
  396. .Where(x => x.UserID == userID),
  397. outerExp, (x => x.UserID), ((x, y) => x))
  398. .Count() == formIDList.Count;
  399. case SYS_DataRange.Department:
  400. return UnitOfWork.Set<T>().Where(keyExp)
  401. .Join(
  402. UnitOfWork.CF_Department.SelectMany(x => x.CF_StaffManageDepartment).Select(x => x.CF_Staff)
  403. .Where(x => x.UserID == userID),
  404. outerExp, (x => x.UserID), ((x, y) => x))
  405. .Count() == formIDList.Count;
  406. case SYS_DataRange.Assistant:
  407. return false;
  408. case SYS_DataRange.Teacher:
  409. return UnitOfWork.Set<T>().Where(keyExp)
  410. .Join(
  411. UnitOfWork.CF_Staff.Where(x => x.UserID == userID),
  412. outerExp, (x => x.UserID), ((x, y) => x))
  413. .Count() == formIDList.Count;
  414. }
  415. }
  416. protected bool IsUserInDataRangeByDepartment<T>(List<Guid> formIDList, Guid userID, Expression<Func<T, Guid?>> departmentColumn = null) where T : class
  417. {
  418. var outerExp = departmentColumn;
  419. ParameterExpression paramter = Expression.Parameter(typeof(T));
  420. if (departmentColumn == null)
  421. {
  422. outerExp = Expression.Lambda<Func<T, Guid?>>(Expression.Convert(Expression.Property(paramter, "DepartmentID"), typeof(Guid?)), paramter);
  423. }
  424. var containMethodInfo = formIDList.GetType().GetMethod("Contains");
  425. var keyExp = Expression.Lambda<Func<T, bool>>(
  426. Expression.Call(Expression.Constant(formIDList), containMethodInfo, Expression.Property(paramter, TableKeyDictionary.GetKeyName<T>())),
  427. paramter
  428. );
  429. var menuNo = HttpHelper.GetMenuNo();
  430. if (string.IsNullOrEmpty(menuNo))
  431. {
  432. menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU");
  433. }
  434. SYS_DataRange dataRange = DataRangeDAL.GetUserDataRange(userID, menuNo);
  435. var queryWithDepartment = UnitOfWork.Set<T>().Where(keyExp)
  436. .Join(UnitOfWork.CF_Department.AsQueryable(), outerExp, (x => x.DepartmentID), ((x, y) => new { result = x, department = y }));
  437. switch (dataRange)
  438. {
  439. case SYS_DataRange.All:
  440. default:
  441. return true;
  442. case SYS_DataRange.Campus:
  443. return queryWithDepartment
  444. .Where(x => x.department.CF_College.CF_Campus.CF_StaffManageCampus.Any(w => w.UserID == userID))
  445. .Count() == formIDList.Count;
  446. case SYS_DataRange.College:
  447. return queryWithDepartment
  448. .Where(x => x.department.CF_College.CF_StaffManageCollege.Any(w => w.UserID == userID))
  449. .Count() == formIDList.Count;
  450. case SYS_DataRange.Department:
  451. return queryWithDepartment
  452. .Where(x => x.department.CF_StaffManageDepartment.Any(w => w.UserID == userID))
  453. .Count() == formIDList.Count;
  454. case SYS_DataRange.Assistant:
  455. return false;
  456. case SYS_DataRange.Teacher:
  457. return queryWithDepartment
  458. .Where(x => x.department.CF_Staff.Any(w => w.UserID == userID))
  459. .Count() == formIDList.Count;
  460. }
  461. }
  462. protected bool IsUserInDataRangeByCollege<T>(List<Guid> formIDList, Guid userID, Expression<Func<T, Guid?>> collegeColumn = null) where T : class
  463. {
  464. var outerExp = collegeColumn;
  465. ParameterExpression paramter = Expression.Parameter(typeof(T));
  466. if (collegeColumn == null)
  467. {
  468. outerExp = Expression.Lambda<Func<T, Guid?>>(Expression.Convert(Expression.Property(paramter, "CollegeID"), typeof(Guid?)), paramter);
  469. }
  470. var containMethodInfo = formIDList.GetType().GetMethod("Contains");
  471. var keyExp = Expression.Lambda<Func<T, bool>>(
  472. Expression.Call(Expression.Constant(formIDList), containMethodInfo, Expression.Property(paramter, TableKeyDictionary.GetKeyName<T>())),
  473. paramter
  474. );
  475. var menuNo = HttpHelper.GetMenuNo();
  476. if (string.IsNullOrEmpty(menuNo))
  477. {
  478. menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU");
  479. }
  480. SYS_DataRange dataRange = DataRangeDAL.GetUserDataRange(userID, menuNo);
  481. var queryWithCollege = UnitOfWork.Set<T>().Where(keyExp)
  482. .Join(UnitOfWork.CF_College.AsQueryable(), outerExp, (x => x.CollegeID), ((x, y) => new { result = x, college = y }));
  483. switch (dataRange)
  484. {
  485. case SYS_DataRange.All:
  486. default:
  487. return true;
  488. case SYS_DataRange.Campus:
  489. return queryWithCollege
  490. .Where(x => x.college.CF_Campus.CF_StaffManageCampus.Any(w => w.UserID == userID))
  491. .Count() == formIDList.Count;
  492. case SYS_DataRange.College:
  493. return queryWithCollege
  494. .Where(x => x.college.CF_StaffManageCollege.Any(w => w.UserID == userID))
  495. .Count() == formIDList.Count;
  496. case SYS_DataRange.Department:
  497. return queryWithCollege
  498. .Where(x => x.college.CF_Department.SelectMany(w => w.CF_StaffManageDepartment).Any(w => w.UserID == userID))
  499. .Count() == formIDList.Count;
  500. case SYS_DataRange.Assistant:
  501. return false;
  502. case SYS_DataRange.Teacher:
  503. return queryWithCollege
  504. .Where(x => x.college.CF_Staff.Any(w => w.UserID == userID))
  505. .Count() == formIDList.Count;
  506. }
  507. }
  508. protected bool IsUserInDataRangeByCampusID<T>(List<Guid> formIDList, Guid userID, Expression<Func<T, Guid?>> campusColumn = null) where T : class
  509. {
  510. var outerExp = campusColumn;
  511. ParameterExpression paramter = Expression.Parameter(typeof(T));
  512. if (campusColumn == null)
  513. {
  514. outerExp = Expression.Lambda<Func<T, Guid?>>(Expression.Convert(Expression.Property(paramter, "CampusID"), typeof(Guid?)), paramter);
  515. }
  516. var containMethodInfo = formIDList.GetType().GetMethod("Contains");
  517. var keyExp = Expression.Lambda<Func<T, bool>>(
  518. Expression.Call(Expression.Constant(formIDList), containMethodInfo, Expression.Property(paramter, TableKeyDictionary.GetKeyName<T>())),
  519. paramter
  520. );
  521. var menuNo = HttpHelper.GetMenuNo();
  522. if (string.IsNullOrEmpty(menuNo))
  523. {
  524. menuNo = HttpContext.Current.Request.UrlReferrer.GetQueryStringByPath("FromMNU");
  525. }
  526. SYS_DataRange dataRange = DataRangeDAL.GetUserDataRange(userID, menuNo);
  527. var queryWithCampus = UnitOfWork.Set<T>().Where(keyExp)
  528. .Join(UnitOfWork.CF_Campus.AsQueryable(), outerExp, (x => x.CampusID), ((x, y) => new { result = x, campus = y }));
  529. switch (dataRange)
  530. {
  531. case SYS_DataRange.All:
  532. default:
  533. return true;
  534. case SYS_DataRange.Campus:
  535. return queryWithCampus
  536. .Where(x => x.campus.CF_StaffManageCampus.Any(w => w.UserID == userID))
  537. .Count() == formIDList.Count;
  538. case SYS_DataRange.College:
  539. return queryWithCampus
  540. .Where(x => x.campus.CF_College.SelectMany(w => w.CF_StaffManageCollege).Any(w => w.UserID == userID))
  541. .Count() == formIDList.Count;
  542. case SYS_DataRange.Department:
  543. return queryWithCampus
  544. .Where(x => x.campus.CF_College.SelectMany(w => w.CF_Department).SelectMany(w => w.CF_StaffManageDepartment).Any(w => w.UserID == userID))
  545. .Count() == formIDList.Count;
  546. case SYS_DataRange.Assistant:
  547. return false;
  548. case SYS_DataRange.Teacher:
  549. return queryWithCampus
  550. .Where(x => x.campus.CF_College.SelectMany(w => w.CF_Staff).Any(w => w.UserID == userID))
  551. .Count() == formIDList.Count;
  552. }
  553. }
  554. public void SetNewStatus<T>(T entity)
  555. {
  556. var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current;
  557. var curUserID = curUser.UserID;
  558. //typeof(T).GetProperty(TableKeyDictionary.GetKeyName(entity))
  559. // .SetValue(entity, Guid.NewGuid(), null);
  560. dynamic dyEntity = entity;
  561. dyEntity.RecordStatus = (int)SYS_STATUS.USABLE;
  562. dyEntity.CreateUserID = curUserID;
  563. dyEntity.ModifyUserID = curUserID;
  564. dyEntity.ModifyTime = dyEntity.CreateTime = DateTime.Now;
  565. }
  566. public void SetNewStatus<T>(T entity, int recordStatus)
  567. {
  568. var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current;
  569. Guid? curUserID = null;
  570. if (curUser != null)
  571. {
  572. curUserID = curUser.UserID;
  573. }
  574. //typeof(T).GetProperty(TableKeyDictionary.GetKeyName(entity))
  575. // .SetValue(entity, Guid.NewGuid(), null);
  576. dynamic dyEntity = entity;
  577. dyEntity.RecordStatus = recordStatus;
  578. dyEntity.CreateUserID = curUserID;
  579. dyEntity.ModifyUserID = curUserID;
  580. dyEntity.ModifyTime = dyEntity.CreateTime = DateTime.Now;
  581. }
  582. public void SetModifyStatus<T>(T entity)
  583. {
  584. var curUser = EMIS.Utility.FormValidate.CustomPrincipal.Current;
  585. Guid? curUserID = null;
  586. if (curUser != null)
  587. {
  588. curUserID = curUser.UserID;
  589. }
  590. dynamic dyEntity = entity;
  591. dyEntity.ModifyUserID = curUserID;
  592. dyEntity.ModifyTime = DateTime.Now;
  593. }
  594. }
  595. }