From e121f39b0a42f392ee28f7a64f7339fd2848eec3 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Tue, 12 Nov 2019 10:19:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=99=E5=AD=A6=E7=BB=84=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../competitions/competitions_controller.rb | 78 +++++++++++++++++++ .../competition_course_course_record.rb | 2 + .../competition_course_shixun_record.rb | 2 + app/models/competition_team.rb | 1 + config/routes.rb | 1 + .../competition_extra_course_statistic.rake | 25 +++--- .../competition_course_course_record_spec.rb | 5 ++ .../competition_course_shixun_record_spec.rb | 5 ++ 8 files changed, 106 insertions(+), 13 deletions(-) create mode 100644 app/models/competition_course_course_record.rb create mode 100644 app/models/competition_course_shixun_record.rb create mode 100644 spec/models/competition_course_course_record_spec.rb create mode 100644 spec/models/competition_course_shixun_record_spec.rb diff --git a/app/controllers/competitions/competitions_controller.rb b/app/controllers/competitions/competitions_controller.rb index a92170668..7084f47b1 100644 --- a/app/controllers/competitions/competitions_controller.rb +++ b/app/controllers/competitions/competitions_controller.rb @@ -146,6 +146,21 @@ class Competitions::CompetitionsController < Competitions::BaseController end end + def export_extra_course_statistics + @competition = current_competition + @all_records = @competition.competition_teams.joins(:competition_scores, user: [user_extension: :school]) + .select("competition_teams.*, competition_scores.score, cost_time").order("score desc, cost_time desc") + + respond_to do |format| + format.xlsx{ + extra_chart_to_xlsx(@all_records) + exercise_export_name = "#{@competition.name}比赛成绩" + render xlsx: "#{exercise_export_name.strip}",template: "competitions/competitions/chart_list.xlsx.axlsx",locals: + {table_columns: @competition_head_cells, chart_lists: @competition_cells_column} + } + end + end + private def current_competition @@ -219,4 +234,67 @@ class Competitions::CompetitionsController < Competitions::BaseController @competition_cells_column.push(row_cells_column) end end + + def extra_chart_to_xlsx records + @competition_head_cells = [] + @competition_cells_column = [] + + @competition_head_cells = %w(序号 姓名 id 学校 实训数 学习人数 被fork发布的学习人数 实训有效作品数 实训应用值 课堂数 学生数量 发布的实训作业数 有效作品数 课堂应用值 总分) + + records.each_with_index do |record, index| + row_cells_column = [] + row_cells_column << index + 1 + record_user = record.user + row_cells_column << record_user.real_name + row_cells_column << record_user.login + row_cells_column << record_user.school_name + + total_score = 0 + shixun_count = 0 + shixun_member_count = 0 + shixun_fork_member_count = 0 + shixun_valid_count = 0 + shixun_score = 0 + shixun_records = record.competition_course_records.where(type: 'CompetitionCourseShixunRecord') + shixun_records.each do |shixun| + shixun_count += 1 + shixun_member_count += shixun.snapshot['myshixuns_count'].to_i + shixun_fork_member_count += shixun.snapshot['forked_myshixun_count'].to_i + shixun_valid_count += shixun.snapshot['valid_myshixun_count'].to_i + shixun_score += shixun.score.to_i + end + + row_cells_column << shixun_count + row_cells_column << shixun_member_count + row_cells_column << shixun_fork_member_count + row_cells_column << shixun_valid_count + row_cells_column << shixun_score + total_score += shixun_score + + course_count = 0 + course_member_count = 0 + course_homework_count = 0 + course_valid_count = 0 + course_score = 0 + course_records = record.competition_course_records.where(type: 'CompetitionCourseCourseRecord') + course_records.each do |course| + course_count += 1 + course_member_count += course.snapshot['members_count'].to_i + course_homework_count += course.snapshot['shixun_homework_count'].to_i + course_valid_count += course.snapshot['valid_myshixun_count'].to_i + course_score += course.score.to_i + end + + row_cells_column << course_count + row_cells_column << course_member_count + row_cells_column << course_homework_count + row_cells_column << course_valid_count + row_cells_column << course_score + + total_score += course_score + row_cells_column << total_score + + @competition_cells_column.push(row_cells_column) + end + end end \ No newline at end of file diff --git a/app/models/competition_course_course_record.rb b/app/models/competition_course_course_record.rb new file mode 100644 index 000000000..29d9e9b11 --- /dev/null +++ b/app/models/competition_course_course_record.rb @@ -0,0 +1,2 @@ +class CompetitionCourseCourseRecord < CompetitionCourseRecord +end diff --git a/app/models/competition_course_shixun_record.rb b/app/models/competition_course_shixun_record.rb new file mode 100644 index 000000000..a6ea0c6ef --- /dev/null +++ b/app/models/competition_course_shixun_record.rb @@ -0,0 +1,2 @@ +class CompetitionCourseShixunRecord < CompetitionCourseRecord +end diff --git a/app/models/competition_team.rb b/app/models/competition_team.rb index ecfdd218a..485be61ef 100644 --- a/app/models/competition_team.rb +++ b/app/models/competition_team.rb @@ -12,6 +12,7 @@ class CompetitionTeam < ApplicationRecord has_many :teachers, -> { only_teachers }, class_name: 'TeamMember' has_many :competition_prize_users, dependent: :destroy + has_many :competition_course_records, dependent: :destroy def group_team_type? team_type.zero? diff --git a/config/routes.rb b/config/routes.rb index 145819b69..0c8f61d3c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -812,6 +812,7 @@ Rails.application.routes.draw do get :md_content post :update_md_content get :charts + get :export_extra_course_statistics get :chart_rules post :update_chart_rules end diff --git a/lib/tasks/competition_extra_course_statistic.rake b/lib/tasks/competition_extra_course_statistic.rake index f561b21c6..924ca959e 100644 --- a/lib/tasks/competition_extra_course_statistic.rake +++ b/lib/tasks/competition_extra_course_statistic.rake @@ -5,8 +5,8 @@ namespace :competition do end_time = Time.parse(args[:end_time]) start_time = Time.parse('2018-06-01') - old_competition = Competition.find 3 - competition = Competition.find 10 + old_competition = Competition.find 9 + competition = Competition.find 13 old_competition_user_ids = old_competition.team_members.pluck(:user_id) @@ -23,12 +23,11 @@ namespace :competition do user_ids = shixun_user_ids + course_user_ids - user_ids.uniq.each do |user_id| - user = User.find user_id - if user - team = CompetitionTeam.create!(competition_id: competition.id, user_id: user_id, name: user.real_name) - TeamMember.create!(competition_team_id: team.id, user_id: user_id, role: 1, competition_id: competition.id) - end + users = User.joins(:user_extension).where(id: user_ids).where(user_extensions: {identity: 0}) + + users.each do |user| + team = CompetitionTeam.create!(competition_id: competition.id, user_id: user.id, name: user.real_name) + TeamMember.create!(competition_team_id: team.id, user_id: user.id, role: 1, competition_id: competition.id) end custom_logger("Start Statistic Competition Score ~") @@ -92,9 +91,9 @@ namespace :competition do user_id: shixun.user.id, username: shixun.user.show_real_name, score: score, - type: 'CompetitionCourseCourseRecord', + type: 'CompetitionCourseShixunRecord', snapshot: { - shixun: shixun.as_json(only: [:id, :name, :identifier, :fork_from])['shixun'], + shixun: shixun.as_json(only: [:id, :name, :identifier, :fork_from]), myshixuns_count: shixun.myshixuns_count.to_i, forked_myshixun_count: shixun['forked_myshixun_count'].to_i, valid_myshixun_count: myshixun_count_map.fetch(shixun.id, 0), @@ -116,7 +115,7 @@ namespace :competition do .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 < ? and hcs.homework_type = 4 and exists(#{subquery}))", end_time) .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 < ?', end_time) + courses = Course.where(id: course_ids).joins(:practice_homeworks).where('homework_commons.publish_time < ?', end_time) courses = courses.select('courses.id, courses.name, courses.members_count, count(*) shixun_homework_count') .group('courses.id').order('shixun_homework_count desc').having('shixun_homework_count > 0') @@ -140,10 +139,10 @@ namespace :competition do user_id: user.id, username: user.show_real_name, score: score, - type: 'CompetitionCourseShixunRecord', + type: 'CompetitionCourseCourseRecord', snapshot: { course: course.as_json(only: [:id, :name]), - members_count: course.members_count.to_i, + members_count: course.students.count.to_i, shixun_homework_count: course['shixun_homework_count'].to_i, valid_myshixun_count: course_myshixun_map.fetch(course.id, 0), } diff --git a/spec/models/competition_course_course_record_spec.rb b/spec/models/competition_course_course_record_spec.rb new file mode 100644 index 000000000..4a31ff596 --- /dev/null +++ b/spec/models/competition_course_course_record_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe CompetitionCourseCourseRecord, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/competition_course_shixun_record_spec.rb b/spec/models/competition_course_shixun_record_spec.rb new file mode 100644 index 000000000..5566425d6 --- /dev/null +++ b/spec/models/competition_course_shixun_record_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe CompetitionCourseShixunRecord, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end