You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pgfqe6ch8/app/services/cnmoocs_service.rb

164 lines
6.4 KiB

class CnmoocsService
include ApplicationHelper
include GamesHelper
def get_resources_data params
page = params[:pageNo].to_i
limit = params[:pageSize] || 16
offset = page * limit.to_i
6 years ago
resouces = []
if params[:level].to_s == "1"
subjects = Subject.find_by_sql("SELECT subjects.id, subjects.name, subjects.status, COUNT(myshixuns.id) AS myshixun_member_count
FROM myshixuns, stage_shixuns, subjects WHERE myshixuns.shixun_id = stage_shixuns.shixun_id
AND stage_shixuns.subject_id = subjects.id AND `subjects`.`hidden` = 0 AND `subjects`.`status` = 2
GROUP BY subjects.id ORDER BY myshixun_member_count DESC limit #{offset},#{limit}")
subjects.each do |subject|
resouces << {resouceId: subject.id, parentId: nil, resouceName: subject.name, accessType: 0, nodeType: 0,
resouceType: 2}
end
totalCount = Subject.where(:status => 2, :hidden => 0).count
count = subjects.count
elsif params[:level].to_s == "2"
return {error: -1, messages: "请求二级及其更高目录时parentId不能为空"} if params[:parentId].blank?
stages = Stage.where(:subject_id => params[:parentId]).offset(offset).limit(limit)
stages.each do |stage|
resouces << {resouceId: stage.id, parentId: params[:parentId], resouceName: stage.name, accessType: 0, nodeType: 0,
resouceType: 2}
end
totalCount = Stage.where(:subject_id => params[:parentId]).count
count = stages.count
elsif params[:level].to_s == "3"
return {error: -1, messages: "请求二级及其更高目录时parentId不能为空"} if params[:parentId].blank?
shixun_ids = StageShixun.where(:stage_id => params[:parentId]).pluck(:shixun_id)
shixuns = Shixun.where(:id => shixun_ids).offset(offset).limit(limit)
shixuns.each do |shixun|
resouces << {resouceId: shixun.identifier, parentId: params[:parentId], resouceName: shixun.name, accessType: 2,
nodeType: 1, resouceType: 1}
end
totalCount = Shixun.where(:id => shixun_ids).count
count = shixuns.count
end
pageCount = ((totalCount / limit.to_f) == (totalCount / limit)) ? (totalCount / limit) : ((totalCount / limit) + 1)
{error: 0, messages: "请求成功",
6 years ago
page: {count: count, totalCount: totalCount, pageNo: page, pageSize: limit, pageCount: pageCount},
data: {resouces: resouces} }
end
def search_resources params
page = params[:pageNo].to_i
limit = params[:pageSize] || 16
offset = page * limit.to_i
shixuns = Shixun.select([:id, :identifier, :name, :myshixuns_count, :averge_star, :challenges_count, :trainee]).
where(status: 2, hidden: 0).where("name like ?", "%#{params[:name]}%")
shixun_count = shixuns.count
pageCount = ((shixun_count / limit.to_f) == (shixun_count / limit)) ? (shixun_count / limit) : ((shixun_count / limit) + 1)
shixuns = shixuns.order("myshixuns_count desc").offset(offset).limit(limit)
shixun_list = shixun_data shixuns
{error: 0, messages: "请求成功",
page: {count: shixuns.count, totalCount: shixun_count, pageNo: page, pageSize: limit, pageCount: pageCount},
data: shixun_list }
end
def find_user params
6 years ago
c_user = CnmoocUser.find_by_uuid(params[:userName])
if c_user
{error: 0, messages: "找到用户", data: { userId: user.id } }
else
{error: -1, messages: "找不到用户"}
end
end
def create_user(params)
c_user = CnmoocUser.find_by_uuid(params[:userName])
if c_user.present?
return { error: -1, messages: '用户已存在' }
end
c_user = CnmoocUser.new(uuid: params[:userName], name: params[:name])
6 years ago
mail = params[:email] || c_user.generate_email
name = params[:name] || "好大学_#{params[:userName]}"
6 years ago
login = generate_login('m')
6 years ago
Rails.logger.info("#######mail: #{mail} #{name}, #{login}")
create_params = {
lastname: name,
mail: mail,
mail_notification: mail,
6 years ago
login: login,
password: OauthController::DEFAULT_PASSWORD,
certification: 1
}
ActiveRecord::Base.transaction do
user = User.create!(create_params)
UserExtensions.create!(user_id: user.id, school_id: School.first.id, identity: 4, gender: 0)
c_user.user_id = user.id
c_user.save!
end
{ error: 0, messages: "创建成功", data: { userId: user.id } }
end
def login_educoder params
user, last_login_on = User.try_to_login(params[:mail], params[:password])
if user
self.logged_user = user
{error: 0, messages: "登录成功"}
else
{error: -1, messages: "登录失败,请检查邮箱和密码是否正确"}
end
end
def source_url(params)
if session[:third_party_user_id].blank?
user = User.find(params[:userId])
session[:third_party_user_id] = user.user_source.id
end
shixun = Shixun.find_by_identifier(params[:resouceId])
if shixun.blank?
return { error: -1, messages: '资源不存在' }
end
{ error: 0, messages: '成功', accessUrl: "#{Redmine::Configuration['educoder_domain']}/shixuns/#{shixun.id}" }
end
6 years ago
def get_students_data params
subject = Subject.find_by_id params[:resouceId]
return {error: -1, messages: "资源id不对请使用一级目录资源查找"} if subject.blank?
shixun_ids = StageShixun.where(:stage_id => subject.stages).pluck(:shixun_id)
myshixuns = Myshixun.where(:user_id => params[:userId], shixun_id: shixun_ids).includes(:games)
score = 0
time = 0
myshixuns.each do |myshixun|
score += myshixun.total_score
myshixun.games.each do |game|
time += game.consumes_time_int
end
end
6 years ago
{error: 0, messages: '成功', data: {time: time, score: score * 10}}
6 years ago
end
private
def shixun_data shixuns
shixun_list = []
shixuns.includes(:tag_repertoires).each do |shixun|
tag_name = shixun.tag_repertoires.first.try(:name)
level = %W(初级 中级 高级 顶级)[shixun.trainee - 1]
shixun_list << {identifier: shixun.identifier, name: shixun.name, students_count: shixun.myshixuns_count,
challenges_count: shixun.challenges_count, score_info: shixun.averge_star, level: level}
end
{resouces: shixun_list}
end
6 years ago
# 为新创建的用户随机生成以m为前缀的用户名m表示该用户是用邮箱注册
def generate_login(login_pre)
us = UsersService.new
us.generate_user_login(login_pre)
end
end