Esempio n. 1
0
        private static Job EditJob(JobModel jobModel, string defaultSCode, EmeEntities edb)
        {
            var result = new ESBResult { Code = "000000", Message = "成功" };
            var editUserId = -111;
            using (var tran = new TransactionScope())
            {
                try
                {
                    // 查询岗位所在的部门
                    var remark = jobModel.DeptId.ToString();
                    int branchId = edb.Branch.Where(b => b.Remark == remark).Select(b => b.Id).FirstOrDefault();

                    if (branchId == 0)
                    {
                        result.Code = "EME0101";
                        result.Message = "请先同步此岗位所在的部门!";
                        return null;
                    }

                    // 是否新增(EHR部门Id和岗位Id确认唯一岗位)
                    bool isAdd = false;
                    var sCode = ESBConvert.ToSCode(jobModel.CName, jobModel.NodeCode);
                    var job = edb.Job.FirstOrDefault(b => b.BranchId == branchId && b.HrId == jobModel.JobId);

                    if (null == job)
                    {
                        isAdd = true;
                        job = new Job();
                    }

                    job.BranchId = branchId;
                    job.CName = jobModel.CName;
                    job.EName = jobModel.EName;
                    job.SCode = sCode;
                    job.IsSystem = false;
                    job.Status = jobModel.Status;

                    // 不存在新增,存在修改
                    if (isAdd)
                    {
                        job.HrId = jobModel.JobId;
                        job.Remark = "EHR同步";
                        job.CreateBy = editUserId;
                        job.CreateTime = DateTime.Now;
                        edb.Job.Add(job);
                    }
                    else
                    {
                        job.UpdateBy = editUserId;
                        job.UpdateTime = DateTime.Now;
                    }

                    edb.SaveChanges();

                    // 根据岗位简称查询角色,找不到用默认的
                    var roleId = edb.Role.Where(r => r.SCode == sCode).Select(r => r.Id).FirstOrDefault();

                    if (roleId == 0)
                    {
                        roleId = edb.Role.Where(r => r.SCode == defaultSCode).Select(r => r.Id).FirstOrDefault();
                    }

                    // 查询岗位角色关系,不存在新增,存在修改
                    var jobRole = edb.JobRoles.FirstOrDefault(jr => jr.JobId == job.Id);

                    if (null == jobRole)
                    {
                        jobRole = new JobRoles
                        {
                            JobId = job.Id,
                            RoleId = roleId,
                            Status = ConvertEnum.StatusTypeForActive,
                            CreateBy = editUserId,
                            CreateTime = DateTime.Now,
                            Remark = "EHR同步"
                        };
                        edb.JobRoles.Add(jobRole);
                    }
                    else
                    {
                        jobRole.RoleId = roleId;
                        jobRole.UpdateBy = editUserId;
                        jobRole.UpdateTime = DateTime.Now;
                    }

                    edb.SaveChanges();
                    tran.Complete();

                    return job;
                }
                catch (Exception ex)
                {
                    tran.Dispose();
                    // 异常日志消息队列
                    QueueManager.AddExceptionLog(new ExceptionLogs()
                    {
                        ExceptionType = CommonHelper.To<int>(ExceptionType.Function),
                        Message = string.Format("UserBLL-EditJob:{0};{1};{2}", ex.Message, ex.InnerException.Message, ex.HelpLink),
                        IsTreat = false,
                        CreateBy = -1,
                        CreateTime = DateTime.Now
                    });

                    result.Code = "EME0102";
                    result.Message = "数据库操作异常,请联系管理员!";
                    return null;
                }

            }
        }
Esempio n. 2
0
        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;
        }