mirror of
https://gitlink.org.cn/Gitlink/forgeplus.git
synced 2026-05-09 22:39:50 +08:00
init project
This commit is contained in:
34
app/queries/admins/apply_item_bank_query.rb
Normal file
34
app/queries/admins/apply_item_bank_query.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
class Admins::ApplyItemBankQuery < ApplicationQuery
|
||||
include CustomSortable
|
||||
|
||||
attr_reader :params
|
||||
|
||||
sort_columns :updated_at, default_by: :updated_at, default_direction: :desc
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
applies = ApplyAction.where(container_type: params[:type].presence || "ItemBank")
|
||||
|
||||
status =
|
||||
case params[:status]
|
||||
when 'pending' then 0
|
||||
when 'processed' then [1, 2]
|
||||
when 'agreed' then 1
|
||||
when 'refused' then 2
|
||||
else 0
|
||||
end
|
||||
applies = applies.where(status: status) if status.present?
|
||||
|
||||
# 关键字模糊查询
|
||||
keyword = params[:keyword].to_s.strip
|
||||
if keyword.present?
|
||||
applies = applies.joins(user: { user_extension: :school })
|
||||
.where('CONCAT(lastname,firstname) LIKE :keyword OR schools.name LIKE :keyword', keyword: "%#{keyword}%")
|
||||
end
|
||||
|
||||
custom_sort(applies, params[:sort_by], params[:sort_direction])
|
||||
end
|
||||
end
|
||||
34
app/queries/admins/apply_user_authentication_query.rb
Normal file
34
app/queries/admins/apply_user_authentication_query.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
class Admins::ApplyUserAuthenticationQuery < ApplicationQuery
|
||||
include CustomSortable
|
||||
|
||||
attr_reader :params
|
||||
|
||||
sort_columns :updated_at, default_by: :updated_at, default_direction: :desc
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
applies = ApplyUserAuthentication.where(auth_type: params[:type].presence || 1)
|
||||
|
||||
status =
|
||||
case params[:status]
|
||||
when 'pending' then 0
|
||||
when 'processed' then [1, 2]
|
||||
when 'agreed' then 1
|
||||
when 'refused' then 2
|
||||
else 0
|
||||
end
|
||||
applies = applies.where(status: status) if status.present?
|
||||
|
||||
# 关键字模糊查询
|
||||
keyword = params[:keyword].to_s.strip
|
||||
if keyword.present?
|
||||
applies = applies.joins(user: { user_extension: :school })
|
||||
.where('CONCAT(lastname,firstname) LIKE :keyword OR schools.name LIKE :keyword', keyword: "%#{keyword}%")
|
||||
end
|
||||
|
||||
custom_sort(applies, params[:sort_by], params[:sort_direction])
|
||||
end
|
||||
end
|
||||
30
app/queries/admins/course_list_query.rb
Normal file
30
app/queries/admins/course_list_query.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
class Admins::CourseListQuery < ApplicationQuery
|
||||
include CustomSortable
|
||||
|
||||
attr_reader :params
|
||||
|
||||
sort_columns :created_at, default_by: :created_at, default_direction: :desc
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
course_lists = CourseList.all
|
||||
|
||||
# 关键字模糊查询
|
||||
keyword = params[:keyword].to_s.strip
|
||||
if keyword.present?
|
||||
search_type = params[:search_type] || "0"
|
||||
case search_type
|
||||
when "0"
|
||||
course_lists = course_lists.joins(:user)
|
||||
.where('CONCAT(lastname, firstname) like :keyword', keyword: "%#{keyword}%")
|
||||
when "1"
|
||||
course_lists = course_lists.where('name like :keyword', keyword: "%#{keyword}%")
|
||||
end
|
||||
end
|
||||
|
||||
custom_sort(course_lists, params[:sort_by], params[:sort_direction])
|
||||
end
|
||||
end
|
||||
44
app/queries/admins/course_query.rb
Normal file
44
app/queries/admins/course_query.rb
Normal file
@@ -0,0 +1,44 @@
|
||||
class Admins::CourseQuery < ApplicationQuery
|
||||
include CustomSortable
|
||||
|
||||
attr_reader :params
|
||||
|
||||
sort_columns :created_at, default_by: :created_at, default_direction: :desc, default_table: 'courses'
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
courses = Course.all
|
||||
|
||||
courses = courses.where(id: params[:id]) if params[:id].present?
|
||||
|
||||
# 状态过滤
|
||||
status =
|
||||
case params[:status].to_s.strip
|
||||
when 'processing' then 0
|
||||
when 'ended' then 1
|
||||
end
|
||||
courses = courses.where(is_end: status) if status
|
||||
|
||||
# 单位
|
||||
if params[:school_id].present?
|
||||
courses = courses.where(school_id: params[:school_id])
|
||||
end
|
||||
|
||||
# 首页展示
|
||||
if params[:homepage_show].present? && params[:homepage_show].to_s == 'true'
|
||||
courses = courses.where(homepage_show: true)
|
||||
end
|
||||
|
||||
# 关键字
|
||||
keyword = params[:keyword].to_s.strip
|
||||
if keyword
|
||||
sql = 'CONCAT(lastname, firstname) LIKE :keyword OR courses.name LIKE :keyword OR course_lists.name LIKE :keyword'
|
||||
courses = courses.joins(:teacher, :course_list).where(sql, keyword: "%#{keyword}%")
|
||||
end
|
||||
|
||||
custom_sort(courses, params[:sort_by], params[:sort_direction])
|
||||
end
|
||||
end
|
||||
24
app/queries/admins/customer_query.rb
Normal file
24
app/queries/admins/customer_query.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
class Admins::CustomerQuery < ApplicationQuery
|
||||
include CustomSortable
|
||||
|
||||
attr_reader :params
|
||||
|
||||
sort_columns :created_at, default_by: :created_at, default_direction: :desc, default_table: 'customers'
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
customers = Customer.all
|
||||
|
||||
if params[:partner_id].present?
|
||||
customers = customers.joins(:partner_customers).where(partner_customers: { partner_id: params[:partner_id] })
|
||||
end
|
||||
|
||||
keyword = params[:keyword].to_s.strip
|
||||
customers = customers.joins(:school).where('schools.name LIKE ?', "%#{keyword}%") if keyword.present?
|
||||
|
||||
custom_sort(customers, params[:sort_by], params[:sort_direction])
|
||||
end
|
||||
end
|
||||
25
app/queries/admins/department_apply_query.rb
Normal file
25
app/queries/admins/department_apply_query.rb
Normal file
@@ -0,0 +1,25 @@
|
||||
class Admins::DepartmentApplyQuery < ApplicationQuery
|
||||
include CustomSortable
|
||||
|
||||
attr_reader :params
|
||||
|
||||
sort_columns :created_at, default_by: :created_at, default_direction: :desc
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
status = params[:status]
|
||||
|
||||
applies = ApplyAddDepartment.where(status: status) if status.present?
|
||||
|
||||
# 关键字模糊查询
|
||||
keyword = params[:keyword].to_s.strip
|
||||
if keyword.present?
|
||||
applies = applies.where('name LIKE :keyword', keyword: "%#{keyword}%")
|
||||
end
|
||||
|
||||
custom_sort(applies, params[:sort_by], params[:sort_direction])
|
||||
end
|
||||
end
|
||||
32
app/queries/admins/department_query.rb
Normal file
32
app/queries/admins/department_query.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
class Admins::DepartmentQuery < ApplicationQuery
|
||||
include CustomSortable
|
||||
|
||||
attr_reader :params
|
||||
|
||||
sort_columns :created_at, default_by: :created_at, default_direction: :desc
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
departments = Department.where(is_auth: true).without_deleted
|
||||
|
||||
keyword = params[:keyword].to_s.strip
|
||||
if keyword.present?
|
||||
departments = departments.joins(:school)
|
||||
.where('schools.name LIKE :keyword OR departments.name LIKE :keyword', keyword: "%#{keyword}%")
|
||||
end
|
||||
|
||||
if params[:with_member].to_s == 'true'
|
||||
subquery = DepartmentMember.where('department_id = departments.id').select('1 AS one').to_sql
|
||||
departments = departments.where("EXISTS(#{subquery})")
|
||||
end
|
||||
|
||||
if params[:with_identifier].to_s == 'true'
|
||||
departments = departments.where.not(identifier: nil).where.not(identifier: '')
|
||||
end
|
||||
|
||||
custom_sort(departments, params[:sort_by], params[:sort_direction])
|
||||
end
|
||||
end
|
||||
23
app/queries/admins/laboratory_query.rb
Normal file
23
app/queries/admins/laboratory_query.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
class Admins::LaboratoryQuery < ApplicationQuery
|
||||
include CustomSortable
|
||||
|
||||
attr_reader :params
|
||||
|
||||
sort_columns :id, default_by: :id, default_direction: :desc
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
laboratories = Laboratory.all
|
||||
|
||||
keyword = strip_param(:keyword)
|
||||
if keyword.present?
|
||||
like_sql = 'schools.name LIKE :keyword OR laboratories.identifier LIKE :keyword'
|
||||
laboratories = laboratories.left_joins(:school).where(like_sql, keyword: "%#{keyword}%")
|
||||
end
|
||||
|
||||
custom_sort laboratories, params[:sort_by], params[:sort_direction]
|
||||
end
|
||||
end
|
||||
36
app/queries/admins/laboratory_shixun_query.rb
Normal file
36
app/queries/admins/laboratory_shixun_query.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
class Admins::LaboratoryShixunQuery < ApplicationQuery
|
||||
attr_reader :laboratory, :params
|
||||
|
||||
def initialize(laboratory, params)
|
||||
@laboratory = laboratory
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
laboratory_shixuns = laboratory.laboratory_shixuns.joins(:shixun)
|
||||
|
||||
keyword = params[:keyword].to_s.strip
|
||||
if keyword.present?
|
||||
like_sql = 'shixuns.name LIKE :keyword OR CONCAT(users.lastname, users.firstname) LIKE :keyword'
|
||||
laboratory_shixuns = laboratory_shixuns.joins(shixun: :user).where(like_sql, keyword: "%#{keyword}%")
|
||||
end
|
||||
|
||||
# 实训状态
|
||||
laboratory_shixuns = laboratory_shixuns.where(shixuns: { status: params[:status] }) if params[:status].present?
|
||||
|
||||
# 技术平台
|
||||
if params[:tag_id].present?
|
||||
laboratory_shixuns = laboratory_shixuns.joins(shixun: :shixun_mirror_repositories)
|
||||
.where(shixun_mirror_repositories: { mirror_repository_id: params[:tag_id] })
|
||||
end
|
||||
|
||||
# 首页展示、单位自建
|
||||
%i[homepage ownership].each do |column|
|
||||
if params[column].present? && params[column].to_s == 'true'
|
||||
laboratory_shixuns = laboratory_shixuns.where(column => true)
|
||||
end
|
||||
end
|
||||
|
||||
laboratory_shixuns
|
||||
end
|
||||
end
|
||||
36
app/queries/admins/laboratory_subject_query.rb
Normal file
36
app/queries/admins/laboratory_subject_query.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
class Admins::LaboratorySubjectQuery < ApplicationQuery
|
||||
attr_reader :laboratory, :params
|
||||
|
||||
def initialize(laboratory, params)
|
||||
@laboratory = laboratory
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
laboratory_subjects = laboratory.laboratory_subjects.joins(:subject)
|
||||
|
||||
keyword = params[:keyword].to_s.strip
|
||||
if keyword.present?
|
||||
like_sql = 'subjects.name LIKE :keyword OR CONCAT(users.lastname, users.firstname) LIKE :keyword'
|
||||
laboratory_subjects = laboratory_subjects.joins(subject: :user).where(like_sql, keyword: "%#{keyword}%")
|
||||
end
|
||||
|
||||
# 状态
|
||||
laboratory_subjects = laboratory_subjects.where(subjects: { status: params[:status] }) if params[:status].present?
|
||||
|
||||
# 创建者单位
|
||||
if params[:school_id].present?
|
||||
laboratory_subjects = laboratory_subjects.joins(subjects: { user: :user_extension })
|
||||
.where(user_extensions: { school_id: params[:school_id] })
|
||||
end
|
||||
|
||||
# 首页展示、单位自建
|
||||
%i[homepage ownership].each do |column|
|
||||
if params[column].present? && params[column].to_s == 'true'
|
||||
laboratory_subjects = laboratory_subjects.where(column => true)
|
||||
end
|
||||
end
|
||||
|
||||
laboratory_subjects
|
||||
end
|
||||
end
|
||||
29
app/queries/admins/library_apply_query.rb
Normal file
29
app/queries/admins/library_apply_query.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
class Admins::LibraryApplyQuery < ApplicationQuery
|
||||
include CustomSortable
|
||||
|
||||
attr_reader :params
|
||||
|
||||
sort_columns :updated_at, default_by: :updated_at, default_direction: :desc
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
status =
|
||||
case params[:status]
|
||||
when 'processed' then %w(agreed refused)
|
||||
else params[:status]
|
||||
end
|
||||
applies = LibraryApply.where(status: status) if status.present?
|
||||
|
||||
# 关键字模糊查询
|
||||
keyword = params[:keyword].to_s.strip
|
||||
if keyword.present?
|
||||
applies = applies.joins(:library)
|
||||
.where('title LIKE :keyword OR uuid LIKE :keyword', keyword: "%#{keyword}%")
|
||||
end
|
||||
|
||||
custom_sort(applies, params[:sort_by], params[:sort_direction])
|
||||
end
|
||||
end
|
||||
20
app/queries/admins/partner_query.rb
Normal file
20
app/queries/admins/partner_query.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
class Admins::PartnerQuery < ApplicationQuery
|
||||
include CustomSortable
|
||||
|
||||
attr_reader :params
|
||||
|
||||
sort_columns :created_at, default_by: :created_at, default_direction: :desc
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
partners = Partner.all
|
||||
|
||||
keyword = params[:keyword].to_s.strip
|
||||
partners = partners.joins(:school).where('schools.name LIKE ?', "%#{keyword}%") if keyword.present?
|
||||
|
||||
custom_sort(partners, params[:sort_by], params[:sort_direction])
|
||||
end
|
||||
end
|
||||
23
app/queries/admins/school_query.rb
Normal file
23
app/queries/admins/school_query.rb
Normal file
@@ -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)
|
||||
Rails.logger.info("###########{keyword}")
|
||||
if keyword
|
||||
schools = schools.where('schools.name LIKE ?', "%#{keyword}%")
|
||||
end
|
||||
schools = schools.left_joins(:user_extensions).select('schools.*, IFNULL(count(user_extensions.user_id),0) users_count').group('schools.id')
|
||||
custom_sort schools, params[:sort_by], params[:sort_direction]
|
||||
end
|
||||
end
|
||||
49
app/queries/admins/subject_query.rb
Normal file
49
app/queries/admins/subject_query.rb
Normal file
@@ -0,0 +1,49 @@
|
||||
class Admins::SubjectQuery < ApplicationQuery
|
||||
include CustomSortable
|
||||
|
||||
attr_reader :params
|
||||
|
||||
sort_columns :created_at, default_by: :created_at, default_direction: :desc, default_table: 'subjects'
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
subjects = Subject.all
|
||||
|
||||
subjects = subjects.where(id: params[:id]) if params[:id].present?
|
||||
|
||||
# 状态过滤
|
||||
status =
|
||||
case params[:status].to_s.strip
|
||||
when "editing" then {status: 0}
|
||||
when "applying" then {status: 2, public: [0, 1]}
|
||||
when "pending" then {public: 1}
|
||||
when "published" then {public: 2}
|
||||
end
|
||||
|
||||
subjects = subjects.where(status) if status
|
||||
|
||||
# 创建者单位
|
||||
if params[:school_id].present?
|
||||
subjects = subjects.joins(user: :user_extension).where(user_extensions: { school_id: params[:school_id] })
|
||||
end
|
||||
|
||||
# 首页展示、金课
|
||||
%i[homepage_show excellent].each do |column|
|
||||
if params[column].present? && params[column].to_s == 'true'
|
||||
subjects = subjects.where(column => true)
|
||||
end
|
||||
end
|
||||
|
||||
# 关键字
|
||||
keyword = params[:keyword].to_s.strip
|
||||
if keyword
|
||||
sql = 'CONCAT(lastname, firstname) LIKE :keyword OR subjects.name LIKE :keyword'
|
||||
subjects = subjects.joins(:user).where(sql, keyword: "%#{keyword}%")
|
||||
end
|
||||
|
||||
custom_sort(subjects, params[:sort_by], params[:sort_direction])
|
||||
end
|
||||
end
|
||||
24
app/queries/admins/unit_apply_query.rb
Normal file
24
app/queries/admins/unit_apply_query.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
class Admins::UnitApplyQuery < ApplicationQuery
|
||||
include CustomSortable
|
||||
|
||||
attr_reader :params
|
||||
|
||||
sort_columns :created_at, default_by: :created_at, default_direction: :desc
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
unit_applies = ApplyAddSchool.where(status:0)
|
||||
|
||||
# 关键字模糊查询
|
||||
keyword = params[:keyword].to_s.strip
|
||||
if keyword.present?
|
||||
unit_applies = unit_applies.where("name like ?","%#{keyword}%")
|
||||
end
|
||||
|
||||
custom_sort(unit_applies, params[:sort_by], params[:sort_direction])
|
||||
end
|
||||
end
|
||||
|
||||
52
app/queries/admins/user_query.rb
Normal file
52
app/queries/admins/user_query.rb
Normal file
@@ -0,0 +1,52 @@
|
||||
class Admins::UserQuery < ApplicationQuery
|
||||
include CustomSortable
|
||||
|
||||
attr_reader :params
|
||||
|
||||
sort_columns :created_on, :last_login_on, :experience, :grade, default_by: :created_on, default_direction: :desc
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
users = User.where(type: 'User')
|
||||
|
||||
# 云上实验室
|
||||
users = users.where(laboratory_id: params[:laboratory_id]) if params[:laboratory_id].present?
|
||||
|
||||
# 状态
|
||||
status = params[:status]
|
||||
users = users.where(status: status) if status.present?
|
||||
|
||||
# 职业
|
||||
users = users.joins(:user_extension).where(user_extensions: { identity: params[:identity] }) if params[:identity].present?
|
||||
|
||||
# 授权类型
|
||||
if params[:auto_trial].present?
|
||||
users = users.joins(user_extension: :school).where(schools: { auto_users_trial: params[:auto_trial].to_i == 1 })
|
||||
end
|
||||
|
||||
# 关键字检索
|
||||
keyword = params[:keyword].to_s.strip.presence
|
||||
if keyword
|
||||
sql = 'CONCAT(lastname, firstname) LIKE :keyword OR login LIKE :keyword OR mail LIKE :keyword OR phone LIKE :keyword'
|
||||
users = users.where(sql, keyword: "%#{keyword}%")
|
||||
end
|
||||
|
||||
# 姓名
|
||||
name = params[:name].to_s.strip.presence
|
||||
if name.present?
|
||||
users = users.where('CONCAT(lastname, firstname) LIKE :name', name: "%#{name}%")
|
||||
end
|
||||
|
||||
# 单位ID
|
||||
users = users.joins(:user_extension).where(user_extensions: { school_id: params[:school_id] }) if params[:school_id].present?
|
||||
|
||||
# 学校名称
|
||||
school_name = params[:school_name].to_s.strip.presence
|
||||
users = users.joins(user_extension: :school).where('schools.name LIKE ?', "%#{school_name}%") if school_name
|
||||
|
||||
custom_sort(users, params[:sort_by], params[:sort_direction])
|
||||
end
|
||||
end
|
||||
142
app/queries/admins/user_statistic_query.rb
Normal file
142
app/queries/admins/user_statistic_query.rb
Normal file
@@ -0,0 +1,142 @@
|
||||
class Admins::UserStatisticQuery < ApplicationQuery
|
||||
include CustomSortable
|
||||
|
||||
attr_reader :params
|
||||
|
||||
sort_columns :study_challenge_count, :finish_challenge_count, :study_shixun_count, :finish_shixun_count,
|
||||
default_by: :finish_shixun_count, default_direction: :desc
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
users = User.where(type: 'User').group(:id)
|
||||
|
||||
users = users.joins(:user_extension).where(user_extensions: { school_id: params[:school_id] }) if params[:school_id].present?
|
||||
|
||||
total = users.count.count
|
||||
|
||||
# 根据排序字段进行查询
|
||||
users = query_by_sort_column(users, params[:sort_by])
|
||||
users = custom_sort(users, params[:sort_by], params[:sort_direction])
|
||||
|
||||
users = users.includes(user_extension: [:school, :department])
|
||||
users = users.limit(page_size).offset(offset).to_a
|
||||
# 查询并组装其它数据
|
||||
users = package_other_data(users)
|
||||
|
||||
[total, users]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def package_other_data(users)
|
||||
ids = users.map(&:id)
|
||||
|
||||
study_myshixun = Myshixun.where(user_id: ids)
|
||||
finish_myshixun = Myshixun.where(user_id: ids, status: 1)
|
||||
study_challenge = Game.where(user_id: ids).where(status: [0, 1, 2])
|
||||
finish_challenge = Game.where(user_id: ids).where(status: 2)
|
||||
|
||||
if time_range.present?
|
||||
study_myshixun = study_myshixun.where(updated_at: time_range)
|
||||
finish_myshixun = finish_myshixun.where(updated_at: time_range)
|
||||
study_challenge = study_challenge.where(updated_at: time_range)
|
||||
finish_challenge = finish_challenge.where(updated_at: time_range)
|
||||
end
|
||||
|
||||
study_myshixun_map = study_myshixun.reorder(nil).group(:user_id).count
|
||||
finish_myshixun_map = finish_myshixun.reorder(nil).group(:user_id).count
|
||||
study_challenge_map = study_challenge.reorder(nil).group(:user_id).count
|
||||
finish_challenge_map = finish_challenge.reorder(nil).group(:user_id).count
|
||||
evaluate_count_map = study_challenge.reorder(nil).group(:user_id).sum(:evaluate_count)
|
||||
cost_time_map = study_challenge.reorder(nil).group(:user_id).sum(:cost_time)
|
||||
|
||||
users.each do |user|
|
||||
user._extra_data = {
|
||||
study_shixun_count: study_myshixun_map.fetch(user.id, 0),
|
||||
finish_shixun_count: finish_myshixun_map.fetch(user.id, 0),
|
||||
study_challenge_count: study_challenge_map.fetch(user.id, 0),
|
||||
finish_challenge_count: finish_challenge_map.fetch(user.id, 0),
|
||||
evaluate_count: evaluate_count_map.fetch(user.id, 0),
|
||||
cost_time: cost_time_map.fetch(user.id, 0),
|
||||
}
|
||||
end
|
||||
|
||||
users
|
||||
end
|
||||
|
||||
def query_by_sort_column(users, sort_by_column)
|
||||
base_query_column = 'users.*'
|
||||
|
||||
case sort_by_column.to_s
|
||||
when 'study_shixun_count' then
|
||||
users =
|
||||
if time_range.present?
|
||||
users.joins("LEFT JOIN myshixuns ON myshixuns.user_id = users.id "\
|
||||
"AND myshixuns.updated_at BETWEEN '#{time_range.min}' AND '#{time_range.max}'")
|
||||
else
|
||||
users.left_joins(:myshixuns)
|
||||
end
|
||||
|
||||
users.select("#{base_query_column}, COUNT(*) study_shixun_count")
|
||||
when 'finish_shixun_count' then
|
||||
users =
|
||||
if time_range.present?
|
||||
users.joins("LEFT JOIN myshixuns ON myshixuns.user_id = users.id AND myshixuns.status = 1 AND "\
|
||||
"myshixuns.updated_at BETWEEN '#{time_range.min}' AND '#{time_range.max}'")
|
||||
else
|
||||
users.joins('LEFT JOIN myshixuns ON myshixuns.user_id = users.id AND myshixuns.status = 1')
|
||||
end
|
||||
|
||||
users.select("#{base_query_column}, COUNT(*) finish_shixun_count")
|
||||
when 'study_challenge_count' then
|
||||
users =
|
||||
if time_range.present?
|
||||
users.joins('LEFT JOIN myshixuns ON myshixuns.user_id = users.id')
|
||||
.joins("LEFT JOIN games ON games.myshixun_id = myshixuns.id "\
|
||||
"AND games.status IN (0,1,2) AND games.updated_at BETWEEN '#{time_range.min}' AND '#{time_range.max}'")
|
||||
else
|
||||
users.joins('LEFT JOIN myshixuns ON myshixuns.user_id = users.id')
|
||||
.joins("LEFT JOIN games ON games.myshixun_id = myshixuns.id AND games.status IN (0,1,2)")
|
||||
end
|
||||
|
||||
users.select("#{base_query_column}, COUNT(*) study_challenge_count")
|
||||
when 'finish_challenge_count' then
|
||||
users =
|
||||
if time_range.present?
|
||||
users#.joins('LEFT JOIN myshixuns ON myshixuns.user_id = users.id')
|
||||
.joins("LEFT JOIN games ON games.user_id = users.id "\
|
||||
"AND games.status = 2 AND games.updated_at BETWEEN '#{time_range.min}' AND '#{time_range.max}'")
|
||||
else
|
||||
users#.joins('LEFT JOIN myshixuns ON myshixuns.user_id = users.id')
|
||||
.joins("LEFT JOIN games ON games.user_id = users.id AND games.status = 2")
|
||||
end
|
||||
|
||||
users.select("#{base_query_column}, COUNT(*) finish_challenge_count")
|
||||
else
|
||||
users
|
||||
end
|
||||
end
|
||||
|
||||
def time_range
|
||||
@_time_range ||= begin
|
||||
case params[:date]
|
||||
when 'weekly' then 1.weeks.ago..Time.now
|
||||
when 'monthly' then 1.months.ago..Time.now
|
||||
when 'quarterly' then 3.months.ago..Time.now
|
||||
when 'yearly' then 1.years.ago..Time.now
|
||||
else ''
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def page_size
|
||||
params[:per_page].to_i.zero? ? 20 : params[:per_page].to_i
|
||||
end
|
||||
|
||||
def offset
|
||||
(params[:page].to_i.zero? ? 0 : params[:page].to_i - 1) * page_size
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user