#encoding=UTF-8
class AboutHomework < ActiveRecord::Migration
  def up
    Bid.where("reward_type = 3").each do |bid|
      transaction do
        if bid.courses.first
          # 作品基础属性
          homework = HomeworkCommon.new
          homework.name = bid.name
          homework.description = bid.description
          homework.user_id =  bid.author_id
          homework.end_time = bid.deadline
          homework.publish_time = bid.deadline
          bid.open_anonymous_evaluation == 1 ? homework.homework_type = 1 : homework.homework_type = 0
          homework.late_penalty = 0
          homework.course_id = bid.courses.first.id
          homework.created_at = bid.created_on
          homework.updated_at = bid.updated_on
          homework.save

          #个人动态
          bid.acts.each do |act|
            act.act_type = homework.class.to_s
            act.act_id = homework.id
            act.save
          end

          #作业附件
          bid.attachments.each do |attach|
            attach.container = homework
            attach.save
          end

          # 匿评作业相关属性
          homework_detail_manual = HomeworkDetailManual.new
          homework_detail_manual.ta_proportion = 0.6
          homework_detail_manual.comment_status = bid.comment_status + 1
          homework_detail_manual.evaluation_start = bid.created_on
          homework_detail_manual.evaluation_end = bid.created_on
          homework_detail_manual.evaluation_num = bid.evaluation_num
          homework_detail_manual.absence_penalty = 0
          homework_detail_manual.homework_common = homework
          homework_detail_manual.save

          #作品列表
          bid.homeworks.each do |homework_attach|
            student_work = StudentWork.new
            student_work.name = homework_attach.name
            student_work.description = homework_attach.description
            student_work.user_id = homework_attach.user_id
            student_work.project_id = homework_attach.project_id
            student_work.homework_common = homework
            student_work.save

            #作品文件
            homework_attach.attachments.each do |attach|
              attach.container = student_work
              attach.save
            end

            #作品匿评列表
            homework_attach.homework_evaluations.each do |homework_evaluation|
              student_work_evaluation = StudentWorksEvaluationDistribution.new
              student_work_evaluation.user_id = homework_evaluation.user_id
              student_work_evaluation.student_work = student_work
              student_work_evaluation.save
            end

            #评分评论相关
            stars_reates = homework_attach.rates(:quality)
            if stars_reates
              stars_reates.each do |reate|
                student_work_score = StudentWorksScore.new
                student_work_score.user_id = reate.rater_id
                student_work_score.score = reate.stars * 20
                student_work_score.student_work = student_work
                rater = User.find reate.rater_id
                if rater
                  member = rater.members.where("course_id = ?",bid.courses.first.id).first
                  if member
                    role =  member.roles.first.name
                    case role
                      when "Teacher"
                        student_work_score.reviewer_role = 1
                      when "Manager"
                        student_work_score.reviewer_role = 1
                      when "TeachingAsistant"
                        student_work_score.reviewer_role = 2
                      when "Student"
                        student_work_score.reviewer_role = 3
                    end
                  else
                    student_work_score.reviewer_role = 3
                  end
                else
                  student_work_score.reviewer_role = 3
                end

                jour = homework_attach.journals_for_messages.where("is_comprehensive_evaluation = 1 and user_id = #{reate.rater_id}").order("created_on DESC").first
                if jour
                  student_work_score.comment = jour.notes
                  student_work_score.save
                  #老师反馈附件
                  homework_attach.attachments.each do |attach|
                    attach.container = student_work_score
                    attach.save
                  end
                else
                  student_work_score.save
                end
              end
            end
          end
        end
      end
    end
  end

  def down
    HomeworkCommon.destroy_all
    HomeworkDetailManual.destroy_all
    StudentWork.destroy_all
    StudentWorksScore.destroy_all
    StudentWorksEvaluationDistribution.destroy_all
  end
end
