#coding=utf-8

namespace :poll_publish do
  desc "publish poll and end poll"

  task :publish => :environment do
    puts "--------------------------------poll_publish start"
    # 统一设置发布时间的问卷
    polls = Poll.where("publish_time is not null and polls_status = 1 and publish_time <=?",Time.now)
    polls.each do |poll|
      poll.update_attributes(:polls_status => 2)
      course = poll.course
      tid_str = ""
      course.teachers.find_each do |member|
        tid_str += "," if tid_str != ""
        tid_str += "(#{member.user_id}, #{poll.user_id}, #{poll.id}, 'Poll', #{poll.id}, 'PollPublish', #{course.id}, 'Course', 0, 'Poll', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
      end
      if poll.unified_setting
        course.students.find_each do |student|
          tid_str += "," if tid_str != ""
          tid_str += "(#{student.user_id}, #{poll.user_id}, #{poll.id}, 'Poll', #{poll.id}, 'PollPublish', #{course.id}, 'Course', 0, 'Poll', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
        end
      end
      if tid_str != ""
        tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str
        ActiveRecord::Base.connection.execute tid_sql
      end

      if poll.poll_users.count == 0
        str = ""
        course.students.find_each do |student|
          str += "," if str != ""
          str += "(#{student.user_id},#{poll.id}, 0, '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
        end

        if str != ""
          sql = "insert into poll_users (user_id, poll_id, commit_status, created_at, updated_at) values" + str
          ActiveRecord::Base.connection.execute sql
        end
      end

      if poll.course_acts.size == 0
        poll.course_acts << CourseActivity.new(:user_id => poll.user_id,:course_id => poll.course_id)
      end
    end

    # 分组设置发布时间的问卷
    poll_group_settings = PollGroupSetting.where("publish_time < ? and publish_time > ?", Time.now + 900, Time.now - 900)
    poll_group_settings.each do |poll_group|
      poll = poll_group.poll
      if poll.present?
        course = poll.course
        poll.update_attributes(:polls_status => 2) if poll.polls_status == 1

        tid_str = ""
        members = course.course_members.where(:course_group_id => poll_group.course_group_id)
        members.find_each do |member|
          tid_str += "," if tid_str != ""
          tid_str += "(#{member.user_id},#{poll.user_id}, #{poll.id}, 'Poll', #{poll.id}, 'PollPublish', #{course.id}, 'Course', 0, 'Poll', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
        end
        if tid_str != ""
          tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str
          ActiveRecord::Base.connection.execute tid_sql
        end
      end
    end

    puts "--------------------------------poll_publish end"
  end

  task :nearly_end => :environment do
    puts "--------------------------------poll_nearly_end start"
    # 统一设置发布时间的问卷
    polls = Poll.where("polls_status = 2 and unified_setting = 1 and end_time <=? and end_time > ?", Time.now + 86400, Time.now + 84600)
    polls.each do |poll|
      if poll.tidings.where(:parent_container_type => "NearlyEnd").count == 0
        course = poll.course
        tid_str = ""
        poll.poll_users.where(:commit_status => 0).find_each do |user|
          tid_str += "," if tid_str != ""
          tid_str += "(#{user.user_id}, #{poll.user_id}, #{poll.id}, 'Poll', #{poll.id}, 'NearlyEnd', #{course.id}, 'Course', 0, 'Poll', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
        end
        if tid_str != ""
          tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str
          ActiveRecord::Base.connection.execute tid_sql
        end
      end
    end

    # 分组设置发布时间的问卷
    poll_group_settings = PollGroupSetting.where("end_time <=? and end_time > ?", Time.now + 86400, Time.now + 84600)
    poll_group_settings.each do |poll_group|
      poll = poll_group.poll
      if poll.present?
        course = poll.course
        members = course.course_members.where(:course_group_id => poll_group.course_group_id)
        if poll.tidings.where(:parent_container_type => "NearlyEnd", :user_id => members.map(&:user_id)).count == 0
          tid_str = ""
          poll.poll_users.where(:commit_status => 0, :user_id => members.map(&:user_id)).find_each do |member|
            tid_str += "," if tid_str != ""
            tid_str += "(#{member.user_id},#{poll.user_id}, #{poll.id}, 'Poll', #{poll.id}, 'NearlyEnd', #{course.id}, 'Course', 0, 'Poll', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}', '#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}')"
          end
          if tid_str != ""
            tid_sql = "insert into tidings (user_id, trigger_user_id, container_id, container_type, parent_container_id, parent_container_type, belong_container_id, belong_container_type, viewed, tiding_type, created_at, updated_at) values" + tid_str
            ActiveRecord::Base.connection.execute tid_sql
          end
        end
      end
    end
    puts "--------------------------------poll_nearly_end end"
  end

  task :end => :environment do
    polls = Poll.where("polls_status = 2 and unified_setting = 1 and end_time <=?",Time.now)
    polls.each do |poll|
      poll.update_attributes(:polls_status => 3)
      poll.poll_users.each do |poll_user|
        if poll_user.commit_status == 0 && !poll_user.start_at.nil?
          poll_user.update_attributes(:commit_status => 1, :end_at => Time.now)
        end
      end
    end

    PollGroupSetting.where("end_time < ? and end_time > ?", Time.now + 1800, Time.now - 1800).each do |poll_setting|
      poll = poll_setting.poll

      users = poll.course.course_members.where(:course_group_id => poll_setting.course_group_id)
      poll_users = poll.poll_users.where(:user_id => users.map(&:user_id))

      poll_users.each do |poll_user|
        if poll_user.commit_status == 0 && !poll_user.start_at.nil?
          poll_user.update_attributes(:commit_status => 1, :end_at => Time.now)
        end
      end
    end
  end
end