-- ════════════════════════════════════════════════════════════════════
-- 消息通知中心 - 数据库表结构
-- 功能：统一管理站内通知，支持审批结果、报价到期、任务分配等场景
-- 执行方式: mysql --default-character-set=utf8mb4 -u root -p < bid_notify.sql
-- ════════════════════════════════════════════════════════════════════
SET NAMES utf8mb4;

-- 1. 消息通知表（存储所有通知消息）
DROP TABLE IF EXISTS biz_notify_message;
CREATE TABLE biz_notify_message (
  message_id      BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '消息ID',
  tenant_id       BIGINT DEFAULT 0 COMMENT '租户ID',
  user_id         BIGINT NOT NULL COMMENT '接收人用户ID',
  notice_type     VARCHAR(32) NOT NULL COMMENT '通知类型(approval-审批结果/quotation_expire-报价到期/rfq_deadline-RFQ截止/task-任务分配/system-系统公告/exception-异常提醒)',
  priority        TINYINT DEFAULT 0 COMMENT '优先级(0普通 1重要 2紧急)',
  title           VARCHAR(200) NOT NULL COMMENT '消息标题',
  content         TEXT COMMENT '消息内容',
  biz_type        VARCHAR(32) COMMENT '关联业务类型(PURCHASE_ORDER/CLIENT_QUOTE/QUOTATION/DELIVERY_ORDER/ORDER_OBJECTION)',
  biz_id          BIGINT COMMENT '关联业务ID',
  biz_url         VARCHAR(255) COMMENT '业务跳转URL',
  is_read         CHAR(1) DEFAULT '0' COMMENT '是否已读(0未读 1已读)',
  read_time       DATETIME COMMENT '阅读时间',
  create_by       VARCHAR(64) DEFAULT '' COMMENT '创建者',
  create_time     DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  update_by       VARCHAR(64) DEFAULT '' COMMENT '更新者',
  update_time     DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  KEY idx_user_read (user_id, is_read),
  KEY idx_user_type (user_id, notice_type),
  KEY idx_tenant (tenant_id),
  KEY idx_create_time (create_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='消息通知表';

-- 2. 通知规则配置表（定义各类通知的触发规则）
DROP TABLE IF EXISTS biz_notify_rule;
CREATE TABLE biz_notify_rule (
  rule_id         BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '规则ID',
  tenant_id       BIGINT DEFAULT 0 COMMENT '租户ID',
  rule_name       VARCHAR(100) NOT NULL COMMENT '规则名称',
  notice_type     VARCHAR(32) NOT NULL COMMENT '通知类型',
  biz_type        VARCHAR(32) COMMENT '业务类型',
  trigger_condition VARCHAR(500) COMMENT '触发条件(JSON格式)',
  advance_days    INT DEFAULT 0 COMMENT '提前提醒天数(用于到期类提醒)',
  enabled         CHAR(1) DEFAULT '0' COMMENT '是否启用(0停用 1启用)',
  create_by       VARCHAR(64) DEFAULT '' COMMENT '创建者',
  create_time     DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  update_by       VARCHAR(64) DEFAULT '' COMMENT '更新者',
  update_time     DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  KEY idx_tenant_type (tenant_id, notice_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='通知规则配置表';

-- 3. 初始化默认通知规则
INSERT INTO biz_notify_rule (rule_name, notice_type, biz_type, trigger_condition, advance_days, enabled, create_by) VALUES
('审批结果通知',     'approval',          NULL,             '{"event":"approve,reject"}', 0, '1', 'admin'),
('报价到期提醒',     'quotation_expire',  'QUOTATION',      '{"field":"submit_time","unit":"day"}', 3, '1', 'admin'),
('报价到期紧急提醒', 'quotation_expire',  'QUOTATION',      '{"field":"submit_time","unit":"day"}', 1, '1', 'admin'),
('RFQ截止提醒',      'rfq_deadline',      NULL,             '{"field":"deadline","unit":"day"}', 2, '1', 'admin'),
('系统公告',         'system',            NULL,             '{}', 0, '1', 'admin');

-- 4. 菜单权限初始化
-- 消息通知中心菜单（挂在"系统配置"目录 menu_id=2130 path=bizconfig 下，与"待我审批"同级）
DELETE FROM sys_menu WHERE menu_id IN (2170, 2171, 2172, 2173, 2174);
INSERT INTO sys_menu (menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark) VALUES
(2170, '消息通知中心', 2130, 100, 'notify', 'bid/notify/index', 1, 0, 'C', '0', '0', 'bid:notify:list', 'message', 'admin', sysdate(), '消息通知中心'),
(2171, '通知查询', 2170, 1, '', '', 1, 0, 'F', '0', '0', 'bid:notify:query', '#', 'admin', sysdate(), ''),
(2172, '通知删除', 2170, 2, '', '', 1, 0, 'F', '0', '0', 'bid:notify:remove', '#', 'admin', sysdate(), ''),
(2173, '标记已读', 2170, 3, '', '', 1, 0, 'F', '0', '0', 'bid:notify:read', '#', 'admin', sysdate(), ''),
(2174, '通知规则配置', 2170, 4, '', '', 1, 0, 'F', '0', '0', 'bid:notify:rule', '#', 'admin', sysdate(), '');

-- 为 admin 角色授权
DELETE FROM sys_role_menu WHERE role_id = 1 AND menu_id IN (2170, 2171, 2172, 2173, 2174);
INSERT INTO sys_role_menu (role_id, menu_id) VALUES
(1, 2170), (1, 2171), (1, 2172), (1, 2173), (1, 2174);
