require_dependency "educode_sales/application_controller" module EducodeSales class StaffsController < ApplicationController def index respond_to do |format| format.html do end format.json do @staffs = Staff.where(is_admin: false, deleted_at: nil).page(params[:page]).per(params[:limit]) if params[:q].present? && params[:q][:name].present? @staffs = @staffs.joins(:user).where('CONCAT(lastname, firstname) LIKE :name', name: "%#{params[:q][:name]}%") end if params[:q].present? && params[:q][:role].present? @staffs = @staffs.where(role: params[:q][:role]) end if params[:q].present? && params[:q][:staff_id].present? @staffs = @staffs.where(staff_id: params[:q][:staff_id]) end if params[:q].present? && params[:q][:staff_type].present? @staffs = @staffs.where(job_type: params[:q][:staff_type]) end if params[:q].present? && params[:q][:banned].present? if params[:q][:banned] == "0" @staffs = @staffs.where("educode_sales_staffs.expired_at is null or educode_sales_staffs.expired_at > ?", Time.now) else @staffs = @staffs.where("educode_sales_staffs.expired_at is null or educode_sales_staffs.expired_at < ?", Time.now) end end end end end def create user = User.find(params[:id]) staff = Staff.new(user_id: user.id) params[:current_admin] = @current_admin.id if staff.save impressionist(staff, params) render_success else render_failure staff end end def edit @staff = Staff.find(params[:id]) gon.area_ids = @staff.area_ids gon.areas = Common.where(clazz: 'area').map { |d| { value: d.id, title: d.name } } #.unshift({value: -1, title: '全国'}) gon.school_ids = @staff.staff_schools.ids gon.schools = @staff.staff_schools.pluck(:school_id).map { |d| { value: d, name: "#{School.find(d)&.name}-#{School.find(d)&.province}" } } #.unshift({value: -1, title: '全国'}) # gon.school_ids = School.all.ids # gon.schools = School.all.first(100).map { |d| { value: d.id, title: d.name } } #.unshift({value: -1, title: '全国'}) @staff_types = Common.where(clazz: 'staff_type').pluck(:name, :id) tag_ids = @staff.staff_school_tags.pluck(:school_tag_id) gon.school_properties = SchoolTag.where(weight: 1).map { |d| {value: d.id, name: d.name, selected: tag_ids.include?(d.id) } } render layout: false end def new render layout: false end def destroy staff = Staff.find(params[:id]) params[:current_admin] = @current_admin.id impressionist(staff, params) staff.update(deleted_at: Time.now, expired_at: Time.now.days_ago(1)) render_success # rescue ActiveRecord::DeleteRestrictionError => e # render_failure '该用户已有数据产生,暂不能删除' end def disable staff = Staff.find(params[:id]) params[:current_admin] = @current_admin.id impressionist(staff, params) staff.update(expired_at: Time.now.days_ago(1)) render_success end def update staff = Staff.find(params[:id]) staff.assign_attributes(role_id: params[:role_id], job_type: params[:job_type], enabled_at: params[:enabled_at]) staff.expired_at = staff.enabled_at.months_since(params[:month].to_i) commons = [] params[:area_ids].each do |d| commons << Common.find(d) end staff.areas = commons if params[:school_ids].present? schools = [] params[:school_ids].each do |d| schools << EducodeSales::StaffSchool.find_or_initialize_by(staff_id: staff.id, school_id: d) end staff.staff_schools = schools else staff.staff_schools.destroy_all end tags = [] params[:school_tag_ids].each do |tag_id| tags << EducodeSales::StaffSchoolTag.find_or_initialize_by(staff_id: staff.id, school_tag_id: tag_id) end staff.staff_school_tags = tags params[:current_admin] = @current_admin.id impressionist(staff, params) if staff.save render_success else render_failure staff end end def follow_up_schools staff = Staff.find(params[:id]) @schools = EducodeSales::Teacher.joins(:follow_up, :department).where("educode_sales_teacher_follows.staff_id = #{staff.id}").group("departments.school_id").select("departments.school_id, max(educode_sales_teacher_follows.updated_at) AS updated_at") + EducodeSales::Business.joins(:follow_ups, :department).where("educode_sales_follow_ups.staff_id = #{staff.id}").group("departments.school_id").select("departments.school_id, max(educode_sales_follow_ups.updated_at) AS updated_at") @count = EducodeSales::Business.joins(:follow_ups, :department).where("educode_sales_follow_ups.staff_id = #{staff.id}").group("departments.school_id").count end def follow_up_departments staff = Staff.find(params[:id]) @schools = (EducodeSales::Business.joins(:follow_ups, [department: :school]).where("educode_sales_follow_ups.staff_id = #{staff.id}").select("departments.id, departments.name, schools.name AS school, educode_sales_follow_ups.updated_at") + EducodeSales::Teacher.joins(:follow_up, [department: :school]).where("educode_sales_teacher_follows.staff_id = #{staff.id}").select("departments.id, departments.name, schools.name AS school, educode_sales_teacher_follows.updated_at")).uniq{|s| s.id} end end end