UserServices.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.Entity;
  6. using Bowin.Common.Utility;
  7. using Bowin.Common.Linq;
  8. using Bowin.Common.Linq.Entity;
  9. using EMIS.DataLogic.Repositories;
  10. using System.Linq.Expressions;
  11. using EMIS.Entities;
  12. using EMIS.DataLogic.SystemDAL;
  13. using EMIS.ViewModel.SystemView;
  14. using EMIS.Utility;
  15. using EMIS.DataLogic.Common.AdministrativeOrgan;
  16. using EMIS.DataLogic.Common.TeacherManagement;
  17. using EMIS.ViewModel;
  18. using EMIS.Utility.SSO;
  19. using EMIS.CommonLogic.Students;
  20. using EMIS.ViewModel.Students;
  21. using EMIS.DataLogic.Common.Students;
  22. using EMIS.DataLogic.Common.Specialtyclass;
  23. using System.Text.RegularExpressions;
  24. namespace EMIS.CommonLogic.SystemServices
  25. {
  26. public class UserServices : BaseServices, IUserServices
  27. {
  28. public UserDAL UserDAL { get; set; }
  29. public ClassmajorDAL ClassmajorDAL { get; set; }
  30. public StaffDAL StaffDAL { get; set; }
  31. public RoleDAL RoleDAL { get; set; }
  32. public CampusDAL CampusDAL { get; set; }
  33. public CollegeDAL CollegeDAL { get; set; }
  34. public StudentsDAL StudentsDAL { get; set; }
  35. public EMIS.DataLogic.Repositories.HRService.UsersRepository CommUserRepository { get; set; }
  36. public Lazy<IControlItemServices> ControlItemServices { get; set; }
  37. public bool Login(string loginID, string password)
  38. {
  39. var encryptPassword = password.ToUpper();
  40. //Bowin.Common.Utility.StringEx.MD5(password);
  41. //岭南不用姓名登陆
  42. //var userName = UserDAL.UserRepository.GetList(u => u.RecordStatus == (int)SYS_STATUS.USABLE && u.Name == loginID).ToList();
  43. //if(userName.Count>1)
  44. //{
  45. // throw new Exception("由于您的名字在系统中存在重名情况,请使用工号进行登录。");
  46. //}
  47. var user = UserDAL.UserRepository.GetList(u => u.RecordStatus == (int)SYS_STATUS.USABLE && (u.LoginID == loginID) && u.Password == encryptPassword)
  48. .Include(x => x.CF_Student).FirstOrDefault();
  49. if (user == null) return false;
  50. if (user.CF_Student != null)
  51. {
  52. ControlItemServices.Value.CheckLoginControlItems(user.UserID);
  53. }
  54. return true;
  55. }
  56. public Entities.Sys_User GetUserByLoginID(string loginID, bool isNeedEnable = true)
  57. {
  58. Expression<Func<Sys_User, bool>> user = (p => p.LoginID == loginID.Trim()||p.Name==loginID);
  59. if (isNeedEnable)
  60. {
  61. //user = user.And(p => p.RecordStatus == 1); 加了个方法代替,因为日后很可能会重用
  62. return UserDAL.GetSingleEnableUser(user);
  63. }
  64. else
  65. {
  66. return UserDAL.UserRepository.GetSingle(user,
  67. (x => x.CF_Staff.CF_College.CF_Campus.CF_University),
  68. (x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CF_College.CF_Campus.CF_University));
  69. }
  70. }
  71. public List<Sys_User> GetUserListByLoginID(IList<string> loginIDList, bool isNeedEnable = true)
  72. {
  73. Expression<Func<Sys_User, bool>> userExp = (x => loginIDList.Contains(x.LoginID));
  74. if (isNeedEnable)
  75. {
  76. userExp = userExp.And(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE);
  77. }
  78. return UserDAL.UserRepository.GetList(userExp).ToList();
  79. }
  80. public Sys_User GetUserByUserID(Guid? userID, bool isNeedEnable = true)
  81. {
  82. Expression<Func<Sys_User, bool>> user = (p => p.UserID == userID);
  83. if (isNeedEnable)
  84. {
  85. //user = user.And(p => p.RecordStatus == 1); 加了个方法代替,因为日后很可能会重用
  86. return UserDAL.GetSingleEnableUser(user);
  87. }
  88. else
  89. {
  90. return UserDAL.UserRepository.GetSingle(user,
  91. (x => x.CF_Staff.CF_College.CF_Campus.CF_University),
  92. (x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CF_College.CF_Campus.CF_University));
  93. }
  94. }
  95. public UserView GetUserViewByID(Guid userID)
  96. {
  97. Expression<Func<Sys_User, bool>> userExp = p => p.UserID == userID;
  98. return UserDAL.GetUserView(userExp, (x => true)).FirstOrDefault();
  99. }
  100. /// <summary>
  101. /// 教师用户列表(用于列表控键,分页)
  102. /// </summary>
  103. /// <param name="configuretView"></param>
  104. /// <param name="campusID"></param>
  105. /// <param name="collegeID"></param>
  106. /// <param name="departmentID"></param>
  107. /// <param name="roleID"></param>
  108. /// <param name="dataRangeID"></param>
  109. /// <param name="stauts"></param>
  110. /// <param name="pageIndex"></param>
  111. /// <param name="pageSize"></param>
  112. /// <returns></returns>
  113. public Bowin.Common.Linq.Entity.IGridResultSet<UserView> GetUserViewList(ViewModel.ConfiguretView configuretView,
  114. Guid? campusID, Guid? collegeID, Guid? departmentID, Guid? roleID, int? dataRangeID, int? stauts, int pageIndex, int pageSize)
  115. {
  116. Expression<Func<Sys_User, bool>> exp = (x => true);
  117. Expression<Func<Sys_Role, bool>> roleExp = (x => true);
  118. if (campusID.HasValue)
  119. {
  120. exp = exp.And(x => x.CF_Staff.CF_College.CampusID == campusID.Value);
  121. }
  122. if (collegeID.HasValue)
  123. {
  124. exp = exp.And(x => x.CF_Staff.CollegeID == collegeID.Value);
  125. }
  126. if (departmentID.HasValue)
  127. {
  128. exp = exp.And(x => x.CF_Staff.DepartmentID == departmentID.Value);
  129. }
  130. //if (roleID.HasValue)
  131. //{
  132. // exp = exp.And(x => x.Sys_Role.Any(w => w.RoleID == roleID));
  133. // //roleExp = roleExp.And(x => x.RoleID == roleID.Value);
  134. //}
  135. //if (dataRangeID.HasValue)
  136. //{
  137. // exp = exp.And(x => x.Sys_Role.Any(w => w.DefaultDataRange == dataRangeID.Value));
  138. // //roleExp = roleExp.And(x => x.DefaultDataRange == dataRangeID.Value);
  139. //}
  140. if (stauts.HasValue)
  141. {
  142. exp = exp.And(x => x.RecordStatus == stauts);
  143. }
  144. IQueryable<UserView> q = UserDAL.GetUserView(exp, roleExp);
  145. //
  146. if (roleID.HasValue)
  147. {
  148. q = q.Where(x => x.RoleID == roleID);
  149. }
  150. if (dataRangeID.HasValue)
  151. {
  152. q = q.Where(x => x.DataRange == dataRangeID);
  153. }
  154. if (!string.IsNullOrEmpty(configuretView.ConditionValue) && !string.IsNullOrEmpty(configuretView.Attribute))
  155. q = q.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue);
  156. q = this.GetQueryByDataRangeByDepartment(q);
  157. return q.OrderBy(x => x.CollegeCode).ThenBy(x => x.LoginID).ToGridResultSet(pageIndex, pageSize);
  158. }
  159. /// <summary>
  160. /// 教师用户列表(用于列表控键,不分页)
  161. /// </summary>
  162. /// <param name="configuretView"></param>
  163. /// <param name="campusID"></param>
  164. /// <param name="collegeID"></param>
  165. /// <param name="departmentID"></param>
  166. /// <param name="roleID"></param>
  167. /// <param name="dataRangeID"></param>
  168. /// <returns></returns>
  169. public IList<ViewModel.SystemView.UserView> GetUserViewList(ViewModel.ConfiguretView configuretView,
  170. Guid? campusID, Guid? collegeID, Guid? departmentID, Guid? roleID, int? dataRangeID)
  171. {
  172. Expression<Func<Sys_User, bool>> exp = (x => true);
  173. Expression<Func<Sys_Role, bool>> roleExp = (x => true);
  174. if (campusID.HasValue)
  175. {
  176. exp = exp.And(x => x.CF_Staff.CF_Department.CF_College.CampusID == campusID.Value);
  177. }
  178. if (collegeID.HasValue)
  179. {
  180. exp = exp.And(x => x.CF_Staff.CF_Department.CollegeID == collegeID.Value);
  181. }
  182. if (departmentID.HasValue)
  183. {
  184. exp = exp.And(x => x.CF_Staff.DepartmentID == departmentID.Value);
  185. }
  186. if (roleID.HasValue)
  187. {
  188. roleExp = roleExp.And(x => x.RoleID == roleID.Value);
  189. }
  190. if (dataRangeID.HasValue)
  191. {
  192. roleExp = roleExp.And(x => x.DefaultDataRange == dataRangeID.Value);
  193. }
  194. IQueryable<UserView> q = UserDAL.GetUserView(exp, roleExp);
  195. if (!string.IsNullOrEmpty(configuretView.ConditionValue) && !string.IsNullOrEmpty(configuretView.Attribute))
  196. q = q.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue);
  197. return q.OrderBy(x => x.CollegeCode).ThenBy(x => x.LoginID).ToList();
  198. }
  199. /// <summary>
  200. /// 学生用户列表(用于列表控键,分页)
  201. /// </summary>
  202. /// <param name="configuretView"></param>
  203. /// <param name="campusID"></param>
  204. /// <param name="collegeID"></param>
  205. /// <param name="educationID"></param>
  206. /// <param name="schoolYearID"></param>
  207. /// <param name="standardID"></param>
  208. /// <param name="learnFormID"></param>
  209. /// <param name="classMajorID"></param>
  210. /// <param name="pageIndex"></param>
  211. /// <param name="pageSize"></param>
  212. /// <returns></returns>
  213. public Bowin.Common.Linq.Entity.IGridResultSet<StudentUserView> GetStudentUserViewList(ViewModel.ConfiguretView configuretView,
  214. Guid? campusID, Guid? collegeID, int? educationID, int? schoolYearID, int? standardID, int? learnFormID,
  215. Guid? classMajorID,string LearnSystem, int pageIndex, int pageSize)
  216. {
  217. Expression<Func<Sys_User, bool>> exp = (x => true);
  218. Expression<Func<Sys_Role, bool>> roleExp = (x => true);
  219. if (campusID.HasValue)
  220. {
  221. exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CF_College.CampusID == campusID.Value);
  222. }
  223. if (collegeID.HasValue)
  224. {
  225. exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CollegeID == collegeID.Value);
  226. }
  227. if (educationID.HasValue)
  228. {
  229. exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.EducationID == educationID.Value);
  230. }
  231. if (schoolYearID.HasValue)
  232. {
  233. exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.SchoolyearID == schoolYearID.Value);
  234. }
  235. if (standardID.HasValue)
  236. {
  237. exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID == standardID.Value);
  238. }
  239. if (learnFormID.HasValue)
  240. {
  241. exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.LearningformID == learnFormID.Value);
  242. }
  243. if (classMajorID.HasValue)
  244. {
  245. exp = exp.And(x => x.CF_Student.ClassmajorID == classMajorID);
  246. }
  247. if (!string.IsNullOrEmpty(LearnSystem) && LearnSystem != "-1")
  248. {
  249. var LearnSystems = Convert.ToDecimal(LearnSystem);
  250. exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.LearnSystem == LearnSystems);
  251. }
  252. IQueryable<StudentUserView> q = UserDAL.GetStudentUserView(exp, roleExp);
  253. if (!string.IsNullOrEmpty(configuretView.ConditionValue) && !string.IsNullOrEmpty(configuretView.Attribute))
  254. q = q.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue);
  255. return this.GetQueryByDataRangeByCollege(q).OrderBy(x => x.Grade).ThenBy(x => x.ClassNo).ThenBy(x => x.LoginID).ToGridResultSet(pageIndex, pageSize);
  256. }
  257. /// <summary>
  258. /// 学生用户列表(用于列表控键,不分页)
  259. /// </summary>
  260. /// <param name="configuretView"></param>
  261. /// <param name="campusID"></param>
  262. /// <param name="collegeID"></param>
  263. /// <param name="educationID"></param>
  264. /// <param name="schoolYearID"></param>
  265. /// <param name="standardID"></param>
  266. /// <param name="learnFormID"></param>
  267. /// <param name="classMajorID"></param>
  268. /// <returns></returns>
  269. public IList<ViewModel.SystemView.StudentUserView> GetStudentUserViewList(ViewModel.ConfiguretView configuretView,
  270. Guid? campusID, Guid? collegeID, int? educationID, int? schoolYearID, int? standardID, int? learnFormID,
  271. Guid? classMajorID, string LearnSystem)
  272. {
  273. Expression<Func<Sys_User, bool>> exp = (x => true);
  274. Expression<Func<Sys_Role, bool>> roleExp = (x => true);
  275. if (campusID.HasValue)
  276. {
  277. exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CF_College.CampusID == campusID.Value);
  278. }
  279. if (collegeID.HasValue)
  280. {
  281. exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CollegeID == collegeID.Value);
  282. }
  283. if (educationID.HasValue)
  284. {
  285. exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.EducationID == educationID.Value);
  286. }
  287. if (schoolYearID.HasValue)
  288. {
  289. exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.SchoolyearID == schoolYearID.Value);
  290. }
  291. if (standardID.HasValue)
  292. {
  293. exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID == standardID.Value);
  294. }
  295. if (learnFormID.HasValue)
  296. {
  297. exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.LearningformID == learnFormID.Value);
  298. }
  299. if (!string.IsNullOrEmpty(LearnSystem) && LearnSystem != "-1")
  300. {
  301. var LearnSystems = Convert.ToDecimal(LearnSystem);
  302. exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.LearnSystem == LearnSystems);
  303. }
  304. IQueryable<StudentUserView> q = UserDAL.GetStudentUserView(exp, roleExp);
  305. if (!string.IsNullOrEmpty(configuretView.ConditionValue) && !string.IsNullOrEmpty(configuretView.Attribute))
  306. q = q.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue);
  307. return q.OrderBy(x => x.Grade).ThenBy(x => x.ClassNo).ThenBy(x => x.LoginID).ToList();
  308. }
  309. /// <summary>
  310. /// 批量初始化密码
  311. /// </summary>
  312. /// <param name="userID">主键ID</param>
  313. /// <returns></returns>
  314. public void BatchInitPassword(IList<Guid?> userID, bool isChange,string pwd)
  315. {
  316. if (userID.Count > 0)
  317. {
  318. if (isChange == false)
  319. {
  320. List<Sys_User> list = UserDAL.UserRepository.Entities.Where(x => userID.Contains(x.UserID)).ToList();
  321. List<CF_Student> studentList = StudentsDAL.StudentRepository.Entities.Where(x => userID.Contains(x.UserID)).ToList();
  322. List<CF_Staff> staffList = StaffDAL.staffRepository.Entities.Where(x => userID.Contains(x.UserID)).ToList();
  323. List<Sys_User> userList = new List<Sys_User>();
  324. if (studentList.Count > 0)
  325. {
  326. foreach (var user in list)
  327. {
  328. var student = studentList.Where(x => x.UserID == user.UserID).FirstOrDefault();
  329. var passwordstr = "A" + student.IDNumber.Substring(student.IDNumber.Length - 6);
  330. user.Password = passwordstr.MD5();
  331. SetModifyStatus(user);
  332. userList.Add(user);
  333. }
  334. }
  335. if (staffList.Count > 0)
  336. {
  337. foreach (var user in list)
  338. {
  339. var staff = staffList.Where(x => x.UserID == user.UserID).FirstOrDefault();
  340. var passwordstr = "A" + staff.CertificatesNum.Substring(staff.CertificatesNum.Length - 6, staff.CertificatesNum.Length);
  341. user.Password = ("888888").MD5();
  342. SetModifyStatus(user);
  343. userList.Add(user);
  344. }
  345. }
  346. UnitOfWork.BatchUpdate(userList);
  347. UnitOfWork.Commit();
  348. }
  349. else
  350. {
  351. List<Sys_User> list = UserDAL.UserRepository.Entities.Where(x => userID.Contains(x.UserID)).ToList();
  352. List<Sys_User> userList = new List<Sys_User>();
  353. foreach (var user in list)
  354. {
  355. user.Password = pwd.MD5();
  356. SetModifyStatus(user);
  357. userList.Add(user);
  358. }
  359. UnitOfWork.BatchUpdate(userList);
  360. UnitOfWork.Commit();
  361. }
  362. }
  363. }
  364. /// <summary>
  365. /// 启用
  366. /// </summary>
  367. /// <param name="userID">主键ID</param>
  368. /// <returns></returns>
  369. public void Usable(IList<Guid?> userID)
  370. {
  371. if (userID.Count > 0)
  372. {
  373. UnitOfWork.Update<Sys_User>(
  374. x => new Sys_User { RecordStatus = (int)SYS_STATUS.USABLE },
  375. x => userID.Contains(x.UserID)
  376. );
  377. List<Sys_User> list = UserDAL.UserRepository.Entities.Where(x => userID.Contains(x.UserID)).ToList();
  378. foreach (var user in list)
  379. {
  380. SetModifyStatus(user);
  381. UnitOfWork.Update(user);
  382. UnitOfWork.Commit();
  383. }
  384. }
  385. }
  386. /// <summary>
  387. /// 禁用
  388. /// </summary>
  389. /// <param name="userID">主键ID</param>
  390. /// <returns></returns>
  391. public void UnUsable(IList<Guid?> userID)
  392. {
  393. if (userID.Count > 0)
  394. {
  395. UnitOfWork.Update<Sys_User>(
  396. x => new Sys_User { RecordStatus = (int)SYS_STATUS.UNUSABLE },
  397. x => userID.Contains(x.UserID)
  398. );
  399. List<Sys_User> list = UserDAL.UserRepository.Entities.Where(x => userID.Contains(x.UserID)).ToList();
  400. foreach (var user in list)
  401. {
  402. SetModifyStatus(user);
  403. UnitOfWork.Update(user);
  404. UnitOfWork.Commit();
  405. }
  406. }
  407. }
  408. /// <summary>
  409. /// 保存用户角色
  410. /// </summary>
  411. /// <param name="userID"></param>
  412. /// <param name="roleID"></param>
  413. public void SaveUserRoles(Guid userID, IList<Guid?> roleID)
  414. {
  415. var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.Sys_Role));
  416. var roleList = this.RoleDAL.RoleRepository.GetList(x => roleID.Contains(x.RoleID)).ToList();
  417. user.Sys_Role = new HashSet<Sys_Role>();
  418. roleList.ForEach(x => user.Sys_Role.Add(x));
  419. this.UnitOfWork.Commit();
  420. }
  421. public IList<CF_Campus> GetUserInchargeCampus(Guid userID)
  422. {
  423. var campusList = this.StaffDAL.GetUserInChargeCampus(x => x.UserID == userID).ToList();
  424. return campusList;
  425. }
  426. public IList<CF_College> GetUserInchargeCollege(Guid userID)
  427. {
  428. var collegeList = this.StaffDAL.GetUserInChargeCollege(x => x.UserID == userID).ToList();
  429. return collegeList;
  430. }
  431. public IList<CF_Department> GetUserInchargeDepartment(Guid userID)
  432. {
  433. var departmentList = this.StaffDAL.GetUserInChargeDepartment(x => x.UserID == userID).ToList();
  434. return departmentList;
  435. }
  436. public IList<Sys_Role> GetUserRoles(Guid userID)
  437. {
  438. var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.Sys_Role));
  439. var roleList = user.Sys_Role.ToList();
  440. return roleList;
  441. }
  442. public Sys_Role GetMaxPrivilegeRoleByUserID(Guid userID)
  443. {
  444. var role = RoleDAL.RoleRepository.GetList(x => x.Sys_User.Any(w => w.UserID == userID))
  445. .OrderByDescending(x => x.Sys_FunctionCode.Count())
  446. .FirstOrDefault();
  447. return role;
  448. }
  449. /// <summary>
  450. /// 保存用户所属校区
  451. /// </summary>
  452. /// <param name="userID"></param>
  453. /// <param name="roleID"></param>
  454. public void SaveUserCampus(Guid userID, IList<Guid?> campusID)
  455. {
  456. var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.CF_Staff.CF_StaffManageCampus));
  457. if (user.CF_Staff == null) throw new Exception("该用户并非教职工用户,请补全该用户的教职工信息再进行操作,或选择其他用户进行操作。");
  458. //var campusList = this.CampusDAL.campusRepository.GetList(x => campusID.Contains(x.CampusID)).ToList();
  459. user.CF_Staff.CF_StaffManageCampus.ToList().ForEach(x => this.UnitOfWork.Remove(x));
  460. campusID.ForEach(x => {
  461. CF_StaffManageCampus staffManageCampus = new CF_StaffManageCampus();
  462. staffManageCampus.StaffManageCampusID = Guid.NewGuid();
  463. staffManageCampus.UserID = userID;
  464. staffManageCampus.CampusID = x;
  465. this.SetNewStatus(staffManageCampus);
  466. this.UnitOfWork.Add(staffManageCampus);
  467. });
  468. this.UnitOfWork.Commit();
  469. }
  470. /// <summary>
  471. /// 保存用户所属学院
  472. /// </summary>
  473. /// <param name="userID"></param>
  474. /// <param name="roleID"></param>
  475. public void SaveUserCollege(Guid userID, IList<Guid?> collegeID)
  476. {
  477. var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.CF_Staff.CF_StaffManageCollege));
  478. if (user.CF_Staff == null) throw new Exception("该用户并非教职工用户,请补全该用户的教职工信息再进行操作,或选择其他用户进行操作。");
  479. user.CF_Staff.CF_StaffManageCollege.ToList().ForEach(x => this.UnitOfWork.Remove(x));
  480. collegeID.ForEach(x =>
  481. {
  482. CF_StaffManageCollege staffManageCollege = new CF_StaffManageCollege();
  483. staffManageCollege.StaffManageCollegeID = Guid.NewGuid();
  484. staffManageCollege.UserID = userID;
  485. staffManageCollege.CollegeID = x;
  486. this.SetNewStatus(staffManageCollege);
  487. this.UnitOfWork.Add(staffManageCollege);
  488. });
  489. this.UnitOfWork.Commit();
  490. }
  491. /// <summary>
  492. /// 保存用户所属教研室
  493. /// </summary>
  494. /// <param name="userID"></param>
  495. /// <param name="departmentID"></param>
  496. public void SaveUserDepartment(Guid userID, IList<Guid?> departmentID)
  497. {
  498. var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.CF_Staff.CF_StaffManageDepartment));
  499. if (user.CF_Staff == null) throw new Exception("该用户并非教职工用户,请补全该用户的教职工信息再进行操作,或选择其他用户进行操作。");
  500. user.CF_Staff.CF_StaffManageDepartment.ToList().ForEach(x => this.UnitOfWork.Remove(x));
  501. departmentID.ForEach(x =>
  502. {
  503. CF_StaffManageDepartment staffManageDepartment = new CF_StaffManageDepartment();
  504. staffManageDepartment.StaffManageDepartmentID = Guid.NewGuid();
  505. staffManageDepartment.UserID = userID;
  506. staffManageDepartment.DepartmentID = x;
  507. this.SetNewStatus(staffManageDepartment);
  508. this.UnitOfWork.Add(staffManageDepartment);
  509. });
  510. this.UnitOfWork.Commit();
  511. }
  512. public IList<RoleView> GetUserRolesView(Guid userID)
  513. {
  514. return RoleDAL.GetRoleView(x => x.Sys_User.Any(w => w.UserID == userID)).OrderBy(x => x.TypeID).ThenBy(x => x.OrderNo).ToList();
  515. }
  516. public string GetUserEmail(Guid userID)
  517. {
  518. var user = UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.CF_Staff.CF_StaffProfile), (x => x.CF_Student.CF_StudentContact));
  519. if (user == null) throw new Exception("指定的用户信息不存在。");
  520. if (user.CF_Student != null && user.CF_Student.CF_StudentContact != null)
  521. {
  522. return user.CF_Student.CF_StudentContact.Email;
  523. }
  524. else if (user.CF_Staff != null && user.CF_Staff.CF_StaffProfile != null)
  525. {
  526. return user.CF_Staff.CF_StaffProfile.Email;
  527. }
  528. else
  529. {
  530. throw new Exception("用户并没有注册电子邮箱信息。");
  531. }
  532. }
  533. public void ChangePassword(ChangePasswordView password, Guid userID)
  534. {
  535. var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.Sys_MailVerifyCode));
  536. user.Password = password.Password.MD5();
  537. user.Sys_MailVerifyCode = null;
  538. UnitOfWork.Commit();
  539. }
  540. public Sys_User GetUserByID(Guid userID)
  541. {
  542. return this.UserDAL.UserRepository.Entities.Where(x => x.UserID == userID).FirstOrDefault();
  543. }
  544. public void SynHRServices()
  545. {
  546. var exceptUser = new string[] { "admin" };
  547. var hrUsers = CommUserRepository.GetList(x => !exceptUser.Contains(x.userid)).ToList();
  548. var staffs = StaffDAL.staffRepository.GetList(x => !exceptUser.Contains(x.Sys_User.LoginID))
  549. .Include(x => x.Sys_User).Include(x => x.CF_StaffProfile).ToList();
  550. hrUsers.ForEach(x => x.status = "D");
  551. foreach (var staff in staffs)
  552. {
  553. var hrUser = hrUsers.FirstOrDefault(x => x.userid == staff.Sys_User.LoginID);
  554. if (hrUser == null)
  555. {
  556. hrUser = new Entities.HRServices.Comm_Users();
  557. hrUser.userid = staff.Sys_User.LoginID;
  558. hrUser.loginid = staff.Sys_User.LoginID;
  559. hrUser.createdt = DateTime.Now;
  560. this.HRUnitOfWork.Add(hrUser);
  561. }
  562. hrUser.username = staff.Sys_User.Name;
  563. if (staff.DepartmentID.HasValue)
  564. {
  565. hrUser.dpid = staff.DepartmentID.ToString();
  566. }
  567. else
  568. {
  569. hrUser.dpid = staff.CollegeID.ToString();
  570. }
  571. hrUser.telephone = staff.CF_StaffProfile.Telephone;
  572. hrUser.mobile = staff.CF_StaffProfile.Mobile;
  573. hrUser.email = staff.CF_StaffProfile.Email;
  574. hrUser.cardid = staff.Sys_User.LoginID;
  575. hrUser.employeeclassid = "";
  576. hrUser.jobtypeid = "教师";
  577. hrUser.birthday = staff.BirthDate;
  578. switch (staff.Sex)
  579. {
  580. case (int)CF_Sex.Male:
  581. hrUser.sex = "1";
  582. break;
  583. case (int)CF_Sex.Female:
  584. hrUser.sex = "2";
  585. break;
  586. default:
  587. hrUser.sex = "0";
  588. break;
  589. }
  590. hrUser.orderNo = "1";
  591. hrUser.userrole = "";
  592. hrUser.userligion = "";
  593. hrUser.password = staff.Sys_User.Password;
  594. hrUser.status = "A";
  595. hrUser.updatedt = DateTime.Now;
  596. hrUser.istmpuser = false;
  597. }
  598. this.HRUnitOfWork.Commit();
  599. }
  600. public virtual string SSOLogin()
  601. {
  602. return SSO.SSOLogin();
  603. }
  604. public Guid? StudentRegist(RegistView registView)
  605. {
  606. string reg = @"(^\d{15}$)|(^\d{18}$)|(^\d{17}\d|X|x)$";
  607. if (registView.CertificatesType == (int)CF_CertificatesType.IdCrad && !Regex.IsMatch(registView.LoginID, reg, RegexOptions.IgnoreCase))
  608. {
  609. throw new Exception("证件号码格式不正确,请检查并修改正确后重试。");
  610. }
  611. var dupUser = UserDAL.UserRepository.GetSingle(x => x.LoginID == registView.LoginID && x.UserID != registView.UserID);
  612. if (dupUser != null)
  613. {
  614. throw new Exception("该证件号码已经注册,请不要重复注册。");
  615. }
  616. var userID = this.SaveStudentByRegistView(registView);
  617. return userID;
  618. }
  619. private Guid? SaveStudentByRegistView(RegistView registView)
  620. {
  621. var classmajor = this.ClassmajorDAL.GetSocialClassmajor(x => x.SchoolAreaID == registView.SchoolAreaID).FirstOrDefault();
  622. CF_Student student = StudentsDAL.StudentRepository.GetSingle(x => x.UserID == registView.UserID,
  623. (x => x.CF_StudentAccount),
  624. (x => x.CF_StudentProfile),
  625. (x => x.CF_StudentContact),
  626. (x => x.CF_Recruitstudents),
  627. (x => x.Sys_User.Sys_Role),
  628. (x => x.Sys_User.Sys_Announcement),
  629. (x => x.Sys_User.Sys_MailVerifyCode),
  630. (x => x.Sys_User.EX_ExaminationRegistrationConfirm));
  631. CF_StudentAccount account;
  632. CF_StudentProfile profile;
  633. CF_StudentContact contact;
  634. CF_Recruitstudents recruits;
  635. Sys_User user;
  636. if (student != null)
  637. {
  638. account = student.CF_StudentAccount;
  639. profile = student.CF_StudentProfile;
  640. contact = student.CF_StudentContact;
  641. recruits = student.CF_Recruitstudents;
  642. user = student.Sys_User;
  643. }
  644. else
  645. {
  646. student = new CF_Student();
  647. account = new CF_StudentAccount();
  648. profile = new CF_StudentProfile();
  649. contact = new CF_StudentContact();
  650. recruits = new CF_Recruitstudents();
  651. user = new Sys_User();
  652. Sys_Role role = RoleDAL.RoleRepository.GetSingle(x => x.CF_StudentRole != null && x.CF_StudentRole.StudentType != null, (x => x.CF_StudentRole));
  653. student.UserID = Guid.NewGuid();
  654. student.ClassmajorID = classmajor.ClassmajorID;
  655. student.StudentCardNo = registView.LoginID;
  656. student.StudentType = role.CF_StudentRole.StudentType;
  657. student.RecordStatus = (int)SYS_STATUS.USABLE;
  658. student.CreateTime = DateTime.Now;
  659. student.CreateUserID = student.UserID;
  660. //提供返回值
  661. registView.UserID = student.UserID;
  662. account.UserID = student.UserID;
  663. account.RecordStatus = (int)SYS_STATUS.USABLE;
  664. account.CreateTime = DateTime.Now;
  665. account.CreateUserID = student.UserID;
  666. profile.UserID = student.UserID;
  667. profile.RecordStatus = (int)SYS_STATUS.USABLE;
  668. profile.CreateTime = DateTime.Now;
  669. profile.CreateUserID = student.UserID;
  670. contact.UserID = student.UserID;
  671. contact.WorkUnit = registView.WorkUnit;
  672. contact.RecordStatus = (int)SYS_STATUS.USABLE;
  673. contact.CreateTime = DateTime.Now;
  674. contact.CreateUserID = student.UserID;
  675. recruits.UserID = student.UserID;
  676. recruits.RecordStatus = (int)SYS_STATUS.USABLE;
  677. recruits.CreateTime = DateTime.Now;
  678. recruits.CreateUserID = student.UserID;
  679. user.UserID = student.UserID;
  680. user.LoginID = registView.LoginID;
  681. user.Password = registView.PCode.MD5();
  682. user.RecordStatus = (int)SYS_STATUS.USABLE;
  683. user.CreateTime = DateTime.Now;
  684. user.CreateUserID = student.UserID;
  685. user.Sys_Role.Add(role);
  686. UnitOfWork.Add(user);
  687. UnitOfWork.Add(student);
  688. UnitOfWork.Add(account);
  689. UnitOfWork.Add(profile);
  690. UnitOfWork.Add(contact);
  691. UnitOfWork.Add(recruits);
  692. }
  693. student.CertificatesType = registView.CertificatesType;
  694. student.IDNumber = registView.LoginID;
  695. if (!string.IsNullOrEmpty(registView.PhotoUrl))
  696. {
  697. student.PhotoUrl = registView.PhotoUrl;
  698. }
  699. student.Sex = registView.Sex;
  700. student.ModifyTime = DateTime.Now;
  701. student.ModifyUserID = student.UserID;
  702. account.ModifyTime = DateTime.Now;
  703. account.ModifyUserID = student.UserID;
  704. profile.BirthDate = registView.BirthDate;
  705. profile.ModifyTime = DateTime.Now;
  706. profile.ModifyUserID = student.UserID;
  707. contact.Mobile = registView.Mobile;
  708. contact.Email = registView.Email;
  709. contact.ModifyTime = DateTime.Now;
  710. contact.ModifyUserID = student.UserID;
  711. recruits.ModifyTime = DateTime.Now;
  712. recruits.ModifyUserID = student.UserID;
  713. user.Name = registView.UserName;
  714. user.ModifyTime = DateTime.Now;
  715. user.ModifyUserID = student.UserID;
  716. UnitOfWork.Commit();
  717. return student.UserID;
  718. }
  719. public bool LoginHistory(string LoginID, bool isSuccess)
  720. {
  721. var today = DateTime.Now.Day;
  722. int? failCount = 0;
  723. bool status = true;
  724. Sys_LoginHistory todayLoginHistorie = new Sys_LoginHistory();
  725. Sys_LoginHistory addloginHistories = new Sys_LoginHistory();
  726. //无论什么情况登陆,先删除当前登陆账号非当天的登陆历史记录
  727. List<Sys_LoginHistory> oldLoginHistories = UserDAL.LoginHistoryRepository.Entities.Where(x => x.LoginID == LoginID && x.LoginFailTime.Value.Day != today).ToList();
  728. UnitOfWork.Delete<Sys_LoginHistory>(oldLoginHistories);
  729. todayLoginHistorie = UserDAL.LoginHistoryRepository.Entities.Where(x => x.LoginID == LoginID && x.LoginFailTime.Value.Day == today).FirstOrDefault();
  730. if (todayLoginHistorie == null)
  731. {
  732. if (!isSuccess)//当天第一次登陆失败
  733. {
  734. addloginHistories.LoginHistoryID = Guid.NewGuid();
  735. addloginHistories.LoginID = LoginID;
  736. addloginHistories.LoginFailTime = DateTime.Now;
  737. addloginHistories.Count = failCount + 1;
  738. UnitOfWork.Add(addloginHistories);
  739. }
  740. status = true;
  741. }
  742. else if (todayLoginHistorie.Count < 5)
  743. {
  744. failCount = todayLoginHistorie.Count;
  745. UnitOfWork.Delete<Sys_LoginHistory>(x => x.LoginHistoryID == todayLoginHistorie.LoginHistoryID);
  746. if (!isSuccess)//当天登陆失败次数大于1,小于5,且本次登陆失败
  747. {
  748. addloginHistories.LoginHistoryID = Guid.NewGuid();
  749. addloginHistories.LoginID = LoginID;
  750. addloginHistories.LoginFailTime = DateTime.Now;
  751. addloginHistories.Count = failCount + 1;
  752. UnitOfWork.Add(addloginHistories);
  753. }
  754. status = true;
  755. }
  756. else if (todayLoginHistorie.Count >= 5)
  757. {
  758. status = false;
  759. }
  760. UnitOfWork.Commit();
  761. return status;
  762. }
  763. }
  764. }