/// <summary> /// 添加一个UserCourseRecord /// </summary> /// <param name="input">实体</param> /// <returns></returns> public async Task Create(CreateUserCourseRecordInput input) { var newmodel = new UserCourseRecord() { UserId = input.UserId, CourseId = input.CourseId, IsFavor = input.IsFavor, IsComplete = input.IsComplete, Score = 0, LearnTime = 0 }; await _repository.InsertAsync(newmodel); }
/// <summary> /// 执行减扣积分的后台任务 /// </summary> /// <param name="courseId"></param> public async Task DeductionScore(Guid courseId) { //当用户必修课程在规定时间内没有修习完成,则需要扣除积分 var course = await _courseRepository.GetAsync(courseId); if (course != null && course.LearnType != CourseLearnType.Selected && course.ComplateTime < DateTime.Now) { var chooseUserId = new List <long>(); var failRecord = new List <UserCourseRecord>(); switch (course.LearnType) { case CourseLearnType.Must: chooseUserId = course.LearnUser.Replace("u_", "").Split(",").Select(long.Parse).ToList(); break; case CourseLearnType.MustAll: chooseUserId = UserManager.Users.Select(x => x.Id).ToList(); break; } //查询有课程记录的人 var learnRecord = await _repository.GetAll() .Where(x => !x.IsDeleted && chooseUserId.Contains(x.UserId) && x.CourseId == courseId) .ToListAsync(); //将所有人分为两组,有记录但未完成课程的人,无记录的人 //未完成记录 failRecord.AddRange(learnRecord.Where(x => x.IsComplete == null)); //无记录的人 var noRecordUserId = chooseUserId.Except(learnRecord.Select(x => x.UserId)).ToList(); //将有课程记录但未完成的人标记为失败 failRecord.ToList().ForEach(async x => { x.IsComplete = false; await _repository.UpdateAsync(x); }); //将没有课程记录的人增加课程记录并标记为失败 noRecordUserId.ForEach(async x => { var norecord = new UserCourseRecord() { CourseId = course.Id, UserId = x, IsComplete = false, Score = 0, LearnTime = 0 }; await _repository.InsertAsync(norecord); //加入失败记录 failRecord.Add(norecord); }); //扣除积分 var setScore = await _courseSettingAppService.GetSetVal(course.LearnType, course.IsSpecial); failRecord.ForEach(async x => { if (setScore.ClassHourScore > 0) { await _trainScoreRecordAppService.Create(new CreateUserTrainScoreRecordInput() { FromType = TrainScoreFromType.CourseLearn, FromId = course.Id, Score = -setScore.ClassHourScore, UserId = x.UserId, BusinessId = x.Id }); } }); } else { //关闭当前任务 RecurringJob.RemoveIfExists($"usercoursefail-{courseId}"); } }
/// <summary> /// 根据主键获取实体 /// </summary> /// <param name="input">主键</param> /// <returns></returns> public async Task <UserCourseRecordOutputDto> Get(NullableIdDto <Guid> input) { var model = await _courseRepository.FirstOrDefaultAsync(x => x.Id == input.Id.Value && x.Status == -1 && !x.IsDelCourse); if (model == null) { throw new UserFriendlyException((int)ErrorCode.CodeValErr, "课程不存在!"); } var userId = (await base.GetCurrentUserAsync()).Id; var record = await _repository.FirstOrDefaultAsync(x => !x.IsDeleted && x.CourseId == model.Id && x.UserId == userId); if (record == null) { record = new UserCourseRecord() { CourseId = model.Id, UserId = userId, IsComplete = null, Score = 0, LearnTime = 0 }; await _repository.InsertAsync(record); } var result = model.MapTo <UserCourseRecordOutputDto>(); result.FavorState = record.IsFavor == null ? CourseFavorState.None : record.IsFavor == false ? CourseFavorState.Diss : CourseFavorState.Favor; //获取总的赞踩情况 var allFavorStates = await _repository.GetAll() .Where(x => !x.IsDeleted && x.CourseId == model.Id && x.IsFavor != null).Select(x => x.IsFavor) .ToListAsync(); result.AllFavor = allFavorStates.Count(x => x == true); result.AllDiss = allFavorStates.Count(x => x == false); switch (result.CourseFileType) { case CourseFileType.Doc: case CourseFileType.Pdf: result.CourseLink = (await _abpFileRelationAppService.GetListAsync(new GetAbpFilesInput() { BusinessId = model.Id.ToString(), BusinessType = (int)AbpFileBusinessType.培训课程文件 })).FirstOrDefault()?.Id.ToString(); break; } //获取我的学习时长 var myRecord = await _courseRecordDetailRepository.GetAll() .Where(x => x.CourseId == model.Id && !x.IsDeleted && x.UserId == userId) .OrderByDescending(x => x.LastModificationTime ?? x.CreationTime).FirstOrDefaultAsync(); if (myRecord != null) { result.MyLearnTime = myRecord.LearningTime; } //每次访问get必定上传一次观看次数 await _numberAppService.Create(new CreateUserCourseRecordNumberInput() { CourseId = model.Id, UserId = userId }); return(result); }