-- ----------------------------
-- 用户工作台个性化布局表
-- ----------------------------
DROP TABLE IF EXISTS `sys_user_dashboard`;
CREATE TABLE `sys_user_dashboard` (
  `user_id`     bigint(20)  NOT NULL                COMMENT '用户ID',
  `layout_json` longtext    NOT NULL                COMMENT '布局 JSON: [{i,x,y,w,h,widgetKey,config}]',
  `create_time` datetime    DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime    DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户工作台布局';

-- ----------------------------
-- 默认工作台布局（存到 sys_config，前端在用户没有自定义布局时回退到此）
-- ----------------------------
INSERT INTO `sys_config` (`config_id`, `config_name`, `config_key`, `config_value`, `config_type`, `create_by`, `create_time`, `remark`)
SELECT (SELECT IFNULL(MAX(config_id), 0) + 1 FROM `sys_config` AS t),
       '默认工作台布局', 'sys.dashboard.defaultLayout',
       '[{"i":"announcements","x":0,"y":0,"w":6,"h":8,"widgetKey":"announcements"},{"i":"projectManagement","x":6,"y":0,"w":6,"h":8,"widgetKey":"projectManagement"},{"i":"ownerTaskList","x":0,"y":8,"w":6,"h":8,"widgetKey":"ownerTaskList"},{"i":"financialCharts","x":6,"y":8,"w":6,"h":8,"widgetKey":"financialCharts"},{"i":"miniCalendar","x":0,"y":16,"w":4,"h":8,"widgetKey":"miniCalendar"},{"i":"quickEntry","x":4,"y":16,"w":8,"h":4,"widgetKey":"quickEntry"}]',
       'Y', 'admin', NOW(), '工作台默认布局，用户首次进入或重置时使用'
FROM dual
WHERE NOT EXISTS (SELECT 1 FROM `sys_config` WHERE `config_key` = 'sys.dashboard.defaultLayout');
