USE `data_center_new`;

DROP procedure IF EXISTS `add_column`;

DELIMITER $$

CREATE PROCEDURE add_column()

BEGIN
    IF NOT EXISTS (
        SELECT * 
        FROM information_schema.columns 
        WHERE table_schema = 'data_center_new' 
        AND table_name = 'basic_monitoring_asset' 
        AND column_name = 'class_big_id'
    ) THEN
        ALTER TABLE basic_monitoring_asset 
        ADD COLUMN `class_big_id` bigint DEFAULT NULL AFTER `class_big`;
    END IF;
    
    IF NOT EXISTS (
        SELECT * 
        FROM information_schema.columns 
        WHERE table_schema = 'data_center_new' 
        AND table_name = 'basic_monitoring_asset' 
        AND column_name = 'class_medium_id'
    ) THEN
        ALTER TABLE basic_monitoring_asset 
        ADD COLUMN `class_medium_id` bigint DEFAULT NULL AFTER `class_medium`;
    END IF;
    
    IF NOT EXISTS (
        SELECT * 
        FROM information_schema.columns 
        WHERE table_schema = 'data_center_new' 
        AND table_name = 'basic_monitoring_asset' 
        AND column_name = 'class_small_id'
    ) THEN
        ALTER TABLE basic_monitoring_asset 
        ADD COLUMN `class_small_id` bigint DEFAULT NULL AFTER `class_small`;
    END IF;
    
END$$

DELIMITER ;


CALL add_column();

DROP procedure IF EXISTS `add_column`;



CREATE TABLE IF NOT EXISTS `basic_asset_class_big` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `company_id` bigint DEFAULT NULL,
  `class_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `create_time` bigint DEFAULT NULL,
  `flag` int DEFAULT '0' COMMENT '0-正常，1-删除',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;



CREATE TABLE IF NOT EXISTS `basic_asset_class_medium` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `company_id` bigint DEFAULT NULL,
  `class_big_id` bigint DEFAULT NULL,
  `class_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `create_time` bigint DEFAULT NULL,
  `flag` int DEFAULT '0' COMMENT '0-正常，1-删除',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;



CREATE TABLE IF NOT EXISTS `basic_asset_class_small` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `company_id` bigint DEFAULT NULL,
  `class_medium_id` bigint DEFAULT NULL,
  `class_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `create_time` bigint DEFAULT NULL,
  `flag` int DEFAULT '0' COMMENT '0-正常，1-删除',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;





-- 插入class_big到basic_asset_class_big
INSERT INTO basic_asset_class_big (company_id, class_name)
SELECT DISTINCT company_id, class_big
FROM basic_monitoring_asset WHERE class_big != '' AND class_big IS NOT NULL
ON DUPLICATE KEY UPDATE class_name=VALUES(class_name);  -- 如果已存在则跳过

-- 更新basic_monitoring_asset的class_big_id
UPDATE basic_monitoring_asset bma
JOIN basic_asset_class_big bacb
ON bma.company_id = bacb.company_id AND bma.class_big = bacb.class_name
SET bma.class_big_id = bacb.id;




-- 插入class_medium到basic_asset_class_medium
INSERT INTO basic_asset_class_medium (company_id, class_big_id, class_name)
SELECT DISTINCT bma.company_id, bacb.id, bma.class_medium
FROM basic_monitoring_asset bma
JOIN basic_asset_class_big bacb
ON bma.company_id = bacb.company_id AND bma.class_big = bacb.class_name
WHERE bma.class_medium != '' AND bma.class_medium IS NOT NULL
ON DUPLICATE KEY UPDATE class_name=VALUES(class_name);  -- 如果已存在则跳过

-- 更新basic_monitoring_asset的class_medium_id
UPDATE basic_monitoring_asset bma
JOIN basic_asset_class_medium bacm
ON bma.company_id = bacm.company_id AND bma.class_medium = bacm.class_name
SET bma.class_medium_id = bacm.id;

-- 更新basic_asset_class_medium的class_big_id
UPDATE basic_asset_class_medium bacm
INNER JOIN basic_monitoring_asset bma ON bma.class_medium_id = bacm.id AND bma.company_id = bacm.company_id
SET bacm.class_big_id = bma.class_big_id;






-- 插入class_small到basic_asset_class_small
INSERT INTO basic_asset_class_small (company_id, class_medium_id, class_name)
SELECT DISTINCT bma.company_id, bacm.id, bma.class_small
FROM basic_monitoring_asset bma
JOIN basic_asset_class_medium bacm
ON bma.company_id = bacm.company_id AND bma.class_medium = bacm.class_name
WHERE bma.class_small != '' AND bma.class_small IS NOT NULL
ON DUPLICATE KEY UPDATE class_name=VALUES(class_name);  -- 如果已存在则跳过

-- 更新basic_monitoring_asset的class_small_id
UPDATE basic_monitoring_asset bma
JOIN basic_asset_class_small bacs
ON bma.company_id = bacs.company_id AND bma.class_small = bacs.class_name
SET bma.class_small_id = bacs.id;

-- 更新basic_asset_class_small的class_medium_id
UPDATE basic_asset_class_small bacs
INNER JOIN basic_monitoring_asset bma ON bma.class_small_id = bacs.id AND bma.company_id = bacs.company_id
SET bacs.class_medium_id = bma.class_medium_id;


