#coding=utf-8

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

  task :publish => :environment do
    puts "--------------------------------poll_publish start"
    # 统一设置发布时间的问卷
    polls = Poll.includes(:poll_users).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)
      if poll.unified_setting
        PollPublishNotifyJob.perform_later(poll.id, nil)
      else
        course = poll.course
        teachers = course.teachers.where.not(id: course.teacher_course_groups.select(:course_member_id))
        tid_str = ""
        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 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
      poll.course_acts << CourseActivity.new(user_id: poll.user_id, course_id: poll.course_id) if !poll.course_acts.exists?
    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?
        poll.update_attributes(:polls_status => 2) if poll.polls_status == 1
        PollPublishNotifyJob.perform_later(poll.id, [poll_group.course_group_id])
      end
    end

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

  task :nearly_end => :environment do
    puts "--------------------------------poll_nearly_end start"
    # 统一设置发布时间的问卷
    polls = Poll.includes(:poll_users).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

    #1.统一设置的截止
    polls = Poll.includes(:poll_users).where("polls_status = 2 AND end_time <=?",Time.now + 900)
    polls.each do |poll|
      poll.update_column('polls_status', 3)
      poll.poll_users.where("commit_status = 0 and start_at is not null").update_all(commit_status: 1, end_at: poll.end_time) if poll.unified_setting
    end

    #2.分班设置的截止

    # polls = Poll.includes(:poll_users).where("polls_status = 2 AND unified_setting = false AND end_time > ?",Time.now + 900)
    # poll_ids = polls.blank? ? "(-1)" : "(" + polls.map(&:id).join(",") + ")"
    # polls_group_settings = PollGroupSetting.where("end_time <= '#{Time.now}' and poll_id in #{poll_ids}")
    # polls_group_settings.each do |poll_setting|
    #   poll = poll_setting.poll
    #   if poll&.end_time <= Time.now
    #     poll.update_column('polls_status', 3)
    #   end
    #   users = poll.course.course_members.where(course_group_id: poll_setting.course_group_id)
    #   poll.poll_users.where(user_id: users.pluck(:user_id)).where("commit_status = 0 and start_at is not null").update_all(commit_status: 1, end_at: Time.now)
    #   # 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 + 900, Time.now - 900).each do |poll_setting|
      poll = poll_setting.poll
      # poll.update_column('polls_status',3)

      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.where("commit_status = 0 and start_at is not null").update_all(commit_status: 1, end_at: poll_setting.end_time)
      # 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
