businesses_a_count = 0 businesses_b_count = 0 businesses_c_count = 0 businesses_d_count = 0 businesses_e_count = 0 businesses_o_count = 0 school_counts = 0 follow_school_rates = 0 follow_department_rates = 0 area_ids = EducodeSales::Common.where(clazz: 'area').ids.sort.to_s json.data do json.array! @staffs do |d| part_a_ids = EducodeSales::Business.where("educode_sales_businesses.last_follow_up_id is null AND staff_id = ?",d.id).ids part_b_ids = EducodeSales::Business.where("educode_sales_businesses.last_follow_up_id is not null").joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.staff_id = ?", d.id).pluck(:business_id) business_ids = part_a_ids + part_b_ids @businesses = EducodeSales::Business.where(id:business_ids) json.id d.id json.name d.user.real_name json.role d.role&.name # json.area d.areas.present? ? d.areas.pluck(:name).join("、") : '' if d.areas.present? if d.areas.ids.sort.to_s == area_ids json.area '全国' else json.area d.areas.pluck(:name).join("、") end else json.area '' end if @year == "全部" business_a = @businesses.joins("JOIN educode_sales_follow_ups on educode_sales_follow_ups.id = educode_sales_businesses.last_follow_up_id").where("educode_sales_follow_ups.clazz_id = ?", @a_clazz&.id).count business_b = @businesses.joins("JOIN educode_sales_follow_ups on educode_sales_follow_ups.id = educode_sales_businesses.last_follow_up_id").where("educode_sales_follow_ups.clazz_id = ?", @b_clazz&.id).count business_c = @businesses.joins("JOIN educode_sales_follow_ups on educode_sales_follow_ups.id = educode_sales_businesses.last_follow_up_id").where("educode_sales_follow_ups.clazz_id = ?", @c_clazz&.id).count business_d = @businesses.joins("JOIN educode_sales_follow_ups on educode_sales_follow_ups.id = educode_sales_businesses.last_follow_up_id").where("educode_sales_follow_ups.clazz_id = ?", @d_clazz&.id).count business_e = @businesses.joins("JOIN educode_sales_follow_ups on educode_sales_follow_ups.id = educode_sales_businesses.last_follow_up_id").where("educode_sales_follow_ups.clazz_id = ?", @e_clazz&.id).count business_o = @businesses.joins("JOIN educode_sales_follow_ups on educode_sales_follow_ups.id = educode_sales_businesses.last_follow_up_id").where("educode_sales_follow_ups.clazz_id = ?", @o_clazz&.id).count school_count = EducodeSales::CustomerExtension.joins(:school).where(customer_staff_id: d.id).size json.goal @businesses.joins("JOIN educode_sales_follow_ups on educode_sales_follow_ups.id = educode_sales_businesses.last_follow_up_id").where("educode_sales_follow_ups.clazz_id != ? AND educode_sales_follow_ups.stage_id in (?)", @x, @step_ids).sum(:total_amount).round(2) json.return_money @businesses.joins(last_follow_up: :money_plans).where("educode_sales_follow_ups.clazz_id != ?", @x).where.not("educode_sales_money_plans.clazz!= ?", 1).sum(:amount).round(2) json.school_count school_count follow_school_count = (EducodeSales::CustomerFollow.where(staff_id: d.id).pluck(:school_id) + EducodeSales::Business.where(id: EducodeSales::FollowUp.where(staff_id: d.id).pluck(:business_id).uniq).pluck(:school_id)).uniq.size json.follow_school_count follow_school_count json.follow_school_counts EducodeSales::CustomerFollow.where(staff_id: d.id).size + EducodeSales::FollowUp.where(staff_id: d.id).size follow_school_rate = (follow_school_count!=0 ? school_count.to_f * 100 / follow_school_count : 0).round(2) json.follow_school_rate follow_school_rate.to_s + '%' follow_department_rate = (follow_school_count!=0 ? school_count.to_f * 110 / follow_school_count : 0).round(2) json.follow_department_rate follow_department_rate.to_s + '%' else business_a = @businesses.joins("JOIN educode_sales_follow_ups on educode_sales_follow_ups.id = educode_sales_businesses.last_follow_up_id").where("educode_sales_follow_ups.clazz_id = ?", @a_clazz&.id).where("educode_sales_follow_ups.year = ?", @year).count business_b = @businesses.joins("JOIN educode_sales_follow_ups on educode_sales_follow_ups.id = educode_sales_businesses.last_follow_up_id").where("educode_sales_follow_ups.clazz_id = ?", @b_clazz&.id).where("educode_sales_follow_ups.year = ?", @year).count business_c = @businesses.joins("JOIN educode_sales_follow_ups on educode_sales_follow_ups.id = educode_sales_businesses.last_follow_up_id").where("educode_sales_follow_ups.clazz_id = ?", @c_clazz&.id).where("educode_sales_follow_ups.year = ?", @year).count business_d = @businesses.joins("JOIN educode_sales_follow_ups on educode_sales_follow_ups.id = educode_sales_businesses.last_follow_up_id").where("educode_sales_follow_ups.clazz_id = ?", @d_clazz&.id).where("educode_sales_follow_ups.year = ?", @year).count business_e = @businesses.joins("JOIN educode_sales_follow_ups on educode_sales_follow_ups.id = educode_sales_businesses.last_follow_up_id").where("educode_sales_follow_ups.clazz_id = ?", @e_clazz&.id).where("educode_sales_follow_ups.year = ?", @year).count business_o = @businesses.joins("JOIN educode_sales_follow_ups on educode_sales_follow_ups.id = educode_sales_businesses.last_follow_up_id").where("educode_sales_follow_ups.clazz_id = ?", @o_clazz&.id).where("educode_sales_follow_ups.year = ?", @year).count school_count = EducodeSales::CustomerExtension.where(customer_staff_id: d.id).size json.goal @businesses.joins("JOIN educode_sales_follow_ups on educode_sales_follow_ups.id = educode_sales_businesses.last_follow_up_id").where("educode_sales_follow_ups.clazz_id != ? AND educode_sales_follow_ups.stage_id in (?)", @x, @step_ids).where("educode_sales_follow_ups.bidded_date >= ? AND educode_sales_follow_ups.bidded_date <= ?", "#{@year}-01-01", "#{@year}-12-31").sum(:total_amount).round(2) json.return_money @businesses.joins(last_follow_up: :money_plans).where("educode_sales_follow_ups.clazz_id != ?", @x).where.not("educode_sales_money_plans.clazz!= ?", 1).where("educode_sales_money_plans.date_at >= ? AND educode_sales_money_plans.date_at <= ? ", "#{@year}-01-01 00:00:00", "#{@year}-12-31 23:59:59").sum(:amount).round(2) json.school_count school_count follow_school_count = (EducodeSales::CustomerFollow.where(staff_id: d.id).where("created_at >= ? AND created_at <= ?", "#{@year}-01-01 00:00:00", "#{@year}-12-31 23:59:59").pluck(:school_id) + EducodeSales::Business.where(id: EducodeSales::FollowUp.where("created_at >= ? AND created_at <= ?", "#{@year}-01-01 00:00:00", "#{@year}-12-31 23:59:59").where(staff_id: d.id).pluck(:business_id).uniq).pluck(:school_id)).uniq.size json.follow_school_count follow_school_count json.follow_school_counts EducodeSales::CustomerFollow.where(staff_id: d.id).where("created_at >= ? AND created_at <= ?", "#{@year}-01-01", "#{@year}-12-31").size + EducodeSales::FollowUp.where(staff_id: d.id).where("created_at >= ? AND created_at <= ?", "#{@year}-01-01", "#{@year}-12-31").size follow_school_rate = (follow_school_count!=0 ? school_count.to_f * 100 / follow_school_count : 0).round(2) json.follow_school_rate follow_school_rate.to_s + '%' follow_department_rate = (follow_school_count!=0 ? school_count.to_f * 110 / follow_school_count : 0).round(2) json.follow_department_rate follow_department_rate.to_s + '%' end json.business_a business_a json.business_b business_b json.business_c business_c json.business_d business_d json.business_e business_e json.business_o business_o json.a_clazz_id @a_clazz&.id json.b_clazz_id @b_clazz&.id json.c_clazz_id @c_clazz&.id json.d_clazz_id @d_clazz&.id json.e_clazz_id @e_clazz&.id json.o_clazz_id @o_clazz&.id businesses_a_count += business_a businesses_b_count += business_b businesses_c_count += business_c businesses_d_count += business_d businesses_e_count += business_e businesses_o_count += business_o school_counts += school_count follow_school_rates += follow_school_rate follow_department_rates += follow_department_rate end end json.totalRow do json.business_a businesses_a_count.to_s json.business_b businesses_b_count.to_s json.business_c businesses_c_count.to_s json.business_d businesses_d_count.to_s json.business_e businesses_e_count.to_s json.business_o businesses_o_count.to_s json.school_count school_counts.to_s json.follow_school_rate (follow_school_rates.to_f / @staffs.size).round(2).to_s + '%' json.follow_department_rate (follow_department_rates.to_f / @staffs.size).round(2).to_s + '%' end json.code 0 json.count @staffs.total_count