37 lines
1.2 KiB
Plaintext
37 lines
1.2 KiB
Plaintext
require 'csv'
|
|
|
|
schools = School.where(province: ["安徽","江苏"]).select(:id)
|
|
|
|
csv_title = ["id", "学校名称", "教师列表", "教师总数", "学生总数", "课堂总数", ]
|
|
|
|
schools.each do |s|
|
|
name = school.name
|
|
teacher_user = UserExtension.where(school_id: s.id, identity: :teacher)&.first.user
|
|
teacher = "#{teacher_user.try(:show_real_name)-teacher_user.try(:phone)-teacher_user.try(:)}"
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
csv_title = ["id", "用户名称", "用户电话","最后登录时间", "用户部门", "所属高校", "高校省份", "高校城市", "高校地址"]
|
|
school_ids = School.select(:id, :province).where(province: ["安徽","江苏"]).pluck(:id)
|
|
user_items = ["users.firstname","users.lastname","users.phone","users.last_login_on","departments.name","schools.name","schools.province","schools.city", "schools.address"]
|
|
|
|
|
|
|
|
users = UserExtension.where(school_id: school_ids, identity: :teacher).joins(:user,:school,:department).pluck(user_items)
|
|
|
|
csv_string = CSV.generate(:col_sep => "\t", :row_sep => "\r\n") do |csv|
|
|
csv << csv_title
|
|
users.each_with_index do |index,user|
|
|
csv << [index+1] + user
|
|
end
|
|
end
|
|
|
|
fh = File.new("/test.csv", "wb")
|
|
fh.puts NKF.nkf("-wL",csv_string)
|
|
fh.close
|
|
|
|
|