-- =============================================
-- 资金服务平台 - 项目管理数据库初始化脚本
-- Database: fund_proj
-- Version: 1.0
-- Created: 2026-02-17
-- =============================================

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='项目表';

-- =============================================
-- 初始化测试数据(租户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;

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