mirror of
https://gitlink.org.cn/Gitlink/forgeplus.git
synced 2026-05-02 19:30:48 +08:00
FIX code review
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
class Admins::AddDepartmentMemberService < ApplicationService
|
||||
|
||||
attr_reader :department, :params
|
||||
|
||||
def initialize(department, params)
|
||||
@department = department
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
columns = %i[]
|
||||
DepartmentMember.bulk_insert(*columns) do |worker|
|
||||
Array.wrap(params[:user_ids]).compact.each do |user_id|
|
||||
next if department.department_members.exists?(user_id: user_id)
|
||||
|
||||
worker.add(department_id: department.id, user_id: user_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,89 +0,0 @@
|
||||
class Admins::CheckShixunMirrorsService < ApplicationService
|
||||
Error = Class.new(StandardError)
|
||||
|
||||
def call
|
||||
bridge_images
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
check_sync_mirrors!
|
||||
|
||||
check_mirrors!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def mirrors
|
||||
bridge_images['images']
|
||||
end
|
||||
|
||||
def sync_mirrors
|
||||
bridge_images['imagesNotSync']
|
||||
end
|
||||
|
||||
def check_mirrors!
|
||||
return if mirrors.blank?
|
||||
image_names = []
|
||||
|
||||
mirrors.each do |data|
|
||||
mirror = JSON.parse(data)
|
||||
|
||||
name_repository = MirrorRepository.find_by(name: mirror['imageName'])
|
||||
id_repository = MirrorRepository.find_by(mirrorID: mirror['imageID'])
|
||||
|
||||
image_names << mirror['imageName']
|
||||
|
||||
if name_repository.blank? && id_repository.present? # 镜像名称被修改
|
||||
id_repository.update_column(:status, 2)
|
||||
MirrorOperationRecord.create!(mirror_repository_id: id_repository.id, mirror_id: mirror['imageID'],
|
||||
mirror_name: mirror['imageName'], status: 2, user_id: -1)
|
||||
elsif name_repository.blank? # 镜像不存在、创建镜像
|
||||
new_repository = MirrorRepository.create!(mirrorID: mirror['imageID'], name: mirror['imageName'])
|
||||
MirrorOperationRecord.create!(mirror_repository_id: new_repository.id, mirror_id: mirror['imageID'],
|
||||
mirror_name: mirror['imageName'], status: 0, user_id: -1)
|
||||
elsif name_repository.mirrorID != mirror['imageID'] # 镜像ID被修改
|
||||
name_repository.update_column(:status, 2)
|
||||
MirrorOperationRecord.create!(mirror_repository_id: name_repository.id, mirror_id: mirror['imageID'],
|
||||
mirror_name: mirror['imageName'], status: 1, user_id: -1)
|
||||
end
|
||||
end
|
||||
|
||||
# 判断中间层镜像是否被删除
|
||||
MirrorRepository.find_each do |mirror|
|
||||
next if mirror&.name.blank? || image_names.index(mirror.name)
|
||||
|
||||
mirror.update_column(:status, 4)
|
||||
MirrorOperationRecord.create!(mirror_repository_id: mirror.id, mirror_id: mirror&.mirrorID,
|
||||
mirror_name: mirror.name, status: 3, user_id: -1)
|
||||
end
|
||||
end
|
||||
|
||||
def check_sync_mirrors!
|
||||
return if sync_mirrors.blank?
|
||||
|
||||
sync_mirrors.each do |data|
|
||||
mirror = JSON.parse(data)
|
||||
|
||||
repository = MirrorRepository.find_by(name: mirror['imageName'])
|
||||
next if repository.blank? || repository.status != 1
|
||||
|
||||
repository.update_column(:status, 5)
|
||||
MirrorOperationRecord.create!(mirror_repository_id: repository.id, mirror_id: mirror['imageID'],
|
||||
mirror_name: mirror['imageName'], status: 4, user_id: -1)
|
||||
end
|
||||
end
|
||||
|
||||
def bridge_images
|
||||
@_bridge_images ||= begin
|
||||
url = "#{EduSetting.get('cloud_bridge')}/bridge/docker/images"
|
||||
res = Faraday.get(url)
|
||||
res = JSON.parse(res.body)
|
||||
raise Error, '拉取镜像信息异常' if res && res['code'] != 0
|
||||
|
||||
res
|
||||
rescue => e
|
||||
Rails.logger.error("get response failed ! #{e.message}")
|
||||
raise Error, '实训云平台繁忙(繁忙等级:84)'
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,35 +0,0 @@
|
||||
class Admins::DragCooperativeService < ApplicationService
|
||||
Error = Class.new(StandardError)
|
||||
|
||||
attr_reader :move, :after
|
||||
|
||||
def initialize(move, after)
|
||||
@move = move
|
||||
@after = after # 移动后下一个位置的元素
|
||||
end
|
||||
|
||||
def call
|
||||
return if move.position + 1 == after&.position # 未移动
|
||||
raise Error, '未知错误' if after && move.img_type != after.img_type
|
||||
|
||||
coo_imgs = CooImg.where(img_type: move.img_type)
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
if after.blank? # 移动至末尾
|
||||
total = coo_imgs.count
|
||||
|
||||
coo_imgs.where('position > ?', move.position).update_all('position = position - 1')
|
||||
move.update!(position: total)
|
||||
return
|
||||
end
|
||||
|
||||
if move.position > after.position # 前移
|
||||
coo_imgs.where('position >= ? AND position < ?', after.position, move.position).update_all('position = position + 1')
|
||||
move.update!(position: after.position)
|
||||
else # 后移
|
||||
coo_imgs.where('position > ? AND position <= ?', move.position, after.position).update_all('position = position - 1')
|
||||
move.update!(position: after.position)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,35 +0,0 @@
|
||||
class Admins::DragPortalImageService < ApplicationService
|
||||
Error = Class.new(StandardError)
|
||||
|
||||
attr_reader :laboratory, :move, :after
|
||||
|
||||
def initialize(laboratory, move, after)
|
||||
@laboratory = laboratory
|
||||
@move = move
|
||||
@after = after # 移动后下一个位置的元素
|
||||
end
|
||||
|
||||
def call
|
||||
return if move.position + 1 == after&.position # 未移动
|
||||
|
||||
images = laboratory.portal_images
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
if after.blank? || move.id == after.id # 移动至末尾
|
||||
total = images.count
|
||||
|
||||
images.where('position > ?', move.position).update_all('position = position - 1')
|
||||
move.update!(position: total)
|
||||
return
|
||||
end
|
||||
|
||||
if move.position > after.position # 前移
|
||||
images.where('position >= ? AND position < ?', after.position, move.position).update_all('position = position + 1')
|
||||
move.update!(position: after.position)
|
||||
else # 后移
|
||||
images.where('position > ? AND position < ?', move.position, after.position).update_all('position = position - 1')
|
||||
move.update!(position: after.position - 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,32 +0,0 @@
|
||||
class Admins::DragWeappAdvertService < ApplicationService
|
||||
attr_reader :move, :after
|
||||
|
||||
def initialize(move, after)
|
||||
@move = move
|
||||
@after = after # 移动后下一个位置的元素
|
||||
end
|
||||
|
||||
def call
|
||||
return if move.position + 1 == after&.position # 未移动
|
||||
|
||||
adverts = WeappSettings::Advert.all
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
if after.blank? || move.id == after.id # 移动至末尾
|
||||
total = adverts.count
|
||||
|
||||
adverts.where('position > ?', move.position).update_all('position = position - 1')
|
||||
move.update!(position: total)
|
||||
return
|
||||
end
|
||||
|
||||
if move.position > after.position # 前移
|
||||
adverts.where('position >= ? AND position < ?', after.position, move.position).update_all('position = position + 1')
|
||||
move.update!(position: after.position)
|
||||
else # 后移
|
||||
adverts.where('position > ? AND position < ?', move.position, after.position).update_all('position = position - 1')
|
||||
move.update!(position: after.position - 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,32 +0,0 @@
|
||||
class Admins::DragWeappCarouselService < ApplicationService
|
||||
attr_reader :move, :after
|
||||
|
||||
def initialize(move, after)
|
||||
@move = move
|
||||
@after = after # 移动后下一个位置的元素
|
||||
end
|
||||
|
||||
def call
|
||||
return if move.position + 1 == after&.position # 未移动
|
||||
|
||||
carousels = WeappSettings::Carousel.all
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
if after.blank? || move.id == after.id # 移动至末尾
|
||||
total = carousels.count
|
||||
|
||||
carousels.where('position > ?', move.position).update_all('position = position - 1')
|
||||
move.update!(position: total)
|
||||
return
|
||||
end
|
||||
|
||||
if move.position > after.position # 前移
|
||||
carousels.where('position >= ? AND position < ?', after.position, move.position).update_all('position = position + 1')
|
||||
move.update!(position: after.position)
|
||||
else # 后移
|
||||
carousels.where('position > ? AND position < ?', move.position, after.position).update_all('position = position - 1')
|
||||
move.update!(position: after.position - 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,63 +0,0 @@
|
||||
class Admins::ImportCourseMemberService < ApplicationService
|
||||
Error = Class.new(StandardError)
|
||||
|
||||
attr_reader :file, :result
|
||||
|
||||
def initialize(file)
|
||||
@file = file
|
||||
@result = { success: 0, fail: [] }
|
||||
end
|
||||
|
||||
def call
|
||||
raise Error, '文件不存在' if file.blank?
|
||||
|
||||
excel = Admins::ImportCourseMemberExcel.new(file)
|
||||
excel.read_each(&method(:create_course_member))
|
||||
|
||||
result
|
||||
rescue ApplicationImport::Error => ex
|
||||
raise Error, ex.message
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_course_member(data)
|
||||
raise '课堂角色必须为 2、3、4' unless [2, 3, 4].include?(data.role.to_i)
|
||||
|
||||
user = User.joins(:user_extension).where(user_extensions: { student_id: data.student_id, school_id: data.school_id }).first
|
||||
raise '该学号的用户不存在' if user.blank?
|
||||
course = Course.find_by(id: data.course_id)
|
||||
raise '该课堂不存在' if course.blank?
|
||||
|
||||
course_group = nil
|
||||
if data.course_group_name.present?
|
||||
course_group = course.course_groups.find_or_create_by!(name: data.course_group_name)
|
||||
end
|
||||
|
||||
member = course.course_members.find_by(user_id: user.id, role: data.role.to_i)
|
||||
# 如果已是课堂成员且是学生身份and不在指定的分班则移动到该分班
|
||||
if member.present? && member.role == 'STUDENT' && course_group && member.course_group_id != course_group&.id.to_i
|
||||
member.update!(course_group_id: course_group&.id.to_i)
|
||||
elsif member.blank?
|
||||
course.course_members.create!(user_id: user.id, role: data.role.to_i, course_group_id: course_group&.id.to_i)
|
||||
extra =
|
||||
case data.role.to_i
|
||||
when 2 then 9
|
||||
when 3 then 7
|
||||
else 10
|
||||
end
|
||||
|
||||
Tiding.create!(user_id: user.id, trigger_user_id: course.tea_id, container_id: course.id,
|
||||
container_type: 'TeacherJoinCourse', belong_container_id: course.id,
|
||||
belong_container_type: 'Course', tiding_type: 'System', extra: extra)
|
||||
end
|
||||
|
||||
result[:success] += 1
|
||||
rescue Exception => ex
|
||||
fail_data = data.as_json
|
||||
fail_data[:data] = fail_data.values.join(',')
|
||||
fail_data[:message] = ex.message
|
||||
|
||||
result[:fail] << fail_data
|
||||
end
|
||||
end
|
||||
@@ -1,123 +0,0 @@
|
||||
class Admins::SchoolDailyStatisticService < ApplicationService
|
||||
include CustomSortable
|
||||
|
||||
attr_reader :params
|
||||
|
||||
sort_columns :student_count, :teacher_count, :homework_count, :other_homework_count,
|
||||
:course_count, :active_course_count, :nearly_course_time, :shixun_count, :shixun_evaluate_count,
|
||||
default_by: :teacher_count, default_direction: :desc
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
schools = School.group('schools.id')
|
||||
|
||||
keyword = params[:keyword].try(:to_s).try(:strip)
|
||||
if keyword.present?
|
||||
schools = schools.where("schools.name LIKE :keyword OR schools.id LIKE :keyword", keyword: "%#{keyword}%")
|
||||
end
|
||||
|
||||
count = schools.count.count
|
||||
|
||||
# 根据排序字段进行查询
|
||||
schools = query_by_sort_column(schools, params[:sort_by])
|
||||
schools = custom_sort(schools, params[:sort_by], params[:sort_direction])
|
||||
|
||||
schools = schools.limit(page_size).offset(offset)
|
||||
# 查询并组装其它数据
|
||||
schools = package_other_data(schools)
|
||||
|
||||
[count, schools]
|
||||
end
|
||||
|
||||
def package_other_data(schools)
|
||||
ids = schools.map(&:id)
|
||||
|
||||
student_map = UserExtension.where(school_id: ids, identity: :student).group(:school_id).count
|
||||
teacher_map = UserExtension.where(school_id: ids, identity: :teacher).group(:school_id).count
|
||||
|
||||
homeworks = HomeworkCommon.joins(:course)
|
||||
shixun_homework_map = homeworks.where(homework_type: 4, courses: { school_id: ids }).group('school_id').count
|
||||
other_homework_map = homeworks.where(homework_type: [1, 3], courses: { school_id: ids }).group('school_id').count
|
||||
|
||||
courses = Course.where(is_delete: 0, school_id: ids).group('school_id')
|
||||
course_map = courses.count
|
||||
nearly_course_time_map = courses.joins(:course_acts).maximum('course_activities.updated_at')
|
||||
active_course_map = courses.where(is_end: false).count
|
||||
|
||||
shixun_map = Shixun.joins(user: :user_extension).where(user_extensions: { identity: :teacher, school_id: ids })
|
||||
.where(fork_from: nil).group('school_id').count
|
||||
|
||||
reports = SchoolReport.where(school_id: ids)
|
||||
evaluate_count_map = reports.each_with_object({}) { |report, obj| obj[report.school_id] = report.shixun_evaluate_count }
|
||||
|
||||
schools.map do |school|
|
||||
{
|
||||
id: school.id,
|
||||
name: school.name,
|
||||
teacher_count: teacher_map[school.id],
|
||||
student_count: student_map[school.id],
|
||||
homework_count: shixun_homework_map[school.id],
|
||||
other_homework_count: other_homework_map[school.id],
|
||||
course_count: course_map[school.id],
|
||||
nearly_course_time: nearly_course_time_map[school.id],
|
||||
active_course_count: active_course_map[school.id],
|
||||
shixun_count: shixun_map.fetch(school.id, 0),
|
||||
shixun_evaluate_count: evaluate_count_map.fetch(school.id, 0)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def query_by_sort_column(schools, sort_by_column)
|
||||
base_query_column = 'schools.id, schools.name'
|
||||
|
||||
case sort_by_column.to_s
|
||||
when 'teacher_count' then
|
||||
schools.joins('LEFT JOIN user_extensions ue ON ue.school_id = schools.id AND ue.identity = 0')
|
||||
.select("#{base_query_column}, COUNT(*) teacher_count")
|
||||
when 'student_count' then
|
||||
schools.joins('LEFT JOIN user_extensions ue ON ue.school_id = schools.id AND ue.identity = 1')
|
||||
.select("#{base_query_column}, COUNT(*) student_count")
|
||||
when 'homework_count' then
|
||||
schools.joins('LEFT JOIN courses ON courses.school_id = schools.id')
|
||||
.joins('LEFT JOIN homework_commons hc ON hc.course_id = courses.id AND hc.homework_type = 4')
|
||||
.select("#{base_query_column}, COUNT(*) homework_count")
|
||||
when 'other_homework_count' then
|
||||
schools.joins('LEFT JOIN courses ON courses.school_id = schools.id')
|
||||
.joins('LEFT JOIN homework_commons hc ON hc.course_id = courses.id AND hc.homework_type IN (1, 3)')
|
||||
.select("#{base_query_column}, COUNT(*) other_homework_count")
|
||||
when 'course_count' then
|
||||
schools.joins('LEFT JOIN courses cs ON cs.school_id = schools.id AND cs.is_delete = 0')
|
||||
.select("#{base_query_column}, COUNT(*) course_count")
|
||||
when 'shixun_count' then
|
||||
schools.joins('LEFT JOIN user_extensions ue ON ue.school_id = schools.id AND ue.identity = 0')
|
||||
.joins('LEFT JOIN users ON users.id = ue.user_id')
|
||||
.joins('LEFT JOIN shixuns sx ON sx.user_id = users.id AND sx.fork_from IS NULL')
|
||||
.select("#{base_query_column}, COUNT(*) shixun_count")
|
||||
when 'shixun_evaluate_count' then
|
||||
schools.joins('LEFT JOIN school_reports ON school_reports.school_id = schools.id')
|
||||
.select("#{base_query_column}, shixun_evaluate_count")
|
||||
when 'nearly_course_time' then
|
||||
schools.joins('LEFT JOIN courses cs ON cs.school_id = schools.id AND cs.is_delete = 0')
|
||||
.joins('LEFT JOIN course_activities acs ON acs.course_id = cs.id')
|
||||
.select("#{base_query_column}, MAX(acs.updated_at) nearly_course_time")
|
||||
when 'active_course_count' then
|
||||
schools.joins('LEFT JOIN courses cs ON cs.school_id = schools.id AND cs.is_delete = 0 AND cs.is_end = false')
|
||||
.select("#{base_query_column}, COUNT(*) active_course_count")
|
||||
else
|
||||
schools.joins('LEFT JOIN user_extensions ue ON ue.school_id = schools.id AND ue.identity = 0')
|
||||
.select("#{base_query_column}, COUNT(*) teacher_count")
|
||||
end
|
||||
end
|
||||
|
||||
def page_size
|
||||
params[:per_page] || 20
|
||||
end
|
||||
|
||||
def offset
|
||||
(params[:page].to_i.zero? ? 0 : params[:page].to_i - 1) * page_size
|
||||
end
|
||||
end
|
||||
@@ -1,43 +0,0 @@
|
||||
class Admins::ShixunAuths::AgreeApplyService < ApplicationService
|
||||
attr_reader :apply, :user, :shixun
|
||||
|
||||
def initialize(apply, user)
|
||||
@apply = apply
|
||||
@user = user
|
||||
@shixun = Shixun.find(apply.container_id)
|
||||
end
|
||||
|
||||
def call
|
||||
ActiveRecord::Base.transaction do
|
||||
apply.update!(status: 1, dealer_id: user.id)
|
||||
shixun.update!(public: 2, publish_time: Time.now)
|
||||
|
||||
# 奖励金币、经验
|
||||
reward_grade_and_experience!
|
||||
|
||||
deal_tiding!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def reward_grade_and_experience!
|
||||
score = shixun.all_score
|
||||
shixun_creator = shixun.user
|
||||
|
||||
RewardGradeService.call(shixun_creator, container_id: shixun.id, container_type: 'shixunPublish', score: score)
|
||||
|
||||
Experience.create!(user_id: shixun_creator.id, container_id: shixun.id, container_type: 'shixunPublish', score: score)
|
||||
shixun_creator.update_column(:experience, shixun_creator.experience.to_i + score)
|
||||
end
|
||||
|
||||
def deal_tiding!
|
||||
apply.tidings.where(tiding_type: 'Apply', status: 0).update_all(status: 1)
|
||||
|
||||
Tiding.create!(user_id: apply.user_id, trigger_user_id: 0,
|
||||
container_id: apply.id, container_type: 'ApplyAction',
|
||||
parent_container_id: apply.container_id, parent_container_type: apply.container_type,
|
||||
belong_container_id: apply.container_id, belong_container_type: 'Shixun',
|
||||
status: 1, tiding_type: 'System')
|
||||
end
|
||||
end
|
||||
@@ -1,35 +0,0 @@
|
||||
class Admins::ShixunAuths::RefuseApplyService < ApplicationService
|
||||
attr_reader :apply, :user, :shixun, :params
|
||||
|
||||
def initialize(apply, user, params)
|
||||
@apply = apply
|
||||
@user = user
|
||||
@shixun = Shixun.find(apply.container_id)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
ActiveRecord::Base.transaction do
|
||||
shixun.update!(public: 0)
|
||||
apply.update!(status: 2, reason: reason, dealer_id: user.id)
|
||||
|
||||
deal_tiding!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def reason
|
||||
params[:reason].to_s.strip
|
||||
end
|
||||
|
||||
def deal_tiding!
|
||||
apply.tidings.where(tiding_type: 'Apply', status: 0).update_all(status: 1)
|
||||
|
||||
Tiding.create!(user_id: apply.user_id, trigger_user_id: 0,
|
||||
container_id: apply.id, container_type: 'ApplyAction',
|
||||
parent_container_id: apply.container_id, parent_container_type: apply.container_type,
|
||||
belong_container_id: apply.container_id, belong_container_type: 'Shixun',
|
||||
status: 2, tiding_type: 'System')
|
||||
end
|
||||
end
|
||||
@@ -1,80 +0,0 @@
|
||||
class Admins::StatisticSchoolContrastDataService < ApplicationService
|
||||
ParameterError = Class.new(StandardError)
|
||||
|
||||
PAGE_SIZE = 20
|
||||
CONTRAST_COLUMN_LIST = %w(
|
||||
teacher_increase_count student_increase_count course_increase_count
|
||||
shixun_increase_count active_user_count shixun_homework_count shixun_evaluate_count
|
||||
).freeze
|
||||
|
||||
attr_reader :params, :sort_direction, :contrast_column
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
@sort_direction = params[:sort_direction].to_s
|
||||
@contrast_column = params[:contrast_column].to_s
|
||||
end
|
||||
|
||||
def call
|
||||
validate_parameter!
|
||||
reports = School.joins(:school_daily_reports).select(select_columns)
|
||||
|
||||
keyword = params[:keyword].try(:to_s).try(:strip)
|
||||
if keyword.present?
|
||||
reports = reports.where("schools.name LIKE :keyword OR schools.id LIKE :keyword", keyword: "%#{keyword}%")
|
||||
end
|
||||
|
||||
count = reports.count('distinct(schools.id)')
|
||||
|
||||
sql = query_report_sql(reports.group('schools.id').to_sql)
|
||||
reports = SchoolDailyReport.find_by_sql(sql)
|
||||
|
||||
[count, reports]
|
||||
end
|
||||
|
||||
private
|
||||
def validate_parameter!
|
||||
if %i[begin_date end_date other_begin_date other_end_date].any? { |key| params[key].blank? }
|
||||
raise ParameterError
|
||||
end
|
||||
|
||||
unless %w(desc asc).include?(sort_direction)
|
||||
raise ParameterError
|
||||
end
|
||||
|
||||
unless CONTRAST_COLUMN_LIST.include?(contrast_column)
|
||||
raise ParameterError
|
||||
end
|
||||
end
|
||||
|
||||
def format_date(date)
|
||||
Time.zone.parse(date).strftime("%Y-%m-%d")
|
||||
end
|
||||
|
||||
def offset
|
||||
(params[:page].to_i.zero? ? 0 : params[:page].to_i - 1) * PAGE_SIZE
|
||||
end
|
||||
|
||||
def select_columns
|
||||
if contrast_column != 'active_user_count'
|
||||
"schools.id school_id, schools.name school_name,"\
|
||||
"(SUM(IF(date BETWEEN '#{format_date(params[:begin_date])}' AND '#{format_date(params[:end_date])}', #{contrast_column}, 0))) total,"\
|
||||
"(SUM(IF(date BETWEEN '#{format_date(params[:other_begin_date])}' AND '#{format_date(params[:other_end_date])}', #{contrast_column}, 0))) other_total"
|
||||
else
|
||||
# 活跃用户对比时处理方法不同
|
||||
relations = SchoolDailyActiveUser.select('COUNT(distinct user_id)').joins(:school_daily_report)
|
||||
.where('school_id = schools.id')
|
||||
total_subquery = relations.where("date BETWEEN '#{format_date(params[:begin_date])}' AND '#{format_date(params[:end_date])}'").to_sql
|
||||
other_total_subquery = relations.where("date BETWEEN '#{format_date(params[:other_begin_date])}' AND '#{format_date(params[:other_end_date])}'").to_sql
|
||||
|
||||
"schools.id school_id, schools.name school_name, (#{total_subquery}) AS total, (#{other_total_subquery}) AS other_total"
|
||||
end
|
||||
end
|
||||
|
||||
def query_report_sql(from_sql)
|
||||
order_by = "(total = 0 AND other_total != 0) #{sort_direction}, (percentage != 0) #{sort_direction}, percentage #{sort_direction}"
|
||||
|
||||
"SELECT reports.*, (other_total - total) increase, (IF(other_total - total = 0, 0.0, round((other_total - total) / IF(total = 0, 1, total), 5))) percentage "\
|
||||
"FROM (#{from_sql}) reports ORDER BY #{order_by} LIMIT #{PAGE_SIZE} OFFSET #{offset}"
|
||||
end
|
||||
end
|
||||
@@ -1,107 +0,0 @@
|
||||
class Admins::StatisticSchoolDataGrowService < ApplicationService
|
||||
include CustomSortable
|
||||
|
||||
PAGE_SIZE = 20
|
||||
|
||||
attr_reader :params
|
||||
|
||||
sort_columns :teacher_increase_count, :student_increase_count,
|
||||
:course_increase_count, :shixun_increase_count, :uniq_active_user_count,
|
||||
:shixun_homework_count, :shixun_evaluate_count,
|
||||
default_by: :teacher_increase_count, default_direction: :desc
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
reports = School.where(nil)
|
||||
|
||||
reports = search_filter(reports)
|
||||
|
||||
count = reports.count
|
||||
|
||||
subquery = SchoolDailyActiveUser.select('COUNT(distinct(user_id))').joins(:school_daily_report)
|
||||
.where(date_condition_sql).where("school_id is not null and school_id = schools.id").to_sql
|
||||
reports = reports.joins("LEFT JOIN school_daily_reports sdr ON sdr.school_id = schools.id AND #{date_condition_sql}")
|
||||
reports = reports.select(
|
||||
'schools.id school_id, schools.name school_name,'\
|
||||
'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(shixun_homework_count) shixun_homework_count,'\
|
||||
'SUM(shixun_evaluate_count) shixun_evaluate_count,'\
|
||||
"(#{subquery}) uniq_active_user_count,"\
|
||||
'SUM(active_user_count) active_user_count').group('schools.id')
|
||||
|
||||
reports = custom_sort(reports, params[:sort_by], params[:sort_direction])
|
||||
reports = reports.order('school_id asc').limit(PAGE_SIZE).offset(offset)
|
||||
|
||||
[count, reports]
|
||||
end
|
||||
|
||||
def grow_summary
|
||||
@_grow_summary ||= begin
|
||||
reports = School.joins("LEFT JOIN school_daily_reports sdr ON sdr.school_id = schools.id")
|
||||
.where(date_condition_sql)
|
||||
|
||||
subquery = SchoolDailyActiveUser.select('COUNT(distinct user_id)')
|
||||
.joins('LEFT JOIN school_daily_reports sdr ON sdr.id = school_daily_active_users.school_daily_report_id')
|
||||
.where(date_condition_sql).to_sql
|
||||
reports = search_filter(reports)
|
||||
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(shixun_homework_count) shixun_homework_count,'\
|
||||
'SUM(shixun_evaluate_count) shixun_evaluate_count,'\
|
||||
"(#{subquery}) uniq_active_user_count,"\
|
||||
'SUM(active_user_count) active_user_count'
|
||||
).first
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def search_filter(relations)
|
||||
keyword = params[:keyword].try(:to_s).try(:strip)
|
||||
if keyword.present?
|
||||
relations = relations.where("schools.name LIKE :keyword OR schools.id LIKE :keyword", keyword: "%#{keyword}%")
|
||||
end
|
||||
|
||||
relations
|
||||
end
|
||||
|
||||
def date_condition_sql
|
||||
date = query_date
|
||||
if date.is_a?(Range)
|
||||
"date BETWEEN '#{date.min.strftime('%Y-%m-%d')}' AND '#{date.max.strftime('%Y-%m-%d')}'"
|
||||
else
|
||||
"date = '#{date.strftime('%Y-%m-%d')}'"
|
||||
end
|
||||
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
|
||||
|
||||
def offset
|
||||
(params[:page].to_i.zero? ? 0 : params[:page].to_i - 1) * PAGE_SIZE
|
||||
end
|
||||
end
|
||||
@@ -1,30 +0,0 @@
|
||||
class Admins::SubjectAuths::AgreeApplyService < ApplicationService
|
||||
attr_reader :apply, :user, :subject
|
||||
|
||||
def initialize(apply, user)
|
||||
@apply = apply
|
||||
@user = user
|
||||
@subject = Subject.find(apply.container_id)
|
||||
end
|
||||
|
||||
def call
|
||||
ActiveRecord::Base.transaction do
|
||||
apply.update!(status: 1, dealer_id: user.id)
|
||||
subject.update!(public: 2, publish_time: Time.now)
|
||||
|
||||
deal_tiding!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def deal_tiding!
|
||||
apply.tidings.where(tiding_type: 'Apply', status: 0).update_all(status: 1)
|
||||
|
||||
Tiding.create!(user_id: apply.user_id, trigger_user_id: 0,
|
||||
container_id: apply.id, container_type: 'ApplyAction',
|
||||
parent_container_id: apply.container_id, parent_container_type: apply.container_type,
|
||||
belong_container_id: apply.container_id, belong_container_type: 'Subject',
|
||||
status: 1, tiding_type: 'System')
|
||||
end
|
||||
end
|
||||
@@ -1,35 +0,0 @@
|
||||
class Admins::SubjectAuths::RefuseApplyService < ApplicationService
|
||||
attr_reader :apply, :user, :subject, :params
|
||||
|
||||
def initialize(apply, user, params)
|
||||
@apply = apply
|
||||
@user = user
|
||||
@subject = Subject.find(apply.container_id)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
ActiveRecord::Base.transaction do
|
||||
subject.update!(public: 0)
|
||||
apply.update!(status: 2, reason: reason, dealer_id: user.id)
|
||||
|
||||
deal_tiding!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def reason
|
||||
params[:reason].to_s.strip
|
||||
end
|
||||
|
||||
def deal_tiding!
|
||||
apply.tidings.where(tiding_type: 'Apply', status: 0).update_all(status: 1)
|
||||
|
||||
Tiding.create!(user_id: apply.user_id, trigger_user_id: 0,
|
||||
container_id: apply.id, container_type: 'ApplyAction',
|
||||
parent_container_id: apply.container_id, parent_container_type: apply.container_type,
|
||||
belong_container_id: apply.container_id, belong_container_type: 'Subject',
|
||||
status: 2, tiding_type: 'System')
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user