diff --git a/app/controllers/competition_teams_controller.rb b/app/controllers/competition_teams_controller.rb index 4570366f..c6b80e73 100644 --- a/app/controllers/competition_teams_controller.rb +++ b/app/controllers/competition_teams_controller.rb @@ -43,12 +43,12 @@ class CompetitionTeamsController < ApplicationController # 课堂 student_count_subquery = CourseMember.where('course_id = courses.id AND role = 5').select('count(*)').to_sql subquery = StudentWork.where('homework_common_id = hcs.id') - .select('count(compelete_status !=0 ) as finish, count(*) as total') + .select('sum(compelete_status !=0 ) as finish, count(*) as total') .having('finish > (total / 2)').to_sql course_ids = Course.where('courses.created_at > ?', Time.parse('2018-06-01')) .where('courses.created_at <= ?', @competition.end_time) .where("(#{student_count_subquery}) >= 10") - .where("exists(select 1 from homework_commons hcs where hcs.course_id = courses.id and hcs.homework_type = 4 and exists(#{subquery}))") + .where("exists(select 1 from homework_commons hcs where hcs.course_id = courses.id and hcs.publish_time is not null and hcs.publish_time < NOW() and hcs.homework_type = 4 and exists(#{subquery}))") .joins('join course_members on course_members.course_id = courses.id and course_members.role in (1,2,3)') .where(course_members: { user_id: @team_user_ids }).pluck(:id) courses = Course.where(id: course_ids).joins(:shixun_homework_commons).where('homework_commons.publish_time < now()') @@ -366,8 +366,8 @@ class CompetitionTeamsController < ApplicationController def get_valid_course_count(ids) percentage_sql = StudentWork.where('homework_common_id = homework_commons.id and homework_commons.publish_time is not null and homework_commons.publish_time < NOW()') - .select('count(compelete_status !=0 ) as finish, count(*) as total') - .having('finish > (total / 2)').to_sql + .select('sum(compelete_status !=0 ) as finish, count(*) as total') + .having('total != 0 && finish > (total / 2)').to_sql Course.joins(shixun_homework_commons: :homework_commons_shixuns) .where('shixun_id in (?)', ids) @@ -376,8 +376,8 @@ class CompetitionTeamsController < ApplicationController end def get_valid_shixun_count(ids) - percentage_sql = StudentWork.where('homework_common_id = homework_commons.id') - .select('count(compelete_status !=0 ) as finish, count(*) as total') + percentage_sql = StudentWork.where('homework_common_id = homework_commons.id and homework_commons.publish_time is not null and homework_commons.publish_time < NOW()') + .select('sum(compelete_status !=0 ) as finish, count(*) as total') .having('finish > (total / 2)').to_sql Shixun.joins(homework_commons_shixuns: :homework_common) .where(homework_commons: { homework_type: 3 })