-- =====================================================================
-- FIGS 教育管理模块 - 升级脚本 v1 (升学服务/里程碑交付)
-- 增量脚本, 在 schema.sql 之后执行
-- 功能: 升学服务包(强基/综评/竞赛/志愿/留学) + 里程碑交付模型
-- =====================================================================

SET NAMES utf8mb4;

-- -----------------------------------------------------------------
-- 1. 课包表扩展: 支持升学服务包 (用存储过程保证幂等, 可重复执行)
-- -----------------------------------------------------------------
DROP PROCEDURE IF EXISTS `edu_upgrade_v1`;
DELIMITER $$
CREATE PROCEDURE `edu_upgrade_v1`()
BEGIN
  -- 放宽 total_hours 约束
  IF (SELECT COUNT(*) FROM information_schema.COLUMNS
      WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='edu_course_package' AND COLUMN_NAME='total_hours') = 1 THEN
    ALTER TABLE `edu_course_package`
      MODIFY COLUMN `total_hours` decimal(10,2) NOT NULL DEFAULT 0.00 COMMENT '总课时(小时, 服务包为0)';
  END IF;
  -- 新增 billing_type
  IF (SELECT COUNT(*) FROM information_schema.COLUMNS
      WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='edu_course_package' AND COLUMN_NAME='billing_type') = 0 THEN
    ALTER TABLE `edu_course_package`
      ADD COLUMN `billing_type`     varchar(16)  DEFAULT 'HOURS'    COMMENT '计费方式 HOURS=按课时 PROJECT=按项目 MILESTONE=按里程碑 EFFECT=按效果' AFTER `is_gift`;
  END IF;
  -- 新增 service_category
  IF (SELECT COUNT(*) FROM information_schema.COLUMNS
      WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='edu_course_package' AND COLUMN_NAME='service_category') = 0 THEN
    ALTER TABLE `edu_course_package`
      ADD COLUMN `service_category` varchar(32)  DEFAULT NULL       COMMENT '升学服务类别(仅SERVICE类型) STRONG_BASE=强基 COMPREHENSIVE=综评 COMPETITION=竞赛 VOLUNTEER=志愿 ABROAD=留学' AFTER `billing_type`;
  END IF;
END$$
DELIMITER ;
CALL `edu_upgrade_v1`();
DROP PROCEDURE IF EXISTS `edu_upgrade_v1`;

-- 1.3 更新字典: package_type 增加 SERVICE, 新增 billing_type / service_category 字典
-- (edu_dict 独立字典表, id 不自增需显式传, 当前最大id=107, 从108起; 幂等: 先删后插)
DELETE FROM `edu_dict` WHERE id BETWEEN 108 AND 121;
INSERT INTO `edu_dict`(`id`,`tenant_id`,`dict_type`,`dict_label`,`dict_value`,`sort_order`,`color`,`status`,`remark`,`create_by`,`create_time`) VALUES
(108,'000000','package_type','升学服务包','SERVICE',5,'danger','0','升学服务类(强基/综评/竞赛/志愿/留学)',1,NOW()),
(109,'000000','billing_type','按课时','HOURS',1,'primary','0',NULL,1,NOW()),
(110,'000000','billing_type','按项目','PROJECT',2,'success','0',NULL,1,NOW()),
(111,'000000','billing_type','按里程碑','MILESTONE',3,'warning','0',NULL,1,NOW()),
(112,'000000','billing_type','按效果','EFFECT',4,'info','0',NULL,1,NOW()),
(113,'000000','service_category','强基计划','STRONG_BASE',1,'danger','0',NULL,1,NOW()),
(114,'000000','service_category','综合评价','COMPREHENSIVE',2,'warning','0',NULL,1,NOW()),
(115,'000000','service_category','竞赛背书','COMPETITION',3,'primary','0',NULL,1,NOW()),
(116,'000000','service_category','志愿填报','VOLUNTEER',4,'success','0',NULL,1,NOW()),
(117,'000000','service_category','留学规划','ABROAD',5,'info','0',NULL,1,NOW());

