x = EducodeSales::Common.find_by(extras: EducodeSales::Common::XTYPE)&.id o = EducodeSales::Common.find_by(extras: EducodeSales::Common::OTYPE)&.id business_ids_group_a = EducodeSales::Business.where(department_id: @customers.map(&:department_id)).group_by(&:department_id) business_ids_group_b = EducodeSales::Business.where(school_id: @customers.map(&:id)).group_by(&:school_id) all_follow_ups = EducodeSales::FollowUp.where(business_id: business_ids_group_a.values.flatten.map(&:id) + business_ids_group_b.values.flatten.map(&:id)) customer_follows_a = EducodeSales::CustomerFollow.where(department_id: @customers.map(&:department_id)).group_by(&:department_id) customer_follows_b = EducodeSales::CustomerFollow.where(school_id: @customers.map(&:id)).group_by(&:school_id) all_school_tags = SchoolTag.joins(:school_tag_middles).where(school_tag_middles: { school_id: @customers.map(&:id) }).select(:school_id, :name, :for_missions) business_count_group_a = EducodeSales::Business.where(department_id: @customers.map(&:department_id)).group(:department_id).count business_count_group_b = EducodeSales::Business.where(school_id: @customers.map(&:id)).group(:school_id).count o_business_count_group_a = EducodeSales::Business.where(department_id: @customers.map(&:department_id)).where("educode_sales_businesses.clazz_id = ?", o).group(:department_id).count o_business_count_group_b = EducodeSales::Business.where(school_id: @customers.map(&:id)).where("educode_sales_businesses.clazz_id = ?", o).group(:school_id).count all_staffs_group = EducodeSales::CustomerExtension.where(school_id: @customers.map(&:id)).includes(customer_staff: :user).group_by(&:school_id) json.data do json.array! @customers do |d| if d['department_id'] business_ids = business_ids_group_a[d['department_id']]&.map(&:id) || [] customer_follows = customer_follows_a[d['department_id']] || [] else business_ids = business_ids_group_b[d.id]&.map(&:id) || [] customer_follows = customer_follows_b[d.id] || [] end follow_ups = all_follow_ups.select { |f| business_ids&.include?(f.business_id) } a_last_follow_time = follow_ups.last&.created_at&.to_s b_last_follow_time = customer_follows.last&.created_at&.to_s if a_last_follow_time.present? && b_last_follow_time.present? json.last_follow_time a_last_follow_time < b_last_follow_time ? b_last_follow_time : a_last_follow_time else json.last_follow_time a_last_follow_time || b_last_follow_time end json.id d.id json.name d.name json.area d.province json.property all_school_tags.select { |t| t.school_id == d.id && t.for_missions? }.pluck(:name).join("、") if d['department_id'] json.business business_count_group_a[d['department_id']] || 0 json.o_business o_business_count_group_a[d['department_id']] || 0 else json.business business_count_group_b[d.id] || 0 json.o_business o_business_count_group_b[d.id] || 0 end json.unit "#{d.name}---#{d['department_name']}" json.follow customer_follows.size json.staff all_staffs_group[d.id]&.map { |d| d.customer_staff&.user&.real_name }&.join("、") json.department d['department_name'] || '' json.major_count d['major_count'] json.department_id d['department_id'] || '' json.teacher_counts d['teacher_count'] json.student_counts d['student_count'] end end json.code 0 if params[:page].present? json.count @customers.total_count end