# 竞赛通知

namespace :competition_notice do

  desc "竞赛通知-进入提交作品状态"
  task submit_work_begin: :environment do
    puts "__________#{Time.now.to_s}__submit_work_begin start______________________"
    competitions = Project.find_by_sql("select * from competitions where status=1 and start_time >='#{(Time.now - 5.minutes).strftime('%Y-%m-%d %H:%M:00')}' and start_time <= '#{Time.now.strftime('%Y-%m-%d %H:%M: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
    puts "__________#{Time.now.to_s}__submit_work_begin end______________________"
  end

  desc "竞赛通知-截止提交作品时间前3天"
  task submit_work_end: :environment do
    puts "__________#{Time.now.to_s}__submit_work_end start______________________"
    competitions = Project.find_by_sql("select * from competitions where status=1 and end_time >='#{(Time.now + 3.days - 5.minutes).strftime('%Y-%m-%d %H:%M:00')}' and end_time <= '#{(Time.now + 3.days).strftime('%Y-%m-%d %H:%M: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
    puts "__________#{Time.now.to_s}__submit_work_end end______________________"
  end

  desc "竞赛通知-报名的竞赛进入成绩公示阶段"
  task submit_work_result: :environment do
    puts "__________#{Time.now.to_s}__submit_work_result start______________________"
    competitions = Project.find_by_sql("select * from competitions where status=1 and review_time >='#{(Time.now - 5.minutes).strftime('%Y-%m-%d %H:%M:00')}' and review_time <= '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}' ")
    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
    puts "__________#{Time.now.to_s}__submit_work_result end______________________"
  end

end