|  |  | @ -1,27 +1,27 @@ | 
			
		
	
		
		
			
				
					
					|  |  |  | class MigrateExerciseSingleQuestionScore < ActiveRecord::Migration[5.2] |  |  |  | class MigrateExerciseSingleQuestionScore < ActiveRecord::Migration[5.2] | 
			
		
	
		
		
			
				
					
					|  |  |  |   def change |  |  |  |   def change | 
			
		
	
		
		
			
				
					
					|  |  |  |     # 删除判断、单选题中生成多条记录,但exercise_choice_id不同的数据 |  |  |  |     # # 删除判断、单选题中生成多条记录,但exercise_choice_id不同的数据 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     sql = %Q(delete from exercise_answers where (exercise_question_id, user_id) in |  |  |  |     # sql = %Q(delete from exercise_answers where (exercise_question_id, user_id) in | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             (select * from (select exercise_question_id, user_id from exercise_answers join exercise_questions eq on exercise_question_id = eq.id where eq.question_type in (0, 2) group by exercise_question_id, user_id having count(*) > 1) a) |  |  |  |     #         (select * from (select exercise_question_id, user_id from exercise_answers join exercise_questions eq on exercise_question_id = eq.id where eq.question_type in (0, 2) group by exercise_question_id, user_id having count(*) > 1) a) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |             and id not in (select * from (select min(exercise_answers.id) from exercise_answers join exercise_questions eq on exercise_question_id = eq.id where eq.question_type in (0, 2) group by exercise_question_id, user_id having count(*) > 1 order by exercise_answers.id) b)) |  |  |  |     #         and id not in (select * from (select min(exercise_answers.id) from exercise_answers join exercise_questions eq on exercise_question_id = eq.id where eq.question_type in (0, 2) group by exercise_question_id, user_id having count(*) > 1 order by exercise_answers.id) b)) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     ActiveRecord::Base.connection.execute sql |  |  |  |     # ActiveRecord::Base.connection.execute sql | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |     # | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     # 更新成绩 |  |  |  |     # # 更新成绩 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     exercise_answers = ExerciseAnswer.joins(:exercise_question).where(score: -1, exercise_questions: {question_type: [0, 2]}) |  |  |  |     # exercise_answers = ExerciseAnswer.joins(:exercise_question).where(score: -1, exercise_questions: {question_type: [0, 2]}) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     exercise_answers.includes(:exercise_choice, exercise_question: :exercise_standard_answers).find_each do |answer| |  |  |  |     # exercise_answers.includes(:exercise_choice, exercise_question: :exercise_standard_answers).find_each do |answer| | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |     # | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       question = answer.exercise_question |  |  |  |     #   question = answer.exercise_question | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       exercise_user = ExerciseUser.find_by(exercise_id: question.exercise_id, user_id: answer.user_id) |  |  |  |     #   exercise_user = ExerciseUser.find_by(exercise_id: question.exercise_id, user_id: answer.user_id) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |     # | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       if exercise_user && exercise_user.commit_status == 1 |  |  |  |     #   if exercise_user && exercise_user.commit_status == 1 | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         user_choice_position = answer.exercise_choice&.choice_position |  |  |  |     #     user_choice_position = answer.exercise_choice&.choice_position | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         if user_choice_position && (user_choice_position.to_i == question.exercise_standard_answers.take&.exercise_choice_id.to_i) |  |  |  |     #     if user_choice_position && (user_choice_position.to_i == question.exercise_standard_answers.take&.exercise_choice_id.to_i) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |           answer.update!(score: question.question_score) |  |  |  |     #       answer.update!(score: question.question_score) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |           score = exercise_user.score.to_f + question.question_score |  |  |  |     #       score = exercise_user.score.to_f + question.question_score | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |           objective_score = exercise_user.objective_score + question.question_score |  |  |  |     #       objective_score = exercise_user.objective_score + question.question_score | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |           exercise_user.update!(score: score, objective_score: objective_score) |  |  |  |     #       exercise_user.update!(score: score, objective_score: objective_score) | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |         end |  |  |  |     #     end | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |       end |  |  |  |     #   end | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     end |  |  |  |     # end | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |   end |  |  |  |   end | 
			
		
	
		
		
			
				
					
					|  |  |  | end |  |  |  | end | 
			
		
	
	
		
		
			
				
					|  |  | 
 |