# 学生匿评进行申诉 消息通知
class StudentWorkScoreAppealNotifyJob < ApplicationJob
  queue_as :notify

  def perform(course_id, score_appeal_id, user_id)
    course = Course.find_by(id: course_id)
    appeal = StudentWorksScoresAppeal.find_by(id: score_appeal_id)
    user = User.find_by(id: user_id)
    return if [course, appeal, user].any?(&:blank?)
    score = appeal.student_works_score

    attrs = %i[user_id trigger_user_id container_id container_type parent_container_id parent_container_type
              belong_container_id belong_container_type tiding_type viewed status created_at updated_at]

    same_attrs = {
      trigger_user_id: user.id,
      container_id: appeal.id, container_type: 'StudentWorksScoresAppeal',
      parent_container_id: score.student_work_id, parent_container_type: 'StudentWork',
      belong_container_id: course.id, belong_container_type: 'Course',
      tiding_type: 'HomeworkCommon', viewed: 0, status: 0
    }

    student_same_attrs = {
      trigger_user_id: 0,
      container_id: appeal.id, container_type: 'StudentWorksScoresAppeal',
      parent_container_id: score.student_work_id, parent_container_type: 'StudentWork',
      belong_container_id: course.id, belong_container_type: 'Course',
      tiding_type: 'System', viewed: 0, status: 0
    }

    Tiding.bulk_insert(*attrs) do |worker|
      course.course_member(user).member_teachers.each do |teacher|
        worker.add same_attrs.merge(user_id: teacher.user_id)
      end
      worker.add student_same_attrs.merge(user_id: score.user_id)
    end
  end
end
