-- =====================================================================
-- v7 升级脚本: 合同分期缴费计划 + 签约负责人ID
-- 补充全量脚本 schema.sql 中新增、但现网库尚未执行的对象
-- 幂等: 建表用 IF NOT EXISTS, 加列先判断, 字典先删后插
-- =====================================================================

SET NAMES utf8mb4;

-- ---------------------------------------------------------------------
-- 1. 合同缴费计划表(分期付款, 对标 edu_contract_milestone)
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `edu_contract_payment_plan` (
  `id`             bigint        NOT NULL                COMMENT '主键',
  `tenant_id`      varchar(20)   DEFAULT '000000'        COMMENT '租户ID',
  `contract_id`    bigint        NOT NULL                COMMENT '合同ID',
  `installment_no` int           NOT NULL                COMMENT '期次号 1/2/3',
  `plan_amount`    decimal(14,2) NOT NULL                COMMENT '本期应收金额',
  `plan_date`      date          NOT NULL                COMMENT '本期应付日期',
  `paid_amount`    decimal(14,2) NOT NULL DEFAULT 0.00   COMMENT '本期实收金额',
  `paid_date`      date          DEFAULT NULL            COMMENT '实际收款日期',
  `payment_method` varchar(32)   DEFAULT NULL            COMMENT '本期支付方式 CASH/WECHAT/ALIPAY/BANK/CARD',
  `status`         char(1)       DEFAULT '0'             COMMENT '状态 0=待收 1=已收 4=已作废',
  `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,
  UNIQUE KEY `uk_tenant_contract_no` (`tenant_id`, `contract_id`, `installment_no`) USING BTREE,
  KEY `idx_tenant_contract` (`tenant_id`, `contract_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='合同缴费计划(分期)';

-- ---------------------------------------------------------------------
-- 2. 合同表新增"签约负责人ID"列(若不存在)
-- ---------------------------------------------------------------------
SET @col_exists = (SELECT COUNT(*) FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'edu_contract' AND COLUMN_NAME = 'sign_user_id');
SET @ddl = IF(@col_exists = 0,
    'ALTER TABLE `edu_contract` ADD COLUMN `sign_user_id` bigint DEFAULT NULL COMMENT ''签约负责人ID(edu_staff.id)'' AFTER `sign_person`',
    'SELECT ''sign_user_id 已存在, 跳过'' AS info');
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- ---------------------------------------------------------------------
-- 3. 缴费计划状态字典(幂等: 先删后插)
-- 注: id 110-120 已被既有字典占用, 这里用 297-299(当前 max id 296 之后)
-- ---------------------------------------------------------------------
DELETE FROM `edu_dict` WHERE `dict_type` = 'payment_plan_status';
INSERT INTO `edu_dict`(`id`,`tenant_id`,`dict_type`,`dict_label`,`dict_value`,`sort_order`,`is_default`,`status`) VALUES
(297,'000000','payment_plan_status','待收','0',1,'Y','0'),
(298,'000000','payment_plan_status','已收','1',2,'N','0'),
(299,'000000','payment_plan_status','已作废','4',3,'N','0');
