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 shixuns = Shixun.select([:id, :identifier, :name, :myshixuns_count, :averge_star, :challenges_count, :trainee]). where(status: 2, hidden: 0) 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 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 user = User.find_by_mail params[:mail] if user {error: 0, messages: "找到用户"} else {error: -1, messages: "找不到用户"} end end def create_user params user = User.find_by_mail params[:mail] if user.blank? ActiveRecord::Base.transaction do # 如果Educoder中已存在与该OpenI用户的邮箱相同的用户,则会直接跳转到登录educoder的登录页面 user = User.new(lastname: params[:name], mail: params[:mail], mail_notification: email) user.login = generate_login('m') user.password = params[:password] user.certification = 1 user.save! UserExtensions.create!(user_id: user.id, school_id: School.first.id, identity: 4, gender: 0) end {error: 0, messages: "创建成功"} else {error: -1, messages: "邮箱已经存在,请直接使用邮箱登录"} end 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 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 end