UserServices.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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 = "Ab!" + 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 = ("Ab#888888").MD5();
  342. SetModifyStatus(user);
  343. userList.Add(user);
  344. }
  345. }
  346. UnitOfWork.BatchUpdate(userList);
  347. UnitOfWork.Commit();
  348. }
  349. else
  350. {
  351. if (!(new Regex("^(?=.*?[0-9])(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[#?!@$%^&*-]).{8,}$").IsMatch(pwd)))
  352. {
  353. throw new Exception("密码须包含大小写英文、特殊字符、数字且长度大于或等于8位");
  354. }
  355. List<Sys_User> list = UserDAL.UserRepository.Entities.Where(x => userID.Contains(x.UserID)).ToList();
  356. List<Sys_User> userList = new List<Sys_User>();
  357. foreach (var user in list)
  358. {
  359. user.Password = pwd.MD5();
  360. SetModifyStatus(user);
  361. userList.Add(user);
  362. }
  363. UnitOfWork.BatchUpdate(userList);
  364. UnitOfWork.Commit();
  365. }
  366. }
  367. }
  368. /// <summary>
  369. /// 启用
  370. /// </summary>
  371. /// <param name="userID">主键ID</param>
  372. /// <returns></returns>
  373. public void Usable(IList<Guid?> userID)
  374. {
  375. if (userID.Count > 0)
  376. {
  377. UnitOfWork.Update<Sys_User>(
  378. x => new Sys_User { RecordStatus = (int)SYS_STATUS.USABLE },
  379. x => userID.Contains(x.UserID)
  380. );
  381. List<Sys_User> list = UserDAL.UserRepository.Entities.Where(x => userID.Contains(x.UserID)).ToList();
  382. foreach (var user in list)
  383. {
  384. SetModifyStatus(user);
  385. UnitOfWork.Update(user);
  386. UnitOfWork.Commit();
  387. }
  388. }
  389. }
  390. /// <summary>
  391. /// 禁用
  392. /// </summary>
  393. /// <param name="userID">主键ID</param>
  394. /// <returns></returns>
  395. public void UnUsable(IList<Guid?> userID)
  396. {
  397. if (userID.Count > 0)
  398. {
  399. UnitOfWork.Update<Sys_User>(
  400. x => new Sys_User { RecordStatus = (int)SYS_STATUS.UNUSABLE },
  401. x => userID.Contains(x.UserID)
  402. );
  403. List<Sys_User> list = UserDAL.UserRepository.Entities.Where(x => userID.Contains(x.UserID)).ToList();
  404. foreach (var user in list)
  405. {
  406. SetModifyStatus(user);
  407. UnitOfWork.Update(user);
  408. UnitOfWork.Commit();
  409. }
  410. }
  411. }
  412. /// <summary>
  413. /// 保存用户角色
  414. /// </summary>
  415. /// <param name="userID"></param>
  416. /// <param name="roleID"></param>
  417. public void SaveUserRoles(Guid userID, IList<Guid?> roleID)
  418. {
  419. var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.Sys_Role));
  420. var roleList = this.RoleDAL.RoleRepository.GetList(x => roleID.Contains(x.RoleID)).ToList();
  421. user.Sys_Role = new HashSet<Sys_Role>();
  422. roleList.ForEach(x => user.Sys_Role.Add(x));
  423. this.UnitOfWork.Commit();
  424. }
  425. public IList<CF_Campus> GetUserInchargeCampus(Guid userID)
  426. {
  427. var campusList = this.StaffDAL.GetUserInChargeCampus(x => x.UserID == userID).ToList();
  428. return campusList;
  429. }
  430. public IList<CF_College> GetUserInchargeCollege(Guid userID)
  431. {
  432. var collegeList = this.StaffDAL.GetUserInChargeCollege(x => x.UserID == userID).ToList();
  433. return collegeList;
  434. }
  435. public IList<CF_Department> GetUserInchargeDepartment(Guid userID)
  436. {
  437. var departmentList = this.StaffDAL.GetUserInChargeDepartment(x => x.UserID == userID).ToList();
  438. return departmentList;
  439. }
  440. public IList<Sys_Role> GetUserRoles(Guid userID)
  441. {
  442. var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.Sys_Role));
  443. var roleList = user.Sys_Role.ToList();
  444. return roleList;
  445. }
  446. public Sys_Role GetMaxPrivilegeRoleByUserID(Guid userID)
  447. {
  448. var role = RoleDAL.RoleRepository.GetList(x => x.Sys_User.Any(w => w.UserID == userID))
  449. .OrderByDescending(x => x.Sys_FunctionCode.Count())
  450. .FirstOrDefault();
  451. return role;
  452. }
  453. /// <summary>
  454. /// 保存用户所属校区
  455. /// </summary>
  456. /// <param name="userID"></param>
  457. /// <param name="roleID"></param>
  458. public void SaveUserCampus(Guid userID, IList<Guid?> campusID)
  459. {
  460. var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.CF_Staff.CF_StaffManageCampus));
  461. if (user.CF_Staff == null) throw new Exception("该用户并非教职工用户,请补全该用户的教职工信息再进行操作,或选择其他用户进行操作。");
  462. //var campusList = this.CampusDAL.campusRepository.GetList(x => campusID.Contains(x.CampusID)).ToList();
  463. user.CF_Staff.CF_StaffManageCampus.ToList().ForEach(x => this.UnitOfWork.Remove(x));
  464. campusID.ForEach(x => {
  465. CF_StaffManageCampus staffManageCampus = new CF_StaffManageCampus();
  466. staffManageCampus.StaffManageCampusID = Guid.NewGuid();
  467. staffManageCampus.UserID = userID;
  468. staffManageCampus.CampusID = x;
  469. this.SetNewStatus(staffManageCampus);
  470. this.UnitOfWork.Add(staffManageCampus);
  471. });
  472. this.UnitOfWork.Commit();
  473. }
  474. /// <summary>
  475. /// 保存用户所属学院
  476. /// </summary>
  477. /// <param name="userID"></param>
  478. /// <param name="roleID"></param>
  479. public void SaveUserCollege(Guid userID, IList<Guid?> collegeID)
  480. {
  481. var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.CF_Staff.CF_StaffManageCollege));
  482. if (user.CF_Staff == null) throw new Exception("该用户并非教职工用户,请补全该用户的教职工信息再进行操作,或选择其他用户进行操作。");
  483. user.CF_Staff.CF_StaffManageCollege.ToList().ForEach(x => this.UnitOfWork.Remove(x));
  484. collegeID.ForEach(x =>
  485. {
  486. CF_StaffManageCollege staffManageCollege = new CF_StaffManageCollege();
  487. staffManageCollege.StaffManageCollegeID = Guid.NewGuid();
  488. staffManageCollege.UserID = userID;
  489. staffManageCollege.CollegeID = x;
  490. this.SetNewStatus(staffManageCollege);
  491. this.UnitOfWork.Add(staffManageCollege);
  492. });
  493. this.UnitOfWork.Commit();
  494. }
  495. /// <summary>
  496. /// 保存用户所属教研室
  497. /// </summary>
  498. /// <param name="userID"></param>
  499. /// <param name="departmentID"></param>
  500. public void SaveUserDepartment(Guid userID, IList<Guid?> departmentID)
  501. {
  502. var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.CF_Staff.CF_StaffManageDepartment));
  503. if (user.CF_Staff == null) throw new Exception("该用户并非教职工用户,请补全该用户的教职工信息再进行操作,或选择其他用户进行操作。");
  504. user.CF_Staff.CF_StaffManageDepartment.ToList().ForEach(x => this.UnitOfWork.Remove(x));
  505. departmentID.ForEach(x =>
  506. {
  507. CF_StaffManageDepartment staffManageDepartment = new CF_StaffManageDepartment();
  508. staffManageDepartment.StaffManageDepartmentID = Guid.NewGuid();
  509. staffManageDepartment.UserID = userID;
  510. staffManageDepartment.DepartmentID = x;
  511. this.SetNewStatus(staffManageDepartment);
  512. this.UnitOfWork.Add(staffManageDepartment);
  513. });
  514. this.UnitOfWork.Commit();
  515. }
  516. public IList<RoleView> GetUserRolesView(Guid userID)
  517. {
  518. return RoleDAL.GetRoleView(x => x.Sys_User.Any(w => w.UserID == userID)).OrderBy(x => x.TypeID).ThenBy(x => x.OrderNo).ToList();
  519. }
  520. public string GetUserEmail(Guid userID)
  521. {
  522. var user = UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.CF_Staff.CF_StaffProfile), (x => x.CF_Student.CF_StudentContact));
  523. if (user == null) throw new Exception("指定的用户信息不存在。");
  524. if (user.CF_Student != null && user.CF_Student.CF_StudentContact != null)
  525. {
  526. return user.CF_Student.CF_StudentContact.Email;
  527. }
  528. else if (user.CF_Staff != null && user.CF_Staff.CF_StaffProfile != null)
  529. {
  530. return user.CF_Staff.CF_StaffProfile.Email;
  531. }
  532. else
  533. {
  534. throw new Exception("用户并没有注册电子邮箱信息。");
  535. }
  536. }
  537. public void ChangePassword(ChangePasswordView password, Guid userID)
  538. {
  539. var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.Sys_MailVerifyCode));
  540. user.Password = password.Password.MD5();
  541. user.Sys_MailVerifyCode = null;
  542. UnitOfWork.Commit();
  543. }
  544. public Sys_User GetUserByID(Guid userID)
  545. {
  546. return this.UserDAL.UserRepository.Entities.Where(x => x.UserID == userID).FirstOrDefault();
  547. }
  548. public void SynHRServices()
  549. {
  550. var exceptUser = new string[] { "admin" };
  551. var hrUsers = CommUserRepository.GetList(x => !exceptUser.Contains(x.userid)).ToList();
  552. var staffs = StaffDAL.staffRepository.GetList(x => !exceptUser.Contains(x.Sys_User.LoginID))
  553. .Include(x => x.Sys_User).Include(x => x.CF_StaffProfile).ToList();
  554. hrUsers.ForEach(x => x.status = "D");
  555. foreach (var staff in staffs)
  556. {
  557. var hrUser = hrUsers.FirstOrDefault(x => x.userid == staff.Sys_User.LoginID);
  558. if (hrUser == null)
  559. {
  560. hrUser = new Entities.HRServices.Comm_Users();
  561. hrUser.userid = staff.Sys_User.LoginID;
  562. hrUser.loginid = staff.Sys_User.LoginID;
  563. hrUser.createdt = DateTime.Now;
  564. this.HRUnitOfWork.Add(hrUser);
  565. }
  566. hrUser.username = staff.Sys_User.Name;
  567. if (staff.DepartmentID.HasValue)
  568. {
  569. hrUser.dpid = staff.DepartmentID.ToString();
  570. }
  571. else
  572. {
  573. hrUser.dpid = staff.CollegeID.ToString();
  574. }
  575. hrUser.telephone = staff.CF_StaffProfile.Telephone;
  576. hrUser.mobile = staff.CF_StaffProfile.Mobile;
  577. hrUser.email = staff.CF_StaffProfile.Email;
  578. hrUser.cardid = staff.Sys_User.LoginID;
  579. hrUser.employeeclassid = "";
  580. hrUser.jobtypeid = "教师";
  581. hrUser.birthday = staff.BirthDate;
  582. switch (staff.Sex)
  583. {
  584. case (int)CF_Sex.Male:
  585. hrUser.sex = "1";
  586. break;
  587. case (int)CF_Sex.Female:
  588. hrUser.sex = "2";
  589. break;
  590. default:
  591. hrUser.sex = "0";
  592. break;
  593. }
  594. hrUser.orderNo = "1";
  595. hrUser.userrole = "";
  596. hrUser.userligion = "";
  597. hrUser.password = staff.Sys_User.Password;
  598. hrUser.status = "A";
  599. hrUser.updatedt = DateTime.Now;
  600. hrUser.istmpuser = false;
  601. }
  602. this.HRUnitOfWork.Commit();
  603. }
  604. public virtual string SSOLogin()
  605. {
  606. return SSO.SSOLogin();
  607. }
  608. public Guid? StudentRegist(RegistView registView)
  609. {
  610. string reg = @"(^\d{15}$)|(^\d{18}$)|(^\d{17}\d|X|x)$";
  611. if (registView.CertificatesType == (int)CF_CertificatesType.IdCrad && !Regex.IsMatch(registView.LoginID, reg, RegexOptions.IgnoreCase))
  612. {
  613. throw new Exception("证件号码格式不正确,请检查并修改正确后重试。");
  614. }
  615. var dupUser = UserDAL.UserRepository.GetSingle(x => x.LoginID == registView.LoginID && x.UserID != registView.UserID);
  616. if (dupUser != null)
  617. {
  618. throw new Exception("该证件号码已经注册,请不要重复注册。");
  619. }
  620. var userID = this.SaveStudentByRegistView(registView);
  621. return userID;
  622. }
  623. private Guid? SaveStudentByRegistView(RegistView registView)
  624. {
  625. var classmajor = this.ClassmajorDAL.GetSocialClassmajor(x => x.SchoolAreaID == registView.SchoolAreaID).FirstOrDefault();
  626. CF_Student student = StudentsDAL.StudentRepository.GetSingle(x => x.UserID == registView.UserID,
  627. (x => x.CF_StudentAccount),
  628. (x => x.CF_StudentProfile),
  629. (x => x.CF_StudentContact),
  630. (x => x.CF_Recruitstudents),
  631. (x => x.Sys_User.Sys_Role),
  632. (x => x.Sys_User.Sys_Announcement),
  633. (x => x.Sys_User.Sys_MailVerifyCode),
  634. (x => x.Sys_User.EX_ExaminationRegistrationConfirm));
  635. CF_StudentAccount account;
  636. CF_StudentProfile profile;
  637. CF_StudentContact contact;
  638. CF_Recruitstudents recruits;
  639. Sys_User user;
  640. if (student != null)
  641. {
  642. account = student.CF_StudentAccount;
  643. profile = student.CF_StudentProfile;
  644. contact = student.CF_StudentContact;
  645. recruits = student.CF_Recruitstudents;
  646. user = student.Sys_User;
  647. }
  648. else
  649. {
  650. student = new CF_Student();
  651. account = new CF_StudentAccount();
  652. profile = new CF_StudentProfile();
  653. contact = new CF_StudentContact();
  654. recruits = new CF_Recruitstudents();
  655. user = new Sys_User();
  656. Sys_Role role = RoleDAL.RoleRepository.GetSingle(x => x.CF_StudentRole != null && x.CF_StudentRole.StudentType != null, (x => x.CF_StudentRole));
  657. student.UserID = Guid.NewGuid();
  658. student.ClassmajorID = classmajor.ClassmajorID;
  659. student.StudentCardNo = registView.LoginID;
  660. student.StudentType = role.CF_StudentRole.StudentType;
  661. student.RecordStatus = (int)SYS_STATUS.USABLE;
  662. student.CreateTime = DateTime.Now;
  663. student.CreateUserID = student.UserID;
  664. //提供返回值
  665. registView.UserID = student.UserID;
  666. account.UserID = student.UserID;
  667. account.RecordStatus = (int)SYS_STATUS.USABLE;
  668. account.CreateTime = DateTime.Now;
  669. account.CreateUserID = student.UserID;
  670. profile.UserID = student.UserID;
  671. profile.RecordStatus = (int)SYS_STATUS.USABLE;
  672. profile.CreateTime = DateTime.Now;
  673. profile.CreateUserID = student.UserID;
  674. contact.UserID = student.UserID;
  675. contact.WorkUnit = registView.WorkUnit;
  676. contact.RecordStatus = (int)SYS_STATUS.USABLE;
  677. contact.CreateTime = DateTime.Now;
  678. contact.CreateUserID = student.UserID;
  679. recruits.UserID = student.UserID;
  680. recruits.RecordStatus = (int)SYS_STATUS.USABLE;
  681. recruits.CreateTime = DateTime.Now;
  682. recruits.CreateUserID = student.UserID;
  683. user.UserID = student.UserID;
  684. user.LoginID = registView.LoginID;
  685. user.Password = registView.PCode.MD5();
  686. user.RecordStatus = (int)SYS_STATUS.USABLE;
  687. user.CreateTime = DateTime.Now;
  688. user.CreateUserID = student.UserID;
  689. user.Sys_Role.Add(role);
  690. UnitOfWork.Add(user);
  691. UnitOfWork.Add(student);
  692. UnitOfWork.Add(account);
  693. UnitOfWork.Add(profile);
  694. UnitOfWork.Add(contact);
  695. UnitOfWork.Add(recruits);
  696. }
  697. student.CertificatesType = registView.CertificatesType;
  698. student.IDNumber = registView.LoginID;
  699. if (!string.IsNullOrEmpty(registView.PhotoUrl))
  700. {
  701. student.PhotoUrl = registView.PhotoUrl;
  702. }
  703. student.Sex = registView.Sex;
  704. student.ModifyTime = DateTime.Now;
  705. student.ModifyUserID = student.UserID;
  706. account.ModifyTime = DateTime.Now;
  707. account.ModifyUserID = student.UserID;
  708. profile.BirthDate = registView.BirthDate;
  709. profile.ModifyTime = DateTime.Now;
  710. profile.ModifyUserID = student.UserID;
  711. contact.Mobile = registView.Mobile;
  712. contact.Email = registView.Email;
  713. contact.ModifyTime = DateTime.Now;
  714. contact.ModifyUserID = student.UserID;
  715. recruits.ModifyTime = DateTime.Now;
  716. recruits.ModifyUserID = student.UserID;
  717. user.Name = registView.UserName;
  718. user.ModifyTime = DateTime.Now;
  719. user.ModifyUserID = student.UserID;
  720. UnitOfWork.Commit();
  721. return student.UserID;
  722. }
  723. public bool LoginHistory(string LoginID, bool isSuccess)
  724. {
  725. var today = DateTime.Now.Day;
  726. int? failCount = 0;
  727. bool status = true;
  728. Sys_LoginHistory todayLoginHistorie = new Sys_LoginHistory();
  729. Sys_LoginHistory addloginHistories = new Sys_LoginHistory();
  730. //无论什么情况登陆,先删除当前登陆账号非当天的登陆历史记录
  731. List<Sys_LoginHistory> oldLoginHistories = UserDAL.LoginHistoryRepository.Entities.Where(x => x.LoginID == LoginID && x.LoginFailTime.Value.Day != today).ToList();
  732. UnitOfWork.Delete<Sys_LoginHistory>(oldLoginHistories);
  733. todayLoginHistorie = UserDAL.LoginHistoryRepository.Entities.Where(x => x.LoginID == LoginID && x.LoginFailTime.Value.Day == today).FirstOrDefault();
  734. if (todayLoginHistorie == null)
  735. {
  736. if (!isSuccess)//当天第一次登陆失败
  737. {
  738. addloginHistories.LoginHistoryID = Guid.NewGuid();
  739. addloginHistories.LoginID = LoginID;
  740. addloginHistories.LoginFailTime = DateTime.Now;
  741. addloginHistories.Count = failCount + 1;
  742. UnitOfWork.Add(addloginHistories);
  743. }
  744. status = true;
  745. }
  746. else if (todayLoginHistorie.Count < 5)
  747. {
  748. failCount = todayLoginHistorie.Count;
  749. UnitOfWork.Delete<Sys_LoginHistory>(x => x.LoginHistoryID == todayLoginHistorie.LoginHistoryID);
  750. if (!isSuccess)//当天登陆失败次数大于1,小于5,且本次登陆失败
  751. {
  752. addloginHistories.LoginHistoryID = Guid.NewGuid();
  753. addloginHistories.LoginID = LoginID;
  754. addloginHistories.LoginFailTime = DateTime.Now;
  755. addloginHistories.Count = failCount + 1;
  756. UnitOfWork.Add(addloginHistories);
  757. }
  758. status = true;
  759. }
  760. else if (todayLoginHistorie.Count >= 5)
  761. {
  762. status = false;
  763. }
  764. UnitOfWork.Commit();
  765. return status;
  766. }
  767. }
  768. }