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 show_majors @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 majors department = Department.find(params[:id]) @majors = department.department_majors end def major department = Department.find(params[:id]) major = department.department_majors.new(name: params[:name]) if major.save render_success else render_failure major end end def new_major render layout: false 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] } gon.school_tags = SchoolTag.where(for_missions: true).map { |d| {value: d.id, name: d.name } } 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 + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id) @customers = School.where(id: school_ids) else @customers = School.all end end part_a_ids = CustomerFollow.all.pluck(:school_id) part_b_ids = Business.where(id: EducodeSales::FollowUp.pluck(:business_id)).pluck(:school_id) ids = part_a_ids + part_b_ids + CustomerAdd.all.pluck(:school_id) @customers = @customers.where(id: ids) 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][:property].present? property = params[:q][:property].split(",").map(&:to_i) # property.each do |p| # case p # when 0 # school_property_ids += SchoolProperty.where(project_985: true).ids # when 1 # school_property_ids += SchoolProperty.where(project_211: true).ids # when 2 # school_property_ids += SchoolProperty.where(regular_college: true).ids # when 3 # school_property_ids += SchoolProperty.where(junior_college: true).ids # when 4 # school_property_ids += SchoolProperty.where(secondary_school: true).ids # when 5 # school_property_ids += SchoolProperty.where(military_school: true).ids # when 6 # school_property_ids += SchoolProperty.where(enterprise: true).ids # when 7 # school_property_ids += SchoolProperty.where(mid_school: true).ids # when 8 # school_property_ids += SchoolProperty.where(ele_school: true).ids # when 9 # school_property_ids += SchoolProperty.where(other: true).ids # else # end # end @customers = @customers.joins(:school_tags).where("school_tags.id in (?)", property).distinct # @customers = @customers.where(school_property_id: school_property_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 if params[:page].present? @customers = @customers.order(id: :desc).page(params[:page]).per(params[:limit]) else @customers = @customers.order(id: :desc) end end end end def list 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] } gon.school_tags = SchoolTag.where(for_missions: true).map { |d| {value: d.id, name: d.name } } end format.json do @customers = School.from("( SELECT schools.id, departments.id AS department_id, COUNT(department_majors.id) AS major_count FROM schools LEFT JOIN departments ON schools.id = departments.school_id LEFT JOIN department_majors ON department_majors.department_id = departments.id GROUP BY schools.id, departments.id ) AS s") 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 = @customers.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 + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id) @customers = @customers.where(id: school_ids) else # @customers = School.all end end @customers = @customers.select(" schools.*, s.major_count, s.department_id, departments.name AS department_name ").joins(" JOIN schools ON s.id = schools.id LEFT JOIN departments ON s.department_id = departments.id ") part_a_ids = CustomerFollow.all.pluck(:school_id) part_b_ids = Business.where(id: EducodeSales::FollowUp.pluck(:business_id)).pluck(:school_id) ids = part_a_ids + part_b_ids + CustomerAdd.all.pluck(:school_id) @customers = @customers.where(id: ids) 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][:property].present? property = params[:q][:property].split(",").map(&:to_i) @customers = @customers.joins(:school_tags).where("school_tags.id in (?)", property).distinct 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 if params[:page].present? @customers = @customers.order(id: :desc).page(params[:page]).per(params[:limit]) else @customers = @customers.order(id: :desc) end end end end def update_major department = Department.find(params[:id]) major = department.department_majors.find(params[:major_id]) if major.update(name: params[:name]) render_success else render_failure major end end def delete_major department = Department.find(params[:id]) major = department.department_majors.find(params[:major_id]) if major.destroy render_success else render_failure major 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_customer_follow respond_to do |format| format.html do @school_name = School.find(params[:id]).name @area = School.find(params[:id]).province @staff = EducodeSales::CustomerExtension.where(school_id: params[:id]).map { |d| d.customer_staff&.user&.real_name}.join("、") render layout: false end format.json do if params[:department_id].present? @follow_ups = EducodeSales::CustomerFollow.where(department_id: params[:department_id]) else @follow_ups = EducodeSales::CustomerFollow.where(school_id: params[:id]) end @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 create params[:school_ids].each do |d| CustomerAdd.create(school_id: d) end render_success end def edit @school = School.find(params[:id]) @school_property = @school.school_property render layout: false end def edit_major @school = School.find(params[:id]) @school_property = @school.school_property if params[:major].present? @major = DepartmentMajor.find_by(id: params[:major]) end if params[:department_id].present? @department = Department.find_by(id: params[:department_id]) end render layout: false end def batch_update_school_tags xlsx = Roo::Spreadsheet.open(params[:file]) ods = xlsx.sheet(0) rows = ods.last_row - 1 i = 0 school_tag_hash = SchoolTag.where(for_missions: true).pluck(:name, :id).to_h rows.times do |r| #行数 i += 1 next unless ods.row(r+1)[0] school = School.find_by(name: ods.row(r+1)[0].to_s.strip) school_list = EducodeSales::CustomerAdd::SCHOOL_LIST.invert if school property_names = ods.row(r+1)[2].to_s.strip tags = [] if property_names property_names.gsub("、", " ").gsub(",", " ").split(" ").each do |tag| if school_tag_hash[tag] tags << SchoolTagMiddle.find_or_initialize_by(school_id: school.id, school_tag_id: school_tag_hash[tag]) end end end school.school_tag_middles = tags end end SchoolTagMiddle.where(school_id: nil).delete_all render json: { succcess: true} end def update @school = School.find(params[:id]) if params[:department_id].present? department = @school.departments.find_by(id: params[:department_id]) if department department.update(name: params[:department]) department.department_majors.find_by(id: params[:major_id])&.update(name: params[:major]) if params[:major_id].present? end end 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, other: params[:other].present? ? 1 : 0, mid_school: params[:mid_school].present? ? 1 : 0, ele_school: params[:ele_school].present? ? 1 : 0, enterprise: params[:enterprise].present? ? 1 : 0) @school.attributes = school_params @school.school_property = property @school.save! #先删除全部的标签,再添加标签 missions_school_tags = SchoolTag.where(for_missions: true) SchoolTagMiddle.where(school_id: @school.id, school_tag_id: missions_school_tags.ids).destroy_all #保存学校标签 keys = %w[regular_college junior_college secondary_school military_school other mid_school ele_school enterprise] names = EducodeSales::CustomerAdd::SCHOOL_LIST property.attributes.each do |key, value| next unless key.in?(keys) next unless value.to_s == "true" school_tag = SchoolTag.find_by(name: names[key], for_missions: true) if school_tag.present? SchoolTagMiddle.create(school_id: @school.id, school_tag_id: school_tag.id) end end 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