From 5874450d14483822cd80819e1c76b102d2a1417c Mon Sep 17 00:00:00 2001 From: alan <547533434@qq.com> Date: Mon, 29 Dec 2014 22:20:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E3=80=8A=E5=AD=A6=E7=94=9F?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E6=8E=92=E5=BA=8F=E7=94=A8=E7=9A=84=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E8=BF=87=E7=A8=8B=E3=80=8B=20Signed-off-by:=20alan=20?= =?UTF-8?q?<547533434@qq.com>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...29141201_create_pro_select_member_score.rb | 108 ++++++++++++++++++ db/schema.rb | 2 +- 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20141229141201_create_pro_select_member_score.rb diff --git a/db/migrate/20141229141201_create_pro_select_member_score.rb b/db/migrate/20141229141201_create_pro_select_member_score.rb new file mode 100644 index 000000000..e7910a5ff --- /dev/null +++ b/db/migrate/20141229141201_create_pro_select_member_score.rb @@ -0,0 +1,108 @@ +class CreateProSelectMemberScore < ActiveRecord::Migration + def up + sql_delete = ("DROP PROCEDURE IF EXISTS `member_score`;") + sql = (" +CREATE PROCEDURE `member_score`(IN courseid INT, IN groupid INT,IN start_from INT, IN nums INT, IN sort_by VARCHAR(10)) +BEGIN + + CREATE TEMPORARY TABLE IF NOT EXISTS course_teachers ( + id INT + + ); + TRUNCATE TABLE course_teachers; + + CREATE TEMPORARY TABLE IF NOT EXISTS mems ( + id INT , + user_id INT, + course_id INT, + created_on DATETIME, + course_group_id INT, + score FLOAT DEFAULT 0 + ); + TRUNCATE TABLE mems; + + CREATE TEMPORARY TABLE IF NOT EXISTS mem_home ( + id INT, + user_id INT, + course_id INT, + created_on DATETIME, + course_group_id INT, + home_id INT, + score FLOAT DEFAULT 0 + ); + TRUNCATE TABLE mem_home; + + CREATE TEMPORARY TABLE IF NOT EXISTS t_scores ( + + home_id INT, + score FLOAT + ); + TRUNCATE TABLE t_scores; + + CREATE TEMPORARY TABLE IF NOT EXISTS s_scores ( + + home_id INT, + score FLOAT + ); + TRUNCATE TABLE s_scores; + + CREATE TEMPORARY TABLE IF NOT EXISTS scores ( + + user_id INT, + score FLOAT + ); + TRUNCATE TABLE scores; + + INSERT INTO course_teachers (SELECT members.user_id FROM members WHERE members.user_id NOT IN (SELECT student_id FROM students_for_courses WHERE course_id = courseid)); + + + + INSERT INTO mems (id, user_id, course_id, created_on, course_group_id) + (SELECT members.id, members.user_id, members.course_id, members.created_on, members.course_group_id + FROM members WHERE course_id = courseid AND members.user_id NOT IN (SELECT id FROM course_teachers)); + + INSERT INTO mem_home (id, user_id, course_id, created_on, course_group_id,home_id) + (SELECT members.id, members.user_id, members.course_id, members.created_on, members.course_group_id, homework_attaches.id + FROM members, homework_attaches WHERE course_id = courseid + AND members.user_id = homework_attaches.user_id AND members.user_id NOT IN (SELECT id FROM course_teachers) + AND homework_attaches.bid_id IN (SELECT bid_id FROM homework_for_courses WHERE course_id = courseid )); + + INSERT INTO t_scores (home_id, score) (SELECT rateable_id,AVG(seems_rateable_rates.stars) + FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id IN (SELECT mem_home.home_id FROM mem_home) + AND rater_id IN (SELECT id FROM course_teachers) + GROUP BY rateable_id); + + INSERT INTO s_scores (home_id, score) (SELECT rateable_id,AVG(seems_rateable_rates.stars) + FROM seems_rateable_rates WHERE rateable_type = 'HomeworkAttach' AND rateable_id IN (SELECT mem_home.home_id FROM mem_home) + AND rater_id NOT IN (SELECT id FROM course_teachers) + GROUP BY rateable_id) ; + + UPDATE mem_home, t_scores SET mem_home.score = t_scores.score WHERE mem_home.home_id = t_scores.home_id ; + + UPDATE mem_home, s_scores SET mem_home.score = s_scores.score WHERE mem_home.home_id = s_scores.home_id AND mem_home.score = 0; + + INSERT INTO scores (user_id, score) (SELECT user_id, SUM(score) FROM mem_home GROUP BY user_id); + UPDATE mems, scores SET mems.score = scores.score WHERE mems.user_id = scores.user_id; + IF sort_by = '' OR sort_by = 'desc' THEN + IF groupid <> 0 THEN + SELECT * FROM mems WHERE course_group_id = groupid ORDER BY score DESC ; + ELSE + SELECT * FROM mems ORDER BY score DESC LIMIT start_from, nums; + END IF; + ELSE + IF groupid <> 0 THEN + SELECT * FROM mems WHERE course_group_id = groupid ORDER BY score ASC ; + ELSE + SELECT * FROM mems ORDER BY score ASC LIMIT start_from, nums; + END IF; + END IF; + END; + +") + execute(sql_delete) + execute(sql) + end + + def down + end +end diff --git a/db/schema.rb b/db/schema.rb index 5725d8221..39e0a7d1d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20141229025925) do +ActiveRecord::Schema.define(:version => 20141229141201) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false