-- 开始事务
START TRANSACTION;

-- 创建临时表
CREATE TEMPORARY TABLE IF NOT EXISTS temp_user_id AS (
    SELECT user_id FROM user_dept WHERE user_id IN (0)
    -- union 0 防止结果集为空
       UNION ALL
    SELECT 0 AS user_id FROM dual
);

CREATE TEMPORARY TABLE IF NOT EXISTS temp_login_id AS (
    SELECT login_id FROM user_dept WHERE user_id IN (select user_id from temp_user_id)
    -- union 0 防止结果集为空
       UNION ALL
    SELECT 0 AS login_id FROM dual
);

CREATE TEMPORARY TABLE IF NOT EXISTS temp_advisor_id AS (
    SELECT id AS advisor_id FROM advisor_info WHERE user_id IN (select user_id from temp_user_id)
    -- union 0 防止结果集为空
       UNION ALL
    SELECT 0 AS login_id FROM dual
);

CREATE TEMPORARY TABLE IF NOT EXISTS temp_video_id AS (
    SELECT id AS video_id FROM video_live WHERE advisor_id IN (select advisor_id from temp_advisor_id)
    -- union 0 防止结果集为空
       UNION ALL
    SELECT 0 AS video_id FROM dual
);

CREATE TEMPORARY TABLE IF NOT EXISTS temp_group_id AS (
   SELECT id AS group_id FROM group_info WHERE advisor_id IN (select advisor_id from temp_advisor_id)
   -- union 0 防止结果集为空
       UNION ALL
   SELECT 0 AS group_id FROM dual
);

CREATE TEMPORARY TABLE IF NOT EXISTS temp_group_message_id AS (
   SELECT id AS message_id FROM group_message WHERE group_id IN (select group_id from temp_group_id)
   -- union 0 防止结果集为空
       UNION ALL
    SELECT 0 AS message_id FROM dual
);

CREATE TEMPORARY TABLE IF NOT EXISTS temp_course_id AS (
   SELECT id AS course_id FROM course WHERE advisor_id IN (select advisor_id from temp_advisor_id)
   -- union 0 防止结果集为空
       UNION ALL
   SELECT 0 AS course_id FROM dual
);

CREATE TEMPORARY TABLE IF NOT EXISTS temp_serial_id AS (
   SELECT id AS serial_id FROM serial WHERE advisor_id IN (select advisor_id from temp_advisor_id)
   -- union 0 防止结果集为空
       UNION ALL
   SELECT 0 AS serial_id FROM dual
);

CREATE TEMPORARY TABLE IF NOT EXISTS temp_short_video_id AS (
   SELECT id AS video_id FROM short_video WHERE advisor_id IN (select advisor_id from temp_advisor_id)
   -- union 0 防止结果集为空
       UNION ALL
   SELECT 0 AS video_id FROM dual
);

-- 测试临时表
-- SELECT * FROM temp_user_id;
-- SELECT * FROM temp_login_id;
-- SELECT * FROM temp_advisor_id;
-- SELECT * FROM temp_video_id;
-- SELECT * FROM temp_group_id;

-- 清理直播
delete from video_user_watch_collect where video_id in (select video_id from temp_video_id);
delete from video_user_time_collect where video_id in (select video_id from temp_video_id);
delete from video_user_flow where video_id in (select video_id from temp_video_id);
delete from video_live_user where video_id in (select video_id from temp_video_id);
delete from video_live_tag where video_id in (select video_id from temp_video_id);
delete from video_live_risk where video_id in (select video_id from temp_video_id);
delete from video_live_push where video_id in (select video_id from temp_video_id);
delete from video_live_mix where video_id in (select video_id from temp_video_id);
delete from video_live_message where video_id in (select video_id from temp_video_id);
delete from video_live_library where video_id in (select video_id from temp_video_id);
delete from video_live_customer_sale where video_id in (select video_id from temp_video_id);
delete from video_live_column_video where video_id in (select video_id from temp_video_id);
delete from video_live_activity where advisor_id in (select advisor_id from temp_advisor_id);
delete from video_cart where video_id in (select video_id from temp_video_id);
delete from video_browse_detail where video_id in (select video_id from temp_video_id);
delete from video_behavior_notify where video_id in (select video_id from temp_video_id);
delete from video_live where id in (select video_id from temp_video_id);

-- 清理圈子
delete from group_user_flow where group_id in (select group_id from temp_group_id);
delete from group_user_collect where group_id in (select group_id from temp_group_id);
delete from group_message_read where message_id in (select message_id from temp_group_message_id);
delete from group_message where group_id in (select group_id from temp_group_id);
delete from group_collect where group_id in (select group_id from temp_group_id);
delete from group_info where id in (select group_id from temp_group_id);

-- 清理课程
delete from course_content where course_id in (select course_id from temp_course_id);
delete from course where id in (select course_id from temp_course_id);

-- 清理合集
delete from serial_content where serial_id in (select serial_id from temp_serial_id);
delete from serial where id in (select serial_id from temp_serial_id);

-- 清理短视频
delete from short_video_watch where video_id in (select video_id from temp_short_video_id);
delete from short_video_share where video_id in (select video_id from temp_short_video_id);
delete from short_video_sale where video_id in (select video_id from temp_short_video_id);
delete from short_video_favor where video_id in (select video_id from temp_short_video_id);
delete from short_video_cart_click where video_id in (select video_id from temp_short_video_id);
delete from short_video_cart where video_id in (select video_id from temp_short_video_id);
delete from short_video where id in (select video_id from temp_short_video_id);

-- 清理订单
delete from app_order where product_id in (select video_id from temp_video_id) and product_type = 3;
delete from app_order where product_id in (select group_id from temp_group_id) and product_type = 41;
delete from app_order where product_id in (select course_id from temp_course_id) and product_type = 32;
delete from app_order where product_id in (select serial_id from temp_serial_id) and product_type = 33;
delete from app_order where product_id in (select video_id from temp_short_video_id) and product_type = 35;

-- 清理RBAC
delete from user_dept where user_id in (select user_id from temp_user_id);
delete from user where id in (select user_id from temp_user_id);
delete from users_roles where user_id in (select user_id from temp_user_id);
delete from user_login where login_id in (select login_id from temp_login_id);

-- 清理投顾
delete from advisor_info where id in (select advisor_id from temp_advisor_id);

-- 清理临时表
DROP TEMPORARY TABLE temp_user_id;

DROP TEMPORARY TABLE temp_login_id;

DROP TEMPORARY TABLE temp_advisor_id;

DROP TEMPORARY TABLE temp_video_id;

DROP TEMPORARY TABLE temp_group_id;

DROP TEMPORARY TABLE temp_group_message_id;

DROP TEMPORARY TABLE temp_course_id;

DROP TEMPORARY TABLE temp_serial_id;

-- 提交事务
COMMIT;