#coding=utf-8
namespace :issue_due_date do
  desc "send a message for Issue'due_date deadline"
  task :end_time => :environment do
    contrast_time = Time.now - 86400
    issues = Issue.where("due_date >=? and due_date <=?",contrast_time,Time.now)
    issues.each do |issue|
      if ForgeMessage.where("forge_message_type =? and forge_message_id =? and status =?", "Issue", issue.id, 1).first.nil?
        recipients = []
        assigner = User.find(issue.assigned_to_id)
        unless assigner.id == issue.author.id
          recipients << issue.author
        end
        recipients << assigner
        recipients.each do |r|
          issue.forge_messages << ForgeMessage.new(:user_id => r.id, :project_id => issue.project_id, :viewed => false, :status => 1)
        end
      end
      # issue截止时间邮件提醒
      Mailer.issue_due_date(issue).deliver
    end
  end
end
