예제 #1
0
        /// <summary>
        /// 我的统计积分-查询db
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        private async Task <UserCourseStatisticsOutputDto> GetMyStatisticsScoreFromDb(long userId)
        {
            var result = new UserCourseStatisticsOutputDto();
            //我的统计积分来自于必修课程与选修课程,必修课程的积分来自于全员必修和部分必修
            //先查询所有已修完毕的记录
            var complateSourceList = await(from b in _repository.GetAll().Where(x => !x.IsDeleted && x.UserId == userId && x.IsComplete == true)
                                           join c in _courseRepository.GetAll().Where(x => !x.IsDeleted && x.Status == -1 && !x.IsDelCourse) on b.CourseId equals c.Id
                                           select new
            {
                c.LearnType
            }).ToListAsync();

            //我的已修必修课数
            result.RequiredComplateSourceScore = complateSourceList
                                                 .Count(x => x.LearnType == CourseLearnType.Must || x.LearnType == CourseLearnType.MustAll);
            //所有必修数
            var userIdStr = "u_" + userId;

            result.RequiredAllSourceScore = await(from a in _courseRepository.GetAll().Where(x =>
                                                                                             !x.IsDeleted && !x.IsDelCourse &&
                                                                                             ((x.LearnType == CourseLearnType.Must && x.LearnUser.GetStrContainsArray(userIdStr)) ||
                                                                                              x.LearnType == CourseLearnType.MustAll) && x.Status == -1)
                                                  select a
                                                  ).CountAsync();
            //我的已修选修课数
            result.ElectiveComplateSourceScore = complateSourceList
                                                 .Count(x => x.LearnType == CourseLearnType.Selected);
            //所有我观看过的选修数
            var setElectiveIsSpecial = (await _courseSettingAppService.GetSetVal(CourseLearnType.Selected, true))
                                       .ClassHourScore;
            var setElectiveNotSpecial = (await _courseSettingAppService.GetSetVal(CourseLearnType.Selected, false))
                                        .ClassHourScore;

            result.ElectiveAllSourceScore =
                await(from a in _repository.GetAll().Where(x => !x.IsDeleted && x.UserId == userId)
                      join c in _courseRepository.GetAll()
                      .Where(x => !x.IsDeleted && !x.IsDelCourse && x.LearnType == CourseLearnType.Selected && x.Status == -1) on a.CourseId equals c.Id
                      select c.IsSpecial ? setElectiveIsSpecial : setElectiveNotSpecial
                      ).CountAsync();
            //我的课数
            result.MyAllSourceScore      = result.RequiredAllSourceScore + result.ElectiveAllSourceScore;
            result.MyComplateSourceScore = result.RequiredComplateSourceScore + result.ElectiveComplateSourceScore;
            //学习时长
            result.LearnTime = await _courseRecordDetailRepository.GetAll()
                               .Where(x => !x.IsDeleted && x.UserId == userId)
                               .Select(x => new
            {
                x.LearningTime,
                x.CourseId,
            }).GroupBy(x => x.CourseId)
                               .Select(x => x.OrderByDescending(y => y.LearningTime).FirstOrDefault().LearningTime).SumAsync();

            //本周排名
            var myRank = await _courseAppService.GetCourseWeekRankById(userId);

            result.ThisWeekRank = myRank?.Rank ?? -1;
            return(result);
        }