Class: EducodeSales::ShixunsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/educode_sales/shixuns_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_admin, #authenticate_request, #current_user, #filter, #is_commissioner_above?, #paginate, #render_failure, #render_success, #subject_members, #subject_staffs, #subject_url

Methods included from ApplicationHelper

#add_businesses_score, #base_url, #collection_amount_score, #completion_rate, #current?, #disk_filename, #get_businesses_chart, #handled_data, #handled_time_data, #handled_time_data_accurate, #relative_path, #signed_amount_score, #storage_path, #url_to_avatar, #visits_score

Instance Method Details

#createObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/controllers/educode_sales/shixuns_controller.rb', line 171

def create
  begin
    ActiveRecord::Base.transaction do

      attr = {
        name: params[:name].to_s,
        category: params[:category].to_i,
        shixun_type: params[:type].to_i,
        shixun_status: params[:status].to_i,
        level: params[:level].to_i,
        link: params[:link].to_s,
        deliver_money: params[:money].to_s,
        deliver_date: params[:time],
        school_id: params[:school_id]
      }

      if params[:shixun_type].to_i == 1
        # 目前支持名称关联实训
        origin_shixun = Shixun.where(hidden: 0).find_by_name(params[:name].to_s)
        attr = origin_shixun.blank? ? attr : attr.merge({shixun_id: origin_shixun.id})
      end
      shixun = BusinessSubjectShixun.create!(attr)

      BusinessSubjectStaff.bulk_insert(:staff_id, :container_id, :container_type, :created_at, :updated_at, :category) do |d|
        params[:manage_id].split(",").each do |m|
          d.add [m.to_i, shixun.id, BusinessSubjectStaff::CONTAINER_TYPES::SHIXUN, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_MANAGE]
        end
      end
      BusinessSubjectStaff.bulk_insert(:staff_id, :container_id, :container_type, :created_at, :updated_at, :category) do |d|
        params[:producer_id].split(",").each do |m|
          d.add [m.to_i, shixun.id, BusinessSubjectStaff::CONTAINER_TYPES::SHIXUN, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_PRODUCER]
        end
      end
      BusinessSubjectStaff.bulk_insert(:staff_id, :container_id, :container_type, :created_at, :updated_at, :category) do |d|
        params[:staff_id].split(",").each do |m|
          d.add [m.to_i, shixun.id, BusinessSubjectStaff::CONTAINER_TYPES::SHIXUN, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_STAFF]
        end
      end
    end
    render_success
  rescue => e
    Rails.logger.error("操作失败:#{e}")
    render_failure("操作失败")
  end
end

#create_filterObject



218
219
220
221
222
223
224
225
# File 'app/controllers/educode_sales/shixuns_controller.rb', line 218

def create_filter
  filter_data = { "school_name" => 0, "subject_name" => 0, "reception_at" => 0, "shixun_name" => 0, "level" => 0, "shixun_status" => 0,
                  "deliver_date" => 0, "commit_dectect_time" => 0, "dectect_feedback" => 0, "shixun_producer" => 0, "shixun_manages" => 0,
                  "max_manage"=> 0, "shixun_staff" => 0 }
  filter = Filter.create_with(extras: filter_data).find_or_create_by(staff_id: @current_admin.id, clazz: "shixuns")

  gon.filter = filter.extras
end

#editObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/educode_sales/shixuns_controller.rb', line 112

def edit
  @item = BusinessSubjectShixun.find(params[:id])
  @last_dectect = ''
  if @item.last_dectect.present?
    @last_dectect = @item.last_dectect.dectect_type
  end
  @producer_list = BusinessSubjectStaff.where(container_type: BusinessSubjectStaff::CONTAINER_TYPES::SHIXUN,
                                              container_id: params[:id],
                                              category: BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_PRODUCER)
                                       .select(:staff_id)
                                       .map { |item| item[:staff_id] }

  @manage_list = BusinessSubjectStaff.where(container_type: BusinessSubjectStaff::CONTAINER_TYPES::SHIXUN,
                                            container_id: params[:id],
                                            category: BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_MANAGE)
                                     .select(:staff_id)
                                     .map { |item| item[:staff_id] }
  gon.staffs = @manages
  render layout: false
