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 @@