BaseServices.cs 33 KB

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