diff --git a/lib/tasks/asia_database_tasks.rake b/lib/tasks/asia_database_tasks.rake index 938a7c1..5c492dc 100644 --- a/lib/tasks/asia_database_tasks.rake +++ b/lib/tasks/asia_database_tasks.rake @@ -160,30 +160,50 @@ module AsiaDatabasePlugin puts "Exhibition(show) #{et.id} saved" if et.save end end + def self.hashify(data_list, grouping_keys) + data_list.each_with_object({}) do |data, result| + val = result + grouping_keys[0...-1].each_with_index do |key, i| + kk = data[key] + val[kk] ||= {} + val = val[kk] + end + kk = data[grouping_keys[-1]] + val[kk] ||= [] + val[kk] << data + end + end end namespace :asia_database do desc "Sync asia database" task :sync,[:user_names, :only_sync_data] => [:environment] do |task,args| + I18n.locale = "zh_tw" if args.user_names.present? asia_teachers = AsiaTeacher.where(:email_id.in=>args.user_names).to_a else if args.only_sync_data.blank? || (args.only_sync_data != 'true' && args.only_sync_data != true) - asia_depart_teachers = JSON.parse(File.read(File.expand_path("../../../asia_depart_teachers.json", __FILE__))) + #{"ukey"=>"107100033", "ad"=>"slliu", "emp_nm"=>"劉淑玲", + #"emp_enm"=>"LIU, SHU-LING", "emp_typeNm"=>"專任", "dept_id"=>"NS00", + #"dept_college"=>"護理學院", "dept_nm"=>"護理學系", "dept_Fullnm"=>"護理學院護理學系", + #"dept_Fullenm"=>"Department of Nursing"} + asia_depart_teachers = AsiaDatabasePlugin.hashify(get_all_teacher_data, ['emp_typeNm', 'dept_college', 'dept_nm'])['專任'] + AsiaAcademy.where(:academy_name.nin => asia_depart_teachers.keys).delete_all + puts asia_depart_teachers.keys.inspect + email_ids = [] asia_depart_teachers.each do |academy_name, departs_info| academy = AsiaAcademy.where("academy_name.zh_tw"=>academy_name).first if academy.nil? academy = AsiaAcademy.create({:academy_name_translations=>AsiaDatabasePlugin.localize_data(academy_name)}) end - departs_info.each do |depart_name, user_names| + puts departs_info.keys.inspect + departs_info.each do |depart_name, data_list| depart = AsiaDepartment.where("department_name.zh_tw"=>depart_name, "asia_academy"=>academy).first if depart.nil? depart = AsiaDepartment.create({:department_name_translations=>AsiaDatabasePlugin.localize_data(depart_name), :asia_academy=>academy}) end - user_names.each do |email_id| - teacher_data = get_sync_data(email_id) - emp = teacher_data["emp"] - next if emp.blank? - emp = emp[0] + data_list.each do |teacher_data| + email_id = teacher_data['ad'] + email_ids << email_id asia_teacher = AsiaTeacher.where(:email_id=>email_id).first if asia_teacher.nil? asia_teacher = AsiaTeacher.new(:email_id=>email_id, :asia_academy=>academy, :asia_department=>depart) @@ -191,12 +211,13 @@ namespace :asia_database do asia_teacher.asia_academy = academy asia_teacher.asia_department = depart end - asia_teacher.ukey = emp["ukey"] - asia_teacher.teacher_translations = {"zh_tw"=>emp["emp_nm"], "en"=>emp["emp_enm"]} + asia_teacher.ukey = teacher_data["ukey"] + asia_teacher.teacher_translations = {"zh_tw"=>teacher_data["emp_nm"], "en"=>teacher_data["emp_enm"]} asia_teacher.save end end end + AsiaTeacher.where(:email_id.nin => email_ids).delete_all end asia_teachers = AsiaTeacher.all.to_a end @@ -242,4 +263,12 @@ namespace :asia_database do res = net_http_get_response(uri + "?#{encrypt_base64}") return JSON.parse(res.body,{object_class: AsiaDatabasePlugin::SafeHash}) end + def get_all_teacher_data + res = net_http_get_response(URI.parse("https://webap.asia.edu.tw/TchEportfolio/API/ResearchTID/Load")) + data = JSON.parse(res.body,{object_class: AsiaDatabasePlugin::SafeHash}) + if data['emp'] + return data['emp'] + end + return [] + end end \ No newline at end of file