BaseExtensions.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data;
  6. using System.Reflection;
  7. using System.Web.Mvc;
  8. using Autofac;
  9. using Bowin.Web.Controls.Mvc;
  10. using Bowin.Common;
  11. using Bowin.Common.Utility;
  12. using EMIS.Utility;
  13. using EMIS.Entities;
  14. using EMIS.ViewModel;
  15. using EMIS.ViewModel.WorkflowManage;
  16. using EMIS.ViewModel.AccountManage;
  17. using EMIS.CommonLogic.StudentManage.StudentProfile;
  18. using EMIS.CommonLogic.CalendarManage;
  19. using EMIS.CommonLogic.SystemServices;
  20. namespace EMIS.Web.Controls
  21. {
  22. public static class BaseExtensions
  23. {
  24. /// <summary>
  25. /// 查询用户字典信息
  26. /// </summary>
  27. public static void GetContextUser()
  28. {
  29. FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
  30. var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  31. fahelper.AuthenticateRequest(HttpContext.Current, cookieName, Const.LOCAL_AUTH_EXCEPTURL);
  32. }
  33. /// <summary>
  34. /// 查询星期下拉列表
  35. /// </summary>
  36. public static List<DropdownListItem> WeekdayDropdownListItemList
  37. {
  38. get
  39. {
  40. return WeekHelper.WeekDictionary.OrderBy(x => (x.Key == 0 ? 7 : x.Key))
  41. .Select(x => new DropdownListItem
  42. {
  43. Text = x.Value,
  44. Value = x.Key
  45. }).ToList();
  46. }
  47. }
  48. /// <summary>
  49. /// 查询当前启用校历对应的学年学期ID(主键ID)
  50. /// </summary>
  51. /// <returns></returns>
  52. public static Guid GetCurrentSchoolYearID()
  53. {
  54. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  55. {
  56. ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
  57. var schoolYearView = SchoolYearServices.GetCurrentSchoolYear();
  58. return schoolYearView.SchoolYearID.Value;
  59. }
  60. }
  61. /// <summary>
  62. /// 查询当前启用校历对应的学年学期Code(学年学期Code)
  63. /// </summary>
  64. /// <returns></returns>
  65. public static string GetCurrentSchoolYearCode()
  66. {
  67. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  68. {
  69. ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
  70. var schoolYearView = SchoolYearServices.GetCurrentSchoolYear();
  71. return schoolYearView.Code;
  72. }
  73. }
  74. /// <summary>
  75. ///查询当前启用校历对应的下一个学年学期ID(主键ID)
  76. /// </summary>
  77. /// <returns></returns>
  78. public static Guid GetNextSchoolYearID()
  79. {
  80. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  81. {
  82. ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
  83. var nextSchoolYear = SchoolYearServices.GetNextSchoolYear();
  84. return nextSchoolYear.SchoolYearID.Value;
  85. }
  86. }
  87. /// <summary>
  88. /// 查询当前启用校历对应的下一个学年学期Code(学年学期Code)
  89. /// </summary>
  90. /// <returns></returns>
  91. public static string GetNextSchoolYearCode()
  92. {
  93. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  94. {
  95. ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
  96. var nextSchoolYear = SchoolYearServices.GetNextSchoolYear();
  97. return nextSchoolYear.Code;
  98. }
  99. }
  100. /// <summary>
  101. /// 查询当前启用校历对应的学年(学年)
  102. /// </summary>
  103. /// <returns></returns>
  104. public static int? GetCurrentYearID()
  105. {
  106. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  107. {
  108. ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
  109. var schoolYear = SchoolYearServices.GetCurrentSchoolYear();
  110. return schoolYear.Years;
  111. }
  112. }
  113. /// <summary>
  114. /// 查询当前启用校历的对应的学年的下一学年(学年)
  115. /// </summary>
  116. /// <returns></returns>
  117. public static int? GetNextYearID()
  118. {
  119. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  120. {
  121. ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
  122. var currentSchoolYear = SchoolYearServices.GetCurrentSchoolYear();
  123. var nextSchoolYear = SchoolYearServices.GetNextSchoolYear();
  124. if (nextSchoolYear.Years <= currentSchoolYear.Years)
  125. {
  126. return nextSchoolYear.Years + 1;
  127. }
  128. else
  129. {
  130. return nextSchoolYear.Years;
  131. }
  132. }
  133. }
  134. /// <summary>
  135. /// 查询当前启用的毕业学期ID(学年学期对应的ID)
  136. /// </summary>
  137. /// <returns></returns>
  138. public static Guid? GetGradSchoolYearID()
  139. {
  140. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  141. {
  142. IParameterServices ParameterServices = scope.Resolve<IParameterServices>();
  143. //查询设置的毕业学期信息
  144. var graduationSchoolyear = ParameterServices.GetParameterValue(CF_ParameterType.GraduationSchoolyear);
  145. if (!string.IsNullOrEmpty(graduationSchoolyear))
  146. {
  147. ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
  148. var schoolYearView = SchoolYearServices.GetSchoolYearView(Guid.Parse(graduationSchoolyear));
  149. if (schoolYearView != null)
  150. {
  151. return schoolYearView.SchoolYearID;
  152. }
  153. }
  154. return null;
  155. }
  156. }
  157. /// <summary>
  158. /// 查询当前启用的毕业学期Code(学年学期对应的Code)
  159. /// </summary>
  160. /// <returns></returns>
  161. public static string GetGradSchoolYearCode()
  162. {
  163. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  164. {
  165. IParameterServices ParameterServices = scope.Resolve<IParameterServices>();
  166. //查询设置的毕业学期信息
  167. var graduationSchoolyear = ParameterServices.GetParameterValue(CF_ParameterType.GraduationSchoolyear);
  168. if (!string.IsNullOrEmpty(graduationSchoolyear))
  169. {
  170. ISchoolYearServices SchoolYearServices = scope.Resolve<ISchoolYearServices>();
  171. var schoolYearView = SchoolYearServices.GetSchoolYearView(Guid.Parse(graduationSchoolyear));
  172. if (schoolYearView != null)
  173. {
  174. return schoolYearView.Code;
  175. }
  176. }
  177. return null;
  178. }
  179. }
  180. /// <summary>
  181. /// 查询对应的工作流程中开始流程环节的状态Pid(根据对应的tableName)
  182. /// </summary>
  183. /// <param name="tableName"></param>
  184. /// <returns></returns>
  185. public static int? GetStartFlowStatus(string tableName)
  186. {
  187. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  188. {
  189. IWorkflowServices WorkflowServices = scope.Resolve<IWorkflowServices>();
  190. return WorkflowServices.GetStartStatus(tableName);
  191. }
  192. }
  193. /// <summary>
  194. /// 查询对应的接入流程表已退回流程环节Pid(注:不包含开始环节、结束环节且为[BP]标识)
  195. /// </summary>
  196. /// <param name="tableName"></param>
  197. /// <returns></returns>
  198. public static int? GetSendBackFlowStatus(string tableName)
  199. {
  200. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  201. {
  202. IWorkflowServices WorkflowServices = scope.Resolve<IWorkflowServices>();
  203. return WorkflowServices.GetSendBackStatus(tableName);
  204. }
  205. }
  206. /// <summary>
  207. /// 查询对应的接入流程表已通过流程环节Pid(注:不包含开始环节、结束环节且为[PASS]标识)
  208. /// </summary>
  209. /// <param name="tableName"></param>
  210. /// <returns></returns>
  211. public static int? GetPassNoEndFlowStatus(string tableName)
  212. {
  213. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  214. {
  215. IWorkflowServices WorkflowServices = scope.Resolve<IWorkflowServices>();
  216. return WorkflowServices.GetPassNoEndStatus(tableName);
  217. }
  218. }
  219. /// <summary>
  220. /// 查询对应的工作流程中结束流程环节的状态Pid(根据对应的tableName,结束环节且不为[BP]标识的)
  221. /// </summary>
  222. /// <param name="tableName"></param>
  223. /// <returns></returns>
  224. public static int? GetEndFlowStatus(string tableName)
  225. {
  226. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  227. {
  228. IWorkflowServices WorkflowServices = scope.Resolve<IWorkflowServices>();
  229. return WorkflowServices.GetCorrectEndStatus(tableName);
  230. }
  231. }
  232. /// <summary>
  233. /// 查询对应的接入流程表非正常结束的流程环节Pid(注:通常为结束环节且为[BP]标识的)
  234. /// </summary>
  235. /// <param name="tableName"></param>
  236. /// <returns></returns>
  237. public static List<int?> GetBackpointStatus(string tableName)
  238. {
  239. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  240. {
  241. IWorkflowServices WorkflowServices = scope.Resolve<IWorkflowServices>();
  242. return WorkflowServices.GetBackpointStatus(tableName);
  243. }
  244. }
  245. /// <summary>
  246. /// 根据当前流程环节ID获取下一步审批动作
  247. /// </summary>
  248. /// <param name="tableName"></param>
  249. /// <param name="formID"></param>
  250. /// <param name="userID"></param>
  251. /// <returns></returns>
  252. public static List<ActionView> GetCurrentActionViewList(string tableName, Guid formID, Guid userID)
  253. {
  254. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  255. {
  256. IWorkflowServices WorkflowServices = scope.Resolve<IWorkflowServices>();
  257. return WorkflowServices.GetActionView(tableName, formID, userID);
  258. }
  259. }
  260. /// <summary>
  261. /// 登录成功
  262. /// </summary>
  263. /// <param name="controller"></param>
  264. /// <param name="model"></param>
  265. /// <param name="userName"></param>
  266. public static void LoginSureccessful(this ControllerBase controller, LogOnModel model, string userName)
  267. {
  268. FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
  269. var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  270. List<string> userData = new List<string>();
  271. Sys_User userpageResult;
  272. Sys_Role defaultRole = null;
  273. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  274. {
  275. IUserServices userServices = scope.Resolve<IUserServices>();
  276. userpageResult = userServices.GetUserByLoginID(userName, true);
  277. if (userpageResult == null)
  278. {
  279. throw new Exception("获取用户失败。");
  280. }
  281. //只要不是学生,就需要获取角色,如果实在没有角色,则不用选,依然允许进入系统,反正他什么都看不到
  282. if (userpageResult.CF_Student == null)
  283. {
  284. defaultRole = userServices.GetMaxPrivilegeRoleByUserID(userpageResult.UserID);
  285. }
  286. }
  287. if (model.RememberMe)
  288. {
  289. controller.RemeberUserCookies(model);
  290. }
  291. else
  292. {
  293. controller.RemoveUserCookies(model);
  294. }
  295. userData.Add(userpageResult.LoginID);
  296. userData.Add(userpageResult.UserID.ToString());
  297. userData.Add(userpageResult.Name);
  298. userData.Add((userpageResult.CF_Staff != null) ? userpageResult.UserID.ToString() : "");//教职工ID
  299. userData.Add((userpageResult.CF_Student != null) ? userpageResult.UserID.ToString() : "");//学生ID
  300. userData.Add((userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_Department != null) ? userpageResult.CF_Staff.DepartmentID.ToString() : "");//部门(教研室)ID
  301. userData.Add((userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_Department != null) ? userpageResult.CF_Staff.CF_Department.HierarchyID : "");//部门(教研室)树结构ID
  302. userData.Add((userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_Department != null) ? userpageResult.CF_Staff.CF_Department.Name : "");//部门(教研室)名称
  303. if (userpageResult.CF_Student == null)
  304. {
  305. if (userpageResult.CF_Staff != null && userpageResult.CF_Staff.CF_College != null)
  306. {
  307. userData.Add(userpageResult.CF_Staff.CollegeID.ToString());//学院ID
  308. userData.Add(userpageResult.CF_Staff.CF_College.CampusID.ToString());//校区ID
  309. userData.Add(userpageResult.CF_Staff.CF_College.CF_Campus.UniversityID.ToString());//学校ID
  310. }
  311. else
  312. {
  313. userData.Add("");
  314. userData.Add("");
  315. userData.Add("");
  316. }
  317. if (defaultRole != null)
  318. {
  319. userData.Add(defaultRole.RoleID.ToString());//默认角色ID
  320. }
  321. else
  322. {
  323. userData.Add("");
  324. }
  325. }
  326. else
  327. {
  328. //学生的后续再加
  329. userData.Add("");
  330. userData.Add("");
  331. userData.Add("");
  332. userData.Add("");
  333. }
  334. fahelper.loginFormsAuthentication(controller.ControllerContext.HttpContext, cookieName, userData);
  335. }
  336. /// <summary>
  337. /// 角色切换
  338. /// </summary>
  339. /// <param name="controller"></param>
  340. /// <param name="roleID"></param>
  341. public static void SwitchUserRole(this ControllerBase controller, Guid roleID)
  342. {
  343. FormsAuthenticationHelper fahelper = new FormsAuthenticationHelper();
  344. var userData = EMIS.Utility.FormValidate.CustomPrincipal.Current.GetUserData();
  345. var cookieName = EMIS.Utility.Const.LOCAL_SETTING_LOGIN_COOKIENAME;
  346. userData[11] = roleID.ToString();
  347. fahelper.loginFormsAuthentication(controller.ControllerContext.HttpContext, cookieName, userData);
  348. }
  349. /// <summary>
  350. ///
  351. /// </summary>
  352. /// <param name="controller"></param>
  353. /// <param name="model"></param>
  354. public static void RemeberUserCookies(this ControllerBase controller, LogOnModel model)
  355. {
  356. HttpCookie cookie = new HttpCookie("username");
  357. cookie.Value = model.UserName;
  358. cookie.Expires = DateTime.Now.AddDays(1);
  359. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie);
  360. HttpCookie cookie2 = new HttpCookie("password");
  361. cookie2.Value = model.Password;
  362. cookie2.Expires = DateTime.Now.AddDays(1);
  363. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie2);
  364. HttpCookie cookie3 = new HttpCookie("rememberme");
  365. cookie3.Value = model.RememberMe.ToString();
  366. cookie3.Expires = DateTime.Now.AddDays(1);
  367. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie3);
  368. }
  369. /// <summary>
  370. ///
  371. /// </summary>
  372. /// <param name="controller"></param>
  373. /// <param name="model"></param>
  374. public static void RemoveUserCookies(this ControllerBase controller, LogOnModel model)
  375. {
  376. HttpCookie cookie = new HttpCookie("username");
  377. cookie.Value = model.UserName;
  378. cookie.Expires = DateTime.Now.AddDays(-1);
  379. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie);
  380. HttpCookie cookie2 = new HttpCookie("password");
  381. cookie2.Value = model.Password;
  382. cookie2.Expires = DateTime.Now.AddDays(-1);
  383. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie2);
  384. HttpCookie cookie3 = new HttpCookie("rememberme");
  385. cookie3.Value = model.RememberMe.ToString();
  386. cookie3.Expires = DateTime.Now.AddDays(-1);
  387. controller.ControllerContext.HttpContext.Response.AppendCookie(cookie3);
  388. }
  389. /// <summary>
  390. ///
  391. /// </summary>
  392. /// <param name="controller"></param>
  393. /// <returns></returns>
  394. public static LogOnModel GetUserCookies(this ControllerBase controller)
  395. {
  396. LogOnModel model = new LogOnModel();
  397. HttpCookie cookie = controller.ControllerContext.HttpContext.Request.Cookies["username"];
  398. if (cookie != null)
  399. {
  400. model.UserName = cookie.Value;
  401. }
  402. HttpCookie cookie2 = controller.ControllerContext.HttpContext.Request.Cookies["password"];
  403. if (cookie2 != null)
  404. {
  405. model.Password = cookie2.Value;
  406. }
  407. HttpCookie cookie3 = controller.ControllerContext.HttpContext.Request.Cookies["rememberme"];
  408. if (cookie3 != null)
  409. {
  410. model.RememberMe = cookie3.Value.ToLower() == "true";
  411. }
  412. return model;
  413. }
  414. /// <summary>
  415. /// 查询默认的学习形式(个性化配置)
  416. /// 默认为全部
  417. /// </summary>
  418. /// <returns></returns>
  419. public static int? GetDefaultLearnformForList()
  420. {
  421. using (var scope = AutofacHelper.Container.BeginLifetimeScope())
  422. {
  423. IStudentServices studentServices = scope.Resolve<IStudentServices>();
  424. return studentServices.GetDefaultLearnformForList();
  425. }
  426. }
  427. /// <summary>
  428. /// 查询入学学期ID(Web.config)
  429. /// </summary>
  430. /// <returns></returns>
  431. public static int? GetEntranceSemesterID()
  432. {
  433. int? entranceSemesterID = null;
  434. if (!string.IsNullOrEmpty(EMIS.Utility.Const.LOCAL_SETTING_ENTRANCESEMESTERID))
  435. {
  436. entranceSemesterID = Convert.ToInt32(EMIS.Utility.Const.LOCAL_SETTING_ENTRANCESEMESTERID);
  437. }
  438. return entranceSemesterID;
  439. }
  440. /// <summary>
  441. /// List转换为DataTable
  442. /// </summary>
  443. /// <typeparam name="T"></typeparam>
  444. /// <param name="items"></param>
  445. /// <returns></returns>
  446. public static DataTable ToDataTable<T>(this List<T> items)
  447. {
  448. DataTable dataTable = new DataTable();
  449. PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  450. foreach (PropertyInfo prop in Props)
  451. {
  452. dataTable.Columns.Add(prop.Name);
  453. }
  454. foreach (T obj in items)
  455. {
  456. var values = new object[Props.Length];
  457. for (int i = 0; i < Props.Length; i++)
  458. {
  459. values[i] = Props[i].GetValue(obj, null);
  460. }
  461. dataTable.Rows.Add(values);
  462. }
  463. return dataTable;
  464. }
  465. }
  466. }