USE iot;

-- DROP TABLE IF EXISTS `easydo_user_role`;
-- DROP TABLE IF EXISTS `easydo_project_users`;
-- DROP TABLE IF EXISTS `easydo_enterprise_users`;
-- DROP TABLE IF EXISTS `easydo_project_devices`;
-- DROP TABLE IF EXISTS `easydo_role_menus`;
-- DROP TABLE IF EXISTS `easydo_enterprise_menus`;
-- DROP TABLE IF EXISTS `easydo_sensor_record`;
-- DROP TABLE IF EXISTS `easydo_sensor`;
-- DROP TABLE IF EXISTS `easydo_device_health`;
-- DROP TABLE IF EXISTS `easydo_device`;
-- DROP TABLE IF EXISTS `easydo_project`;
-- DROP TABLE IF EXISTS `easydo_enterprise_relevancy`;
-- DROP TABLE IF EXISTS `easydo_enterprise`;
-- DROP TABLE IF EXISTS `easydo_menus_relevancy`;
-- DROP TABLE IF EXISTS `easydo_menus`;
-- DROP TABLE IF EXISTS `easydo_user_info`;
-- DROP TABLE IF EXISTS `easydo_users`;
-- DROP TABLE IF EXISTS `easydo_roles`;

--  ///  easydo_roles BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_roles` (
	`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,
	`role_name` VARCHAR(64) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_roles` ADD PRIMARY KEY pk_easydo_roles_id (`id`);
ALTER TABLE `easydo_roles` ADD KEY idx_easydo_roles_create_time (`create_time`);
ALTER TABLE `easydo_roles` ADD KEY idx_easydo_roles_update_time (`update_time`);
ALTER TABLE `easydo_roles` ADD UNIQUE KEY uk_easydo_roles_role_name (`role_name`);

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

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

--  ///  easydo_roles END

--  ///  easydo_users BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_users` (
	`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,
	`username` VARCHAR(32) NOT NULL,
	`password` VARCHAR(32) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_users` ADD PRIMARY KEY pk_easydo_users_id (`id`);
ALTER TABLE `easydo_users` ADD KEY idx_easydo_users_create_time (`create_time`);
ALTER TABLE `easydo_users` ADD KEY idx_easydo_users_update_time (`update_time`);
ALTER TABLE `easydo_users` ADD UNIQUE KEY uk_easydo_users_username (`username`);

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

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

--  ///  easydo_users END

--  ///  easydo_user_info BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_user_info` (
	`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,
	`user_id` VARCHAR(36) NOT NULL,
	`nick_name` VARCHAR(128) NOT NULL,
	`birthday` DATETIME NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_user_info` ADD PRIMARY KEY pk_easydo_user_info_id (`id`);
