x = EducodeSales::Common.find_by(extras: EducodeSales::Common::XTYPE)&.id json.data do json.array! @customers do |d| business_ids = EducodeSales::Business.where(school_id: d.id).ids follow_ups = EducodeSales::FollowUp.where(business_id: business_ids) customer_follows = EducodeSales::CustomerFollow.where(school_id: d.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 d.school_tags.where(for_missions: true).pluck(:name).join("、") json.business EducodeSales::Business.joins(:last_follow_up).where("educode_sales_follow_ups.clazz_id != ?", x).where("educode_sales_businesses.school_id = ?", d.id).size json.follow follow_ups.size + customer_follows.size json.staff EducodeSales::CustomerExtension.find_by(school_id: d.id)&.customer_staff&.user&.real_name json.department d['department_name'] || '' json.major d['major'] || '' json.major_id d.major_id || '' json.department_id d.department_id || '' # json.teacher_counts UserExtension.where(department_id: d.department_id, identity: 'teacher').size #json.student_counts UserExtension.where(department_id: d.department_id, identity: 'student').size end end json.code 0 if params[:page].present? json.count @customers.total_count end