/// <summary> /// 作者:Vincen /// 时间:2014.04.30 /// 描述:创建User /// --------------------- /// 描述:更新使用新的Context实列 /// </summary> /// <param name="user"></param> /// <param name="userInfo"></param> /// <param name="userBranch"></param> /// <param name="teacher"></param> /// <param name="loginUserId"></param> /// <returns></returns> public static bool CreateUser(User user, UserInfo userInfo, UserBranch userBranch, Teacher teacher, int loginUserId) { using (var tran = new TransactionScope()) { using (var edb = new EmeEntities()) { try { var isExists = edb.User.Any(p => p.UserName == user.UserName); if (isExists) return false; edb.Entry(user).State = EntityState.Added; edb.SaveChanges(); userInfo.UserId = user.Id; edb.Entry(userInfo).State = EntityState.Added; userBranch.UserId = user.Id; edb.Entry(userBranch).State = EntityState.Added; if (user.UserType == CommonHelper.To<int>(UserType.Teacher)) { teacher.UserId = user.Id; edb.Entry(teacher).State = EntityState.Added; } var result = edb.SaveChanges() > 0; tran.Complete(); return result; } catch (Exception ex) { tran.Dispose(); //Common.MSMQ.QueueManager.AddExceptionLog(new ExceptionLogs() //{ // ExceptionType = CommonHelper.To<int>(ExceptionType.Message), // Message = string.Format("UserBLL-CreateUser:{0};{1};{2}", ex.Message, ex.InnerException, ex.HelpLink), // IsTreat = false, // CreateBy = loginUserId, // CreateTime = DateTime.Now //}); return false; } } } }
public JsonResult CreateTeacher(FormCollection fc) { var branchId = fc["CreateUserBranchId"]; var userType = fc["CreateUserType"]; var userName = fc["CreateUserName"]; var cName = fc["CreateUserInfoCName"]; var eName = fc["CreateUserInfoEName"]; var mobile = fc["CreateUserInfoMobile"]; var email = fc["CreateUserInfoEmail"]; var sex = fc["CreateUserInfoSex"]; var teacherType = fc["CreateTeacherType"]; var remark = fc["CreateUserInfoRemark"]; var activeStatus = Utility.CommonHelper.To<int>(Eme.Enum.StatusType.Active); var user = new Eme.Model.Eme.User() { UserName = userName, Password = WebCommon.Global.InitPasswordNew, UserType = Utility.CommonHelper.To<int>(userType), Status = activeStatus, CreateBy = LoginUserManager.CurrLoginUser.UserId, CreateTime = DateTime.Now }; var userInfo = new UserInfo() { CName = cName, EName = eName, IconUrl = null, Sex = Utility.CommonHelper.To<int>(sex), Email = email, Birthday = null, Mobile = mobile, EducationType = null, JobStatusType = Utility.CommonHelper.To<int>(Eme.Enum.JobStatusType.AtWork), Status = activeStatus, CreateBy = LoginUserManager.CurrLoginUser.UserId, CreateTime = DateTime.Now, Remark = remark }; var userBranch = new UserBranch() { BranchId = Utility.CommonHelper.To<int>(branchId), IsMain = true, OrderNum = 0, Status = activeStatus, CreateBy = LoginUserManager.CurrLoginUser.UserId, CreateTime = DateTime.Now }; var teacher = new Model.Eme.Teacher(); if (user.UserType == Utility.CommonHelper.To<int>(Eme.Enum.UserType.Teacher)) { teacher = new Eme.Model.Eme.Teacher() { IsFullTime = false, TeacherType = Utility.CommonHelper.To<int>(teacherType), Status = activeStatus, CreateBy = LoginUserManager.CurrLoginUser.UserId, CreateTime = DateTime.Now }; } var result = UserBLL.CreateUser(user, userInfo, userBranch, teacher, LoginUserManager.CurrLoginUser.UserId); return Json(new { status = result }, JsonRequestBehavior.AllowGet); }
public static ESBResult EditEmployee(EmployeeModel employee, string ltSCode, string ftSCode, string saSCode, int headBranchId, string jobScode) { var result = new ESBResult { Code = "000000", Message = "成功" }; var editUserId = -111; if (string.IsNullOrWhiteSpace(employee.UserName)) { result.Code = "EME0101"; result.Message = "同步失败,员工的[UserName]为NULL!"; return result; } using (var edb = new EmeEntities()) { using (var tran = new TransactionScope()) { try { // 查询员工的部门 var remark = employee.DeptId.ToString(); var branchId = headBranchId; // 默认总部 var centerOrHead = edb.Branch.FirstOrDefault(b => b.Remark == remark); if (null == centerOrHead) { result.Code = "EME0101"; result.Message = "请先同步此员工所在的部门!"; return result; //先新增部门 } #region 职务(岗位)同步 // 查询员工的岗位 var job = edb.Job.FirstOrDefault(j => j.BranchId == centerOrHead.Id && j.HrId == employee.JobId); if (null == job) { //先新增岗位 var jobModel = new JobModel { JobId = employee.JobId, DeptId = employee.DeptId, CName = employee.JobName, Status = ConvertEnum.StatusTypeForActive, EName = employee.JobName, NodeCode = jobScode }; job = EditJob(jobModel, jobScode, edb); if (job == null) { result.Code = "EME0101"; result.Message = "职务同步失败!"; return result; } } #endregion while (centerOrHead.BranchType != ConvertEnum.BranchTypeForSchool && centerOrHead.BranchType != ConvertEnum.BranchTypeForArea && centerOrHead.BranchType != ConvertEnum.BranchTypeForCompany) { if (null == centerOrHead) break; centerOrHead = edb.Branch.FirstOrDefault(b => b.Id == centerOrHead.ParentId); } if (null != centerOrHead) { if (centerOrHead.BranchType == ConvertEnum.BranchTypeForSchool) branchId = centerOrHead.Id; else if (centerOrHead.BranchType == ConvertEnum.BranchTypeForArea) branchId = centerOrHead.Id; } // 员工类型默认员工 var userType = ConvertEnum.UserTypeForEmployee; var status = ConvertEnum.StatusTypeForActive; var jobStatusType = ESBConvert.ToJobStatusType(employee.JobStatus); if (job.SCode == ltSCode || job.SCode == ftSCode) userType = ConvertEnum.UserTypeForTeacher; if (jobStatusType == ConvertEnum.JobStatusTypeForLeave) status = ConvertEnum.StatusTypeForInactive; #region 同步员工基本信息(User、UserInfo、UserBranch、UserJob) #region User // 是否新增 var isAddUser = false; //根据老用户名称是否存在 2014-11-4 raymond 修改 var userName = string.IsNullOrEmpty(employee.OldName) ? employee.UserName : employee.OldName; var user = edb.User.FirstOrDefault(u => u.UserName == userName); //var user = edb.User.FirstOrDefault(u => u.UserName == employee.OldName); if (null == user) { isAddUser = true; user = new User(); } user.UserType = userType; user.Status = status; user.Email = employee.Email; user.Mobile = employee.Mobile; user.Remark = "修改前" + employee.OldName; // 不存在新增,存在修改 if (isAddUser) { user.UserName = employee.UserName; user.Password = "******"; user.Remark = "EHR同步"; user.CreateBy = editUserId; user.CreateTime = employee.CreateTime; edb.User.Add(user); edb.SaveChanges(); } else { user.IsSync = false; user.UpdateBy = editUserId; user.UpdateTime = DateTime.Now; } #endregion #region UserInfo // 是否新增 var isAddUserInfo = false; var userInfo = edb.UserInfo.FirstOrDefault(u => u.UserId == user.Id); if (null == userInfo) { isAddUserInfo = true; userInfo = new UserInfo(); } userInfo.UserId = user.Id; userInfo.EName = employee.EName; userInfo.CName = employee.CName; userInfo.Sex = ESBConvert.ToSex(employee.Sex); userInfo.Email = employee.Email; userInfo.Mobile = employee.Mobile; userInfo.Birthday = employee.Birthday; userInfo.JobStatusType = jobStatusType; userInfo.Status = status; if (isAddUserInfo) { userInfo.Remark = "EHR同步"; userInfo.CreateBy = editUserId; userInfo.CreateTime = employee.CreateTime; edb.UserInfo.Add(userInfo); } else { userInfo.UpdateBy = editUserId; userInfo.UpdateTime = DateTime.Now; } #endregion #region UserBranch // 是否新增 var isAddUserBranch = false; var userBranch = edb.UserBranch.FirstOrDefault(b => b.UserId == user.Id/* && b.BranchId == branchId*/ && b.IsMain); if (null == userBranch) { isAddUserBranch = true; userBranch = new UserBranch(); } userBranch.UserId = user.Id; userBranch.BranchId = branchId; userBranch.Status = status; userBranch.IsMain = true; if (isAddUserBranch) { userBranch.Remark = "EHR同步"; userBranch.CreateBy = editUserId; userBranch.CreateTime = employee.CreateTime; edb.UserBranch.Add(userBranch); } else { userBranch.UpdateBy = editUserId; userBranch.UpdateTime = DateTime.Now; } #endregion #region UserJob // 是否新增 var isAddUserJob = false; var userJobList = edb.UserJob.Where(u => u.UserId == user.Id).ToList(); var activeCount = userJobList.Count(u => u.Status == ConvertEnum.StatusTypeForActive); var userJob = userJobList.FirstOrDefault(u => u.JobId == job.Id); // 如果userJob数量=1,直接修改此userJob if (activeCount == 1) { userJob = userJobList.FirstOrDefault(u => u.Status == ConvertEnum.StatusTypeForActive); } if (null == userJob) { isAddUserJob = true; userJob = new UserJob(); } userJob.JobId = job.Id; userJob.Status = status; if (isAddUserJob) { userJob.UserId = user.Id; userJob.Remark = "EHR同步"; userJob.CreateBy = editUserId; userJob.CreateTime = employee.CreateTime; edb.UserJob.Add(userJob); } else { userJob.UpdateBy = editUserId; userJob.UpdateTime = DateTime.Now; } #endregion edb.SaveChanges(); #endregion #region 同步老师(Teacher、TeachRange) // 教师和学习指导同步到Teacher、TeachRange if (job.SCode == ltSCode || job.SCode == ftSCode || job.SCode == saSCode) { var teacherType = ConvertEnum.TeacherTypeForSA; if (job.SCode == ltSCode) teacherType = ConvertEnum.TeacherTypeForLT; if (job.SCode == ftSCode) teacherType = ConvertEnum.TeacherTypeForFT; // 是否新增 var isAddTeacher = false; var teacher = edb.Teacher.FirstOrDefault(t => t.UserId == user.Id); if (null == teacher) { isAddTeacher = true; teacher = new Teacher(); } teacher.UserId = user.Id; teacher.TeacherType = teacherType; teacher.Status = status; teacher.IsFullTime = true; var flag = true; //默认添加 TeachRange if (isAddTeacher) { teacher.Remark = "EHR同步"; teacher.CreateBy = editUserId; teacher.CreateTime = DateTime.Now; edb.Teacher.Add(teacher); edb.SaveChanges(); } else { teacher.UpdateBy = editUserId; teacher.UpdateTime = DateTime.Now; flag = !edb.TeachRange.Any(t => t.TeacherId == teacher.Id); } if (flag) { // 最近的同级的范围 var newSameLvelTeacherId = (from t in edb.Teacher join r in edb.TeachRange on t.Id equals r.TeacherId where t.TeacherType == teacherType && t.Status == ConvertEnum.StatusTypeForActive orderby t.Id descending select t.Id).Take(1).FirstOrDefault(); if (newSameLvelTeacherId > 0) { var rangeList = edb.TeachRange.Where(t => t.TeacherId == newSameLvelTeacherId && t.Status == ConvertEnum.StatusTypeForActive).ToList(); foreach (var range in rangeList) { var teachRange = new TeachRange { TeacherId = teacher.Id, ProductLevelId = range.ProductLevelId, Status = status, CreateBy = editUserId, CreateTime = DateTime.Now }; edb.TeachRange.Add(teachRange); } } } edb.SaveChanges(); } #endregion tran.Complete(); } catch (Exception ex) { tran.Dispose(); // 异常日志消息队列 QueueManager.AddExceptionLog(new ExceptionLogs() { ExceptionType = CommonHelper.To<int>(ExceptionType.Function), Message = string.Format("UserBLL-EditEmployee:{0};{1};{2}", ex.Message, ex.InnerException.Message, ex.HelpLink), IsTreat = false, CreateBy = -1, CreateTime = DateTime.Now }); result.Code = "EME0102"; result.Message = "数据库操作异常,请联系管理员!Cause:" + ex.Message; } } } return result; }