# 竞赛通知

namespace :competition_notice do

  desc "竞赛通知-进入提交作品状态"
  task submit_work_begin: :environment do
    competitions = Project.find_by_sql("select * from competitions where status=1 and DATE_FORMAT(enroll_end_time,'%Y-%m-%d %h:00:00') ='#{(Time.now - 1.day).strftime('%Y-%m-%d %H:00:00')}' ")
    competitions.each do |c|
      competition_teams =  Project.find_by_sql("select * from competition_teams where competition_id=#{c.id} ")
      competition_teams.each do |team|
        SendTemplateMessageJob.perform_later('CompetitionBegin', team.user_id, c.id)
      end
    end
  end

  desc "竞赛通知-截止提交作品时间前3小时"
  task submit_work_end: :environment do
    competitions = Project.find_by_sql("select * from competitions where status=1 and DATE_FORMAT(end_time,'%Y-%m-%d %h:00:00') ='#{(Time.now + 3.hours).strftime('%Y-%m-%d %H:00:00')}' ")
    competitions.each do |c|
      competition_teams =  Project.find_by_sql("select * from competition_teams where competition_id=#{c.id} ")
      competition_teams.each do |team|
        SendTemplateMessageJob.perform_later('CompetitionReview', team.user_id, c.id)
      end
    end
  end

  desc "竞赛通知-报名的竞赛进入成绩公示阶段"
  task submit_work_result: :environment do
    competitions = Project.find_by_sql("select * from competitions where status=1 and DATE_FORMAT(review_time,'%Y-%m-%d %h:00:00') ='#{(Time.now - 1.day).strftime('%Y-%m-%d %H:00:00')}' ")
    competitions.each do |c|
      competition_teams =  Project.find_by_sql("select * from competition_teams where competition_id=#{c.id} ")
      competition_teams.each do |team|
        SendTemplateMessageJob.perform_later('CompetitionResult', team.user_id, c.id)
      end
    end
  end

end