-- 修改科研计划与国际合作表结构，从单条记录改为多条记录

-- 1. 备份现有数据
CREATE TABLE IF NOT EXISTS `sys_science_plan_backup` AS SELECT * FROM `sys_science_plan`;

-- 2. 修改表结构，添加title和content字段
ALTER TABLE `sys_science_plan` ADD COLUMN `title` varchar(255) DEFAULT NULL COMMENT '标题' AFTER `id`;
ALTER TABLE `sys_science_plan` ADD COLUMN `content` text DEFAULT NULL COMMENT '内容（富文本）' AFTER `title`;
ALTER TABLE `sys_science_plan` ADD COLUMN `sort_order` int(11) DEFAULT 0 COMMENT '排序顺序' AFTER `content`;

-- 3. 将单条记录拆分为多条记录
-- 插入从title1和content1转换的数据
INSERT INTO `sys_science_plan` (`title`, `content`, `sort_order`, `create_time`, `update_time`, `create_by`, `update_by`, `remark`)
SELECT `title1`, `content1`, 1, NOW(), NOW(), 'admin', 'admin', '从title1转换'
FROM `sys_science_plan` WHERE `id` = 1 AND `title1` IS NOT NULL AND `title1` != '';

-- 插入从title2和content2转换的数据
INSERT INTO `sys_science_plan` (`title`, `content`, `sort_order`, `create_time`, `update_time`, `create_by`, `update_by`, `remark`)
SELECT `title2`, `content2`, 2, NOW(), NOW(), 'admin', 'admin', '从title2转换'
FROM `sys_science_plan` WHERE `id` = 1 AND `title2` IS NOT NULL AND `title2` != '';

-- 插入从title3和content3转换的数据
INSERT INTO `sys_science_plan` (`title`, `content`, `sort_order`, `create_time`, `update_time`, `create_by`, `update_by`, `remark`)
SELECT `title3`, `content3`, 3, NOW(), NOW(), 'admin', 'admin', '从title3转换'
FROM `sys_science_plan` WHERE `id` = 1 AND `title3` IS NOT NULL AND `title3` != '';

-- 4. 删除原始的单条记录（id=1）
DELETE FROM `sys_science_plan` WHERE `id` = 1;

-- 5. 删除旧的字段
ALTER TABLE `sys_science_plan` DROP COLUMN `title1`;
ALTER TABLE `sys_science_plan` DROP COLUMN `content1`;
ALTER TABLE `sys_science_plan` DROP COLUMN `title2`;
ALTER TABLE `sys_science_plan` DROP COLUMN `content2`;
ALTER TABLE `sys_science_plan` DROP COLUMN `title3`;
ALTER TABLE `sys_science_plan` DROP COLUMN `content3`;
