USE iot;

-- DROP TABLE IF EXISTS `zero_appsecret`;

--  ///  zero_appsecret BEGIN 

CREATE TABLE IF NOT EXISTS `zero_appsecret` (
	`id` VARCHAR(36) NOT NULL DEFAULT '-',
	`create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
	`update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
	`features` JSON NOT NULL,
	`app_id` VARCHAR(64) NOT NULL,
	`app_secret` VARCHAR(64) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `zero_appsecret` ADD PRIMARY KEY pk_zero_appsecret_id (`id`);
ALTER TABLE `zero_appsecret` ADD KEY idx_zero_appsecret_create_time (`create_time`);
ALTER TABLE `zero_appsecret` ADD KEY idx_zero_appsecret_update_time (`update_time`);
ALTER TABLE `zero_appsecret` ADD KEY idx_zero_appsecret_app_id (`app_id`);

DROP TRIGGER IF EXISTS `zero_appsecret_uuid`;
DELIMITER $
CREATE TRIGGER `zero_appsecret_uuid`
BEFORE INSERT
ON `zero_appsecret`
FOR EACH ROW
BEGIN
	IF new.id = '-'
    THEN 
		SET new.id = (SELECT uuid());
	END IF;
END$
DELIMITER ;

DROP TRIGGER IF EXISTS `zero_appsecret_update_time`;
DELIMITER $
CREATE TRIGGER `zero_appsecret_update_time`
BEFORE UPDATE
ON `zero_appsecret`
FOR EACH ROW
BEGIN
	SET new.update_time = (SELECT now());
END$
DELIMITER ;

--  ///  zero_appsecret END 