訂單支付成功後存儲過程 - MYSQL

BEGIN
    SET @userId = (SELECT user_id FROM t_shoporder WHERE id = orderId); /*修改訂單狀態,改爲已支付*/
    UPDATE t_shoporder SET `status` = 1,update_time = NOW() WHERE id = orderId; /*查詢用戶是否已經學習改課程*/
    SET @count = (SELECT count(1) FROM t_course_user WHERE course_id = courseId AND user_id = @userId); IF @count = 0 THEN
        /*保存用戶學習課程的記錄*/
        INSERT INTO t_course_user (course_id, user_id, STATUS, finished) VALUES(courseId, @userId,1,0); END IF; /*檢查該課程是否存在對應的班級*/
    SET @classId = (SELECT id FROM t_class WHERE course_id = courseId); /*若是存在就進行學習課程進入班級*/
    IF @classId = IS NOT NULL THEN
        /*檢查學生是否已經加入到該班級了,若是是就再也不加入*/
        SET @ccount = (SELECT COUNT(1) FROM t_classstudent WHERE class_id = @classId AND user_id = @userId); IF @ccount = 0 THEN
            /**將學生自動加入對應vip教室班級*/
            INSERT INTO t_classstudent(class_id, user_id, course_id,stuno) VALUES(@classId, @userId, courseId, CONCAT("ms_",DATE_FORMAT(NOW(),'%Y%m%d'),"_",courseId, "_"),?); END IF ; /*用戶升級爲vip和用戶積分、等級的累加*/
        SET @type = (SELECT type FROM t_user WHERE id = @userId); /*學生升級權限*/
        IF @type = 1 THEN 
            UPDATE t_user SET type = 5,jifen = jifen + 100 WHERE id = @userId; ELSE
            UPDATE t_user SET jifen = jifen + 100 WHERE id = @userId; END IF; INSERT INTO t_user_jifen( user_id, score, description, type, mark )VALUES( @userId, 100, '支付訂單積分加+100分', 1, 1 ); SELECT @userId; END
相關文章
相關標籤/搜索