department_ids = @customers.map(&:department_id).uniq customer_ids = @customers.map(&:id).uniq user_extensions = UserExtension.select( "COUNT(IF(user_extensions.identity = 0, user_extensions.id, 0)) AS teacher_count, COUNT(IF(user_extensions.identity = 1, user_extensions.id, 1)) AS student_count, department_id" ).where(department_id: department_ids).group("department_id").group_by(&:department_id) business_ids_group_a = EducodeSales::Business.where(department_id: department_ids).group_by(&:department_id) business_ids_group_b = EducodeSales::Business.where(school_id: customer_ids).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: department_ids).group_by(&:department_id) customer_follows_b = EducodeSales::CustomerFollow.where(school_id: customer_ids).group_by(&:school_id) all_school_tags = SchoolTag.joins(:school_tag_middles).where(school_tag_middles: { school_id: customer_ids }).select(:school_id, :name, :for_missions) o_business_count_group_a = EducodeSales::Business.where(department_id: department_ids).where("educode_sales_businesses.clazz_id = ?", @o).group(:department_id).count o_business_count_group_b = EducodeSales::Business.where(school_id: customer_ids).where("educode_sales_businesses.clazz_id = ?", @o).group(:school_id).count all_staffs_group = EducodeSales::CustomerExtension.where(school_id: customer_ids).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 d['department_business_count'] || 0 json.o_business o_business_count_group_a[d['department_id']] || 0 json.teacher_counts user_extensions[d['department_id']] && user_extensions[d['department_id']][0]['teacher_count'] || 0 json.student_counts user_extensions[d['department_id']] && user_extensions[d['department_id']][0]['student_count'] || 0 else json.business d['school_business_count'] || 0 json.o_business o_business_count_group_b[d.id] || 0 json.teacher_counts 0 json.student_counts 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'] || '' end end json.code 0 if params[:page].present? json.count @count end