class CnmoocsService include ApplicationHelper include GamesHelper def get_resources_data params resources = [] 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") subjects.each do |subject| resources << {resourceId: subject.id, parentId: nil, resourceName: subject.name, accessType: 0, nodeType: 0, resourceType: 2} end elsif params[:level].to_s == "2" return {error: -1, messages: "请求二级及其更高目录时,parentId不能为空"} if params[:parentId].blank? stages = Stage.where(:subject_id => params[:parentId]) stages.each do |stage| resources << {resourceId: stage.id, parentId: params[:parentId], resourceName: stage.name, accessType: 0, nodeType: 0, resourceType: 2} end 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) shixuns.each do |shixun| resources << {resourceId: shixun.id, parentId: params[:parentId], resourceName: shixun.name, accessType: 2, nodeType: 1, resourceType: 1} end end {error: 0, messages: "请求成功", data: {resources: resources} } end def search_resources params shixuns = Shixun.select([:id, :identifier, :name, :myshixuns_count, :averge_star, :challenges_count, :trainee]). where(status: 2, hidden: 0).where("name like ?", "%#{params[:name]}%") shixuns = shixuns.order("myshixuns_count desc") shixun_list = shixun_data shixuns {error: 0, messages: "请求成功", data: shixun_list } end def find_user params c_user = CnmoocUser.find_by_uuid(params[:userName]) if c_user {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]) mail = params[:email] || c_user.generate_email name = params[:name] || "好大学_#{params[:userName]}" login = generate_login('m') Rails.logger.info("#######mail: #{mail}, #{name}, #{login}") create_params = { lastname: name, mail: mail, mail_notification: mail, password: OauthController::DEFAULT_PASSWORD, certification: 1 } 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, token) shixun = Shixun.find_by_id(params[:resourceId]) if shixun.blank? return { error: -1, messages: '资源不存在' } end { error: 0, messages: '成功', data: {accessUrl: "#{Redmine::Configuration['educoder_domain']}/shixuns/#{shixun.identifier}/challenges?authToken=#{token.value}" }} end def get_students_data params shixun = Shixun.find_by_id params[:resourceId] return {error: -1, messages: "资源id不对,请使用资源的id查找"} if shixun.blank? myshixun = shixun.myshixuns.where(:user_id => params[:userId]).includes(:games).first if myshixun.present? score = myshixun.total_score time = 0 myshixun.games.each do |game| time += game.consumes_time_int end {error: 0, messages: '成功', data: {experiment: {time: time, totalTime: score * 10}}} else {error: -1, messages: '用户还未开始学习此资源'} end 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 {resources: shixun_list} end # 为新创建的用户随机生成以m为前缀的用户名,m表示该用户是用邮箱注册 def generate_login(login_pre) us = UsersService.new us.generate_user_login(login_pre) end end