-- ----------------------------
-- 邮箱配置表补充IMAP字段
-- ----------------------------
ALTER TABLE sys_mail_config 
ADD COLUMN imap_host VARCHAR(100) DEFAULT NULL COMMENT 'IMAP服务器地址' AFTER encryption,
ADD COLUMN imap_port INT DEFAULT NULL COMMENT 'IMAP端口号' AFTER imap_host,
ADD COLUMN imap_encryption VARCHAR(20) DEFAULT 'ssl' COMMENT 'IMAP加密方式(none/ssl/tls)' AFTER imap_port,
ADD COLUMN enable_receive CHAR(1) DEFAULT '0' COMMENT '启用收信(0否 1是)' AFTER imap_encryption;


-- ----------------------------
-- 收件箱表
-- ----------------------------
DROP TABLE IF EXISTS sys_mail_inbox;
CREATE TABLE sys_mail_inbox (
  inbox_id           BIGINT(20)    NOT NULL AUTO_INCREMENT COMMENT '收件ID',
  config_id          BIGINT(20)    NOT NULL COMMENT '配置ID',
  message_id         VARCHAR(200)  DEFAULT NULL COMMENT '邮件消息ID',
  from_address       VARCHAR(200)  NOT NULL COMMENT '发件人地址',
  from_name          VARCHAR(100)  DEFAULT NULL COMMENT '发件人名称',
  to_address         VARCHAR(1000) NOT NULL COMMENT '收件人地址',
  cc_address         VARCHAR(1000) DEFAULT NULL COMMENT '抄送地址',
  subject            VARCHAR(500)  NOT NULL COMMENT '邮件主题',
  content            LONGTEXT      COMMENT '邮件内容',
  content_type       VARCHAR(20)   DEFAULT 'html' COMMENT '内容类型(html/text)',
  attachments        TEXT          COMMENT '附件信息(JSON)',
  has_attachment     CHAR(1)       DEFAULT '0' COMMENT '是否有附件(0否 1是)',
  is_read            CHAR(1)       DEFAULT '0' COMMENT '是否已读(0未读 1已读)',
  is_starred         CHAR(1)       DEFAULT '0' COMMENT '是否星标(0否 1是)',
  folder             VARCHAR(50)   DEFAULT 'INBOX' COMMENT '文件夹(INBOX/SENT/TRASH等)',
  receive_time       DATETIME      NOT NULL COMMENT '接收时间',
  del_flag           CHAR(1)       DEFAULT '0' COMMENT '删除标志(0存在 2删除)',
  create_time        DATETIME      COMMENT '创建时间',
  PRIMARY KEY (inbox_id),
  KEY idx_config_id (config_id),
  KEY idx_receive_time (receive_time),
  KEY idx_is_read (is_read),
  UNIQUE KEY uk_message_id (config_id, message_id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='收件箱表';


-- ----------------------------
-- 添加菜单
-- ----------------------------
-- 获取邮件管理的ID
SET @mailParentId = (SELECT menu_id FROM sys_menu WHERE menu_name = '邮件管理' AND menu_type = 'M');

-- 发送邮件菜单
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) VALUES
('发送邮件', @mailParentId, 4, 'send', 'system/mail/send/index', 1, 0, 'C', '0', '0', 'system:mail:send', 'email', 'admin', NOW(), '发送邮件菜单');

-- 收件箱菜单
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) VALUES
('收件箱', @mailParentId, 5, 'inbox', 'system/mail/inbox/index', 1, 0, 'C', '0', '0', 'system:mail:inbox:list', 'email-service', 'admin', NOW(), '收件箱菜单');

-- 收件箱按钮权限
SET @inboxMenuId = (SELECT menu_id FROM sys_menu WHERE menu_name = '收件箱' AND parent_id = @mailParentId);
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time) VALUES
('收件查询', @inboxMenuId, 1, '', '', 1, 0, 'F', '0', '0', 'system:mail:inbox:query', '#', 'admin', NOW()),
('收件删除', @inboxMenuId, 2, '', '', 1, 0, 'F', '0', '0', 'system:mail:inbox:remove', '#', 'admin', NOW()),
('同步邮件', @inboxMenuId, 3, '', '', 1, 0, 'F', '0', '0', 'system:mail:inbox:sync', '#', 'admin', NOW());
