@ -17,22 +17,27 @@ class CompetitionTeamsController < ApplicationController
@team_user_ids = @team . team_members . pluck ( :user_id )
@team_user_ids = @team . team_members . pluck ( :user_id )
shixuns = Shixun . where ( user_id : @team_user_ids , status : 2 )
shixuns = Shixun . where ( user_id : @team_user_ids , status : 2 ) . where ( 'shixuns.created_at > ?' , Time . parse ( '2018-06-01' ) )
shixuns = shixuns . joins ( 'left join shixuns forked_shixuns on forked_shixuns.fork_from = shixuns.id and forked_shixuns.status = 2' )
shixuns = shixuns . joins ( 'left join shixuns forked_shixuns on forked_shixuns.fork_from = shixuns.id and forked_shixuns.status = 2' )
shixuns = shixuns . joins ( 'left join myshixuns on myshixuns.shixun_id = shixuns.id and exists(select 1 from games where games.myshixun_id = myshixuns.id and games.status = 2)' )
shixuns = shixuns . select ( 'shixuns.id, shixuns.identifier, shixuns.user_id, shixuns.myshixuns_count, shixuns.name, shixuns.fork_from, sum(forked_shixuns.myshixuns_count) forked_myshixun_count' )
shixuns = shixuns . select ( 'shixuns.id, shixuns.identifier, shixuns.user_id, shixuns.myshixuns_count, shixuns.name, shixuns.fork_from, sum(forked_shixuns.myshixuns_count) forked_myshixun_count' )
@shixuns = shixuns . group ( 'shixuns.id' ) . order ( 'shixuns.myshixuns_count desc' ) . includes ( :creator )
@shixuns = shixuns . group ( 'shixuns.id' ) . order ( 'shixuns.myshixuns_count desc' ) . includes ( :creator )
@myshixun_count_map = Myshixun . where ( shixun_id : @shixuns . map ( & :id ) )
. where ( 'exists(select 1 from games where games.myshixun_id = myshixuns.id and games.status = 2)' )
shixun_ids = @shixuns . map ( & :id )
. group ( 'shixun_id' ) . count
@myshixun_count_map = get_valid_myshixun_count ( shixun_ids )
# forked shixun valid myshixun count
forked_shixun_map = Shixun . where ( status : 2 , fork_from : shixun_ids ) . select ( 'id, fork_from' )
forked_shixun_map = forked_shixun_map . each_with_object ( { } ) { | sx , obj | obj [ sx . id ] = sx . fork_from }
forked_myshixun_count_map = get_valid_myshixun_count ( forked_shixun_map . keys )
forked_myshixun_count_map . each { | k , v | @myshixun_count_map [ forked_shixun_map [ k ] ] += v }
# todo: 使用新版course_members
# todo: 使用新版course_members
course_ids = Course . joins ( 'join members on members.course_id = courses.id' )
course_ids = Course . where ( 'courses.created_at > ?' , Time . parse ( '2018-06-01' ) )
. joins ( 'join members on members.course_id = courses.id' )
. joins ( 'join member_roles on member_roles.member_id = members.id and member_roles.role_id in (3,7,9)' )
. joins ( 'join member_roles on member_roles.member_id = members.id and member_roles.role_id in (3,7,9)' )
. where ( members : { user_id : @team_user_ids } ) . pluck ( :id )
. where ( members : { user_id : @team_user_ids } ) . pluck ( :id )
courses = Course . where ( id : course_ids ) . joins ( :shixun_homework_commons ) . where ( 'homework_commons.publish_time < now()' )
courses = Course . where ( id : course_ids ) . joins ( :shixun_homework_commons ) . where ( 'homework_commons.publish_time < now()' )
@courses = courses . select ( 'courses.id, courses.name, courses.members_count, count(*) shixun_homework_count' )
@courses = courses . select ( 'courses.id, courses.name, courses.members_count, count(*) shixun_homework_count' )
. group ( 'courses.id' ) . order ( 'shixun_homework_count desc' )
. group ( 'courses.id' ) . order ( 'shixun_homework_count desc' ) . having ( 'shixun_homework_count > 0' )
@course_myshixun_map = Myshixun . joins ( student_works : :homework_common )
@course_myshixun_map = Myshixun . joins ( student_works : :homework_common )
. where ( homework_commons : { course_id : @courses . map ( & :id ) } )
. where ( homework_commons : { course_id : @courses . map ( & :id ) } )
@ -322,4 +327,10 @@ class CompetitionTeamsController < ApplicationController
true
true
end
end
def get_valid_myshixun_count ( ids )
Myshixun . where ( shixun_id : ids )
. where ( 'exists(select 1 from games where games.myshixun_id = myshixuns.id and games.status = 2)' )
. group ( 'shixun_id' ) . count
end
end
end