From 1fc9ff448f6f6b4e699007a2fbf74afe6b9cd427 Mon Sep 17 00:00:00 2001 From: jingquan huang Date: Fri, 31 May 2019 14:16:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E6=8E=A5=E5=8F=A3=E5=88=86?= =?UTF-8?q?=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/services/cnmoocs_service.rb | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/app/services/cnmoocs_service.rb b/app/services/cnmoocs_service.rb index 72318905..bce54650 100644 --- a/app/services/cnmoocs_service.rb +++ b/app/services/cnmoocs_service.rb @@ -3,60 +3,49 @@ class CnmoocsService include GamesHelper def get_resources_data params - page = params[:pageNo].to_i - limit = params[:pageSize] || 16 - offset = page * limit.to_i + 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 limit #{offset},#{limit}") + 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 - 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 = 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 - 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 = 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 - 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: "请求成功", - page: {count: count, totalCount: totalCount, pageNo: page, pageSize: limit, pageCount: pageCount}, data: {resources: resources} } 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) + + shixuns = shixuns.order("myshixuns_count desc") 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