require_dependency "educode_sales/application_controller" module EducodeSales class CustomersController < ApplicationController 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.all 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.all 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? ids = EducodeSales::CustomerFollow.all.pluck(:school_id) @customers = @customers.where(id: ids) school_ids =[] date = params[:q][:date].split(" - ") @customers.each do |d| business_ids = EducodeSales::Business.where(school_id: d.id).ids follow_ups = EducodeSales::FollowUp.where(business_id: business_ids) customer_follows = EducodeSales::CustomerFollow.where(school_id: d.id) next if follow_ups.blank? && customer_follows.blank? a_last_follow_time = follow_ups.last&.created_at&.to_s b_last_follow_time = customer_follows.last&.created_at&.to_s if a_last_follow_time.present? && b_last_follow_time.present? last_follow_time = ((a_last_follow_time < b_last_follow_time) ? b_last_follow_time : a_last_follow_time) else last_follow_time = (a_last_follow_time || b_last_follow_time) end if last_follow_time.present? && last_follow_time > date[0] && last_follow_time < date[1] school_ids << d.id end end @customers = @customers.where(id: school_ids) 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 @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 @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 ActiveRecord::Base.transaction do property = SchoolProperty.find_or_create_by!(project_985: params[:project_985].present? ? 1 : 0, project_211: params[:project_211].present? ? 1 : 0, regular_college: params[:regular_college].present? ? 1 : 0, junior_college: params[:junior_college].present? ? 1 : 0, secondary_school: params[:secondary_school].present? ? 1 : 0, military_school: params[:military_school].present? ? 1 : 0, enterprise: params[:enterprise].present? ? 1 : 0) @school = School.new @school.attributes = school_params @school.school_property = property @school.save! EducodeSales::CustomerExtension.create(customer_staff_id: @current_admin.id, school_id: @school.id) end render_success end def edit @school = School.find(params[:id]) @school_property = @school.school_property render layout: false end def update @school = School.find(params[:id]) ActiveRecord::Base.transaction do property = SchoolProperty.find_or_create_by!(project_985: params[:project_985].present? ? 1 : 0, project_211: params[:project_211].present? ? 1 : 0, regular_college: params[:regular_college].present? ? 1 : 0, junior_college: params[:junior_college].present? ? 1 : 0, secondary_school: params[:secondary_school].present? ? 1 : 0, military_school: params[:military_school].present? ? 1 : 0, enterprise: params[:enterprise].present? ? 1 : 0) @school.attributes = school_params @school.school_property = property @school.save! end render_success end private def school_property_params params.permit(:project_985, :project_211, :regular_college, :junior_college, :secondary_school, :military_school, :enterprise) end def school_params params.permit(:name, :province, :city, :address) end end end