class MigrateCompetitionScore < ActiveRecord::Migration[5.2] def change CompetitionModule.where(name: "排行榜", hidden: 0).each do |com_module| competition = com_module.competition if competition.present? puts competition.id if competition.identifier == 'hn' || competition.identifier == 'gcc-task-2019' p_rate = 0.2 f_rate = 0.8 else p_rate = 0.15 f_rate = 0.85 end pre_stage = competition.competition_stages.where(:name => "预赛").first final_stage = competition.competition_stages.where(:name => "决赛").first competition.competition_teams.each do |team| f_score = team.competition_scores.where(:competition_stage_id => final_stage.try(:id)).first # 预赛记录 p_score = team.competition_scores.where(:competition_stage_id => pre_stage.try(:id)).first s_score = (f_score.try(:score).to_f * f_rate + p_score.try(:score).to_f * p_rate).try(:round, 2) s_spend_time = f_score.try(:cost_time).to_i + p_score.try(:cost_time).to_i CompetitionScore.create(:user_id => team.user_id, :competition_team_id => team.id, :competition_id => competition.id, :competition_stage_id => 0, :score => s_score, :cost_time => s_spend_time) end end end end end