更新同步程式

This commit is contained in:
邱博亞 2023-06-14 22:33:10 +08:00
parent bd9bab2b3e
commit 976dcf6418
1 changed files with 38 additions and 9 deletions

View File

@ -160,30 +160,50 @@ module AsiaDatabasePlugin
puts "Exhibition(show) #{et.id} saved" if et.save puts "Exhibition(show) #{et.id} saved" if et.save
end end
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 end
namespace :asia_database do namespace :asia_database do
desc "Sync asia database" desc "Sync asia database"
task :sync,[:user_names, :only_sync_data] => [:environment] do |task,args| task :sync,[:user_names, :only_sync_data] => [:environment] do |task,args|
I18n.locale = "zh_tw"
if args.user_names.present? if args.user_names.present?
asia_teachers = AsiaTeacher.where(:email_id.in=>args.user_names).to_a asia_teachers = AsiaTeacher.where(:email_id.in=>args.user_names).to_a
else else
if args.only_sync_data.blank? || (args.only_sync_data != 'true' && args.only_sync_data != true) 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| asia_depart_teachers.each do |academy_name, departs_info|
academy = AsiaAcademy.where("academy_name.zh_tw"=>academy_name).first academy = AsiaAcademy.where("academy_name.zh_tw"=>academy_name).first
if academy.nil? if academy.nil?
academy = AsiaAcademy.create({:academy_name_translations=>AsiaDatabasePlugin.localize_data(academy_name)}) academy = AsiaAcademy.create({:academy_name_translations=>AsiaDatabasePlugin.localize_data(academy_name)})
end 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 depart = AsiaDepartment.where("department_name.zh_tw"=>depart_name, "asia_academy"=>academy).first
if depart.nil? if depart.nil?
depart = AsiaDepartment.create({:department_name_translations=>AsiaDatabasePlugin.localize_data(depart_name), :asia_academy=>academy}) depart = AsiaDepartment.create({:department_name_translations=>AsiaDatabasePlugin.localize_data(depart_name), :asia_academy=>academy})
end end
user_names.each do |email_id| data_list.each do |teacher_data|
teacher_data = get_sync_data(email_id) email_id = teacher_data['ad']
emp = teacher_data["emp"] email_ids << email_id
next if emp.blank?
emp = emp[0]
asia_teacher = AsiaTeacher.where(:email_id=>email_id).first asia_teacher = AsiaTeacher.where(:email_id=>email_id).first
if asia_teacher.nil? if asia_teacher.nil?
asia_teacher = AsiaTeacher.new(:email_id=>email_id, :asia_academy=>academy, :asia_department=>depart) 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_academy = academy
asia_teacher.asia_department = depart asia_teacher.asia_department = depart
end end
asia_teacher.ukey = emp["ukey"] asia_teacher.ukey = teacher_data["ukey"]
asia_teacher.teacher_translations = {"zh_tw"=>emp["emp_nm"], "en"=>emp["emp_enm"]} asia_teacher.teacher_translations = {"zh_tw"=>teacher_data["emp_nm"], "en"=>teacher_data["emp_enm"]}
asia_teacher.save asia_teacher.save
end end
end end
end end
AsiaTeacher.where(:email_id.nin => email_ids).delete_all
end end
asia_teachers = AsiaTeacher.all.to_a asia_teachers = AsiaTeacher.all.to_a
end end
@ -242,4 +263,12 @@ namespace :asia_database do
res = net_http_get_response(uri + "?#{encrypt_base64}") res = net_http_get_response(uri + "?#{encrypt_base64}")
return JSON.parse(res.body,{object_class: AsiaDatabasePlugin::SafeHash}) return JSON.parse(res.body,{object_class: AsiaDatabasePlugin::SafeHash})
end 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 end