Method: EducodeSales::ApplicationHelper#add_businesses_score

Defined in:
app/helpers/educode_sales/application_helper.rb

#add_businesses_score(staff_id, start_time, end_time) ⇒ Object

新增商机数 得分规则



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/helpers/educode_sales/application_helper.rb', line 85

def add_businesses_score(staff_id,start_time,end_time)
  if @current_admin.is_admin?
    @businesses = Business.all
  else
    level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level
    case level
    when '自己'
      # Business.joins(Business字段: :表的名称)----> Business表(belongs_to) Business字段关联的表(has_many)  Business字段关联的表在关联的表(belongs_to)
      business_ids = Business.joins(last_follow_up: :assign_follow_ups).where("educode_sales_assign_follow_ups.staff_id = ?", @current_admin.id).pluck(:id)
      @businesses = Business.where("educode_sales_businesses.staff_id = ? OR educode_sales_businesses.id in (?)", @current_admin.id, business_ids)
    when '区域'
      school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id)
      business_ids = Business.joins(last_follow_up: :assign_follow_ups).where("educode_sales_assign_follow_ups.staff_id = ?", @current_admin.id).pluck(:id)
      @businesses = Business.joins("JOIN departments ON educode_sales_businesses.department_id = departments.id").where("departments.school_id in (?) OR educode_sales_businesses.staff_id = #{@current_admin.id}  OR educode_sales_businesses.id in (?)", school_ids, business_ids)
    else
      @businesses = Business.all
    end
  end

  ids_a_b = Common.where(extras: %w[a_class b_class ]).pluck(:id)
  ids_c_d = Common.where(extras: %w[c_class d_class]).pluck(:id)
  @businesses_a_b_count = @businesses.joins("JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id").where("educode_sales_follow_ups.clazz_id in (?)", ids_a_b)
                                     .where("educode_sales_businesses.staff_id = ?", staff_id)
                                     .where("educode_sales_businesses.created_at >= ? and educode_sales_businesses.created_at <= ?", "#{@assessment_year}-#{start_time} 00:00:00".to_date,
                                            "#{@assessment_year}-#{end_time} 23:59:00".to_date)
  @businesses_c_d_count = @businesses.joins("JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id").where("educode_sales_follow_ups.clazz_id in (?)", ids_c_d)
                                     .where("educode_sales_businesses.staff_id = ?", staff_id)
                                     .where("educode_sales_businesses.created_at >= ? and educode_sales_businesses.created_at <= ?", "#{@assessment_year}-#{start_time} 00:00:00".to_date,
                                            "#{@assessment_year}-#{end_time} 23:59:00".to_date)
  @businesses_a_b_count.count.to_i * 10 + @businesses_c_d_count.count.to_i * 5
end