#coding=utf-8


desc "纠正学生实训耗时不准的问题"

namespace :shixuns do
  task correct_the_shixun_cost_time: :environment do
    puts "myshixun_id: #{ENV['myshixun_id']}"
    if ENV['myshixun_id'].present?
      myshixun = Myshixun.find(ENV['myshixun_id'])
      myshixun.games.where(status: 2).each do |game|
        puts "open_time: #{game.open_time.to_i}"
        puts "end_time: #{game.end_time.to_i}"
        puts "game_id: #{game.id}"
        cost_time = game.end_time.to_i - game.open_time.to_i
        puts "cost_time: #{cost_time}"
        game.update_attributes!(cost_time: cost_time)
      end
    end
  end

  task correct_all_cost_time: :environment do
    # 找出误差大于2秒的数据进行更新
    games = Game.where(status: 2).where.not(cost_time: nil, open_time: nil, end_time: nil).where("cost_time > (TIME_TO_SEC(end_time - open_time) + 2)")
    games.find_each do |g|
      next if g.cost_time <= (g.end_time.to_i - g.open_time.to_i + 2)
      puts "game_id: #{g.id}"
      puts "end_time: #{g.end_time.to_i}"
      puts "open_time: #{g.open_time.to_i}"
      puts "old_cost_time: #{g.end_time.to_i - g.open_time.to_i}"
      puts "cost_time: #{g.cost_time}"
      g.update_column(:cost_time, g.end_time.to_i - g.open_time.to_i)
    end
  end
end