require_dependency "educode_sales/application_controller" module EducodeSales class CustomersController < ApplicationController # before_action :find_school, only: [:edit, :update] # before_action :must_admin!, only: [:destroy] def new_department render layout: false end def create_department department_name = params[:department_name].to_s.strip school = School.find(params[:school_id]) return render_failure('部门名称重复') if school.departments.exists?(name: department_name) ActiveRecord::Base.transaction do department = school.departments.create!(name: department_name, is_auth: 1) ApplyAddDepartment.create!(school_id: school.id, status: 1, name: department.name, department_id: department.id, user_id: current_user.id) end render_success end def edit_department @department = Department.find(params[:id]) render layout: false end def update_department department = Department.find(params[:id]) department.update(name: params[:department_name]) render_success end def index authorize! :read, Customer respond_to do |format| format.html do common = Common.find_by(clazz: 'staff_type', name: '销售') @staffs = Staff.joins(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id]} end format.json do if @current_admin.is_admin? @customers = School else level = @current_admin.role.role_areas.find_by(clazz: '客户管理').level case level when '自己' school_ids = CustomerExtension.where(customer_staff_id: @current_admin.id).pluck(:school_id) @customers = School.where(id: school_ids) when '区域' a_school_ids = School.where(province: @current_admin.areas.pluck(:name)).ids b_school_ids = CustomerExtension.where(customer_staff_id: @current_admin.id).pluck(:school_id) school_ids = a_school_ids + b_school_ids @customers = School.where(id: school_ids) else @customers = School end end if params[:q].present? && params[:q][:name].present? @customers = @customers.where("schools.name like ?", "%#{params[:q][:name]}%") end if params[:q].present? && params[:q][:area].present? @customers = @customers.where("schools.province = ?", "#{params[:q][:area]}") end if params[:q].present? && params[:q][:staff_id].present? school_ids = EducodeSales::CustomerExtension.where(customer_staff_id: params[:q][:staff_id]).pluck(:school_id) @customers = @customers.where(id: school_ids) end # if params[:q].present? && params[:q][:date].present? # department_ids = # business_ids = EducodeSales::Business.where(department_id: department_ids).ids # follow_ups = EducodeSales::FollowUp.where(business_id: business_ids) # customer_follows = EducodeSales::CustomerFollow.where(school_id: d.id) # a_last_follow_time = follow_ups.last&.created_at&.to_s(:date) # b_last_follow_time = customer_follows.last&.created_at&.to_s(:date) # 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 # date = params[:q][:date].split(" - ") # @customers = @customers.where("educode_sales_businesses.created_at > ? AND educode_sales_businesses.created_at < ?", date[0], date[1]) # end @customers = @customers.order(id: :desc).page(params[:page]).per(params[:limit]) end end end def give common = Common.find_by(clazz: 'staff_type', name: '销售') @staffs = Staff.joins(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id]} render layout: false end def show_follow respond_to do |format| format.html do @school_name = School.find(params[:id]).name @area = School.find(params[:id]).province @staff = EducodeSales::CustomerExtension.find_by(school_id: params[:id])&.customer_staff&.user&.real_name render layout: false end format.json do @follow_ups = EducodeSales::CustomerFollow.where(school_id: params[:id]) @latest = @follow_ups.order(created_at: :desc).first # if params[:field] # @follow_ups = @follow_ups.order("#{params[:field]} #{params[:order]}") # else # @follow_ups = @follow_ups.order("created_at desc") # end @follow_ups = @follow_ups.order("created_at desc") @follow_ups = @follow_ups.page(params[:page]).per(params[:limit]) end end end def show_department @departments = School.find(params[:id]).departments # if params[:field] # @follow_ups = @follow_ups.order("#{params[:field]} #{params[:order]}") # else # @follow_ups = @follow_ups.order("created_at desc") # end @departments = @departments.order("created_at desc") @departments = @departments.page(params[:page]).per(params[:limit]) end def new_follow_record @school = School.find(params[:id]) render layout: false end def show_follow_record @follow_up = EducodeSales::CustomerFollow.find(params[:id]) @school = School.find(@follow_up.school_id) render layout: false end def edit_follow_record @follow_up = EducodeSales::CustomerFollow.find(params[:id]) @school = School.find(@follow_up.school_id) render layout: false end def new render layout: false end def create @school = School.find(params[:id]) ActiveRecord::Base.transaction do property = SchoolProperty.find_or_create_by!(school_property_params) @school = School.new @school.attributes = school_params @school.school_property = property @school.save! end end def edit @school = School.find(params[:id]) render layout: false end def update @school = School.find(params[:id]) ActiveRecord::Base.transaction do property = SchoolProperty.find_or_create_by!(school_property_params) @school.attributes = school_params @school.school_property = property @school.save! end end private def school_property_params params.require(:school).permit(:project_985, :project_211, :regular_college, :junior_college, :secondary_school, :military_school, :enterprise) end def school_params params.require(:school).permit(:name, :province, :city, :address) end end end