class CompetitionNoticeJob < ApplicationJob
  queue_as :default

  def perform
    submit_work_begin
    submit_work_end
    submit_work_result
  end

  # "竞赛通知-进入提交作品状态"
  def submit_work_begin
    Rails.logger.info "__________#{Time.now.to_s}__submit_work_begin start______________________"
    competitions = CompetitionInfo.where("start_upload_date >=? and start_upload_date <= ? ", Time.now.utc.strftime('%Y-%m-%d %H:%M:00'),(Time.now + 5.minutes).utc.strftime('%Y-%m-%d %H:%M:00'))
    competitions.each do |c|
      competition_users = CompetitionUser.where(competition_info_id: c.id)
      competition_users.each do |team|
        SendTemplateMessageJob.perform_later('CompetitionBegin', team.user_id, c.id)
      end
    end
    Rails.logger.info "__________#{Time.now.to_s}__submit_work_begin end______________________"
  end

  # "竞赛通知-截止提交作品时间前3天"
  def submit_work_end
    Rails.logger.info "__________#{Time.now.to_s}__submit_work_end start______________________"
    competitions = CompetitionInfo.where("upload_date >=? and upload_date <= ? ", (Time.now - 3.days - 5.minutes).utc.strftime('%Y-%m-%d %H:%M:00'), Time.now.utc.strftime('%Y-%m-%d %H:%M:00'))
    competitions.each do |c|
      competition_users = CompetitionUser.where(competition_info_id: c.id)
      competition_users.each do |team|
        SendTemplateMessageJob.perform_later('CompetitionReview', team.user_id, c.id)
      end
    end
    Rails.logger.info "__________#{Time.now.to_s}__submit_work_end end______________________"
  end

  # "竞赛通知-报名的竞赛进入成绩公示阶段"
  def submit_work_result
    Rails.logger.info "__________#{Time.now.to_s}__submit_work_result start______________________"
    competitions = CompetitionInfo.where("upload_date >=? and upload_date <= ? ", Time.now.utc.strftime('%Y-%m-%d %H:%M:00'), (Time.now + 5.minutes).utc.strftime('%Y-%m-%d %H:%M:00'))
    competitions.each do |c|
      competition_users = CompetitionUser.where(competition_info_id: c.id)
      competition_users.each do |team|
        SendTemplateMessageJob.perform_later('CompetitionResult', team.user_id, c.id)
      end
    end
    Rails.logger.info "__________#{Time.now.to_s}__submit_work_result end______________________"
  end

end