USE `data_center_new`;

DROP procedure IF EXISTS `add_column`;

DELIMITER $$

CREATE PROCEDURE add_column()

BEGIN
    -- 检查并添加 basic_company 表的 mfa_switch 字段
    IF NOT EXISTS (
        SELECT * 
        FROM information_schema.columns 
        WHERE table_schema = 'data_center_new' 
        AND table_name = 'basic_company' 
        AND column_name = 'mfa_switch'
    ) THEN
        ALTER TABLE basic_company 
        ADD COLUMN `mfa_switch` int DEFAULT 0 COMMENT '谷歌mfa服务开关。0-关闭，1-开启' AFTER `company_name`;
    END IF;

    -- 检查并添加 basic_user 表的 mfa_secret 字段
    IF NOT EXISTS (
        SELECT * 
        FROM information_schema.columns 
        WHERE table_schema = 'data_center_new' 
        AND table_name = 'basic_user' 
        AND column_name = 'mfa_secret'
    ) THEN
        ALTER TABLE basic_user 
        ADD COLUMN `mfa_secret` varchar(255)  DEFAULT NULL AFTER `mobile_number`;
    END IF;

    -- 检查并添加 basic_user 表的 mfa_bind 字段
    IF NOT EXISTS (
        SELECT * 
        FROM information_schema.columns 
        WHERE table_schema = 'data_center_new' 
        AND table_name = 'basic_user' 
        AND column_name = 'mfa_bind'
    ) THEN
        ALTER TABLE basic_user 
        ADD COLUMN `mfa_bind` int DEFAULT 0 COMMENT '用户是否绑定了mfa设备。0-未绑定，1-已绑定' AFTER `mfa_secret`;
    END IF;
    
    -- 检查并添加 basic_user 表的 mfa_bind 字段
    IF NOT EXISTS (
        SELECT * 
        FROM information_schema.columns 
        WHERE table_schema = 'data_center_new' 
        AND table_name = 'basic_user' 
        AND column_name = 'super_role'
    ) THEN
        ALTER TABLE basic_user 
        ADD COLUMN `super_role` int DEFAULT '0' COMMENT '超管权限，1超管，0普通';
    END IF;
    
    -- 检查并添加 basic_user 表的 user_type 字段
    IF NOT EXISTS (
        SELECT * 
        FROM information_schema.columns 
        WHERE table_schema = 'data_center_new' 
        AND table_name = 'basic_user' 
        AND column_name = 'user_type'
    ) THEN
        ALTER TABLE basic_user ADD COLUMN `user_type` int DEFAULT '0' COMMENT '0-未知，1-管理平台用户，2-普通平台用户' AFTER `id`;
        update basic_user set user_type = 2;
        insert  into `basic_user`(`user_type`,`company_id`,`username`,`login_name`,`password`,`salt`,`email`,`mobile_number`,`mfa_secret`,`mfa_bind`,`last_login_time`,`flag`,`expire_time`,`create_time`,`creator_id`,`modify_time`,`modifier_id`) values (1,1,'super_admin','superadmin','nVg+buw0YAs=','09bc3a7898',NULL,NULL,NULL,0,1713232531343,0,4114487556000,4114487556000,NULL,1670312031273,NULL);
    END IF;
    
    update basic_user set super_role = 1 where login_name='superadmin';
END$$

DELIMITER ;


CALL add_column();

DROP procedure IF EXISTS `add_column`;



USE `data_center_new`;

/*Table structure for table `mail_sent_result` */

DROP TABLE IF EXISTS `mail_sent_result`;

CREATE TABLE `mail_sent_result` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `device_id` varchar(255) DEFAULT NULL,
  `status` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT 'Failed-失败，Successed-成功',
  `error_msg` text,
  `log_time` bigint DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;