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).page(params[:page]).per(params[:per_page]) end end end def create user = User.find(params[:id]) staff = Staff.new(user_id: user.id) if staff.save 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: '全国'}) @staff_types = Common.where(clazz: 'staff_type').pluck(:name, :id) render layout: false end def new render layout: false end def destroy staff = Staff.find(params[:id]) staff.destroy render_success rescue ActiveRecord::DeleteRestrictionError => e render_failure '该用户已有数据产生,暂不能删除' end def disable staff = Staff.find(params[:id]) 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 staff.save render_success else render_failure staff end end def follow_up_schools staff = Staff.find(params[:id]) @schools = EducodeSales::Business.joins(:last_follow_up, :department).where("educode_sales_follow_ups.staff_id = #{staff.id}").group("school_id").select("school_id, max(educode_sales_follow_ups.updated_at) AS updated_at").page(params[:page]).per(params[:per_page]) @count = EducodeSales::Business.joins(:last_follow_up, :department).where("educode_sales_follow_ups.staff_id = #{staff.id}").group("school_id").count end def follow_up_departments staff = Staff.find(params[:id]) @schools = EducodeSales::Business.joins(:last_follow_up, [department: :school]).where("educode_sales_follow_ups.staff_id = #{staff.id}").select("departments.name, schools.name AS school, educode_sales_follow_ups.updated_at").page(params[:page]).per(params[:per_page]) end end end