-- 创建表单表
DROP TABLE IF EXISTS `mp_activity_form`;
CREATE TABLE `mp_activity_form` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `form_name` varchar(255) NOT NULL COMMENT '表单名称',
  `form_description` text COMMENT '表单描述',
  `config` text  COMMENT '表单的配置信息',
  `fields` text  COMMENT '表单项的数组',
  `create_time` datetime  NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_time` datetime  NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `deleted` bit(1)  NULL DEFAULT b'0' COMMENT '是否删除',
  `tenant_id` varchar(20) DEFAULT 'Default' COMMENT '租户id',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='活动表单表';

-- 创建表单字段定义表
DROP TABLE IF EXISTS `mp_activity_form_field`;
CREATE TABLE `mp_activity_form_field` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `form_id` int NOT NULL COMMENT '所属ID',
  `field_name` varchar(255) NOT NULL COMMENT '字段名称',
  `field_code` varchar(100) NOT NULL COMMENT '字段代码（英文标识）',
  `field_type` varchar(50) NOT NULL COMMENT '字段类型：text, textarea, select, radio, checkbox, date, number等',
  `field_options` text COMMENT '字段选项（JSON格式，适用于select, radio, checkbox等）',
  `placeholder` varchar(500) DEFAULT NULL COMMENT '占位符提示',
  `is_required` tinyint NOT NULL DEFAULT '0' COMMENT '是否必填：0-否，1-是',
  `sort_order` int DEFAULT '0' COMMENT '排序',
  `validation_rules` text COMMENT '验证规则（JSON格式）',
  `description` text COMMENT '字段描述',
  `create_time` datetime  NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_time` datetime  NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `deleted` bit(1)  NULL DEFAULT b'0' COMMENT '是否删除',
  `tenant_id` varchar(20) DEFAULT 'Default' COMMENT '租户id',
  PRIMARY KEY (`id`),
  KEY `idx_form_id` (`form_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='活动表单字段定义表';

-- 创建表单执行实例表
DROP TABLE IF EXISTS `mp_activity_form_execution`;
CREATE TABLE `mp_activity_form_execution` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `execution_no` varchar(100) NOT NULL COMMENT '执行编号',
  `activity_id` int NOT NULL COMMENT '活动ID',
  `agent_id` int NOT NULL COMMENT '代理商ID',
  `form_id` int NOT NULL COMMENT '使用的表单ID',
  `submitter_id` int DEFAULT NULL COMMENT '提交人ID（家长ID）',
  `submitter_name` varchar(255) DEFAULT NULL COMMENT '提交人姓名',
  `submit_time` datetime DEFAULT NULL COMMENT '提交时间',
  `status` varchar(50) NOT NULL DEFAULT 'draft' COMMENT '状态：draft-草稿，submitted-已提交，approved-已批准，rejected-已拒绝',
  `create_time` datetime  NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_time` datetime  NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `deleted` bit(1)  NULL DEFAULT b'0' COMMENT '是否删除',
  `tenant_id` varchar(20) DEFAULT 'Default' COMMENT '租户id',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_execution_no` (`execution_no`),
  KEY `idx_activity_id` (`activity_id`),
  KEY `idx_agent_id` (`agent_id`),
  KEY `idx_form_id` (`form_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='活动表单执行实例表';

-- 创建表单变量表
DROP TABLE IF EXISTS `mp_activity_form_variable`;
CREATE TABLE `mp_activity_form_variable` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `execution_id` int NOT NULL COMMENT '表单执行实例ID',
  `variable_name` varchar(255) NOT NULL COMMENT '变量名称',
  `variable_code` varchar(100) NOT NULL COMMENT '变量代码',
  `variable_value` text NOT NULL COMMENT '变量值',
  `data_type` varchar(50) DEFAULT 'string' COMMENT '数据类型：string, number, boolean, json等',
  `create_time` datetime  NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime  NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `deleted` bit(1)  NULL DEFAULT b'0' COMMENT '是否删除',
  `tenant_id` varchar(20) DEFAULT 'Default' COMMENT '租户id',
  PRIMARY KEY (`id`),
  KEY `idx_execution_id` (`execution_id`),
  KEY `idx_variable_code` (`variable_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='活动表单变量表';

-- 创建活动与表单关联表
DROP TABLE IF EXISTS `mp_activity_form_relation`;
CREATE TABLE `mp_activity_form_relation` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `activity_id` int NOT NULL COMMENT '活动ID',
  `form_id` int NOT NULL COMMENT '表单ID',
  `is_primary` tinyint NOT NULL DEFAULT '0' COMMENT '是否主要：0-否，1-是',
  `version` varchar(32) NOT NULL DEFAULT '1.0.0' COMMENT '版本号',
  `create_time` datetime  NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime  NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `create_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '创建人',
  `update_by` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_german2_ci NULL DEFAULT NULL COMMENT '修改人',
  `deleted` bit(1)  NULL DEFAULT b'0' COMMENT '是否删除',
  `tenant_id` varchar(20) DEFAULT 'Default' COMMENT '租户id',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_activity_template` (`activity_id`, `form_id`),
  KEY `idx_activity_id` (`activity_id`),
  KEY `idx_form_id` (`form_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='活动与表单关联表';