class DepartmentController < ApplicationController before_filter :find_department, :only => [:destroy] #根据部门名字或者拼音来查询 def on_search school = School.find params[:school_id] if school condition = "#{params[:name].strip}".gsub(" ","") #获取拼音的第一次出现的位置 if(condition == '') @departments = school.departments.is_active.order("CONVERT(name USING gbk) COLLATE gbk_chinese_ci asc").page((params[:page].to_i || 1) - 1).per(100) @department_count = school.departments.is_active.count else @departments = school.departments.where("name like ?", "%#{condition}%").is_active.order("CONVERT(name USING gbk) COLLATE gbk_chinese_ci asc").page((params[:page].to_i || 1) - 1).per(100) @department_count = school.departments.where("name like ?", "%#{condition}%").is_active.count end render :json =>{ :departments => @departments,:count=>@department_count}.to_json end end #申请学院或部门(单位) name:名称 remarks:备注 def apply_add_department school = School.find params[:school_id] data = {result:0,name:params[:name],department_id:0} #0 成功 1参数错误 2名称已存在 3.失败 data[:result] = 0 if school #检验参数 if params[:name] == "" data[:result] = 1 else department = Department.where(:school_id => school.id, :name => params[:name]).first if department data[:result] = 2 else department = Department.new department.name = params[:name].strip department.school = school #status 0未处理 1通过 2拒绝 apply_department = ApplyAddDepartment.new #用belongs_to 可以一起存数据库 apply_department.department = department apply_department.school = department.school apply_department.name = department.name apply_department.status = 0 apply_department.remarks = params[:remarks] apply_department.user_id = User.current.id if apply_department.save data[:department_id] = department.id unless User.current.professional_certification user_extention = User.current.extensions user_extention.department_id = department.id user_extention.save! end # 向管理员发送信息 users = User.where(:admin => 1) users.each do |user| AppliedMessage.create(:user_id => user.id, :status => 0, :applied_user_id => User.current.id, :viewed => 0, :applied_id => apply_department.id, :applied_type => "ApplyAddDepartment", :name => department.name ) end else data[:result] = 3 end end end else data[:result] = 4 end render :json =>data end def search_repeat_departmentname status = 0 #没有重复的 name = params[:name] school = School.where(:id => params[:school_id].to_i).first if school if name department = school.departments.where(:name => name).first if department status = 1 #有重复的 else status = 2 #无重复的 end end else status = 0 end render :json =>status end def destroy if @department # if UserExtensions.where(:department_id => @department.id).count == 0 ApplyAddDepartment.where(:department_id=>@department.id).update_all(:status => 2) @apply_dep = ApplyAddDepartment.where(:department_id => @department.id).first user_ids = UserExtensions.where(:department_id => @department.id, :school_id => @department.school_id).pluck(:user_id) logger.info("###########user_ids: ###{user_ids}") if user_ids.present? UserExtensions.where(:department_id => @department.id).update_all(:department_id => nil) user_ids.each do |member| Tiding.create(:container_id => @department.id, :container_type => "Department", :user_id => member, :trigger_user_id => User.current.id, :tiding_type => "System", :status => 4) end @department.update_attribute(:is_delete, true) else @department.destroy end # end respond_to do |format| format.js end end end private def find_department @department = Department.find params[:id] rescue ActiveRecord::RecordNotFound render_404 end end