-- =====================================================
--  智能会议纪要 (Smart Meeting Minutes)
--  - 会议可绑定 sys_oa_project.project_id，也可不绑定（非项目会议）
--  - 主持人/参会/待办负责人 统一存 user_id
--  - 待办在保存时可同步生成 sys_oa_task（带操作日志和 IM 通知）
--  本脚本可重复执行（幂等）。
--  注意：sys_dict_type/sys_dict_data/sys_menu 主键为雪花ID（非自增），必须显式指定。
-- =====================================================

-- ---------------- 会议纪要表 ----------------
CREATE TABLE IF NOT EXISTS `oa_meeting_minutes` (
  `id`                  bigint(20)     NOT NULL AUTO_INCREMENT             COMMENT '主键ID',
  `meeting_code`        varchar(32)    NOT NULL                            COMMENT '会议编号 MT-yyyyMMddHHmmss+3位随机',
  `meeting_date`        date           NOT NULL                            COMMENT '会议日期',
  `project_id`          bigint(20)     DEFAULT NULL                        COMMENT '关联 sys_oa_project.project_id（可空=非项目会议）',
  `meeting_type`        varchar(20)    DEFAULT 'other'                     COMMENT '类型 字典 oa_meeting_type',
  `subject`             varchar(500)   NOT NULL                            COMMENT '会议主题',
  `location`            varchar(255)   DEFAULT NULL                        COMMENT '会议地点',
  `host_user_id`        bigint(20)     DEFAULT NULL                        COMMENT '主持人 sys_user.user_id',
  `attendee_user_ids`   varchar(1000)  DEFAULT NULL                        COMMENT '参会人员 user_id 列表（逗号分隔）',
  `topic`               text                                               COMMENT '会议议题',
  `discussion`          text                                               COMMENT '讨论内容',
  `decision`            text                                               COMMENT '决议事项',
  `tasks_json`          text                                               COMMENT '待办 JSON：[{assigneeUserId,assigneeName,content,deadline,status,taskId}]',
  `sync_task`           tinyint(1)     DEFAULT 1                           COMMENT '是否将待办同步为 OA 任务',
  `create_by`           varchar(64)    DEFAULT NULL,
  `create_time`         datetime       DEFAULT CURRENT_TIMESTAMP,
  `update_by`           varchar(64)    DEFAULT NULL,
  `update_time`         datetime       DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `del_flag`            char(1)        DEFAULT '0'                         COMMENT '删除标志：0正常 2删除（mybatis-plus logicDeleteValue=2）',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_meeting_code` (`meeting_code`, `del_flag`),
  KEY `idx_date` (`meeting_date`),
  KEY `idx_project` (`project_id`),
  KEY `idx_type` (`meeting_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='会议纪要';

-- ---------------- 清理早期错误数据（dict_id/dict_code 误插为 0） ----------------
DELETE FROM `sys_dict_type` WHERE `dict_id` = 0 AND `dict_type` IN ('oa_meeting_type', 'oa_meeting_task_status');
DELETE FROM `sys_dict_data` WHERE `dict_code` = 0 AND `dict_type` IN ('oa_meeting_type', 'oa_meeting_task_status');

-- ---------------- 字典：会议类型 ----------------
INSERT IGNORE INTO `sys_dict_type` (`dict_id`, `dict_name`, `dict_type`, `status`, `create_by`, `create_time`, `remark`)
VALUES (2063900000000000001, '会议类型', 'oa_meeting_type', '0', 'admin', NOW(), '智能会议纪要-会议类型');

INSERT IGNORE INTO `sys_dict_data`
  (`dict_code`, `dict_sort`, `dict_label`, `dict_value`, `dict_type`, `css_class`, `list_class`, `is_default`, `status`, `create_by`, `create_time`, `remark`)
VALUES
  (2063900000000000011, 1, '技术评审', 'tech',    'oa_meeting_type', '', 'info',    'N', '0', 'admin', NOW(), ''),
  (2063900000000000012, 2, '项目推进', 'project', 'oa_meeting_type', '', 'primary', 'N', '0', 'admin', NOW(), ''),
  (2063900000000000013, 3, '客户沟通', 'client',  'oa_meeting_type', '', 'warning', 'N', '0', 'admin', NOW(), ''),
  (2063900000000000014, 4, '周例会',   'weekly',  'oa_meeting_type', '', 'success', 'N', '0', 'admin', NOW(), ''),
  (2063900000000000015, 5, '其他',     'other',   'oa_meeting_type', '', 'default', 'Y', '0', 'admin', NOW(), '');

-- ---------------- 字典：待办状态 ----------------
INSERT IGNORE INTO `sys_dict_type` (`dict_id`, `dict_name`, `dict_type`, `status`, `create_by`, `create_time`, `remark`)
VALUES (2063900000000000002, '会议待办状态', 'oa_meeting_task_status', '0', 'admin', NOW(), '智能会议纪要-待办状态');

INSERT IGNORE INTO `sys_dict_data`
  (`dict_code`, `dict_sort`, `dict_label`, `dict_value`, `dict_type`, `css_class`, `list_class`, `is_default`, `status`, `create_by`, `create_time`, `remark`)
VALUES
  (2063900000000000021, 1, '待办',   'pending',  'oa_meeting_task_status', '', 'info',    'Y', '0', 'admin', NOW(), ''),
  (2063900000000000022, 2, '进行中', 'progress', 'oa_meeting_task_status', '', 'primary', 'N', '0', 'admin', NOW(), ''),
  (2063900000000000023, 3, '已完成', 'done',     'oa_meeting_task_status', '', 'success', 'N', '0', 'admin', NOW(), '');

-- ---------------- 菜单：信息 > 会议纪要 ----------------
-- 父菜单 1774989374680858626 = 「信息」
INSERT IGNORE INTO `sys_menu`
  (`menu_id`, `menu_name`, `parent_id`, `order_num`, `path`, `component`, `menu_type`, `visible`, `status`, `perms`, `icon`, `create_by`, `create_time`)
VALUES
  (2063809716454174722, '会议纪要', 1774989374680858626, 3,
   'meeting', 'oa/meeting/index', 'C', '0', '0',
   NULL, 'documentation', 'admin', NOW());

UPDATE `sys_menu` SET `icon` = 'documentation' WHERE `menu_id` = 2063809716454174722 AND (`icon` = '#' OR `icon` IS NULL);

-- ---------------- 角色授权（与「信息」下兄弟菜单一致的角色集） ----------------
INSERT IGNORE INTO `sys_role_menu` (`role_id`, `menu_id`)
VALUES
  (1743186990678077442, 2063809716454174722),  -- 总经理
  (1743204526291349506, 2063809716454174722),  -- 技术总监
  (1743205028123045890, 2063809716454174722),  -- 信息化部
  (1852970465740505090, 2063809716454174722),  -- 普通员工
  (1859257980152692738, 2063809716454174722),  -- 职工
  (1859548445766717441, 2063809716454174722),  -- 后勤
  (1893987128812761089, 2063809716454174722),  -- 新员工临时身份
  (1914212623781187585, 2063809716454174722),  -- 技术总工
  (1914213026883162113, 2063809716454174722),  -- 设计主任
  (1925062159919448065, 2063809716454174722);  -- 外贸专责

-- ---------------- 校验 ----------------
SELECT menu_id, menu_name, path, component, icon FROM sys_menu WHERE menu_id = 2063809716454174722;
SELECT dict_type, dict_label, dict_value FROM sys_dict_data
 WHERE dict_type IN ('oa_meeting_type', 'oa_meeting_task_status') ORDER BY dict_type, dict_sort;
