DataRangeDAL.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using EMIS.DataLogic.Repositories;
  6. using EMIS.ViewModel;
  7. using EMIS.ViewModel.SystemView;
  8. using System.Linq.Expressions;
  9. using EMIS.Entities;
  10. namespace EMIS.DataLogic.SystemDAL
  11. {
  12. public class DataRangeDAL
  13. {
  14. public UserRepository UserRepository { get; set; }
  15. public RoleRepository RoleRepository { get; set; }
  16. public MenuRepository MenuRepository { get; set; }
  17. public ClassmajorRepository ClassmajorRepository { get; set; }
  18. public StaffManageCampusRepository StaffManageCampusRepository { get; set; }
  19. public StaffManageCollegeRepository StaffManageCollegeRepository { get; set; }
  20. public RoleDataRangeRepository RoleDataRangeRepository { get; set; }
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. /// <param name="roleID"></param>
  25. /// <param name="menuNo"></param>
  26. /// <returns></returns>
  27. public SYS_DataRange GetRoleDataRange(Guid? roleID, string menuNo)
  28. {
  29. var query = (from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE && (roleID == null || x.RoleID == roleID))
  30. where rdr.MenuNo == menuNo
  31. select new
  32. {
  33. DataRange = (SYS_DataRange)rdr.DataRangeID,
  34. Order = 0
  35. }).Concat(
  36. from r in RoleRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  37. where r.RoleID == roleID
  38. select new {
  39. DataRange = (SYS_DataRange)(r.DefaultDataRange ?? (int)SYS_DataRange.All),
  40. Order = 1
  41. }).OrderBy(x => x.Order).ThenBy(x => x.DataRange);
  42. var result = query.FirstOrDefault();
  43. if (result == null)
  44. {
  45. return SYS_DataRange.Department;
  46. }
  47. else
  48. {
  49. return result.DataRange;
  50. }
  51. }
  52. /// <summary>
  53. ///
  54. /// </summary>
  55. /// <param name="userID"></param>
  56. /// <param name="menuNo"></param>
  57. /// <returns></returns>
  58. public SYS_DataRange GetUserDataRange(Guid? userID, string menuNo)
  59. {
  60. var roleQuery = (from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  61. where rdr.MenuNo == menuNo
  62. select new
  63. {
  64. rdr.RoleID,
  65. DataRange = (SYS_DataRange)rdr.DataRangeID,
  66. Order = 0
  67. }).Concat(
  68. from r in RoleRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  69. select new
  70. {
  71. RoleID = (Guid?)r.RoleID,
  72. DataRange = (SYS_DataRange)(r.DefaultDataRange ?? (int)SYS_DataRange.All),
  73. Order = 1
  74. })
  75. .GroupBy(x => x.RoleID)
  76. .Select(x => x.OrderBy(w => w.Order).ThenBy(w => w.DataRange).FirstOrDefault());
  77. var query = (from u in UserRepository.GetList(x => x.UserID == userID)
  78. from r in u.Sys_Role
  79. join rdr in roleQuery on r.RoleID equals rdr.RoleID
  80. orderby rdr.DataRange
  81. select rdr
  82. );
  83. var result = query.FirstOrDefault();
  84. if (result == null)
  85. {
  86. return SYS_DataRange.Department;
  87. }
  88. else
  89. {
  90. return result.DataRange;
  91. }
  92. }
  93. /// <summary>
  94. ///
  95. /// </summary>
  96. /// <param name="exp"></param>
  97. /// <param name="roleExp"></param>
  98. /// <param name="menuExp"></param>
  99. /// <returns></returns>
  100. public IQueryable<RoleDataRangeView> GetRoleDataRange(Expression<Func<Sys_RoleDataRange, bool>> exp,
  101. Expression<Func<Sys_Role, bool>> roleExp = null, Expression<Func<Sys_Menu, bool>> menuExp = null)
  102. {
  103. if (roleExp == null) roleExp = (x => true);
  104. if (menuExp == null) menuExp = (x => true);
  105. var q = (from m in MenuRepository.GetList(menuExp)
  106. join rdr in
  107. (
  108. from rdr in RoleDataRangeRepository.GetList(exp)
  109. join r in RoleRepository.GetList(roleExp) on rdr.RoleID equals r.RoleID
  110. select new { range = rdr, role = r }
  111. ) on m.MenuNo equals rdr.range.MenuNo into drdr
  112. from erdr in drdr.DefaultIfEmpty()
  113. select new RoleDataRangeView {
  114. MenuNo = m.MenuNo,
  115. MenuName = m.MenuName,
  116. ParentMenuNo = m.ParentMenuNo,
  117. OrderNo = m.OrderNo,
  118. RoleID = (erdr == null) ? null : (Guid?)erdr.role.RoleID,
  119. RoleDataRangeID = (erdr == null) ? null : (Guid?)erdr.range.RoleDataRangeID,
  120. DataRangeID = (erdr == null) ? null : (int?)erdr.range.DataRangeID,
  121. IsLeaf = m.IsLeaf ?? false
  122. });
  123. return q;
  124. }
  125. /// <summary>
  126. ///
  127. /// </summary>
  128. /// <param name="departmentID"></param>
  129. /// <param name="menuNo"></param>
  130. /// <returns></returns>
  131. public IQueryable<Sys_User> GetDataRangeUserByDepartmentID(Guid departmentID, string menuNo)
  132. {
  133. var q = (from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  134. from u in rdr.Sys_Role.Sys_User
  135. from rd in u.CF_Staff.CF_StaffManageDepartment
  136. where rdr.DataRangeID == (int)SYS_DataRange.Department
  137. && rdr.MenuNo == menuNo && rd.DepartmentID == departmentID
  138. select u).Concat(
  139. from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  140. from u in rdr.Sys_Role.Sys_User
  141. from c in u.CF_Staff.CF_StaffManageCollege
  142. from dl in c.CF_College.CF_Department
  143. where rdr.DataRangeID == (int)SYS_DataRange.College
  144. && rdr.MenuNo == menuNo && dl.DepartmentID == departmentID
  145. select u).Concat(
  146. from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  147. from u in rdr.Sys_Role.Sys_User
  148. from c in u.CF_Staff.CF_StaffManageCampus
  149. from cl in c.CF_Campus.CF_College
  150. from dl in cl.CF_Department
  151. where rdr.DataRangeID == (int)SYS_DataRange.Campus
  152. && rdr.MenuNo == menuNo && dl.DepartmentID == departmentID
  153. select u).Concat(
  154. from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  155. from u in rdr.Sys_Role.Sys_User
  156. where rdr.DataRangeID == (int)SYS_DataRange.All
  157. && rdr.MenuNo == menuNo
  158. select u).Concat(
  159. from u in UserRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  160. from r in u.Sys_Role
  161. from rdr in r.Sys_RoleDataRange.Where(x => x.MenuNo == menuNo).DefaultIfEmpty()
  162. from rd in u.CF_Staff.CF_StaffManageDepartment
  163. where r.DefaultDataRange == (int)SYS_DataRange.Department
  164. && rdr.RoleDataRangeID == null && rd.DepartmentID == departmentID
  165. select u).Concat(
  166. from u in UserRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  167. from r in u.Sys_Role
  168. from rdr in r.Sys_RoleDataRange.Where(x => x.MenuNo == menuNo).DefaultIfEmpty()
  169. from rc in u.CF_Staff.CF_StaffManageCollege
  170. from dl in rc.CF_College.CF_Department
  171. where r.DefaultDataRange == (int)SYS_DataRange.College
  172. && rdr.RoleDataRangeID == null && dl.DepartmentID == departmentID
  173. select u).Concat(
  174. from u in UserRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  175. from r in u.Sys_Role
  176. from rdr in r.Sys_RoleDataRange.Where(x => x.MenuNo == menuNo).DefaultIfEmpty()
  177. from c in u.CF_Staff.CF_StaffManageCampus
  178. from cl in c.CF_Campus.CF_College
  179. from dl in cl.CF_Department
  180. where r.DefaultDataRange == (int)SYS_DataRange.Campus
  181. && rdr.RoleDataRangeID == null && dl.DepartmentID == departmentID
  182. select u).Concat(
  183. from u in UserRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  184. from r in u.Sys_Role
  185. from rdr in r.Sys_RoleDataRange.Where(x => x.MenuNo == menuNo).DefaultIfEmpty()
  186. where r.DefaultDataRange == (int)SYS_DataRange.All
  187. && rdr.RoleDataRangeID == null
  188. select u).Distinct();
  189. return q;
  190. }
  191. /// <summary>
  192. ///
  193. /// </summary>
  194. /// <param name="collegeID"></param>
  195. /// <param name="menuNo"></param>
  196. /// <returns></returns>
  197. public IQueryable<Sys_User> GetDataRangeUserByCollegeID(Guid collegeID, string menuNo)
  198. {
  199. var q = (from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  200. from u in rdr.Sys_Role.Sys_User
  201. from rd in u.CF_Staff.CF_StaffManageDepartment
  202. where rdr.DataRangeID == (int)SYS_DataRange.Department
  203. && rdr.MenuNo == menuNo && rd.CF_Department.CollegeID == collegeID
  204. select u).Concat(
  205. from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  206. from u in rdr.Sys_Role.Sys_User
  207. from rc in u.CF_Staff.CF_StaffManageCollege
  208. where rdr.DataRangeID == (int)SYS_DataRange.College
  209. && rdr.MenuNo == menuNo && rc.CollegeID == collegeID
  210. select u).Concat(
  211. from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  212. from u in rdr.Sys_Role.Sys_User
  213. from c in u.CF_Staff.CF_StaffManageCampus
  214. from cl in c.CF_Campus.CF_College
  215. where rdr.DataRangeID == (int)SYS_DataRange.Campus
  216. && rdr.MenuNo == menuNo && cl.CollegeID == collegeID
  217. select u).Concat(
  218. from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  219. from u in rdr.Sys_Role.Sys_User
  220. where rdr.DataRangeID == (int)SYS_DataRange.All
  221. && rdr.MenuNo == menuNo
  222. select u).Concat(
  223. from u in UserRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  224. from r in u.Sys_Role
  225. from rdr in r.Sys_RoleDataRange.Where(x => x.MenuNo == menuNo).DefaultIfEmpty()
  226. from rd in u.CF_Staff.CF_StaffManageDepartment
  227. where r.DefaultDataRange == (int)SYS_DataRange.Department
  228. && rdr.RoleDataRangeID == null && rd.CF_Department.CollegeID == collegeID
  229. select u).Concat(
  230. from u in UserRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  231. from r in u.Sys_Role
  232. from rdr in r.Sys_RoleDataRange.Where(x => x.MenuNo == menuNo).DefaultIfEmpty()
  233. from rc in u.CF_Staff.CF_StaffManageCollege
  234. where r.DefaultDataRange == (int)SYS_DataRange.College
  235. && rdr.RoleDataRangeID == null && rc.CollegeID == collegeID
  236. select u).Concat(
  237. from u in UserRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  238. from r in u.Sys_Role
  239. from rdr in r.Sys_RoleDataRange.Where(x => x.MenuNo == menuNo).DefaultIfEmpty()
  240. from c in u.CF_Staff.CF_StaffManageCampus
  241. from cl in c.CF_Campus.CF_College
  242. where r.DefaultDataRange == (int)SYS_DataRange.Campus
  243. && rdr.RoleDataRangeID == null && cl.CollegeID == collegeID
  244. select u).Concat(
  245. from u in UserRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  246. from r in u.Sys_Role
  247. from rdr in r.Sys_RoleDataRange.Where(x => x.MenuNo == menuNo).DefaultIfEmpty()
  248. where r.DefaultDataRange == (int)SYS_DataRange.All
  249. && rdr.RoleDataRangeID == null
  250. select u).Distinct();
  251. return q;
  252. }
  253. /// <summary>
  254. ///
  255. /// </summary>
  256. /// <param name="campusID"></param>
  257. /// <param name="menuNo"></param>
  258. /// <returns></returns>
  259. public IQueryable<Sys_User> GetDataRangeUserByCampusID(Guid campusID, string menuNo)
  260. {
  261. var q = (from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  262. from u in rdr.Sys_Role.Sys_User
  263. from rd in u.CF_Staff.CF_StaffManageDepartment
  264. where rdr.DataRangeID == (int)SYS_DataRange.Department
  265. && rdr.MenuNo == menuNo && rd.CF_Department.CF_College.CampusID == campusID
  266. select u).Concat(
  267. from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  268. from u in rdr.Sys_Role.Sys_User
  269. from rc in u.CF_Staff.CF_StaffManageCollege
  270. where rdr.DataRangeID == (int)SYS_DataRange.College
  271. && rdr.MenuNo == menuNo && rc.CF_College.CampusID == campusID
  272. select u).Concat(
  273. from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  274. from u in rdr.Sys_Role.Sys_User
  275. from c in u.CF_Staff.CF_StaffManageCampus
  276. where rdr.DataRangeID == (int)SYS_DataRange.Campus
  277. && rdr.MenuNo == menuNo && c.CampusID == campusID
  278. select u).Concat(
  279. from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  280. from u in rdr.Sys_Role.Sys_User
  281. where rdr.DataRangeID == (int)SYS_DataRange.All
  282. && rdr.MenuNo == menuNo
  283. select u).Concat(
  284. from u in UserRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  285. from r in u.Sys_Role
  286. from rdr in r.Sys_RoleDataRange.Where(x => x.MenuNo == menuNo).DefaultIfEmpty()
  287. from rd in u.CF_Staff.CF_StaffManageDepartment
  288. where r.DefaultDataRange == (int)SYS_DataRange.Department
  289. && rdr.RoleDataRangeID == null && rd.CF_Department.CF_College.CampusID == campusID
  290. select u).Concat(
  291. from u in UserRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  292. from r in u.Sys_Role
  293. from rdr in r.Sys_RoleDataRange.Where(x => x.MenuNo == menuNo).DefaultIfEmpty()
  294. from rc in u.CF_Staff.CF_StaffManageCollege
  295. where r.DefaultDataRange == (int)SYS_DataRange.College
  296. && rdr.RoleDataRangeID == null && rc.CF_College.CampusID == campusID
  297. select u).Concat(
  298. from u in UserRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  299. from r in u.Sys_Role
  300. from rdr in r.Sys_RoleDataRange.Where(x => x.MenuNo == menuNo).DefaultIfEmpty()
  301. from c in u.CF_Staff.CF_StaffManageCampus
  302. where r.DefaultDataRange == (int)SYS_DataRange.Campus
  303. && rdr.RoleDataRangeID == null && c.CampusID == campusID
  304. select u).Concat(
  305. from u in UserRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  306. from r in u.Sys_Role
  307. from rdr in r.Sys_RoleDataRange.Where(x => x.MenuNo == menuNo).DefaultIfEmpty()
  308. where r.DefaultDataRange == (int)SYS_DataRange.All
  309. && rdr.RoleDataRangeID == null
  310. select u).Distinct();
  311. return q;
  312. }
  313. /// <summary>
  314. ///
  315. /// </summary>
  316. /// <param name="userID"></param>
  317. /// <param name="menuNo"></param>
  318. /// <returns></returns>
  319. public IQueryable<Sys_User> GetDataRangeUserByUserID(Guid userID, string menuNo)
  320. {
  321. var q = (from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  322. from u in rdr.Sys_Role.Sys_User
  323. from rd in u.CF_Staff.CF_StaffManageDepartment
  324. from cu in rd.CF_Department.CF_College.CF_Staff
  325. where rdr.DataRangeID == (int)SYS_DataRange.Department
  326. && rdr.MenuNo == menuNo && cu.UserID == userID
  327. select u).Concat(
  328. from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  329. from u in rdr.Sys_Role.Sys_User
  330. from rc in u.CF_Staff.CF_StaffManageCollege
  331. from cu in rc.CF_College.CF_Staff
  332. where rdr.DataRangeID == (int)SYS_DataRange.College
  333. && rdr.MenuNo == menuNo && cu.UserID == userID
  334. select u).Concat(
  335. from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  336. from u in rdr.Sys_Role.Sys_User
  337. from c in u.CF_Staff.CF_StaffManageCampus
  338. from cl in c.CF_Campus.CF_College
  339. from cu in cl.CF_Staff
  340. where rdr.DataRangeID == (int)SYS_DataRange.Campus
  341. && rdr.MenuNo == menuNo && cu.UserID == userID
  342. select u).Concat(
  343. from rdr in RoleDataRangeRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  344. from u in rdr.Sys_Role.Sys_User
  345. where rdr.DataRangeID == (int)SYS_DataRange.All
  346. && rdr.MenuNo == menuNo
  347. select u).Concat(
  348. from u in UserRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  349. from r in u.Sys_Role
  350. from rdr in r.Sys_RoleDataRange.Where(x => x.MenuNo == menuNo).DefaultIfEmpty()
  351. from rd in u.CF_Staff.CF_StaffManageDepartment
  352. from cu in rd.CF_Department.CF_College.CF_Staff
  353. where r.DefaultDataRange == (int)SYS_DataRange.Department
  354. && rdr.RoleDataRangeID == null && cu.UserID == userID
  355. select u).Concat(
  356. from u in UserRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  357. from r in u.Sys_Role
  358. from rdr in r.Sys_RoleDataRange.Where(x => x.MenuNo == menuNo).DefaultIfEmpty()
  359. from rc in u.CF_Staff.CF_StaffManageCollege
  360. from cu in rc.CF_College.CF_Staff
  361. where r.DefaultDataRange == (int)SYS_DataRange.College
  362. && rdr.RoleDataRangeID == null && cu.UserID == userID
  363. select u).Concat(
  364. from u in UserRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  365. from r in u.Sys_Role
  366. from rdr in r.Sys_RoleDataRange.Where(x => x.MenuNo == menuNo).DefaultIfEmpty()
  367. from c in u.CF_Staff.CF_StaffManageCampus
  368. from cl in c.CF_Campus.CF_College
  369. from cu in cl.CF_Staff
  370. where r.DefaultDataRange == (int)SYS_DataRange.Campus
  371. && rdr.RoleDataRangeID == null && cu.UserID == userID
  372. select u).Concat(
  373. from u in UserRepository.GetList(x => x.RecordStatus >= (int)SYS_STATUS.USABLE)
  374. from r in u.Sys_Role
  375. from rdr in r.Sys_RoleDataRange.Where(x => x.MenuNo == menuNo).DefaultIfEmpty()
  376. where r.DefaultDataRange == (int)SYS_DataRange.All
  377. && rdr.RoleDataRangeID == null
  378. select u).Distinct();
  379. return q;
  380. }
  381. }
  382. }