USE `third`;

-- ----------------------------
-- Procedure structure for p_job_rawData_tomorrow
-- ----------------------------
DROP PROCEDURE IF EXISTS `p_job_rawData_tomorrow`;
delimiter ;;
CREATE PROCEDURE `p_job_rawData_tomorrow`(IN tabledate VARCHAR(60))
BEGIN
	SET @sqlcmd = CONCAT('CREATE TABLE IF NOT EXISTS ',tabledate,' (
	  `hashId` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
		`id` int NULL DEFAULT NULL,
		`methodType` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`deviceId` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`platformIdentifyId` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`deviceTypeName` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`alertLevel` int NULL DEFAULT NULL,
        `alertLevelName` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`alertContent` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`alertTitle` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
        `alertTypeName` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`alertCancelTitle` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`alertCancelContent` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`rawData` varchar(2000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`status` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`messageId` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`buildingInfo` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`floorInfo` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`spaceInfo` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`equipmentInfo` varchar(2000) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`projectInfo` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`receive_ts` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`ts` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
        `companyId` int NULL DEFAULT NULL,
		`company` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`dateKey` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`yearKey` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`monthKey` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`dayKey` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`srcType` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`needTransfer` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`pure_target_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`targetId` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`problemReportCategoryId` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`forwardType` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`buildingId` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`buildingCode` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`target_id_mapper` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
		`updata_interval` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
   		KEY `ts` (`ts`) USING BTREE
	) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic');
    PREPARE stmt FROM @sqlcmd;
	EXECUTE stmt;
	DEALLOCATE PREPARE stmt;	
END
;;
delimiter ;

-- 计算当天的日期，并生成表名
-- SET @tabledate = DATE_FORMAT(NOW(), '%Y_%m_%d');
SET @zeta_table = '1year_rawData';

-- 调用存储过程，创建当天的表
CALL `p_job_rawData_tomorrow`(@zeta_table);


-- ----------------------------
-- Event structure for e_job_rawData_Tomorrow
-- ----------------------------
DROP EVENT IF EXISTS `e_job_rawData_Tomorrow`;
delimiter ;;
CREATE EVENT `e_job_rawData_Tomorrow`
ON SCHEDULE
EVERY '4' HOUR STARTS '2014-12-01 01:23:00'
ON COMPLETION PRESERVE
DO BEGIN  
	    SET @tabledate = DATE_FORMAT(DATE_ADD(NOW(),INTERVAL 1 DAY),'%Y_%m_%d');
			SET @rawdata_table = CONCAT('rawData_', @tabledate);
	    CALL `p_job_rawData_tomorrow`(@rawdata_table);
			-- SET @zeta_table = CONCAT('1year_rawData_', @tabledate);
	    -- CALL `p_job_rawData_tomorrow`(@zeta_table);
	  
	END
;;
delimiter ;

-- ----------------------------
-- Procedure structure for p_service_cleardata
-- ----------------------------
DROP PROCEDURE IF EXISTS `p_service_cleardata`;
delimiter ;;
CREATE PROCEDURE `p_service_cleardata`()
BEGIN
 DECLARE t_error INTEGER DEFAULT 0;  
 DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;
 START TRANSACTION;
 
 DELETE FROM rawData WHERE updata_interval = 1 and receive_ts < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 7 DAY)) * 1000;
 DELETE FROM rawData WHERE updata_interval = 10 and receive_ts < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY)) * 1000;
 DELETE FROM rawData WHERE updata_interval = 60 and receive_ts < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 90 DAY)) * 1000;
 DELETE FROM rawData WHERE updata_interval = 1440 and receive_ts < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 730 DAY)) * 1000;
 
 DELETE FROM 1year_rawData WHERE receive_ts < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 365 DAY)) * 1000;
 
 DELETE FROM raw_data_10min WHERE endpoint_milli_ts < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY)) * 1000;
 DELETE FROM raw_data_1hour WHERE endpoint_milli_ts < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 90 DAY)) * 1000;
 DELETE FROM raw_data_24hour WHERE endpoint_milli_ts < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 730 DAY)) * 1000;
 
 DELETE FROM rawData_single_id WHERE receive_ts < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 7 DAY)) * 1000;
 
 DELETE FROM alertData WHERE receive_ts < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 YEAR)) * 1000;
 
 IF t_error = 1 THEN 
   ROLLBACK; 
 ELSE 
   COMMIT; 
 END IF;
END
;;
delimiter ;


-- ----------------------------
-- Procedure structure for p_service_clear_table
-- ----------------------------
DROP PROCEDURE IF EXISTS `p_service_clear_table`;
delimiter ;;
CREATE PROCEDURE `p_service_clear_table`()
BEGIN  
    DECLARE done INT DEFAULT FALSE;  
    DECLARE tableName CHAR(255);  
    DECLARE cur CURSOR FOR   
        SELECT table_name   
        FROM information_schema.tables   
        WHERE table_schema = 'third' 
        AND (
            -- 删除 rawData_ 开头 7 天前的表
            (table_name LIKE 'rawData_%'   
             AND table_name REGEXP '^rawData_[0-9]{4}_[0-9]{2}_[0-9]{2}$'  
             AND STR_TO_DATE(SUBSTRING_INDEX(table_name, '_', -3), '%Y_%m_%d') < DATE_SUB(CURDATE(), INTERVAL 7 DAY))
            -- OR 
            -- 删除 1year_rawData_ 开头 31 天前的表
            -- (table_name LIKE '1year_rawData_%'   
            -- AND table_name REGEXP '^1year_rawData_[0-9]{4}_[0-9]{2}_[0-9]{2}$'  
            -- AND STR_TO_DATE(SUBSTRING_INDEX(table_name, '_', -3), '%Y_%m_%d') < DATE_SUB(CURDATE(), INTERVAL 365 DAY))
        );  
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;  
  
    OPEN cur;  
  
    read_loop: LOOP  
        FETCH cur INTO tableName;  
        IF done THEN  
            LEAVE read_loop;  
        END IF;  
        SET @sql = CONCAT('DROP TABLE IF EXISTS ', tableName, ';');  
        PREPARE stmt FROM @sql;  
        EXECUTE stmt;  
        DEALLOCATE PREPARE stmt;  
    END LOOP;  
  
    CLOSE cur;  
END
;;
delimiter ;

