using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Entity; using Bowin.Common.Utility; using Bowin.Common.Linq; using Bowin.Common.Linq.Entity; using EMIS.DataLogic.Repositories; using System.Linq.Expressions; using EMIS.Entities; using EMIS.DataLogic.SystemDAL; using EMIS.ViewModel.SystemView; using EMIS.Utility; using EMIS.DataLogic.UniversityManage.AdministrativeOrgan; using EMIS.DataLogic.UniversityManage.TeacherManage; using EMIS.ViewModel; using EMIS.Utility.SSO; using EMIS.ViewModel.SystemManage.UserManage; namespace EMIS.CommonLogic.SystemServices { public class UserServices : BaseServices, IUserServices { public UserDAL UserDAL { get; set; } public StaffDAL StaffDAL { get; set; } public RoleDAL RoleDAL { get; set; } public CampusDAL CampusDAL { get; set; } public CollegeDAL CollegeDAL { get; set; } public Lazy ControlItemServices { get; set; } public EMIS.DataLogic.Repositories.HRService.UsersRepository CommUserRepository { get; set; } public bool Login(string loginID, string password) { var encryptPassword = password.ToUpper(); //var encryptPassword = Bowin.Common.Utility.StringEx.MD5(password); var userName = UserDAL.UserRepository.GetList(u => u.RecordStatus == (int)SYS_STATUS.USABLE && u.Name == loginID).ToList(); if(userName.Count>1) { throw new Exception("由于您的名字在系统中存在重名情况,请使用工号进行登录。"); } var user = UserDAL.UserRepository.GetList(u => u.RecordStatus == (int)SYS_STATUS.USABLE && (u.LoginID == loginID||u.Name==loginID) && u.Password == encryptPassword) .Include(x => x.CF_Student).FirstOrDefault(); if (user == null) return false; if (user.CF_Student != null) { ControlItemServices.Value.CheckLoginControlItems(user.UserID); } return true; } public Entities.Sys_User GetUserByLoginID(string loginID, bool isNeedEnable = true) { Expression> user = (p => p.LoginID == loginID.Trim()||p.Name==loginID); if (isNeedEnable) { //user = user.And(p => p.RecordStatus == 1); 加了个方法代替,因为日后很可能会重用 return UserDAL.GetSingleEnableUser(user); } else { return UserDAL.UserRepository.GetSingle(user, (x => x.CF_Staff.CF_College.CF_Campus.CF_University), (x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CF_College.CF_Campus.CF_University)); } } public List GetUserListByLoginID(IList loginIDList, bool isNeedEnable = true) { Expression> userExp = (x => loginIDList.Contains(x.LoginID)); if (isNeedEnable) { userExp = userExp.And(x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); } return UserDAL.UserRepository.GetList(userExp).ToList(); } public UserView GetUserViewByID(Guid userID) { Expression> userExp = p => p.UserID == userID; return UserDAL.GetUserView(userExp, (x => true)).FirstOrDefault(); } /// /// 教师用户列表 /// /// /// /// /// /// /// /// /// /// /// public IGridResultSet GetUserViewList(ConfiguretView configuretView, Guid? campusID, Guid? collegeID, Guid? departmentID, Guid? roleID, int? dataRangeID, int? stauts, int pageIndex, int pageSize) { Expression> exp = (x => true); Expression> roleExp = (x => true); if (campusID.HasValue) { exp = exp.And(x => x.CF_Staff.CF_College.CampusID == campusID.Value); } if (collegeID.HasValue) { exp = exp.And(x => x.CF_Staff.CollegeID == collegeID.Value); } if (departmentID.HasValue) { exp = exp.And(x => x.CF_Staff.DepartmentID == departmentID.Value); } //if (roleID.HasValue) //{ // exp = exp.And(x => x.Sys_Role.Any(w => w.RoleID == roleID)); // //roleExp = roleExp.And(x => x.RoleID == roleID.Value); //} //if (dataRangeID.HasValue) //{ // exp = exp.And(x => x.Sys_Role.Any(w => w.DefaultDataRange == dataRangeID.Value)); // //roleExp = roleExp.And(x => x.DefaultDataRange == dataRangeID.Value); //} if (stauts.HasValue) { exp = exp.And(x => x.RecordStatus == stauts); } IQueryable q = UserDAL.GetUserView(exp, roleExp); if (roleID.HasValue) { q = q.Where(x => x.RoleID == roleID); } if (dataRangeID.HasValue) { q = q.Where(x => x.DataRange == dataRangeID); } if (!string.IsNullOrEmpty(configuretView.ConditionValue) && !string.IsNullOrEmpty(configuretView.Attribute)) q = q.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue); q = this.GetQueryByDataRangeByDepartment(q); return q.OrderBy(x => x.CollegeCode).ThenBy(x => x.LoginID).ToGridResultSet(pageIndex, pageSize); } /// /// 教师用户列表 /// /// /// /// /// /// /// /// public IList GetUserViewList(ConfiguretView configuretView, Guid? campusID, Guid? collegeID, Guid? departmentID, Guid? roleID, int? dataRangeID) { Expression> exp = (x => true); Expression> roleExp = (x => true); if (campusID.HasValue) { exp = exp.And(x => x.CF_Staff.CF_Department.CF_College.CampusID == campusID.Value); } if (collegeID.HasValue) { exp = exp.And(x => x.CF_Staff.CF_Department.CollegeID == collegeID.Value); } if (departmentID.HasValue) { exp = exp.And(x => x.CF_Staff.DepartmentID == departmentID.Value); } if (roleID.HasValue) { roleExp = roleExp.And(x => x.RoleID == roleID.Value); } if (dataRangeID.HasValue) { roleExp = roleExp.And(x => x.DefaultDataRange == dataRangeID.Value); } IQueryable q = UserDAL.GetUserView(exp, roleExp); if (!string.IsNullOrEmpty(configuretView.ConditionValue) && !string.IsNullOrEmpty(configuretView.Attribute)) q = q.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue); return q.OrderBy(x => x.CollegeCode).ThenBy(x => x.LoginID).ToList(); } /// /// 学生用户列表 /// /// /// /// /// /// /// /// /// /// /// /// public IGridResultSet GetStudentUserViewList(ViewModel.ConfiguretView configuretView, Guid? campusID, Guid? collegeID, int? educationID, int? schoolYearID, int? standardID, int? learnFormID, Guid? classMajorID,string LearnSystem, int pageIndex, int pageSize) { Expression> exp = (x => true); Expression> roleExp = (x => true); if (campusID.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CF_College.CampusID == campusID.Value); } if (collegeID.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CollegeID == collegeID.Value); } if (educationID.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.EducationID == educationID.Value); } if (schoolYearID.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.GradeID == schoolYearID.Value); } if (standardID.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID == standardID.Value); } if (learnFormID.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.LearningformID == learnFormID.Value); } if (classMajorID.HasValue) { exp = exp.And(x => x.CF_Student.ClassmajorID == classMajorID); } if (!string.IsNullOrEmpty(LearnSystem) && LearnSystem != "-1") { var LearnSystems = Convert.ToDecimal(LearnSystem); exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.LearnSystem == LearnSystems); } IQueryable q = UserDAL.GetStudentUserView(exp, roleExp); if (!string.IsNullOrEmpty(configuretView.ConditionValue) && !string.IsNullOrEmpty(configuretView.Attribute)) q = q.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue); return this.GetQueryByDataRangeByCollege(q).OrderBy(x => x.Grade).ThenBy(x => x.ClassNo).ThenBy(x => x.LoginID).ToGridResultSet(pageIndex, pageSize); } /// /// 学生用户列表 /// /// /// /// /// /// /// /// /// /// public IList GetStudentUserViewList(ViewModel.ConfiguretView configuretView, Guid? campusID, Guid? collegeID, int? educationID, int? schoolYearID, int? standardID, int? learnFormID, Guid? classMajorID, string LearnSystem) { Expression> exp = (x => true); Expression> roleExp = (x => true); if (campusID.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CF_College.CampusID == campusID.Value); } if (collegeID.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.CollegeID == collegeID.Value); } if (educationID.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.EducationID == educationID.Value); } if (schoolYearID.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.GradeID == schoolYearID.Value); } if (standardID.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.StandardID == standardID.Value); } if (learnFormID.HasValue) { exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.LearningformID == learnFormID.Value); } if (!string.IsNullOrEmpty(LearnSystem) && LearnSystem != "-1") { var LearnSystems = Convert.ToDecimal(LearnSystem); exp = exp.And(x => x.CF_Student.CF_Classmajor.CF_Grademajor.CF_Facultymajor.LearnSystem == LearnSystems); } IQueryable q = UserDAL.GetStudentUserView(exp, roleExp); if (!string.IsNullOrEmpty(configuretView.ConditionValue) && !string.IsNullOrEmpty(configuretView.Attribute)) q = q.DynamicWhere(configuretView.Attribute, configuretView.Condition, configuretView.ConditionValue); return q.OrderBy(x => x.Grade).ThenBy(x => x.ClassNo).ThenBy(x => x.LoginID).ToList(); } /// /// 批量初始化密码 /// /// 主键ID /// public virtual void BatchInitPassword(IList userID, bool isChange, string pwd) { if (userID.Count > 0) { if (isChange == false) { List list = UserDAL.UserRepository.Entities.Where(x => userID.Contains(x.UserID)).ToList(); List userList = new List(); foreach (var user in list) { user.Password = ("A" + user.LoginID).MD5(); SetModifyStatus(user); userList.Add(user); } UnitOfWork.BatchUpdate(userList); UnitOfWork.Commit(); } else { List list = UserDAL.UserRepository.Entities.Where(x => userID.Contains(x.UserID)).ToList(); List userList = new List(); foreach (var user in list) { user.Password = pwd.MD5(); SetModifyStatus(user); userList.Add(user); } UnitOfWork.BatchUpdate(userList); UnitOfWork.Commit(); } } } /// /// 启用 /// /// 主键ID /// public void Usable(IList userID) { if (userID.Count > 0) { UnitOfWork.Update( x => new Sys_User { RecordStatus = (int)SYS_STATUS.USABLE }, x => userID.Contains(x.UserID) ); List list = UserDAL.UserRepository.Entities.Where(x => userID.Contains(x.UserID)).ToList(); foreach (var user in list) { SetModifyStatus(user); UnitOfWork.Update(user); UnitOfWork.Commit(); } } } /// /// 禁用 /// /// 主键ID /// public void UnUsable(IList userID) { if (userID.Count > 0) { UnitOfWork.Update( x => new Sys_User { RecordStatus = (int)SYS_STATUS.UNUSABLE }, x => userID.Contains(x.UserID) ); List list = UserDAL.UserRepository.Entities.Where(x => userID.Contains(x.UserID)).ToList(); foreach (var user in list) { SetModifyStatus(user); UnitOfWork.Update(user); UnitOfWork.Commit(); } } } /// /// 保存用户角色 /// /// /// public void SaveUserRoles(Guid userID, IList roleID) { var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.Sys_Role)); var roleList = this.RoleDAL.RoleRepository.GetList(x => roleID.Contains(x.RoleID)).ToList(); user.Sys_Role = new HashSet(); roleList.ForEach(x => user.Sys_Role.Add(x)); this.UnitOfWork.Commit(); } public IList GetUserInchargeCampus(Guid userID) { var campusList = this.StaffDAL.GetUserInChargeCampus(x => x.UserID == userID).ToList(); return campusList; } public IList GetUserInchargeCollege(Guid userID) { var collegeList = this.StaffDAL.GetUserInChargeCollege(x => x.UserID == userID).ToList(); return collegeList; } public IList GetUserInchargeDepartment(Guid userID) { var departmentList = StaffDAL.GetUserInChargeDepartment(x => x.UserID == userID).ToList(); return departmentList; } public IList GetUserRoles(Guid userID) { var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.Sys_Role)); var roleList = user.Sys_Role.ToList(); return roleList; } public Sys_Role GetMaxPrivilegeRoleByUserID(Guid userID) { var role = RoleDAL.RoleRepository.GetList(x => x.Sys_User.Any(w => w.UserID == userID)) .OrderByDescending(x => x.Sys_FunctionCode.Count()) .FirstOrDefault(); return role; } /// /// 保存用户所属校区 /// /// /// public void SaveUserCampus(Guid userID, IList campusID) { var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.CF_Staff.CF_StaffManageCampus)); if (user.CF_Staff == null) throw new Exception("该用户并非教职工用户,请补全该用户的教职工信息再进行操作,或选择其他用户进行操作。"); //var campusList = this.CampusDAL.campusRepository.GetList(x => campusID.Contains(x.CampusID)).ToList(); user.CF_Staff.CF_StaffManageCampus.ToList().ForEach(x => this.UnitOfWork.Remove(x)); campusID.ForEach(x => { CF_StaffManageCampus staffManageCampus = new CF_StaffManageCampus(); staffManageCampus.StaffManageCampusID = Guid.NewGuid(); staffManageCampus.UserID = userID; staffManageCampus.CampusID = x; this.SetNewStatus(staffManageCampus); this.UnitOfWork.Add(staffManageCampus); }); this.UnitOfWork.Commit(); } /// /// 保存用户所属学院 /// /// /// public void SaveUserCollege(Guid userID, IList collegeID) { var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.CF_Staff.CF_StaffManageCollege)); if (user.CF_Staff == null) throw new Exception("该用户并非教职工用户,请补全该用户的教职工信息再进行操作,或选择其他用户进行操作。"); user.CF_Staff.CF_StaffManageCollege.ToList().ForEach(x => this.UnitOfWork.Remove(x)); collegeID.ForEach(x => { CF_StaffManageCollege staffManageCollege = new CF_StaffManageCollege(); staffManageCollege.StaffManageCollegeID = Guid.NewGuid(); staffManageCollege.UserID = userID; staffManageCollege.CollegeID = x; this.SetNewStatus(staffManageCollege); this.UnitOfWork.Add(staffManageCollege); }); this.UnitOfWork.Commit(); } /// /// 保存用户所属教研室 /// /// /// public void SaveUserDepartment(Guid userID, IList departmentID) { var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.CF_Staff.CF_StaffManageDepartment)); if (user.CF_Staff == null) throw new Exception("该用户并非教职工用户,请补全该用户的教职工信息再进行操作,或选择其他用户进行操作。"); user.CF_Staff.CF_StaffManageDepartment.ToList().ForEach(x => this.UnitOfWork.Remove(x)); departmentID.ForEach(x => { CF_StaffManageDepartment staffManageDepartment = new CF_StaffManageDepartment(); staffManageDepartment.StaffManageDepartmentID = Guid.NewGuid(); staffManageDepartment.UserID = userID; staffManageDepartment.DepartmentID = x; this.SetNewStatus(staffManageDepartment); this.UnitOfWork.Add(staffManageDepartment); }); this.UnitOfWork.Commit(); } public IList GetUserRolesView(Guid userID) { return RoleDAL.GetRoleView(x => x.Sys_User.Any(w => w.UserID == userID)).OrderBy(x => x.TypeID).ThenBy(x => x.OrderNo).ToList(); } public string GetUserEmail(Guid userID) { var user = UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.CF_Staff.CF_StaffProfile), (x => x.CF_Student.CF_StudentContact)); if (user == null) throw new Exception("指定的用户信息不存在。"); if (user.CF_Student != null && user.CF_Student.CF_StudentContact != null) { return user.CF_Student.CF_StudentContact.Email; } else if (user.CF_Staff != null && user.CF_Staff.CF_StaffProfile != null) { return user.CF_Staff.CF_StaffProfile.Email; } else { throw new Exception("用户并没有注册电子邮箱信息。"); } } public void ChangePassword(ChangePasswordView password, Guid userID) { var user = this.UserDAL.UserRepository.GetSingle(x => x.UserID == userID, (x => x.Sys_MailVerifyCode)); user.Password = password.Password.MD5(); user.Sys_MailVerifyCode = null; UnitOfWork.Commit(); } public Sys_User GetUserByID(Guid userID) { return this.UserDAL.UserRepository.Entities.Where(x => x.UserID == userID).FirstOrDefault(); } public void SynHRServices() { var exceptUser = new string[] { "admin" }; var hrUsers = CommUserRepository.GetList(x => !exceptUser.Contains(x.userid)).ToList(); var staffs = StaffDAL.staffRepository.GetList(x => !exceptUser.Contains(x.Sys_User.LoginID)) .Include(x => x.Sys_User).Include(x => x.CF_StaffProfile).ToList(); hrUsers.ForEach(x => x.status = "D"); foreach (var staff in staffs) { var hrUser = hrUsers.FirstOrDefault(x => x.userid == staff.Sys_User.LoginID); if (hrUser == null) { hrUser = new Entities.HRServices.Comm_Users(); hrUser.userid = staff.Sys_User.LoginID; hrUser.loginid = staff.Sys_User.LoginID; hrUser.createdt = DateTime.Now; this.HRUnitOfWork.Add(hrUser); } hrUser.username = staff.Sys_User.Name; if (staff.DepartmentID.HasValue) { hrUser.dpid = staff.DepartmentID.ToString(); } else { hrUser.dpid = staff.CollegeID.ToString(); } hrUser.telephone = staff.CF_StaffProfile.Telephone; hrUser.mobile = staff.CF_StaffProfile.Mobile; hrUser.email = staff.CF_StaffProfile.Email; hrUser.cardid = staff.Sys_User.LoginID; hrUser.employeeclassid = ""; hrUser.jobtypeid = "教师"; hrUser.birthday = staff.BirthDate; switch (staff.SexID) { case (int)CF_Sex.Male: hrUser.sex = "1"; break; case (int)CF_Sex.Female: hrUser.sex = "2"; break; default: hrUser.sex = "0"; break; } hrUser.orderNo = "1"; hrUser.userrole = ""; hrUser.userligion = ""; hrUser.password = staff.Sys_User.Password; hrUser.status = "A"; hrUser.updatedt = DateTime.Now; hrUser.istmpuser = false; } this.HRUnitOfWork.Commit(); } public virtual string SSOLogin() { return SSO.SSOLogin(); } } }