查詢關鍵字新增查詢老師名稱

This commit is contained in:
邱博亞 2023-08-21 20:36:03 +08:00
parent 6c9cb2b23d
commit ffce95eeaf
1 changed files with 45 additions and 29 deletions

View File

@ -345,20 +345,20 @@ class AsiaDatabasesController < ApplicationController
render json: { 'success' => true }.to_json render json: { 'success' => true }.to_json
end end
def filter_keywords(asia_data,select_field,keywords) def filter_keywords(asia_data,select_field,keywords)
member_fields = [] # member_fields = []
file_fields = [] # file_fields = []
link_fields = [] # link_fields = []
if select_field == nil # if select_field == nil
asia_data = asia_data.where(:slug_title=>/#{gsub_invalid_character(keywords)}/) # asia_data = asia_data.where(:slug_title=>/#{gsub_invalid_character(keywords)}/)
elsif select_field == "member_profile" # elsif select_field == "member_profile"
ms = MemberProfile.all.select{|m| m.name.include?(keywords)} # ms = MemberProfile.all.select{|m| m.name.include?(keywords)}
asia_data = asia_data.where(:member_profile_id.in=>ms.map{|m| m.id}) # asia_data = asia_data.where(:member_profile_id.in=>ms.map{|m| m.id})
elsif member_fields.include?(select_field) # elsif member_fields.include?(select_field)
ms = MemberProfile.all.select{|m| m.name.include?(keywords)} # ms = MemberProfile.all.select{|m| m.name.include?(keywords)}
m_ids = ms.map{|m| m.id.to_s } # m_ids = ms.map{|m| m.id.to_s }
tmp_asia_data = asia_data.select{|p| (p.send("#{select_field.singularize}_ids") & m_ids).count != 0} # tmp_asia_data = asia_data.select{|p| (p.send("#{select_field.singularize}_ids") & m_ids).count != 0}
asia_data = asia_data.where(:id.in=>tmp_asia_data.map{|p| p.id}) # asia_data = asia_data.where(:id.in=>tmp_asia_data.map{|p| p.id})
elsif select_field.split(".").count > 1 if select_field.split(".").count > 1
relate_name = select_field.split(".").first relate_name = select_field.split(".").first
field_name = select_field.split(".").last.gsub(/^\$+/, '') field_name = select_field.split(".").last.gsub(/^\$+/, '')
relate = relate_name.camelize.constantize relate = relate_name.camelize.constantize
@ -388,22 +388,38 @@ class AsiaDatabasesController < ApplicationController
elsif (asia_data.klass.fields[select_field].options[:type] == Integer rescue false) elsif (asia_data.klass.fields[select_field].options[:type] == Integer rescue false)
tmp_asia_data = asia_data.select{|p| p.send(select_field).to_s.include?(keywords)} tmp_asia_data = asia_data.select{|p| p.send(select_field).to_s.include?(keywords)}
asia_data = asia_data.where(:id.in=>tmp_asia_data.map{|p| p.id}) asia_data = asia_data.where(:id.in=>tmp_asia_data.map{|p| p.id})
elsif file_fields.include?(select_field) # elsif file_fields.include?(select_field)
file_field = select_field.camelize.constantize # file_field = select_field.camelize.constantize
ids1 = file_field.where(:file=>/#{gsub_invalid_character(keywords)}/).pluck(:id) # ids1 = file_field.where(:file=>/#{gsub_invalid_character(keywords)}/).pluck(:id)
ids2 = file_field.where(:title=>/#{gsub_invalid_character(keywords)}/).pluck(:id) # ids2 = file_field.where(:title=>/#{gsub_invalid_character(keywords)}/).pluck(:id)
ids = ids1 + ids2 # ids = ids1 + ids2
tmp_asia_data = asia_data.select{|p| (p.send("#{select_field}_ids") & ids).count != 0} # tmp_asia_data = asia_data.select{|p| (p.send("#{select_field}_ids") & ids).count != 0}
asia_data = asia_data.where(:id.in=>tmp_asia_data.map{|p| p.id}) # asia_data = asia_data.where(:id.in=>tmp_asia_data.map{|p| p.id})
elsif link_fields.include?(select_field) # elsif link_fields.include?(select_field)
link_field = select_field.camelize.constantize # link_field = select_field.camelize.constantize
ids1 = link_field.where(:title=>/#{gsub_invalid_character(keywords)}/).pluck(:id) # ids1 = link_field.where(:title=>/#{gsub_invalid_character(keywords)}/).pluck(:id)
ids2 = link_field.where(:url=>/#{gsub_invalid_character(keywords)}/).pluck(:id) # ids2 = link_field.where(:url=>/#{gsub_invalid_character(keywords)}/).pluck(:id)
ids = ids1 + ids2 # ids = ids1 + ids2
tmp_asia_data = asia_data.select{|p| (p.send("#{select_field}_ids") & ids).count != 0} # tmp_asia_data = asia_data.select{|p| (p.send("#{select_field}_ids") & ids).count != 0}
asia_data = asia_data.where(:id.in=>tmp_asia_data.map{|p| p.id}) # asia_data = asia_data.where(:id.in=>tmp_asia_data.map{|p| p.id})
else else
asia_data = asia_data.where(select_field=>/#{gsub_invalid_character(keywords)}/) if asia_data.klass == AsiaTeacher
asia_data = asia_data.any_of([
{
select_field=> /#{gsub_invalid_character(keywords)}/
},{
teacher => /#{gsub_invalid_character(keywords)}/
}
])
else
asia_data = asia_data.any_of([
{
select_field=> /#{gsub_invalid_character(keywords)}/
},{
:asia_teacher_id.in => AsiaTeacher.where(teacher: /#{gsub_invalid_character(keywords)}/).pluck(:id)
}
])
end
end end
return asia_data return asia_data
end end