-- =====================================================================
-- FIGS 教育管理模块 - 升级脚本 v2 (课时预警/续费提醒)
-- 增量脚本, 在 schema_upgrade_v1.sql 之后执行
-- 功能: 课时不足/即将到期/已过期 的预警记录与处理
-- =====================================================================

SET NAMES utf8mb4;

-- -----------------------------------------------------------------
-- 1. 预警记录表 edu_alert
-- -----------------------------------------------------------------
DROP TABLE IF EXISTS `edu_alert`;
CREATE TABLE `edu_alert` (
  `id`              bigint        NOT NULL                COMMENT '主键',
  `tenant_id`       varchar(20)   DEFAULT '000000'        COMMENT '租户ID',
  `student_id`      bigint        DEFAULT NULL            COMMENT '学生ID',
  `contract_id`     bigint        NOT NULL                COMMENT '合同ID',
  `contract_no`     varchar(64)   DEFAULT NULL            COMMENT '合同号(冗余便于展示)',
  `alert_type`      varchar(32)   NOT NULL                COMMENT '预警类型 HOURS_LOW=课时不足 EXPIRING=即将到期 EXPIRED=已过期',
  `level`           varchar(16)   NOT NULL                COMMENT '级别 INFO=提醒 WARN=警告 URGENT=紧急',
  `title`           varchar(200)  NOT NULL                COMMENT '预警标题',
  `content`         varchar(1000) DEFAULT NULL            COMMENT '预警内容(如: 剩余8课时(20%)建议续费)',
  `threshold_value` decimal(10,2) DEFAULT NULL            COMMENT '触发阈值(剩余课时/剩余天数)',
  `current_value`   decimal(10,2) DEFAULT NULL            COMMENT '当前实际值',
  `housekeeper_id`  bigint        DEFAULT NULL            COMMENT '责任管家(关联staff)',
  `status`          char(1)       DEFAULT '0'             COMMENT '处理状态 0=待处理 1=已处理 2=已忽略',
  `handled_by`      bigint        DEFAULT NULL            COMMENT '处理人',
  `handled_at`      datetime      DEFAULT NULL            COMMENT '处理时间',
  `handle_remark`   varchar(500)  DEFAULT NULL            COMMENT '处理说明',
  `del_flag`        char(1)       DEFAULT '0'             COMMENT '删除标志 0=存在 1=删除',
  `create_dept`     bigint        DEFAULT NULL            COMMENT '创建部门',
  `create_by`       bigint        DEFAULT NULL            COMMENT '创建者',
  `create_time`     datetime      DEFAULT NULL            COMMENT '创建时间',
  `update_by`       bigint        DEFAULT NULL            COMMENT '更新者',
  `update_time`     datetime      DEFAULT NULL            COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_tenant_status_type` (`tenant_id`,`status`,`alert_type`) USING BTREE,
  KEY `idx_contract_type_status` (`tenant_id`,`contract_id`,`alert_type`,`status`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='课时/到期预警记录';

-- -----------------------------------------------------------------
-- 2. 字典: 预警类型 / 级别 / 处理状态
--    幂等: 先删后插 (edu_dict.id 不自增, 从122起)
-- -----------------------------------------------------------------
DELETE FROM `edu_dict` WHERE id BETWEEN 122 AND 135;
INSERT INTO `edu_dict`(`id`,`tenant_id`,`dict_type`,`dict_label`,`dict_value`,`sort_order`,`color`,`status`,`remark`,`create_by`,`create_time`) VALUES
(122,'000000','edu_alert_type','课时不足','HOURS_LOW',1,'warning','0',NULL,1,NOW()),
(123,'000000','edu_alert_type','即将到期','EXPIRING',2,'danger','0',NULL,1,NOW()),
(124,'000000','edu_alert_type','已过期','EXPIRED',3,'danger','0',NULL,1,NOW()),
(125,'000000','edu_alert_level','提醒','INFO',1,'info','0',NULL,1,NOW()),
(126,'000000','edu_alert_level','警告','WARN',2,'warning','0',NULL,1,NOW()),
(127,'000000','edu_alert_level','紧急','URGENT',3,'danger','0',NULL,1,NOW()),
(128,'000000','edu_alert_status','待处理','0',1,'warning','0',NULL,1,NOW()),
(129,'000000','edu_alert_status','已处理','1',2,'success','0',NULL,1,NOW()),
(130,'000000','edu_alert_status','已忽略','2',3,'info','0',NULL,1,NOW());

-- -----------------------------------------------------------------
-- 3. 菜单: 课时预警 (教务管理2000下, 菜单2090)
-- -----------------------------------------------------------------
DELETE FROM `sys_menu` WHERE menu_id IN (2090, 2091, 2092, 2093, 2094);
INSERT INTO `sys_menu`(`menu_id`,`menu_name`,`parent_id`,`order_num`,`path`,`component`,`query_param`,`is_frame`,`is_cache`,`menu_type`,`visible`,`status`,`perms`,`icon`,`create_dept`,`create_by`,`create_time`,`remark`) VALUES
(2090,'课时预警',2000,6,'alert','edu/alert/index',NULL,1,0,'C','0','0','edu:alert:list','message-warning',NULL,1,NOW(),'课时/到期预警中心'),
(2091,'预警查询',2090,1,'#','',NULL,1,0,'F','0','0','edu:alert:query','#',NULL,1,NOW(),''),
(2092,'预警处理',2090,2,'#','',NULL,1,0,'F','0','0','edu:alert:handle','#',NULL,1,NOW(),''),
(2093,'预警忽略',2090,3,'#','',NULL,1,0,'F','0','0','edu:alert:ignore','#',NULL,1,NOW(),''),
(2094,'预警删除',2090,4,'#','',NULL,1,0,'F','0','0','edu:alert:remove','#',NULL,1,NOW(),'');
