DROP PROCEDURE IF EXISTS add_tb_column;
DELIMITER $$
CREATE PROCEDURE add_tb_column()
BEGIN
		DECLARE  CurrentDatabase VARCHAR(100);
SELECT DATABASE() INTO CurrentDatabase;

-- fs_user_course_video_red_package 表添加 period_id 字段
IF NOT EXISTS (SELECT 1 FROM information_schema.COLUMNS  WHERE TABLE_SCHEMA=CurrentDatabase
            AND TABLE_NAME='fs_user_course_video_red_package'
            AND COLUMN_NAME='period_id' )
		THEN
            alter table fs_user_course_video_red_package add column period_id bigint COMMENT '营期id';
        END IF;

-- fs_user_course_video_red_package 表添加 data_type 字段
IF NOT EXISTS (SELECT 1 FROM information_schema.COLUMNS  WHERE TABLE_SCHEMA=CurrentDatabase
				AND TABLE_NAME='fs_user_course_video_red_package'
				AND COLUMN_NAME='data_type' )
		THEN
            alter table fs_user_course_video_red_package add column data_type int COMMENT '类型，1-课程；2-营期；3-sop模板';
        END IF;

-- company 表添加 fs_user_is_default_black 字段
IF NOT EXISTS (SELECT 1 FROM information_schema.COLUMNS  WHERE TABLE_SCHEMA=CurrentDatabase
				AND TABLE_NAME='company'
				AND COLUMN_NAME='fs_user_is_default_black')
		THEN
            alter table company add column fs_user_is_default_black tinyint(1) COMMENT '会员是否默认黑名单，1-是；0-否（用于销售分享成为会员的操作）';
        END IF;

-- company_user 表添加 ma_open_id 字段
IF NOT EXISTS (SELECT 1 FROM information_schema.COLUMNS  WHERE TABLE_SCHEMA=CurrentDatabase
				AND TABLE_NAME='company_user'
				AND COLUMN_NAME='ma_open_id')
		THEN
            alter table company_user add column ma_open_id varchar(50) COMMENT '微信小程序OPENID（如果有小程序授权）';
        END IF;

--  修改统计表字段
IF EXISTS (SELECT 1 FROM information_schema.COLUMNS  WHERE TABLE_SCHEMA=CurrentDatabase
				AND TABLE_NAME='fs_user_course_count'
				AND COLUMN_NAME='course_id')
		THEN
            alter table fs_user_course_count change course_id course_ids VARCHAR(500) COMMENT '关联课程（营期）id';
        END IF;

--  添加 fs_course_watch_log 表的索引，字段为 period_id
IF NOT EXISTS ( SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = CurrentDatabase
                AND TABLE_NAME = 'fs_course_watch_log'
                AND INDEX_NAME = 'idx_period_id' )
        THEN
            alter table fs_course_watch_log ADD INDEX idx_period_id ( `period_id` ASC );
        END IF;

--  添加 fs_course_answer_logs 表的索引，字段为 period_id
IF NOT EXISTS ( SELECT 1 FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = CurrentDatabase
                AND TABLE_NAME = 'fs_course_answer_logs'
                AND INDEX_NAME = 'idx_period_id' )
        THEN
ALTER TABLE fs_course_answer_logs ADD INDEX idx_period_id(`period_id` ASC);
END IF;

END;

CALL add_tb_column;

DROP PROCEDURE IF EXISTS add_tb_column;
