-- =============================================
-- 资金服务平台 - 用款申请数据库初始化脚本
-- Database: fund_req
-- Version: 1.0
-- Created: 2026-02-17
-- =============================================

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

USE fund_req;

-- =============================================
-- 用款申请表 (fund_request)
-- =============================================
CREATE TABLE IF NOT EXISTS fund_request (
    id BIGINT NOT NULL AUTO_INCREMENT COMMENT '申请ID',
    tenant_id BIGINT NOT NULL DEFAULT 0 COMMENT '租户ID',
    request_no VARCHAR(64) NOT NULL COMMENT '申请单号',
    project_id BIGINT NOT NULL COMMENT '项目ID',
    request_amount DECIMAL(18,2) NOT NULL COMMENT '申请金额',
    request_type VARCHAR(32) NOT NULL COMMENT '申请类型',
    request_date DATE NOT NULL COMMENT '申请日期',
    purpose VARCHAR(500) COMMENT '用途说明',
    status TINYINT NOT NULL DEFAULT 0 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_no (tenant_id, request_no, deleted),
    KEY idx_tenant_id (tenant_id),
    KEY idx_project_id (project_id),
    KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用款申请表';

INSERT INTO fund_request (id, tenant_id, request_no, project_id, request_amount, request_type, request_date, purpose, status, created_by, created_time)
VALUES (1, 1, 'REQ20260101001', 1, 50000.00, '项目费用', '2026-01-15', '测试用款申请', 0, 1, NOW())
ON DUPLICATE KEY UPDATE request_no=request_no;
