DataRangeDAL.cs 20 KB

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