-- -----------------------------------------------------------------
-- 2. 里程碑模板表 (服务包的交付节点定义)
-- -----------------------------------------------------------------
DROP TABLE IF EXISTS `edu_service_milestone`;
CREATE TABLE `edu_service_milestone` (
  `id`              bigint        NOT NULL                COMMENT '主键',
  `tenant_id`       varchar(20)   DEFAULT '000000'        COMMENT '租户ID',
  `package_id`      bigint        NOT NULL                COMMENT '关联课包ID(服务包)',
  `milestone_name`  varchar(128)  NOT NULL                COMMENT '里程碑名称(测评定位/笔试培训/面试集训/文书交付等)',
  `milestone_code`  varchar(32)   DEFAULT NULL            COMMENT '里程碑编码',
  `order_num`       int           DEFAULT 0               COMMENT '顺序号(从小到大依次交付)',
  `planned_amount`  decimal(12,2) DEFAULT 0.00            COMMENT '该节点应收金额',
  `planned_hours`   decimal(10,2) DEFAULT 0.00            COMMENT '该节点计划课时(可空,部分服务含课时)',
  `deliverable_desc` varchar(1000) DEFAULT NULL           COMMENT '交付物说明(如:出测评报告/完成文书初稿)',
  `status`          char(1)       DEFAULT '0'             COMMENT '状态 0=启用 1=停用',
  `del_flag`        char(1)       DEFAULT '0'             COMMENT '删除标志 0=存在 1=删除',
  `remark`          varchar(500)  DEFAULT NULL            COMMENT '备注',
  `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_package` (`tenant_id`,`package_id`,`order_num`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='服务包里程碑模板(交付节点定义)';

-- -----------------------------------------------------------------
-- 3. 合同里程碑实例表 (挂在合同上的实际交付节点)
--    交付即"消课": 状态推进触发金额确认
-- -----------------------------------------------------------------
DROP TABLE IF EXISTS `edu_contract_milestone`;
CREATE TABLE `edu_contract_milestone` (
  `id`              bigint        NOT NULL                COMMENT '主键',
  `tenant_id`       varchar(20)   DEFAULT '000000'        COMMENT '租户ID',
  `contract_id`     bigint        NOT NULL                COMMENT '合同ID',
  `package_id`      bigint        DEFAULT NULL            COMMENT '服务包ID',
  `milestone_name`  varchar(128)  NOT NULL                COMMENT '里程碑名称',
  `milestone_code`  varchar(32)   DEFAULT NULL            COMMENT '里程碑编码',
  `order_num`       int           DEFAULT 0               COMMENT '顺序号',
  `planned_amount`  decimal(12,2) DEFAULT 0.00            COMMENT '计划应收金额',
  `paid_amount`     decimal(12,2) DEFAULT 0.00            COMMENT '已收金额',
  `actual_amount`   decimal(12,2) DEFAULT 0.00            COMMENT '实际确认金额',
  `planned_hours`   decimal(10,2) DEFAULT 0.00            COMMENT '计划课时',
  `status`          char(1)       DEFAULT '0'             COMMENT '状态 0=待交付 1=进行中 2=已交付 3=已跳过',
  `deliverable_url` varchar(500)  DEFAULT NULL            COMMENT '交付物附件URL(文书/材料)',
  `delivered_by`    bigint        DEFAULT NULL            COMMENT '交付人(员工ID, 通常是文书老师)',
  `delivered_at`    datetime      DEFAULT NULL            COMMENT '交付时间',
  `del_flag`        char(1)       DEFAULT '0'             COMMENT '删除标志 0=存在 1=删除',
  `remark`          varchar(500)  DEFAULT NULL            COMMENT '备注',
  `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_contract` (`tenant_id`,`contract_id`,`order_num`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='合同里程碑实例(升学服务交付节点)';

-- -----------------------------------------------------------------
-- 4. 菜单: 升学服务 + 里程碑(复用课包/合同页面, 仅加按钮权限)
--    课包菜单=2002, 合同菜单=2003, 幂等: 先删后插
-- -----------------------------------------------------------------
DELETE FROM `sys_menu` WHERE menu_id IN (2070, 2080, 2081);
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
-- 课包下: 服务包里程碑模板维护按钮
(2070,'里程碑模板',2002,6,'#','',NULL,1,0,'F','0','0','edu:package:milestone','#',NULL,1,NOW(),'服务包里程碑模板'),
-- 合同下: 里程碑交付相关按钮
(2080,'里程碑交付',2003,9,'#','',NULL,1,0,'F','0','0','edu:contract:milestone','#',NULL,1,NOW(),'查看合同里程碑'),
(2081,'推进里程碑',2003,10,'#','',NULL,1,0,'F','0','0','edu:contract:deliver','#',NULL,1,NOW(),'推进里程碑交付');

-- 5. ContractAction 补充(代码常量已加, 此处字典对齐)
INSERT INTO `edu_dict`(`id`,`tenant_id`,`dict_type`,`dict_label`,`dict_value`,`sort_order`,`color`,`status`,`remark`,`create_by`,`create_time`) VALUES
(118,'000000','contract_type','补购','ADDON',6,'info','0',NULL,1,NOW()),
(119,'000000','contract_action','续费','RENEW',6,'primary','0',NULL,1,NOW()),
(120,'000000','contract_action','补购','ADDON',7,'info','0',NULL,1,NOW()),
(121,'000000','contract_action','里程碑交付','MILESTONE_DELIVER',8,'success','0',NULL,1,NOW());
