From 50d7ed2deb55a522f02451343d62995d91501c96 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Tue, 30 Apr 2019 11:34:45 +0800 Subject: [PATCH] export school statistic data feature --- Gemfile | 3 ++ .../managements/schools_controller.rb | 23 ++++++++++++- app/models/homework_common.rb | 32 ++++++------------- .../management/school_report_service.rb | 2 +- app/views/layouts/base_management.html.erb | 4 --- .../schools/_data_contrast_list.html.erb | 8 ++--- .../schools/_statistics_list.html.erb | 2 +- .../managements/schools/statistics.html.erb | 26 +++++++++++++++ .../schools/statistics_xlsx.xlsx.axlsx | 20 ++++++++++++ config/routes.rb | 1 + 10 files changed, 88 insertions(+), 33 deletions(-) create mode 100644 app/views/managements/schools/statistics_xlsx.xlsx.axlsx diff --git a/Gemfile b/Gemfile index b9e1b4e2..f4697bf5 100644 --- a/Gemfile +++ b/Gemfile @@ -62,6 +62,9 @@ gem 'elasticsearch-rails' gem 'oauth2' +# xlsx +gem 'axlsx_rails', '0.3.0' + #Ruby 2.2+ has removed test/unit from the core library. if RUBY_VERSION>='2.2' gem 'test-unit', '~> 3.0' diff --git a/app/controllers/managements/schools_controller.rb b/app/controllers/managements/schools_controller.rb index 6dfe84fb..381c7493 100644 --- a/app/controllers/managements/schools_controller.rb +++ b/app/controllers/managements/schools_controller.rb @@ -7,8 +7,13 @@ class Managements::SchoolsController < Managements::BaseController params[:sort_by] ||= :teacher_count params[:sort_direction] ||= :desc - schools = Management::SchoolReportService.new(params).call + schools = Management::SchoolReportService.new(params).call @schools = paginateHelper schools + + respond_to do |format| + format.html { load_statistic_total } + format.js + end end def yesterday_data @@ -31,6 +36,13 @@ class Managements::SchoolsController < Managements::BaseController raise '参数错误' end + def statistics_xlsx + @schools = Management::SchoolReportService.new(params).call + + filename = ["学校统计总表", params[:keyword], Time.zone.now.strftime('%Y%m%d%H%M%S')].join('-') << ".xlsx" + render xlsx: "statistics_xlsx", filename: filename + end + private def set_navigation_bar @menu_type = 1 @@ -41,4 +53,13 @@ class Managements::SchoolsController < Managements::BaseController [I18n.t("school_daily_report.#{column}"), column] end end + + def load_statistic_total + @teacher_total = User.teacher.count + @student_total = User.student.count + @course_total = Course.count + @active_course_total = Course.where(is_end: false).count + @shixun_homework_total = HomeworkCommon.where(homework_type: 4).count + @other_homework_total = HomeworkCommon.where(homework_type: [1, 3]).count + end end diff --git a/app/models/homework_common.rb b/app/models/homework_common.rb index 0b7aa420..abe09e08 100644 --- a/app/models/homework_common.rb +++ b/app/models/homework_common.rb @@ -63,33 +63,21 @@ class HomeworkCommon < ActiveRecord::Base #作业类型 def homework_type_ch - type = self.homework_type - case type - when 1 - type = "普通" - when 2 - type = "编程" - when 3 - type = "分组" - when 4 - type = "实训" + case homework_type + when 1 then '普通' + when 2 then '编程' + when 3 then '分组' + when 4 then '实训' end - type end def homework_type_module - type = self.homework_type - case type - when 1 - type = "common_homework" - when 2 - type = "code_homework" - when 3 - type = "group_homework" - when 4 - type = "shixun_homework" + case homework_type + when 1 then 'common_homework' + when 2 then 'code_homework' + when 3 then 'group_homework' + when 4 then 'shixun_homework' end - type end def act_as_activity diff --git a/app/services/management/school_report_service.rb b/app/services/management/school_report_service.rb index 5a962bd5..e9ee1bae 100644 --- a/app/services/management/school_report_service.rb +++ b/app/services/management/school_report_service.rb @@ -39,7 +39,7 @@ class Management::SchoolReportService ( SELECT COUNT(*) FROM homework_commons hc LEFT JOIN courses ON courses.id = hc.course_id - WHERE courses.school_id = schools.id + WHERE courses.school_id = schools.id AND hc.homework_type = 4 ) homework_count, ( SELECT COUNT(*) FROM homework_commons hc diff --git a/app/views/layouts/base_management.html.erb b/app/views/layouts/base_management.html.erb index 9d1d77c8..d357bd09 100644 --- a/app/views/layouts/base_management.html.erb +++ b/app/views/layouts/base_management.html.erb @@ -44,10 +44,6 @@
  • <%= link_to "课堂列表", classroom_classment_managements_path %>
  • <%= link_to "实训作业", shixun_task_managements_path%>
  • <%= link_to "项目列表", project_list_managements_path%>
  • - - - -
  • 精选实训+ diff --git a/app/views/managements/schools/_data_contrast_list.html.erb b/app/views/managements/schools/_data_contrast_list.html.erb index af63aee8..c2699601 100644 --- a/app/views/managements/schools/_data_contrast_list.html.erb +++ b/app/views/managements/schools/_data_contrast_list.html.erb @@ -35,20 +35,20 @@ percentage = report['total'].zero? ? increase.to_f * 100 : (increase / report['total'].to_f) * 100 %> <% if increase > 0 %> - + +<%= increase %> - +<%= percentage.round(5) %>% + +<%= percentage.round(5) %>% <% elsif increase.zero? %> <%= increase %> <%= percentage.round(5) %>% <% else %> - + <%= increase %> - <%= percentage.round(5) %>% + <%= percentage.round(5) %>% <% end %> <% end %> diff --git a/app/views/managements/schools/_statistics_list.html.erb b/app/views/managements/schools/_statistics_list.html.erb index 609a1cca..cffb316c 100644 --- a/app/views/managements/schools/_statistics_list.html.erb +++ b/app/views/managements/schools/_statistics_list.html.erb @@ -17,7 +17,7 @@ <% @schools.each_with_index do |school, index| %> - <% p school %><%= (@obj_pages.page - 1) * @obj_pages.per_page + index + 1 %> + <%= (@obj_pages.page - 1) * @obj_pages.per_page + index + 1 %> <%= school['id'] %> <%= school['name'] %> <%= school['teacher_count'] %> diff --git a/app/views/managements/schools/statistics.html.erb b/app/views/managements/schools/statistics.html.erb index 1597fe45..5a68f75c 100644 --- a/app/views/managements/schools/statistics.html.erb +++ b/app/views/managements/schools/statistics.html.erb @@ -4,7 +4,26 @@ class: 'fl task-form-30 task-height-30 mt10', style: 'margin: 10px 10px 10px 25px;' %> <%= link_to '搜索', 'javascript:void(0)', class: 'fl task-btn task-btn-orange ml5 mt10', onclick: "$('#school_report_search_form').submit();" %> <%= link_to '清除', 'javascript:clearSearchCondition()', class: 'fl task-btn ml5 mt2 mt10' %> + +
    + <%= link_to '导出Excel', school_statistics_xlsx_managements_path(format: :xlsx), + class: 'fl task-btn task-btn-orange mt10 mr20 export-statistic-btn', + onclick: 'exportSchoolStatistic();' %> +
    <% end %> + + + +
    +
    + 统计总计: + 教师总人数<%= @teacher_total %>人, + 学生总人数<%= @student_total %>人, + 课堂总数<%= @course_total %>个, + 正在进行课堂总数<%= @active_course_total %>个, + 实训作业总数<%= @shixun_homework_total %>个, + 其它作业总数<%= @other_homework_total %>个, +
    @@ -16,4 +35,11 @@ $("#school_report_search_form input[name='keyword']").val(""); $('#school_report_search_form').submit(); } + + function exportSchoolStatistic(){ + var form = $("#school_report_search_form") + var exportLink = form.find(".export-statistic-btn"); + var keyword = form.find("input[name='keyword'").val(); + exportLink.attr("href", exportLink.attr("href") + "?keyword=" + keyword); + } \ No newline at end of file diff --git a/app/views/managements/schools/statistics_xlsx.xlsx.axlsx b/app/views/managements/schools/statistics_xlsx.xlsx.axlsx new file mode 100644 index 00000000..b68a817c --- /dev/null +++ b/app/views/managements/schools/statistics_xlsx.xlsx.axlsx @@ -0,0 +1,20 @@ +wb = xlsx_package.workbook +wb.add_worksheet(name: "统计总表") do |sheet| + sheet.add_row %w(ID 单位名称 教师总人数 学生总人数 课堂总数 正在进行课堂数 实训作业总数 其它作业总数 动态时间) + + total = @schools.count + buffer_size = 500 + times = total % 500 > 0 ? total / 500 + 1 : total / 500 + + times.times do |index| + schools = @schools.limit(buffer_size).offset(index * buffer_size) + + schools.each do |school| + sheet.add_row([ + school['id'].to_s, school['name'].to_s, school['teacher_count'].to_s, school['student_count'].to_s, + school['course_count'].to_s, school['active_course_count'].to_s, school['homework_count'].to_s, + school['other_homework_count'].to_s, format_time(school['nearly_course_time']) + ]) + end + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 26cddf96..85ec25c9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -728,6 +728,7 @@ RedmineApp::Application.routes.draw do ## oauth相关 get :school_report, controller: 'managements::schools', action: 'statistics' get :school_yesterday_data, controller: 'managements::schools', action: 'yesterday_data' get :school_data_contrast, controller: 'managements::schools', action: 'data_contrast' + get :school_statistics_xlsx, controller: 'managements::schools', action: 'statistics_xlsx' end end # Enable Grack support