From 8348c95b57db63df9f4a39de37d41163da5841be Mon Sep 17 00:00:00 2001 From: p31729568 Date: Tue, 30 Apr 2019 15:40:40 +0800 Subject: [PATCH] modify school contrast data feature --- Gemfile | 3 +- .../managements/schools_controller.rb | 19 ++- .../management/school_data_grow_service.rb | 59 ++++++++ .../school_yesterday_data_service.rb | 32 ---- app/views/layouts/base_management.html.erb | 2 +- .../schools/_contrast_search_form.html.erb | 142 ++++++++++++++---- .../schools/_data_contrast_list.html.erb | 9 ++ ...list.html.erb => _data_grow_list.html.erb} | 27 +++- ...erday_data.html.erb => data_grow.html.erb} | 2 +- ...yesterday_data.js.erb => data_grow.js.erb} | 2 +- config/routes.rb | 2 +- 11 files changed, 223 insertions(+), 76 deletions(-) create mode 100644 app/services/management/school_data_grow_service.rb delete mode 100644 app/services/management/school_yesterday_data_service.rb rename app/views/managements/schools/{_yesterday_data_list.html.erb => _data_grow_list.html.erb} (56%) rename app/views/managements/schools/{yesterday_data.html.erb => data_grow.html.erb} (79%) rename app/views/managements/schools/{yesterday_data.js.erb => data_grow.js.erb} (63%) diff --git a/Gemfile b/Gemfile index 7dca07cd..7a7deeab 100644 --- a/Gemfile +++ b/Gemfile @@ -63,7 +63,8 @@ gem 'elasticsearch-rails' gem 'oauth2' # xlsx -# gem 'axlsx_rails', '0.3.0' +gem 'axlsx', '2.1.0.pre' +gem 'axlsx_rails', '0.3.0' #Ruby 2.2+ has removed test/unit from the core library. if RUBY_VERSION>='2.2' diff --git a/app/controllers/managements/schools_controller.rb b/app/controllers/managements/schools_controller.rb index 381c7493..24889869 100644 --- a/app/controllers/managements/schools_controller.rb +++ b/app/controllers/managements/schools_controller.rb @@ -16,12 +16,16 @@ class Managements::SchoolsController < Managements::BaseController end end - def yesterday_data + def data_grow @sub_type = 2 + params[:data_type] ||= 'grow' params[:sort_by] ||= :teacher_increase_count params[:sort_direction] ||= :desc - reports = Management::SchoolYesterdayDataService.new(params).call + service = Management::SchoolDataGrowService.new(params) + @grow_summary = service.grow_summary + + reports = service.call @reports = paginateHelper reports end @@ -30,6 +34,12 @@ class Managements::SchoolsController < Managements::BaseController params[:sort_direction] ||= :desc params[:sort_by] ||= :percentage + # 无对比日期时直接返回无数据页面 + if useless_contrast_date_parameter? + @obj_count, @reports = 0, [] + return + end + @obj_count, @reports = Management::SchoolDataContrastService.new(params).call @obj_pages = Paginator.new(@obj_count, Management::SchoolDataContrastService::PAGE_SIZE, params[:page]) rescue Management::SchoolDataContrastService::ParameterError @@ -54,6 +64,11 @@ class Managements::SchoolsController < Managements::BaseController end end + def useless_contrast_date_parameter? + params[:begin_date].blank? && params[:end_date].blank? && + params[:other_begin_date].blank? &¶ms[:other_end_date].blank? + end + def load_statistic_total @teacher_total = User.teacher.count @student_total = User.student.count diff --git a/app/services/management/school_data_grow_service.rb b/app/services/management/school_data_grow_service.rb new file mode 100644 index 00000000..02df364b --- /dev/null +++ b/app/services/management/school_data_grow_service.rb @@ -0,0 +1,59 @@ +class Management::SchoolDataGrowService + include CustomSortable + + attr_reader :params + + sort_columns :teacher_increase_count, :student_increase_count, + :course_increase_count, :shixun_increase_count, :active_user_count, + default_by: :teacher_increase_count, default_direction: :desc + + def initialize(params) + @params = params + end + + def call + custom_sort(query_reports, params[:sort_by], params[:sort_direction]) + end + + def grow_summary + @_grow_summary ||= begin + query_reports.select( + 'SUM(teacher_increase_count) teacher_increase_count,'\ + 'SUM(student_increase_count) student_increase_count,'\ + 'SUM(course_increase_count) course_increase_count,'\ + 'SUM(shixun_increase_count) shixun_increase_count,'\ + 'SUM(active_user_count) active_user_count' + ).first + end + end + + private + def query_reports + reports = SchoolDailyReport.where(date: query_date) + + keyword = params[:keyword].try(:to_s).try(:strip) + if keyword.present? + reports = reports.where("school_name LIKE :keyword OR school_id LIKE :keyword", keyword: "%#{keyword}%") + end + + reports + end + + def query_date + if params[:grow_begin_date].present? + begin_time = Time.zone.parse(params[:grow_begin_date]) + end_date = if params[:grow_end_date].present? + Time.zone.parse(params[:grow_end_date]) + end + + end_date.blank? || end_date == begin_time ? begin_time : begin_time..end_date + else + yesterday + end + end + + def yesterday + # 每日凌晨5点为节点, 25日凌晨4点、3点、2点等等,未到更新数据时间点,看到的数据是:23日-24日的统计数据 + (Time.zone.now - 5.hours).beginning_of_day - 1.days + end +end diff --git a/app/services/management/school_yesterday_data_service.rb b/app/services/management/school_yesterday_data_service.rb deleted file mode 100644 index 238a7297..00000000 --- a/app/services/management/school_yesterday_data_service.rb +++ /dev/null @@ -1,32 +0,0 @@ -class Management::SchoolYesterdayDataService - include CustomSortable - - attr_reader :params - - sort_columns :student_increase_count, :teacher_increase_count, - :course_increase_count, :shixun_increase_count, :active_user_count, - default_by: :teacher_increase_count, default_direction: :desc - - def initialize(params) - @params = params - end - - def call - reports = SchoolDailyReport.where(date: yesterday) - - keyword = params[:keyword].try(:to_s).try(:strip) - if keyword.present? - reports = reports.where("school_name LIKE :keyword OR school_id LIKE :keyword", keyword: "%#{keyword}%") - end - - reports = custom_sort(reports, params[:sort_by], params[:sort_direction]) - - reports - end - - private - def yesterday - # 每日凌晨5点为节点, 25日凌晨4点、3点、2点等等,未到更新数据时间点,看到的数据是:23日-24日的统计数据 - (Time.zone.now - 5.hours).beginning_of_day - 1.days - end -end diff --git a/app/views/layouts/base_management.html.erb b/app/views/layouts/base_management.html.erb index 201e26f1..eb6ce06d 100644 --- a/app/views/layouts/base_management.html.erb +++ b/app/views/layouts/base_management.html.erb @@ -35,7 +35,7 @@
  • 学校+
  • 课堂+ diff --git a/app/views/managements/schools/_contrast_search_form.html.erb b/app/views/managements/schools/_contrast_search_form.html.erb index 2140eb97..4d24a810 100644 --- a/app/views/managements/schools/_contrast_search_form.html.erb +++ b/app/views/managements/schools/_contrast_search_form.html.erb @@ -1,26 +1,37 @@
    + <%= hidden_field_tag :contrast_column, params[:contrast_column] %> <%= hidden_field_tag :begin_date, params[:begin_date] %> <%= hidden_field_tag :end_date, params[:end_date] %> <%= hidden_field_tag :other_begin_date, params[:other_begin_date] %> <%= hidden_field_tag :other_end_date, params[:other_end_date] %> + + <%= hidden_field_tag :grow_begin_date, params[:grow_begin_date] %> + <%= hidden_field_tag :grow_end_date, params[:grow_end_date] %> - <%= text_field_tag :date_input, params[:date_input], - class: 'date-input winput-200-30', placeholder: '请选择时间段一' %> - VS - <%= text_field_tag :other_date_input, params[:other_date_input], - class: 'other-date-input winput-200-30', placeholder: '请选择时间段二' %> +
    + <%= text_field_tag :grow_date_input, params[:grow_date_input], + class: 'grow-date-input winput-220-30', placeholder: '请选择时间段', + style: 'width: 400px;' %> +
    +
    + <%= text_field_tag :date_input, params[:date_input], + class: 'date-input winput-200-30', placeholder: '请选择时间段一' %> + VS + <%= text_field_tag :other_date_input, params[:other_date_input], + class: 'other-date-input winput-200-30', placeholder: '请选择时间段二' %> +
    - <%= hidden_field_tag :data_type, params[:data_type] || 'yesterday' %> + <%= hidden_field_tag :data_type, params[:data_type] || 'grow' %> <% if params[:data_type] == 'contrast' %> 时间对比 - 日新增 + 新增数据 <% else %> 时间对比 - 日新增 + 新增数据 <% end %>
    @@ -31,28 +42,36 @@
    \ No newline at end of file diff --git a/app/views/managements/schools/_data_contrast_list.html.erb b/app/views/managements/schools/_data_contrast_list.html.erb index c2699601..ef5b5b17 100644 --- a/app/views/managements/schools/_data_contrast_list.html.erb +++ b/app/views/managements/schools/_data_contrast_list.html.erb @@ -6,8 +6,12 @@ options_for_select(@select_options, params[:contrast_column]), class: 'fl task-height-30 contrast-column-select', style: 'position: absolute; right: 30px; top: 15px;' %> +
    + 说明:新增数=时段二-时段一;新增百分比=(新增数/时段一)*100%(保留小数点后五位) +
    +<% if @obj_count.nonzero? %> @@ -63,6 +67,11 @@
    +<% else %> +
    + 暂无数据,请选择时间段对比 +
    +<% end %>