/// <summary> /// 作者:Kylin /// 时间:2014.04.10 /// 描述:创建培训计划 (test ing ) /// </summary> /// <param name="trainSchedule"></param> /// <param name="trainBranchRights">分配的中心Ids</param> /// <param name="createBy"></param> /// <returns></returns> public static int CreateTrainSchedule(TrainSchedule trainSchedule, string[] trainBranchRights, int createBy) { using (var edb = new EmeEntities()) { using (var tran = new TransactionScope()) { try { //针对主讲人生成订课记录 var result = 100; //Default Create Result if (trainSchedule.SpeakerId.HasValue) { if ( edb.UserSchedule.Any( p => p.BeginTime < trainSchedule.EndTime && p.EndTime > trainSchedule.BeginTime && p.Status == ConvertEnum.StatusTypeForActive && p.UserId == trainSchedule.SpeakerId)) { result = 101; //已存在其他安排 return result; } if ( edb.TrainSchedule.Any( p => p.BeginTime < trainSchedule.EndTime && p.EndTime > trainSchedule.BeginTime && p.SpeakerId == trainSchedule.SpeakerId && p.Status == ConvertEnum.StatusTypeForActive)) { result = 103; //已存在其他培训 return result; } if (edb.TrainRecord.Any(p => p.TrainSchedule.BeginTime < trainSchedule.EndTime && p.TrainSchedule.EndTime > trainSchedule.BeginTime && p.Status == ConvertEnum.StatusTypeForActive && p.UserId == trainSchedule.SpeakerId)) { return 104; //主讲人已预订其他培训 } edb.Entry(trainSchedule).State = EntityState.Added; edb.SaveChanges(); if (trainBranchRights.Length > 0) { //Create Train Branch Rights foreach (var bId in trainBranchRights.Select(CommonHelper.To<int>)) { if (bId > 0) { edb.TrainRights.Add(new TrainRights() { BranchId = bId, TrainScheduleId = trainSchedule.Id, CreateBy = createBy, CreateTime = DateTime.Now }); } } } //当主讲人是老师时直接写入主讲人订课记录,提供老师端登入,并且视为主讲人进入会议平台 if ( edb.Teacher.Any( p => p.UserId == trainSchedule.SpeakerId && p.Status == ConvertEnum.StatusTypeForActive)) { if (trainSchedule.SpeakerId != null) { var model = new TrainRecord() { TrainScheduleId = trainSchedule.Id, UserId = trainSchedule.SpeakerId.Value, MeetingAttendType = ConvertEnum.MeetingAttendTypeForSpeaker, IsUnBook = false, Status = ConvertEnum.StatusTypeForActive, CreateBy = createBy, CreateTime = DateTime.Now }; edb.Entry(model).State = EntityState.Added; } } } //写入任务 if (trainSchedule.SpeakerId != null) { var userTaskModel = new UserTask() { IsSystem = true, IsComplete = false, Status = ConvertEnum.StatusTypeForActive, BeginTime = trainSchedule.BeginTime, EndTime = trainSchedule.EndTime, TaskValue = trainSchedule.Id, TaskNum = 1, TaskType = ConvertEnum.TaskTypeForTraining, CreateBy = createBy, CreateTime = DateTime.Now, UserId = trainSchedule.SpeakerId.Value }; edb.Entry(userTaskModel).State = EntityState.Added; } result = edb.SaveChanges() > 0 ? 100 : 102; //添加需要指定中心权限 tran.Complete(); return result; } catch (Exception e) { tran.Dispose(); // 异常日志消息队列 Common.MSMQ.QueueManager.AddExceptionLog(new ExceptionLogs() { ExceptionType = CommonHelper.To<int>(ExceptionType.Function), Message = string.Format("TrainBLL-CreateTrainSchedule:{0};{1};{2}", e.Message, e.InnerException.Message, e.HelpLink), IsTreat = false, CreateBy = createBy, CreateTime = DateTime.Now }); return 105; } } } }
/// <summary> /// 作者:Vincen /// 时间:2013.12.26 /// 描述:预订指定编号的培训,通过用户编号 /// 作者:Kylin /// 时间:2014.05.04 /// 描述:添加用户计划记录 /// 时间:2014.06.24 /// 描述:添加事务及添加 userschedule 限制 /// </summary> /// <param name="trainScheduleId"></param> /// <param name="userId"></param> /// <returns></returns> public static int CreateTrainRecord(int trainScheduleId, int userId) { using (var edb = new EmeEntities()) { using (var tran = new TransactionScope(TransactionScopeOption.Required)) { try { var trainSchedule = edb.TrainSchedule.SingleOrDefault(p => p.Id == trainScheduleId && p.Status == ConvertEnum.StatusTypeForActive); if (trainSchedule == null) { return 109;//不存在 } if (!trainSchedule.IsBook) { return 108;//已关闭 } if (trainSchedule.CurrBookNum >= trainSchedule.MaxBookNum) { return 110;//已出 出超 } if (edb.TrainRecord.Any(p => p.TrainScheduleId == trainScheduleId && !p.IsUnBook && p.UserId == userId && p.Status == ConvertEnum.StatusTypeForActive)) { return 101;//已预订 } if (edb.UserSchedule.Any(p => p.BeginTime < trainSchedule.EndTime && p.EndTime > trainSchedule.BeginTime && p.Status == ConvertEnum.StatusTypeForActive && p.UserId == userId)) { return 102;//已存在其他安排 } if (edb.TrainSchedule.Any(p => p.BeginTime < trainSchedule.EndTime && p.EndTime > trainSchedule.BeginTime && p.SpeakerId == userId && p.Status == ConvertEnum.StatusTypeForActive)) { return 103; //已存在其他培训 } if (edb.TrainSchedule.Any(p => p.BeginTime < trainSchedule.EndTime && p.EndTime > trainSchedule.BeginTime && p.TrainRecord.Any(r => r.UserId == userId && !r.IsUnBook && r.Status == ConvertEnum.StatusTypeForActive) && p.Status == ConvertEnum.StatusTypeForActive)) { return 103; //已存在其他培训 } //offline training if (trainSchedule.MeetingPlatformType == ConvertEnum.MeetingPlatformTypeForOffline) { var teacherModel = edb.Teacher.SingleOrDefault(p => p.UserId == userId && p.Status == ConvertEnum.StatusTypeForActive); if (teacherModel != null) { if (teacherModel.IsFullTime) //when teacher is fulltime { if (!edb.UserWorkTime.Any( p => p.WorkDate == trainSchedule.BeginTime.Date && p.UserId == userId && p.Status == ConvertEnum.StatusTypeForActive)) { return 105; //线下培训,未排班 } if (edb.UserWorkTime.Any( p => p.WorkDate == trainSchedule.BeginTime.Date && p.UserId == userId && p.CurrentTaskNum >= p.MaxTaskNum && p.Status == ConvertEnum.StatusTypeForActive)) { return 106; //线下培训,任务量已排满 } //更新老师的排班任务量 var userWorktimes = edb.UserWorkTime.Where( p => p.WorkDate == trainSchedule.BeginTime.Date && p.UserId == userId); foreach ( var userWorktime in userWorktimes.Where( userWorktime => userWorktime.CurrentTaskNum < userWorktime.MaxTaskNum)) { userWorktime.CurrentTaskNum++; edb.Entry(userWorktime).State = EntityState.Modified; } } else //when teacher is parttime { var teachRequeire = edb.TeachRequirement.Where( p => p.TeachStartTime == trainSchedule.BeginTime && p.TeachEndTime == trainSchedule.EndTime && p.TeacherId == teacherModel.Id && p.Status == ConvertEnum.StatusTypeForActive).AsNoTracking(); if (!teachRequeire.Any()) { return 107; //兼职老师没有提报需求 } foreach (var teachRequirement in teachRequeire) { teachRequirement.IsSuccessful = true; teachRequirement.UpdateTime = DateTime.Now; edb.Entry(teachRequirement).State = EntityState.Modified; } } // 写入User Schedule edb.Entry(new UserSchedule() { UserId = userId, ScheduleType = CommonHelper.To<int>(ScheduleType.Training), BeginTime = trainSchedule.BeginTime, EndTime = trainSchedule.EndTime, Status = ConvertEnum.StatusTypeForActive, CreateBy = trainSchedule.CreateBy, CreateTime = DateTime.Now }).State = EntityState.Added; } } //offline training block end var model = new TrainRecord() { TrainScheduleId = trainScheduleId, UserId = userId, MeetingAttendType = CommonHelper.To<int>(MeetingAttendType.Attendee), IsUnBook = false, Status = ConvertEnum.StatusTypeForActive, CreateBy = userId, CreateTime = DateTime.Now }; trainSchedule.CurrBookNum = trainSchedule.CurrBookNum + 1; edb.Entry(trainSchedule).State = EntityState.Modified; edb.Entry(model).State = EntityState.Added; var result = edb.SaveChanges() > 0 ? 111 : 104; // 成功 : 失败 //写入任务 if (edb.SaveChanges() > 0) { if (trainSchedule.SpeakerId != null) { var userTaskModel = new UserTask() { IsSystem = true, IsComplete = false, Status = ConvertEnum.StatusTypeForActive, BeginTime = trainSchedule.BeginTime, EndTime = trainSchedule.EndTime, TaskValue = trainSchedule.Id, TaskNum = 1, TaskType = ConvertEnum.TaskTypeForTraining, CreateBy = userId, CreateTime = DateTime.Now, UserId = trainSchedule.SpeakerId.Value }; edb.Entry(userTaskModel).State = EntityState.Added; } edb.SaveChanges(); } tran.Complete(); return result; } catch (Exception e) { // 异常日志消息队列 Common.MSMQ.QueueManager.AddExceptionLog(new ExceptionLogs() { ExceptionType = CommonHelper.To<int>(ExceptionType.Function), Message = string.Format("TrainBLL-CreateTrainRecord:{0};{1};{2}", e.Message, e.InnerException.Message, e.HelpLink), IsTreat = false, CreateBy = userId, CreateTime = DateTime.Now }); return 104; //异常 } } } }
/// <summary> /// 作者:Kylin /// 时间:2014.04.10 /// 描述:创建培训计划 (test ing ) /// </summary> /// <param name="trainSchedule"></param> /// <param name="trainBranchRights">分配的中心Ids</param> /// <param name="editSpeaker"></param> /// <param name="updateBy"></param> /// <returns></returns> public static int UpdateTrainSchedule(TrainSchedule trainSchedule, string[] trainBranchRights, int editSpeaker, int updateBy) { using (var edb = new EmeEntities()) { using (var tran = new TransactionScope()) { try { //针对主讲人生成订课记录 if (trainSchedule.Id <= 0 || !edb.TrainSchedule.Any(p => p.Id == trainSchedule.Id && p.Status == ConvertEnum.StatusTypeForActive)) { return 106; // no schedule existed } //check online training is booked for any students if (edb.TrainSchedule.Any(p => p.Id == trainSchedule.Id && p.Status == ConvertEnum.StatusTypeForActive && p.MeetingPlatformType != trainSchedule.MeetingPlatformType && p.TrainRecord.Any(t => t.TrainScheduleId == p.Id && !t.IsUnBook && t.Status == ConvertEnum.StatusTypeForActive && t.UserId != p.SpeakerId))) { return 108; // exist anyone booked this shcedule and try to change online or offline setting } if (trainSchedule.SpeakerId.HasValue) { //Check is validate for speaker var isValidate = CheckTrainingScheduleIsValidate(trainSchedule, trainSchedule.SpeakerId == editSpeaker); if (isValidate > 0) { return isValidate; } //Delete history schedule speaker's schedule and tasks #region update schedule //update主讲人记录 var speakerRecords = edb.TrainRecord.Where(p => p.TrainScheduleId == trainSchedule.Id && p.Status == ConvertEnum.StatusTypeForActive && p.UserId == editSpeaker); if (speakerRecords.Any()) { foreach (var trainRecord in speakerRecords) { trainRecord.Status = ConvertEnum.StatusTypeForActive; trainRecord.UpdateBy = updateBy; trainRecord.UpdateTime = DateTime.Now; trainRecord.UserId = (int)trainSchedule.SpeakerId; edb.Entry(trainRecord).State = EntityState.Modified; } } else { //当主讲人是老师时直接写入主讲人订课记录,提供老师端登入,并且视为主讲人进入会议平台 if ( edb.Teacher.Any( p => p.UserId == trainSchedule.SpeakerId && p.Status == ConvertEnum.StatusTypeForActive)) { if (trainSchedule.SpeakerId != null) { var model = new TrainRecord() { TrainScheduleId = trainSchedule.Id, UserId = trainSchedule.SpeakerId.Value, MeetingAttendType = ConvertEnum.MeetingAttendTypeForSpeaker, IsUnBook = false, Status = ConvertEnum.StatusTypeForActive, CreateBy = updateBy, CreateTime = DateTime.Now }; edb.Entry(model).State = EntityState.Added; } } } var trainRights = edb.TrainRights.Where(p => p.TrainScheduleId == trainSchedule.Id); foreach (var right in trainRights) //删除历史中心权限 { edb.Entry(right).State = EntityState.Deleted; } var trainAttachment = trainSchedule.TrainAttachment; foreach (var attachment in trainAttachment) //删除历史附件 { attachment.UpdateBy = updateBy; attachment.UpdateTime = DateTime.Now; attachment.Status = ConvertEnum.StatusTypeForDelete; edb.Entry(attachment).State = EntityState.Modified; } //取消任务 var modelUserTaskList = edb.UserTask.Where(p => p.TaskType == ConvertEnum.TaskTypeForTraining && p.TaskValue == trainSchedule.Id && p.BeginTime == trainSchedule.BeginTime && p.EndTime == trainSchedule.EndTime).ToList(); foreach (var item in modelUserTaskList) { item.UpdateTime = DateTime.Now; item.UpdateBy = updateBy; item.Status = ConvertEnum.StatusTypeForDelete; edb.Entry(item).State = EntityState.Modified; } #endregion //update Schedule trainSchedule.UpdateBy = updateBy; trainSchedule.UpdateTime = DateTime.Now; edb.Entry(trainSchedule).State = EntityState.Modified; //Create BranchRights if (trainBranchRights.Length > 0) { //Create Train Branch Rights foreach (var bId in trainBranchRights.Select(CommonHelper.To<int>)) { if (bId > 0) { edb.TrainRights.Add(new TrainRights() { BranchId = bId, TrainScheduleId = trainSchedule.Id, CreateBy = updateBy, CreateTime = DateTime.Now }); } } } //写入任务 if (trainSchedule.SpeakerId != null) { var userTaskModel = new UserTask() { IsSystem = true, IsComplete = false, Status = ConvertEnum.StatusTypeForActive, BeginTime = trainSchedule.BeginTime, EndTime = trainSchedule.EndTime, TaskValue = trainSchedule.Id, TaskNum = 1, TaskType = ConvertEnum.TaskTypeForTraining, CreateBy = updateBy, CreateTime = DateTime.Now, UserId = trainSchedule.SpeakerId.Value }; edb.Entry(userTaskModel).State = EntityState.Added; } var result = edb.SaveChanges() > 0 ? 100 : 102; //添加需要指定中心权限 tran.Complete(); return result; } else { return 107;//speaker not exists } } catch (Exception e) { tran.Dispose(); // 异常日志消息队列 Common.MSMQ.QueueManager.AddExceptionLog(new ExceptionLogs() { ExceptionType = CommonHelper.To<int>(ExceptionType.Function), Message = string.Format("TrainBLL-UpdateTrainSchedule:{0};{1};{2}", e.Message, e.InnerException.Message, e.HelpLink), IsTreat = false, CreateBy = updateBy, CreateTime = DateTime.Now }); return 105; } } } }