ALTER TABLE `easydo_user_info` ADD KEY idx_easydo_user_info_create_time (`create_time`);
ALTER TABLE `easydo_user_info` ADD KEY idx_easydo_user_info_update_time (`update_time`);
ALTER TABLE `easydo_user_info` ADD CONSTRAINT fk_easydo_user_info_user_id
	FOREIGN KEY (`user_id`) 
 		REFERENCES easydo_users(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;

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

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

--  ///  easydo_user_info END

--  ///  easydo_menus BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_menus` (
	`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,
	`sort` INT(8) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_menus` ADD PRIMARY KEY pk_easydo_menus_id (`id`);
ALTER TABLE `easydo_menus` ADD KEY idx_easydo_menus_create_time (`create_time`);
ALTER TABLE `easydo_menus` ADD KEY idx_easydo_menus_update_time (`update_time`);
ALTER TABLE `easydo_menus` ADD KEY idx_easydo_menus_sort (`sort`);

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

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

--  ///  easydo_menus END

--  ///  easydo_menus_relevancy BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_menus_relevancy` (
	`id` VARCHAR(36) NOT NULL DEFAULT '-',
	`menu_id` VARCHAR(36) NOT NULL,
	`sub_menu_id` VARCHAR(36) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_menus_relevancy` ADD PRIMARY KEY pk_easydo_menus_relevancy_id (`id`);
ALTER TABLE `easydo_menus_relevancy` ADD CONSTRAINT fk_easydo_menus_relevancy_menu_id
	FOREIGN KEY (`menu_id`) 
 		REFERENCES easydo_menus(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;
ALTER TABLE `easydo_menus_relevancy` ADD CONSTRAINT fk_easydo_menus_relevancy_sub_menu_id
	FOREIGN KEY (`sub_menu_id`) 
 		REFERENCES easydo_menus(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;

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

--  ///  easydo_menus_relevancy END

--  ///  easydo_enterprise BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_enterprise` (
	`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,
	`enterprise_name` VARCHAR(256) NOT NULL,
	`enterprise_code` VARCHAR(64) NOT NULL,
	`enterprise_type` VARCHAR(64) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_enterprise` ADD PRIMARY KEY pk_easydo_enterprise_id (`id`);
ALTER TABLE `easydo_enterprise` ADD KEY idx_easydo_enterprise_create_time (`create_time`);
ALTER TABLE `easydo_enterprise` ADD KEY idx_easydo_enterprise_update_time (`update_time`);
ALTER TABLE `easydo_enterprise` ADD UNIQUE KEY uk_easydo_enterprise_code (`enterprise_code`);

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

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

--  ///  easydo_enterprise END

--  ///  easydo_enterprise_relevancy BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_enterprise_relevancy` (
	`id` VARCHAR(36) NOT NULL DEFAULT '-',
	`enterprise_id` VARCHAR(36) NOT NULL,
	`sub_enterprise_id` VARCHAR(36) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_enterprise_relevancy` ADD PRIMARY KEY pk_easydo_enterprise_relevancy_id (`id`);
ALTER TABLE `easydo_enterprise_relevancy` ADD CONSTRAINT fk_easydo_enterprise_relevancy_epid
	FOREIGN KEY (`enterprise_id`) 
 		REFERENCES easydo_enterprise(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;
ALTER TABLE `easydo_enterprise_relevancy` ADD CONSTRAINT fk_easydo_enterprise_relevancy_subepid
	FOREIGN KEY (`sub_enterprise_id`) 
 		REFERENCES easydo_enterprise(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;

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

--  ///  easydo_enterprise_relevancy END

--  ///  easydo_project BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_project` (
	`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,
	`enterprise_id` VARCHAR(36) NOT NULL,
	`project_code` VARCHAR(64) NOT NULL,
	`project_name` VARCHAR(256) NOT NULL,
	`project_type` VARCHAR(64) NOT NULL,
	`project_status` VARCHAR(64) NOT NULL,
	`project_scale` VARCHAR(64) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_project` ADD PRIMARY KEY pk_easydo_project_id (`id`);
ALTER TABLE `easydo_project` ADD KEY idx_easydo_project_create_time (`create_time`);
ALTER TABLE `easydo_project` ADD KEY idx_easydo_project_update_time (`update_time`);
ALTER TABLE `easydo_project` ADD UNIQUE KEY uk_easydo_project_code (`project_code`);
ALTER TABLE `easydo_project` ADD CONSTRAINT fk_easydo_project_enterprise_id
	FOREIGN KEY (`enterprise_id`) 
 		REFERENCES easydo_enterprise(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;

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

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

--  ///  easydo_project END

--  ///  easydo_device BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_device` (
	`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,
	`device_identifier` VARCHAR(64) NOT NULL,
	`device_type` VARCHAR(64) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_device` ADD PRIMARY KEY pk_easydo_device_id (`id`);
ALTER TABLE `easydo_device` ADD KEY idx_easydo_device_create_time (`create_time`);
ALTER TABLE `easydo_device` ADD KEY idx_easydo_device_update_time (`update_time`);
ALTER TABLE `easydo_device` ADD UNIQUE KEY uk_easydo_device_identifier (`device_identifier`);

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

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

--  ///  easydo_device END

--  ///  easydo_device_health BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_device_health` (
	`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,
	`device_id` VARCHAR(36) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_device_health` ADD PRIMARY KEY pk_easydo_device_health_id (`id`);
ALTER TABLE `easydo_device_health` ADD KEY idx_easydo_device_health_create_time (`create_time`);
ALTER TABLE `easydo_device_health` ADD KEY idx_easydo_device_health_update_time (`update_time`);
ALTER TABLE `easydo_device_health` ADD CONSTRAINT fk_easydo_device_health_device_id
	FOREIGN KEY (`device_id`) 
 		REFERENCES easydo_device(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;

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

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

--  ///  easydo_device_health END

--  ///  easydo_sensor BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_sensor` (
	`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,
	`device_id` VARCHAR(36) NOT NULL,
	`sensor_identifier` VARCHAR(64) NOT NULL,
	`sensor_type` VARCHAR(64) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_sensor` ADD PRIMARY KEY pk_easydo_sensor_id (`id`);
ALTER TABLE `easydo_sensor` ADD KEY idx_easydo_sensor_create_time (`create_time`);
ALTER TABLE `easydo_sensor` ADD KEY idx_easydo_sensor_update_time (`update_time`);
ALTER TABLE `easydo_sensor` ADD UNIQUE KEY uk_easydo_sensor_identifier (`sensor_identifier`);
ALTER TABLE `easydo_sensor` ADD CONSTRAINT fk_easydo_sensor_device_id
	FOREIGN KEY (`device_id`) 
 		REFERENCES easydo_device(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;

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

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

--  ///  easydo_sensor END

--  ///  easydo_sensor_record BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_sensor_record` (
	`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,
	`sensor_id` VARCHAR(36) NOT NULL,
	`report_time` DATETIME NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_sensor_record` ADD PRIMARY KEY pk_easydo_sensor_record_id (`id`);
ALTER TABLE `easydo_sensor_record` ADD KEY idx_easydo_sensor_record_create_time (`create_time`);
ALTER TABLE `easydo_sensor_record` ADD KEY idx_easydo_sensor_record_update_time (`update_time`);
ALTER TABLE `easydo_sensor_record` ADD KEY idx_easydo_sensor_record_report_time (`report_time`);
ALTER TABLE `easydo_sensor_record` ADD CONSTRAINT fk_easydo_sensor_record_sensor_id
	FOREIGN KEY (`sensor_id`) 
 		REFERENCES easydo_sensor(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;

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

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

--  ///  easydo_sensor_record END

--  ///  easydo_enterprise_menus BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_enterprise_menus` (
	`id` VARCHAR(36) NOT NULL DEFAULT '-',
	`enterprise_id` VARCHAR(36) NOT NULL,
	`menu_id` VARCHAR(36) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_enterprise_menus` ADD PRIMARY KEY pk_easydo_enterprise_menus_id (`id`);
ALTER TABLE `easydo_enterprise_menus` ADD CONSTRAINT fk_easydo_enterprise_menus_epid
	FOREIGN KEY (`enterprise_id`) 
 		REFERENCES easydo_enterprise(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;
ALTER TABLE `easydo_enterprise_menus` ADD CONSTRAINT fk_easydo_enterprise_menus_id
	FOREIGN KEY (`menu_id`) 
 		REFERENCES easydo_menus(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;

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

--  ///  easydo_enterprise_menus END

--  ///  easydo_role_menus BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_role_menus` (
	`id` VARCHAR(36) NOT NULL DEFAULT '-',
	`role_id` VARCHAR(36) NOT NULL,
	`menu_id` VARCHAR(36) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_role_menus` ADD PRIMARY KEY pk_easydo_role_menus_id (`id`);
ALTER TABLE `easydo_role_menus` ADD CONSTRAINT fk_easydo_role_menus_roleid
	FOREIGN KEY (`role_id`) 
 		REFERENCES easydo_roles(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;
ALTER TABLE `easydo_role_menus` ADD CONSTRAINT fk_easydo_role_menus_id
	FOREIGN KEY (`menu_id`) 
 		REFERENCES easydo_menus(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;

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

--  ///  easydo_role_menus END

--  ///  easydo_project_devices BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_project_devices` (
	`id` VARCHAR(36) NOT NULL DEFAULT '-',
	`project_id` VARCHAR(36) NOT NULL,
	`device_id` VARCHAR(36) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_project_devices` ADD PRIMARY KEY pk_easydo_project_devices_id (`id`);
ALTER TABLE `easydo_project_devices` ADD CONSTRAINT fk_easydo_project_devices_project_id
	FOREIGN KEY (`project_id`) 
 		REFERENCES easydo_project(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;
ALTER TABLE `easydo_project_devices` ADD CONSTRAINT fk_easydo_project_devices_id
	FOREIGN KEY (`device_id`) 
 		REFERENCES easydo_device(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;

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

--  ///  easydo_project_devices END

--  ///  easydo_enterprise_users BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_enterprise_users` (
	`id` VARCHAR(36) NOT NULL DEFAULT '-',
	`enterprise_id` VARCHAR(36) NOT NULL,
	`user_id` VARCHAR(36) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_enterprise_users` ADD PRIMARY KEY pk_easydo_enterprise_users_id (`id`);
ALTER TABLE `easydo_enterprise_users` ADD CONSTRAINT fk_easydo_enterprise_users_epid
	FOREIGN KEY (`enterprise_id`) 
 		REFERENCES easydo_enterprise(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;
ALTER TABLE `easydo_enterprise_users` ADD CONSTRAINT fk_easydo_enterprise_users_id
	FOREIGN KEY (`user_id`) 
 		REFERENCES easydo_users(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;

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

--  ///  easydo_enterprise_users END

--  ///  easydo_project_users BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_project_users` (
	`id` VARCHAR(36) NOT NULL DEFAULT '-',
	`project_id` VARCHAR(36) NOT NULL,
	`user_id` VARCHAR(36) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_project_users` ADD PRIMARY KEY pk_easydo_project_users_id (`id`);
ALTER TABLE `easydo_project_users` ADD CONSTRAINT fk_easydo_project_users_projectid
	FOREIGN KEY (`project_id`) 
 		REFERENCES easydo_project(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;
ALTER TABLE `easydo_project_users` ADD CONSTRAINT fk_easydo_project_users_id
	FOREIGN KEY (`user_id`) 
 		REFERENCES easydo_users(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;

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

--  ///  easydo_project_users END

--  ///  easydo_user_role BEGIN 

CREATE TABLE IF NOT EXISTS `easydo_user_role` (
	`id` VARCHAR(36) NOT NULL DEFAULT '-',
	`user_id` VARCHAR(36) NOT NULL,
	`role_id` VARCHAR(36) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

ALTER TABLE `easydo_user_role` ADD PRIMARY KEY pk_easydo_user_role_id (`id`);
ALTER TABLE `easydo_user_role` ADD CONSTRAINT fk_easydo_user_role_users_id
	FOREIGN KEY (`user_id`) 
 		REFERENCES easydo_users(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;
ALTER TABLE `easydo_user_role` ADD CONSTRAINT fk_easydo_user_role_role_id
	FOREIGN KEY (`role_id`) 
 		REFERENCES easydo_roles(`id`) 
 		ON DELETE CASCADE 
 		ON UPDATE CASCADE;

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

--  ///  easydo_user_role END

--  ///  easydo_menus_view BEGIN

DROP VIEW IF EXISTS `easydo_menus_view`;
CREATE VIEW `easydo_menus_view` AS 
SELECT 
	a.*,
	b.menu_id,
	b.sub_menu_id
FROM 
	easydo_menus a 
LEFT JOIN 
	easydo_menus_relevancy b 
ON 
	a.ID = b.menu_id;

--  ///  easydo_menus_view END 

--  ///  easydo_enterprise_view BEGIN

DROP VIEW IF EXISTS `easydo_enterprise_view`;
CREATE VIEW `easydo_enterprise_view` AS 
SELECT 
	a.*,
	b.enterprise_id,
	b.sub_enterprise_id
FROM 
	easydo_enterprise a 
LEFT JOIN 
	easydo_enterprise_relevancy b 
ON 
	a.ID = b.enterprise_id;

--  ///  easydo_enterprise_view END 