-- =============================================
-- 资金服务平台 - 项目管理数据库初始化脚本
-- Database: fund_proj
-- Version: 1.1
-- Created: 2026-02-17
-- Updated: 2026-02-22 (添加requirement表)
-- =============================================

CREATE DATABASE IF NOT EXISTS fund_proj DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE fund_proj;

-- =============================================
-- 1. 项目表 (project)
-- =============================================
CREATE TABLE IF NOT EXISTS project (
    id BIGINT NOT NULL AUTO_INCREMENT COMMENT '项目ID',
    tenant_id BIGINT NOT NULL DEFAULT 0 COMMENT '租户ID',
    project_code VARCHAR(64) NOT NULL COMMENT '项目编码',
    project_name VARCHAR(128) NOT NULL COMMENT '项目名称',
    customer_id BIGINT NOT NULL COMMENT '客户ID',
    project_type VARCHAR(32) NOT NULL COMMENT '项目类型',
    budget_amount DECIMAL(18,2) COMMENT '预算金额',
    start_date DATE COMMENT '开始日期',
    end_date DATE COMMENT '结束日期',
    project_manager VARCHAR(64) COMMENT '项目经理',
    status TINYINT NOT NULL DEFAULT 1 COMMENT '状态: 0-已关闭, 1-进行中, 2-已完成',
    remark VARCHAR(500) COMMENT '备注',
    created_by BIGINT COMMENT '创建人',
    created_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_by BIGINT COMMENT '更新人',
    updated_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted TINYINT NOT NULL DEFAULT 0 COMMENT '删除标记: 0-未删除, 1-已删除',
    PRIMARY KEY (id),
    UNIQUE KEY uk_tenant_code (tenant_id, project_code, deleted),
    KEY idx_tenant_id (tenant_id),
    KEY idx_customer_id (customer_id),
    KEY idx_project_name (project_name),
    KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='项目表';

-- =============================================
-- 2. 需求工单表 (requirement)
-- =============================================
CREATE TABLE IF NOT EXISTS requirement (
    id BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键，需求ID',
    tenant_id BIGINT NOT NULL COMMENT '租户ID',
    requirement_code VARCHAR(50) NOT NULL COMMENT '需求编号',
    requirement_name VARCHAR(200) NOT NULL COMMENT '需求名称',
    description TEXT COMMENT '需求描述',
    project_id BIGINT NOT NULL COMMENT '项目ID',
    customer_id BIGINT NOT NULL COMMENT '客户ID',
    priority VARCHAR(20) DEFAULT 'normal' COMMENT '优先级：high-高，normal-中，low-低',
    estimated_hours DECIMAL(8,2) DEFAULT 0.00 COMMENT '预估开发工时（小时）',
    actual_hours DECIMAL(8,2) DEFAULT 0.00 COMMENT '实际开发工时（小时）',
    planned_start DATE COMMENT '计划开始日期',
    planned_end DATE COMMENT '计划结束日期',
    actual_start DATE COMMENT '实际开始日期',
    actual_end DATE COMMENT '实际结束日期',
    delivery_date DATE COMMENT '交付日期',
    receivable_amount DECIMAL(15,2) DEFAULT 0.00 COMMENT '应收款金额',
    receivable_date DATE COMMENT '应收款日期',
    status VARCHAR(20) DEFAULT 'pending' COMMENT '状态：pending-待开发，developing-开发中，delivered-已交付，completed-已完成',
    progress INT DEFAULT 0 COMMENT '开发进度（0-100）',
    attachment_url VARCHAR(500) COMMENT '附件URL',
    created_by BIGINT COMMENT '创建人ID',
    created_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_by BIGINT COMMENT '更新人ID',
    updated_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    deleted TINYINT DEFAULT 0 COMMENT '逻辑删除：0-未删除，1-已删除',
    PRIMARY KEY (id),
    UNIQUE KEY uk_tenant_code (tenant_id, requirement_code),
    KEY idx_tenant (tenant_id),
    KEY idx_project (project_id),
    KEY idx_customer (customer_id),
    KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='需求工单表';

-- =============================================
-- 初始化测试数据(租户ID=1)
-- =============================================
INSERT INTO project (id, tenant_id, project_code, project_name, customer_id, project_type, budget_amount, start_date, status, created_by, created_time)
VALUES (1, 1, 'PROJ001', '测试项目A', 1, '开发项目', 1000000.00, '2026-01-01', 1, 1, NOW())
ON DUPLICATE KEY UPDATE project_code=project_code;

-- =============================================
-- 脚本执行完成
-- =============================================
