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

162 lines
6.3 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|
6 years ago
resouces << {resouceId: shixun.id, parentId: params[:parentId], resouceName: shixun.name, accessType: 2,
6 years ago
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
6 years ago
{error: 0, messages: "找到用户", data: { userId: c_user.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,
password: OauthController::DEFAULT_PASSWORD,
certification: 1
}
6 years ago
user = User.new(create_params)
# login 有问题,只能这样赋值
user.login = login
ActiveRecord::Base.transaction do
user.save!
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)
shixun = Shixun.find_by_identifier(params[:resouceId])
if shixun.blank?
return { error: -1, messages: '资源不存在' }
end
6 years ago
{ error: 0, messages: '成功', accessUrl: "#{Redmine::Configuration['educoder_domain']}/shixuns/#{shixun.identifier}" }
end
6 years ago
def get_students_data params
6 years ago
shixun = Shixun.find_by_id params[:resouceId]
return {error: -1, messages: "资源id不对请使用资源的id查找"} if shixun.blank?
6 years ago
myshixun = shixun.myshixuns.where(:user_id => params[:userId]).includes(:games).first
6 years ago
if myshixun.present?
6 years ago
score = myshixun.total_score
time = 0
6 years ago
myshixun.games.each do |game|
time += game.consumes_time_int
end
6 years ago
{error: 0, messages: '成功', data: {time: time, score: score * 10}}
else
{error: -1, messages: '用户还未开始学习此资源'}
6 years ago
end
6 years ago
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