end

#have_permission?Boolean

查看cancancan Gem优化代码

Returns:

  • (Boolean)


10
11
12
13
14
# File 'app/controllers/educode_sales/shixuns_controller.rb', line 10

def have_permission?
  have_permission = can?(:shixun, EducodeSales::BusinessDeliverSubject) || can?(:all_shixun, EducodeSales::BusinessDeliverSubject)

  have_permission || @current_admin.is_admin ? "" : authorize!(:shixun, BusinessDeliverSubject)
end

#indexObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
# File 'app/controllers/educode_sales/shixuns_controller.rb', line 17

def index

  # 判断类型是否存在 1 管培 2 全职 3 专职 如果不存在就默认为1
  category = params[:q].present? ? params[:q][:category].to_i : BusinessSubjectShixun::CATEGORY_TYPE::TRAINING
  trends_category = params[:trends_category]

  gon.category = trends_category.present? ? trends_category.to_i : 1
  gon.is_admin =  @current_admin.is_admin  || ["销售总监", "副总", "生态运营总监", "方案总监", "售后总监", "课程组总监"].include?(@current_admin.role.name)

  business_subject_shixuns = BusinessSubjectShixun

  # 根据身份获取对应数据
  business_subject_shixuns  = if is_commissioner_above?("shixun")
                                business_subject_shixuns
                              else
                                # 实训本身的成员
                                smembers_ids = business_subject_shixuns.joins(shixun_members: :staff).where("educode_sales_staffs.id = #{@current_admin.id} ")
                                                                       .ids

                                # 父类课程经理
                                deliver_ids = business_subject_shixuns.joins(business_subject: [business_deliver_subject: [manages: :staff]])
                                                                      .where("educode_sales_staffs.id = #{@current_admin.id} ")
                                                                      .ids

                                subject_ids = business_subject_shixuns.joins(business_subject: [manages: :staff])
                                                                      .where("educode_sales_business_subjects.business_id is null and educode_sales_staffs.id = #{@current_admin.id} ")
                                                                      .ids

                                business_subject_shixuns.where(id: smembers_ids +  deliver_ids + subject_ids)
                              end

  @shixuns = business_subject_shixuns.left_joins(:last_dectect)
                                  .left_joins(:school, :shixun , business_subject: [:subject, business: :last_follow_up])
                                  .where(category: category)

  # if params[:q].present? && params[:q][:business_name].present?
  #   @shixuns = @shixuns.where("educode_sales_businesses.name like '%#{params[:q][:business_name]}%' ")
  # end

  # 项目级别 0 全部
  if params[:q].present? && params[:q][:level].present? && params[:q][:level].to_i != 0
    @shixuns = @shixuns.where(level: params[:q][:level])
  end
  # 项目状态 0 全部
  if params[:q].present? && params[:q][:status].present? && params[:q][:status].to_i != 0
    @shixuns = @shixuns.where(shixun_status: params[:q][:status])
  end
  if params[:q] && params[:q][:time].present?
    start_time = params[:q][:time].split(" - ")[0]
    end_time = params[:q][:time].split(" - ")[-1]
    # @shixuns = @shixuns.where("educode_sales_follow_ups.reception_at  BETWEEN '#{start_time}' AND '#{end_time}'
    #                            or educode_sales_business_subject_shixuns.deliver_date BETWEEN '#{start_time}' AND '#{end_time}' ")
    @shixuns = @shixuns.where("educode_sales_follow_ups.reception_at  BETWEEN '#{start_time}' AND '#{end_time}'")
  end
  if params[:q].present? && params[:q][:shixun_name].present?
    @shixuns = @shixuns.where("educode_sales_business_subject_shixuns.name like '%#{params[:q][:shixun_name]}%'")
  end
  if params[:q].present? && params[:q][:subject_name].present?
    @shixuns = @shixuns.where("subjects.name like '%#{params[:q][:subject_name]}%'")
  end
  if params[:q].present? && params[:q][:shixun_type].present? && params[:q][:shixun_type].to_i != 0
    @shixuns = @shixuns.where(shixun_type:params[:q][:shixun_type])
  end
  if params[:q] && params[:q][:deliver_date].present?
    start_time = params[:q][:deliver_date].split(" - ")[0]
    end_time = params[:q][:deliver_date].split(" - ")[-1]
    @shixuns = @shixuns.where("educode_sales_business_subject_shixuns.deliver_date BETWEEN '#{start_time}' AND '#{end_time}' ")
  end
  shixun_staffs = {}
  BusinessSubjectShixun.includes(business_subject:[ business_deliver_subject: [:manages, business: [:school, last_follow_up: :assign_follow_ups]] ]).find_each do |d|
    b_d_s = d.business_subject
    if b_d_s.blank?
      shixun_staffs.merge!({"#{d.id}": d.shixun_staff })
    elsif b_d_s.present? && b_d_s.business_deliver_subject.present?
      shixun_staffs.merge!({ "#{d.id}": b_d_s.business_deliver_subject.business.staff_name })
    elsif b_d_s.present?
      shixun_staffs.merge!({ "#{d.id}": b_d_s.staffs_name })
    end
  end
  @shixun_staffs = shixun_staffs.stringify_keys
  p @shixun_staffs
  @shixuns = @shixuns.select("educode_sales_business_subject_shixuns.*,
                              educode_sales_follow_ups.reception_at,
                              schools.name as school_name,
                              subjects.name as subject_name,
                              subjects.identifier as subjects_identifier,
                              shixuns.name as shixun_name,
                              shixuns.identifier as s_identifier,
                              educode_sales_shixun_dectects.created_at as shixun_dectects_time,
                              educode_sales_shixun_dectects.content as dectects_content,
                              shixuns.identifier")
                     .page(params[:page])
                     .per(params[:limit])
end

#newObject



165
166
167
168
169
# File 'app/controllers/educode_sales/shixuns_controller.rb', line 165

def new
  gon.manage = @manages
  gon.staffs = @staffs
  render layout: false
end

#updateObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/controllers/educode_sales/shixuns_controller.rb', line 133

def update
  ActiveRecord::Base.transaction do

    if params[:id].present?
      item = BusinessSubjectShixun.find(params[:id])
      if item.present?
        item.update!(category: params[:category],
                     level: params[:level],
                     deliver_money: params[:money],
                     shixun_status: params[:status],
                     deliver_date: params[:time],
                     shixun_type: params[:type])
        item.staffs.where(category: BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_MANAGE).destroy_all
        item.staffs.where(category: BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_PRODUCER).destroy_all
        BusinessSubjectStaff.bulk_insert(:staff_id, :container_id, :container_type, :created_at, :updated_at, :category) do |d|
          params[:manage_id].split(",").each do |m|
            d.add [m.to_i, params[:id], BusinessSubjectStaff::CONTAINER_TYPES::SHIXUN, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_MANAGE]
          end
        end
        BusinessSubjectStaff.bulk_insert(:staff_id, :container_id, :container_type, :created_at, :updated_at, :category) do |d|
          params[:producer_id].split(",").each do |m|
            d.add [m.to_i, params[:id], BusinessSubjectStaff::CONTAINER_TYPES::SHIXUN, Time.now, Time.now, BusinessSubjectStaff::CATEGORY_TYPES::SHIXUN_PRODUCER]
          end
        end
      end
      render_success
    else
      render_failure("操作失败")
    end
  end
end