#coding=utf-8

desc "合并高校的数据,第一个参数是： 正确高校的id, 第二个参数是： 错误高校需要合并到正确高校的id"
# 命令： bundle exec rake schools:merge_school_data f_school=1 s_school=2,3
namespace :schools do
  task merge_school_data: :environment do
    f_school = ENV['f_school'].to_i
    school = School.find_by(id: f_school)
    return if school.blank?

    s_school = ENV['s_school'].split(",")
    merge_schools = School.where(id: s_school)

    # 改变用户的学校id 和 单位
    UserExtension.where(school_id: merge_schools)
        .update_all(school_id: f_school, department_id: nil)

    # 改变课堂的学校id
    Course.where(school_id: merge_schools).update_all(school_id: f_school)

    # 实训报告表迁移数据
    s_report = SchoolReport.find_by(school_id: f_school)
    SchoolReport.where(school_id: merge_schools).each do |sr|
      s_report.update_column(:shixun_evaluate_count, (s_report.shixun_evaluate_count+sr.shixun_evaluate_count))
      sr.update(shixun_evaluate_count: 0)
    end
  end
end