x = EducodeSales::Common.find_by(extras: EducodeSales::Common::XTYPE)&.id json.data do json.array! @customers do |d| if d['department_id'] business_ids = EducodeSales::Business.where(department_id: d['department_id']).ids customer_follows = EducodeSales::CustomerFollow.where(department_id: d['department_id']) else business_ids = EducodeSales::Business.where(school_id: d.id).ids customer_follows = EducodeSales::CustomerFollow.where(school_id: d.id) end follow_ups = EducodeSales::FollowUp.where(business_id: business_ids) 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("、") if d['department_id'] json.business EducodeSales::Business.joins(:last_follow_up).where("educode_sales_follow_ups.clazz_id != ?", x).where("educode_sales_businesses.department_id = ?", d['department_id']).size else 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 end json.unit "#{d.name}---#{d['department_name']}" # json.follow follow_ups.size + customer_follows.size json.follow customer_follows.size json.staff EducodeSales::CustomerExtension.where(school_id: 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 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