businesses_a_count = 0 businesses_b_count = 0 businesses_c_count = 0 businesses_d_count = 0 businesses_e_count = 0 businesses_o_count = 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 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 businesses_a_count += business_a json.business_a business_a 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 businesses_b_count += business_b json.business_b business_b 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 businesses_c_count += business_c json.business_c business_c 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 businesses_d_count += business_d json.business_d business_b 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 businesses_e_count += business_e json.business_e business_b 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 businesses_o_count += business_o json.business_o business_o 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("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 != ?", @x).sum("educode_sales_businesses.return_money").round(2) 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 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 end json.code 0 json.count @staffs.total_count