/// <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; } } } }
public JsonResult CreateUserTask(DateTime sDate, string description) { var model = new UserTask() { UserId = LoginUserManager.CurrLoginUser.UserId, TaskType = ConvertEnum.TaskTypeForCustom, TaskNum = 0, BeginTime = sDate, EndTime = sDate,//自定义,设置结束时间为 Description = description, IsComplete = false, CompleteBy = LoginUserManager.CurrLoginUser.UserId, IsSystem = false, Status = ConvertEnum.StatusTypeForActive, CreateBy = LoginUserManager.CurrLoginUser.UserId, CreateTime = DateTime.Now }; var result = UserBLL.CreateUserTask(model); var msg = result ? "新增成功!" : "新增失败!"; return Json(new { status = result, sDate = sDate, msg = msg }, JsonRequestBehavior.DenyGet); }
/// <summary> /// 作者:Primo /// 时间:2014.07.11 /// 描述:创建任务 /// </summary> /// <param name="userTask"></param> /// <returns></returns> public static bool CreateUserTask(UserTask userTask) { using (var edb = new EmeEntities()) { try { edb.Entry(userTask).State = EntityState.Added; var result = edb.SaveChanges() > 0; return result; } catch (Exception ex) { Common.MSMQ.QueueManager.AddExceptionLog(new ExceptionLogs() { ExceptionType = CommonHelper.To<int>(ExceptionType.Message), Message = string.Format("UserBLL-CreateUserTask:{0};{1};{2}", ex.Message, ex.InnerException, ex.HelpLink), IsTreat = false, CreateBy = userTask.CreateBy, CreateTime = DateTime.Now }); return false; } } }
/// <summary> /// 作者:Primo /// 时间:2014.07.21 /// 描述:修改我的任务 /// </summary> /// <param name="userTask"></param> /// <returns></returns> public static bool UpdateUserTask(UserTask userTask) { using (var edb = new EmeEntities()) { try { var model = edb.UserTask.FirstOrDefault(p => p.Id == userTask.Id); if (null == model) return false; model.UserId = userTask.UserId; model.TaskType = userTask.TaskType; model.TaskValue = userTask.TaskValue; model.BeginTime = userTask.BeginTime; model.EndTime = userTask.EndTime; model.Description = userTask.Description; model.IsComplete = userTask.IsComplete; model.CompleteBy = userTask.CompleteBy; model.CompleteTime = userTask.CompleteTime; model.IsSystem = userTask.IsSystem; model.Status = userTask.Status; model.UpdateBy = userTask.UpdateBy; model.UpdateTime = userTask.UpdateTime; edb.Entry(model).State = EntityState.Modified; var result = edb.SaveChanges() > 0; return result; } catch (Exception ex) { Common.MSMQ.QueueManager.AddExceptionLog(new ExceptionLogs() { ExceptionType = CommonHelper.To<int>(ExceptionType.Message), Message = string.Format("UserBLL-UpdateUserTask:{0};{1};{2}", ex.Message, ex.InnerException, ex.HelpLink), IsTreat = false, CreateBy = userTask.UpdateBy, CreateTime = DateTime.Now }); return false; } } }
/// <summary> /// 作者:Kylin /// 时间:2014.06.20 /// 描述:添加用户计划标注 /// Update Date :2014.08.23 /// </summary> /// <param name="model"></param> /// <returns></returns> public static bool CreateUserSchedule(UserSchedule model) { if (model != null) { // Step1.验证教师是否已存在相同时间段日程 if (IsExistUserScheduleByUserId(model.UserId, model.BeginTime, model.EndTime)) { return false; } } using (var tran = new TransactionScope(TransactionScopeOption.Required)) { using (var edb = new EmeEntities()) { try { if (model != null) { // Step2.添加user schedule model.Status = ConvertEnum.StatusTypeForActive; model.CreateTime = DateTime.Now; edb.Entry(model).State = EntityState.Added; var teacherModel = edb.Teacher.SingleOrDefault(p => p.UserId == model.UserId && p.Status == ConvertEnum.StatusTypeForActive); if (teacherModel != null) { if (teacherModel.IsFullTime) //when teacher is fulltime then do { //Step3.更新老师的排班任务量(听课不计工作量) if (model.ScheduleType != ConvertEnum.ScheduleTypeForObservation) { var userWorktime = edb.UserWorkTime.FirstOrDefault( p => p.WorkDate == model.BeginTime.Date && p.UserId == model.UserId && p.Status == ConvertEnum.StatusTypeForActive); if (userWorktime != null && userWorktime.CurrentTaskNum < userWorktime.MaxTaskNum) { userWorktime.CurrentTaskNum++; edb.Entry(userWorktime).State = EntityState.Modified; } else { return false; //when userWorktime.CurrentTaskNum > userWorktime.MaxTaskNum return false } } } else { var teachRequeire = edb.TeachRequirement.Where( p => p.TeachEndTime == model.BeginTime && p.TeachEndTime == model.EndTime && p.TeacherId == teacherModel.Id && p.Status == ConvertEnum.StatusTypeForActive); foreach (var teachRequirement in teachRequeire) { teachRequirement.IsSuccessful = true; teachRequirement.UpdateTime = DateTime.Now; edb.Entry(teachRequirement).State = EntityState.Modified; } } } edb.SaveChanges(); // 下面要用到 Model.Id //Step4. create user task var scheduleTypeStr = model.ScheduleType.ToString(CultureInfo.InvariantCulture); if (BaseEnum.EmeEnum<ScheduleType>.GetEnumEnumerator("Scode").Any(p => p.Value.Equals(scheduleTypeStr))) { var scheduleTypeItemEname = BaseEnum.EmeEnum<ScheduleType>.GetEnameByValue(model.ScheduleType); var taskType = BaseEnum.EmeEnum<TaskType>.GetEnumEnumerator("Ename").Single(p => p.Text == scheduleTypeItemEname); var taskNum = BaseEnum.EmeEnum<ScheduleType>.GetEnumEnumerator("Ename") .Any(p => !p.Value.Equals(taskType.Text)) ? 1 : 0; var userTaskModel = new UserTask() { IsSystem = true, IsComplete = false, Status = ConvertEnum.StatusTypeForActive, BeginTime = DateTime.Now, EndTime = DateTime.Now, TaskValue = model.Id, TaskNum = taskNum, TaskType = CommonHelper.To<int>(taskType.Value), CreateBy = model.UserId, CreateTime = DateTime.Now, UserId = model.UserId }; edb.Entry(userTaskModel).State = EntityState.Added; } } else { tran.Dispose(); return false; } var result = edb.SaveChanges() > 0; tran.Complete(); return result; } catch (Exception e) { tran.Dispose(); // 异常日志消息队列 Common.MSMQ.QueueManager.AddExceptionLog(new ExceptionLogs() { ExceptionType = CommonHelper.To<int>(ExceptionType.Function), Message = string.Format("UserBLL-CreateUserSchedule:{0};{1};{2}", e.Message, e.InnerException.Message, e.HelpLink), IsTreat = false, //CreateBy = model.UpdateBy, CreateTime = DateTime.Now }); return false; } } } }