From 7da238def9c71456595c5147d130ff0544764f78 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Fri, 6 Sep 2019 10:13:52 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E9=97=A8=E7=9A=84=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/managements_controller.rb | 38 ++++++++++--------- ...0190906014148_migrate_delete_department.rb | 16 ++++++++ 2 files changed, 36 insertions(+), 18 deletions(-) create mode 100644 db/migrate/20190906014148_migrate_delete_department.rb diff --git a/app/controllers/managements_controller.rb b/app/controllers/managements_controller.rb index c1cf4771..96dea860 100644 --- a/app/controllers/managements_controller.rb +++ b/app/controllers/managements_controller.rb @@ -1746,24 +1746,26 @@ end # 删除 def delete_applied_departments - applied_department = ApplyAddDepartment.find params[:depart_id] - applied_message = AppliedMessage.where(:applied_id => applied_department.id, :applied_type => "ApplyAddDepartment") - applied_message.update_all(:status => 3) - applied_department.update_attribute(:status, 3) - # 未审批删除 - if params[:tip] == "unapplied" - # AppliedMessage.create(:user_id => applied_department.user_id, :status => 3, :viewed => 0, :applied_id => applied_department.id, :applied_type => "ApplyAddDepartment", :name => applied_department.name ) - Tiding.where(:user_id => 1, :trigger_user_id => applied_department.user_id, :container_id => applied_department.id, :container_type => 'ApplyAddDepartment', :status => 0, :tiding_type => "Apply").update_all(:status => 1) - Tiding.create(:user_id => applied_department.user_id, :trigger_user_id => 0, :container_id => applied_department.id, :container_type =>'ApplyAddDepartment', :belong_container_id => applied_department.department.school_id, :belong_container_type=> 'School', :tiding_type => "System", :status => 2, :extra => params[:reason]) - # 删除学校的用户 - user_exs = UserExtensions.where("department_id = #{applied_department.department_id}") - users = User.where(id: user_exs.pluck(:user_id)) - user_exs.update_all(department_id: nil) - users.update_all(profile_completed: false) - applied_department.department.destroy - # 已审批删除 - elsif params[:tip] == "applied" - applied_department.destroy + transaction do + applied_department = ApplyAddDepartment.find params[:depart_id] + applied_message = AppliedMessage.where(:applied_id => applied_department.id, :applied_type => "ApplyAddDepartment") + applied_message.update_all(:status => 3) + applied_department.update_attribute(:status, 3) + # 未审批删除 + if params[:tip] == "unapplied" + # AppliedMessage.create(:user_id => applied_department.user_id, :status => 3, :viewed => 0, :applied_id => applied_department.id, :applied_type => "ApplyAddDepartment", :name => applied_department.name ) + Tiding.where(:user_id => 1, :trigger_user_id => applied_department.user_id, :container_id => applied_department.id, :container_type => 'ApplyAddDepartment', :status => 0, :tiding_type => "Apply").update_all(:status => 1) + Tiding.create(:user_id => applied_department.user_id, :trigger_user_id => 0, :container_id => applied_department.id, :container_type =>'ApplyAddDepartment', :belong_container_id => applied_department.department.school_id, :belong_container_type=> 'School', :tiding_type => "System", :status => 2, :extra => params[:reason]) + # 删除学校的用户 + user_exs = UserExtensions.where("department_id = #{applied_department.department_id}") + users = User.where(id: user_exs.pluck(:user_id)) + user_exs.update_all(department_id: nil) + users.update_all(profile_completed: false) + applied_department.department.destroy + # 已审批删除 + elsif params[:tip] == "applied" + applied_department.destroy + end end respond_to do |format| format.html{ redirect_to depart_managements_path } diff --git a/db/migrate/20190906014148_migrate_delete_department.rb b/db/migrate/20190906014148_migrate_delete_department.rb new file mode 100644 index 00000000..32a84b44 --- /dev/null +++ b/db/migrate/20190906014148_migrate_delete_department.rb @@ -0,0 +1,16 @@ +class MigrateDeleteDepartment < ActiveRecord::Migration + def up + ApplyAddDepartment.where("status= 3 and created_at > '2019-07-25 00:00:00'").each do |apply| + if apply.department.present? && !apply.department.is_auth + user_exs = UserExtensions.where("department_id = #{apply.department_id}") + users = User.where(id: user_exs.pluck(:user_id)) + user_exs.update_all(department_id: nil) + users.update_all(profile_completed: false) + apply.department.destroy + end + end + end + + def down + end +end