diff --git a/app/assets/javascripts/admins/schools/index.js b/app/assets/javascripts/admins/schools/index.js new file mode 100644 index 000000000..20ea080fb --- /dev/null +++ b/app/assets/javascripts/admins/schools/index.js @@ -0,0 +1,4 @@ +$(document).on('turbolinks:load', function() { + if ($('body.admins-schools-index-page').length > 0) { + } +}); \ No newline at end of file diff --git a/app/assets/stylesheets/admins/schools.scss b/app/assets/stylesheets/admins/schools.scss new file mode 100644 index 000000000..8dff1eaff --- /dev/null +++ b/app/assets/stylesheets/admins/schools.scss @@ -0,0 +1,3 @@ +.admins-schools-index-page { + +} \ No newline at end of file diff --git a/app/controllers/admins/schools_controller.rb b/app/controllers/admins/schools_controller.rb new file mode 100644 index 000000000..8c4f1d59e --- /dev/null +++ b/app/controllers/admins/schools_controller.rb @@ -0,0 +1,30 @@ +class Admins::SchoolsController < Admins::BaseController + def index + params[:sort_by] ||= 'created_at' + params[:sort_direction] ||= 'desc' + + schools = Admins::SchoolQuery.call(params) + + @schools = paginate schools + + school_ids = @schools.map(&:id) + @department_count = Department.where(school_id: school_ids).group(:school_id).count + end + + def destroy + users = User.joins(:user_extension).where(user_extensions: { school_id: current_school.id }) + + ActiveRecord::Base.transaction do + users.update_all(profile_completed: false) + current_school.destroy! + end + + render_delete_success + end + + private + + def current_school + @_current_school ||= School.find(params[:id]) + end +end \ No newline at end of file diff --git a/app/libs/util/file_manage.rb b/app/libs/util/file_manage.rb index f592ca4e7..b6cd79e57 100644 --- a/app/libs/util/file_manage.rb +++ b/app/libs/util/file_manage.rb @@ -18,10 +18,18 @@ module Util::FileManage File.exist?(disk_filename(source_type, source_id)) end + def exists?(source) + File.exist?(disk_filename(source.class, source.id)) + end + def disk_file_url(source_type, source_id) File.join('/images', relative_path, "#{source_type}", "#{source_id}") end + def source_disk_file_url(source) + File.join('/images', relative_path, "#{source.class}", "#{source.id}") + end + def disk_auth_filename(source_type, source_id, type) File.join(storage_path, "#{source_type}", "#{source_id}#{type}") end diff --git a/app/models/department.rb b/app/models/department.rb index 3b0672cce..2c537fd91 100644 --- a/app/models/department.rb +++ b/app/models/department.rb @@ -5,7 +5,7 @@ class Department < ApplicationRecord has_many :member_users, through: :department_members, source: :user has_many :user_extensions, dependent: :nullify - has_many :apply_add_departments + has_many :apply_add_departments, dependent: :destroy scope :without_deleted, -> { where(is_delete: false) } diff --git a/app/models/school.rb b/app/models/school.rb index 24de02496..387b6b5d8 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -17,6 +17,7 @@ class School < ApplicationRecord has_many :partners, dependent: :destroy has_many :apply_add_departments, dependent: :destroy + has_many :user_extensions, dependent: :nullify # 学校管理员 def manager?(user) diff --git a/app/queries/admins/school_query.rb b/app/queries/admins/school_query.rb new file mode 100644 index 000000000..7361588c4 --- /dev/null +++ b/app/queries/admins/school_query.rb @@ -0,0 +1,23 @@ +class Admins::SchoolQuery < ApplicationQuery + include CustomSortable + + attr_reader :params + + sort_columns :users_count, :created_at, default_by: :created_at, default_direction: :desc + + def initialize(params) + @params = params + end + + def call + schools = School.all + + keyword = strip_param(:keyword) + schools = schools.where('schools.name LIKE ?', "%#{keyword}%") if keyword + + schools = schools.joins(:user_extensions).group(:id) + schools = schools.select('schools.*, COUNT(*) AS users_count') + + custom_sort schools, params[:sort_by], params[:sort_direction] + end +end \ No newline at end of file diff --git a/app/views/admins/schools/index.html.erb b/app/views/admins/schools/index.html.erb new file mode 100644 index 000000000..0dd5b0bc5 --- /dev/null +++ b/app/views/admins/schools/index.html.erb @@ -0,0 +1,16 @@ +<% define_admin_breadcrumbs do %> + <% add_admin_breadcrumb('单位列表') %> +<% end %> + +
ID | +LOGO | +标识码 | +单位名称 | +地区 | +城市 | +详细地址 | +<%= sort_tag('用户数', name: 'users_count', path: admins_schools_path) %> | +部门数 | +<%= sort_tag('创建时间', name: 'created_at', path: admins_schools_path) %> | +操作 | +
---|---|---|---|---|---|---|---|---|---|---|
<%= school.id %> | ++ <% if Util::FileManage.exists?(school) %> + <%= image_tag(Util::FileManage.source_disk_file_url(school).to_s + "?#{Time.now.to_i}", width: 40, height: 40, class: 'preview-image') %> + <% else %> + <%= content_tag(:span, '--', class: 'text-secondary') %> + <% end %> + | +<%= display_text school.identifier %> | +<%= school.name %> | +<%= school.province %> | +<%= school.city %> | +<%= school.address %> | +<%= school.users_count %> | +<%= @department_count.fetch(school.id, 0) %> | +<%= school.created_at&.strftime('%Y-%m-%d %H:%M') %> | ++ <%= delete_link '删除', admins_school_path(school, element: ".school-item-#{school.id}"), class: 'delete-school-action' %> + | +