-- 数据导入导出服务初始化脚本
-- 创建日期: 2026-04-05

-- 创建导入任务表
CREATE TABLE IF NOT EXISTS `import_task` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `task_name` VARCHAR(200) NOT NULL COMMENT '任务名称',
    `biz_type` VARCHAR(50) NOT NULL COMMENT '业务类型',
    `file_format` VARCHAR(20) NOT NULL COMMENT '文件格式',
    `original_file_name` VARCHAR(500) NOT NULL COMMENT '原始文件名',
    `file_path` VARCHAR(1000) NOT NULL COMMENT '文件存储路径',
    `file_size` BIGINT DEFAULT 0 COMMENT '文件大小（字节）',
    `total_rows` INT DEFAULT 0 COMMENT '总行数',
    `success_rows` INT DEFAULT 0 COMMENT '成功行数',
    `fail_rows` INT DEFAULT 0 COMMENT '失败行数',
    `skip_rows` INT DEFAULT 0 COMMENT '跳过行数',
    `status` VARCHAR(20) NOT NULL DEFAULT 'pending' COMMENT '任务状态',
    `tenant_id` BIGINT NOT NULL COMMENT '租户ID',
    `creator_id` BIGINT NOT NULL COMMENT '创建人ID',
    `creator_name` VARCHAR(100) DEFAULT '' COMMENT '创建人名称',
    `finish_time` DATETIME DEFAULT NULL COMMENT '处理完成时间',
    `error_summary` TEXT DEFAULT NULL COMMENT '错误信息摘要',
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `deleted_at` DATETIME DEFAULT NULL COMMENT '删除时间（逻辑删除）',
    PRIMARY KEY (`id`),
    KEY `idx_tenant_id` (`tenant_id`),
    KEY `idx_biz_type` (`biz_type`),
    KEY `idx_status` (`status`),
    KEY `idx_created_at` (`created_at`),
    KEY `idx_creator_id` (`creator_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='导入任务表';

-- 创建导入记录表
CREATE TABLE IF NOT EXISTS `import_record` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `task_id` BIGINT NOT NULL COMMENT '导入任务ID',
    `row_number` INT NOT NULL COMMENT '行号',
    `original_data` TEXT DEFAULT NULL COMMENT '原始数据（JSON格式）',
    `processed_data` TEXT DEFAULT NULL COMMENT '处理后数据（JSON格式）',
    `target_id` BIGINT DEFAULT NULL COMMENT '目标表主键值',
    `result` VARCHAR(20) NOT NULL DEFAULT 'success' COMMENT '导入结果',
    `error_message` TEXT DEFAULT NULL COMMENT '错误信息',
    `error_field` VARCHAR(100) DEFAULT NULL COMMENT '错误字段',
    `tenant_id` BIGINT NOT NULL COMMENT '租户ID',
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    PRIMARY KEY (`id`),
    KEY `idx_task_id` (`task_id`),
    KEY `idx_result` (`result`),
    KEY `idx_row_number` (`row_number`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='导入记录表';

-- 创建导入模板表
CREATE TABLE IF NOT EXISTS `import_template` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    `template_name` VARCHAR(200) NOT NULL COMMENT '模板名称',
    `template_code` VARCHAR(100) NOT NULL COMMENT '模板编码',
    `biz_type` VARCHAR(50) NOT NULL COMMENT '业务类型',
    `description` VARCHAR(500) DEFAULT NULL COMMENT '模板描述',
    `file_path` VARCHAR(1000) DEFAULT NULL COMMENT '模板文件路径',
    `column_definition` TEXT DEFAULT NULL COMMENT '列定义（JSON格式）',
    `is_default` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否为默认模板',
    `tenant_id` BIGINT NOT NULL COMMENT '租户ID',
    `creator_id` BIGINT NOT NULL COMMENT '创建人ID',
    `creator_name` VARCHAR(100) DEFAULT '' COMMENT '创建人名称',
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `deleted_at` DATETIME DEFAULT NULL COMMENT '删除时间（逻辑删除）',
    PRIMARY KEY (`id`),
    KEY `idx_tenant_id` (`tenant_id`),
    KEY `idx_biz_type` (`biz_type`),
    KEY `idx_template_code` (`template_code`),
    KEY `idx_is_default` (`is_default`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='导入模板表';

-- 插入默认模板数据
INSERT INTO `import_template` (`template_name`, `template_code`, `biz_type`, `description`, `column_definition`, `is_default`, `tenant_id`, `creator_id`, `creator_name`) VALUES
('商品导入模板', 'goods_default', 'goods', '商品数据导入模板，包含商品基本信息', '[{"fieldName":"name","headerName":"商品名称","dataType":"string","required":true,"maxLength":200},{"fieldName":"code","headerName":"商品编码","dataType":"string","required":true,"maxLength":50},{"fieldName":"barcode","headerName":"条形码","dataType":"string","required":false,"maxLength":30},{"fieldName":"category","headerName":"商品分类","dataType":"string","required":false,"maxLength":100},{"fieldName":"unit","headerName":"单位","dataType":"string","required":false,"maxLength":20},{"fieldName":"retailPrice","headerName":"零售价","dataType":"decimal","required":false},{"fieldName":"costPrice","headerName":"成本价","dataType":"decimal","required":false}]', 1, 1, 1, '系统管理员'),
('客户导入模板', 'customer_default', 'customer', '客户数据导入模板，包含客户基本信息', '[{"fieldName":"name","headerName":"客户名称","dataType":"string","required":true,"maxLength":200},{"fieldName":"contact","headerName":"联系人","dataType":"string","required":false,"maxLength":100},{"fieldName":"phone","headerName":"联系电话","dataType":"string","required":false,"maxLength":20},{"fieldName":"address","headerName":"地址","dataType":"string","required":false,"maxLength":500},{"fieldName":"email","headerName":"邮箱","dataType":"string","required":false,"maxLength":100}]', 1, 1, 1, '系统管理员'),
('供应商导入模板', 'supplier_default', 'supplier', '供应商数据导入模板', '[{"fieldName":"name","headerName":"供应商名称","dataType":"string","required":true,"maxLength":200},{"fieldName":"contact","headerName":"联系人","dataType":"string","required":false,"maxLength":100},{"fieldName":"phone","headerName":"联系电话","dataType":"string","required":false,"maxLength":20},{"fieldName":"address","headerName":"地址","dataType":"string","required":false,"maxLength":500}]', 1, 1, 1, '系统管理员');
