-- =============================================
-- 资金服务平台 - 收款管理数据库初始化脚本
-- Database: fund_receipt
-- Version: 1.0
-- Created: 2026-02-22
-- =============================================

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

USE fund_receipt;

-- =============================================
-- 1. 应收款表 (receivable)
-- =============================================
CREATE TABLE IF NOT EXISTS receivable (
    id BIGINT NOT NULL AUTO_INCREMENT COMMENT '应收款ID',
    tenant_id BIGINT NOT NULL DEFAULT 0 COMMENT '租户ID',
    receivable_code VARCHAR(64) NOT NULL COMMENT '应收款编号',
    requirement_id BIGINT COMMENT '需求ID',
    project_id BIGINT NOT NULL COMMENT '项目ID',
    customer_id BIGINT NOT NULL COMMENT '客户ID',
    receivable_amount DECIMAL(18,2) NOT NULL COMMENT '应收款金额',
    received_amount DECIMAL(18,2) DEFAULT 0.00 COMMENT '已收款金额',
    unpaid_amount DECIMAL(18,2) DEFAULT 0.00 COMMENT '未收款金额',
    receivable_date DATE NOT NULL COMMENT '应收款日期',
    payment_due_date DATE COMMENT '付款截止日期',
    payment_method VARCHAR(32) COMMENT '付款方式: transfer-转账, cash-现金, check-支票, other-其他',
    bank_account VARCHAR(128) COMMENT '收款账户',
    status VARCHAR(32) NOT NULL DEFAULT 'pending' COMMENT '状态: pending-待收款, partial-部分收款, received-已收款, overdue-逾期',
    overdue_days INT DEFAULT 0 COMMENT '逾期天数',
    confirm_status INT DEFAULT 0 COMMENT '确认状态: 0-待确认, 1-已确认',
    confirm_time DATETIME COMMENT '确认时间',
    confirm_by BIGINT COMMENT '确认人ID',
    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, receivable_code, deleted),
    KEY idx_tenant_id (tenant_id),
    KEY idx_requirement_id (requirement_id),
    KEY idx_project_id (project_id),
    KEY idx_customer_id (customer_id),
    KEY idx_receivable_date (receivable_date),
    KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='应收款表';

-- =============================================
-- 2. 收款记录表 (fund_receipt)
-- =============================================
CREATE TABLE IF NOT EXISTS fund_receipt (
    id BIGINT NOT NULL AUTO_INCREMENT COMMENT '收款记录ID',
    tenant_id BIGINT NOT NULL DEFAULT 0 COMMENT '租户ID',
    receipt_no VARCHAR(64) NOT NULL COMMENT '收款单号',
    title VARCHAR(200) COMMENT '收款标题',
    amount DECIMAL(18,2) NOT NULL COMMENT '收款金额',
    currency VARCHAR(16) DEFAULT 'CNY' COMMENT '币种',
    receipt_type INT COMMENT '收款类型: 1-项目收款, 2-服务费, 3-利息收入, 4-其他',
    payer_name VARCHAR(128) COMMENT '付款单位',
    payer_bank VARCHAR(128) COMMENT '付款银行',
    payer_account VARCHAR(64) COMMENT '付款账号',
    receipt_date DATETIME COMMENT '收款日期',
    purpose VARCHAR(500) COMMENT '用途说明',
    project_id BIGINT COMMENT '项目ID',
    customer_id BIGINT COMMENT '客户ID',
    receivable_id BIGINT COMMENT '应收款ID',
    receipt_status INT DEFAULT 0 COMMENT '收款状态: 0-待确认, 1-已确认, 2-已核销',
    confirm_time DATETIME COMMENT '确认时间',
    confirm_by BIGINT COMMENT '确认人ID',
    write_off_time DATETIME COMMENT '核销时间',
    write_off_by BIGINT COMMENT '核销人ID',
    voucher VARCHAR(255) COMMENT '收款凭证',
    invoice_no VARCHAR(64) COMMENT '发票号',
    attachments VARCHAR(1000) COMMENT '附件URL',
    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, receipt_no, deleted),
    KEY idx_tenant_id (tenant_id),
    KEY idx_receivable_id (receivable_id),
    KEY idx_project_id (project_id),
    KEY idx_customer_id (customer_id),
    KEY idx_receipt_date (receipt_date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='收款记录表';

-- =============================================
-- 初始化测试数据(租户ID=1)
-- =============================================
INSERT INTO receivable (id, tenant_id, receivable_code, project_id, customer_id, receivable_amount, received_amount, unpaid_amount, receivable_date, payment_due_date, status, created_by, created_time)
VALUES (1, 1, 'REC20260101001', 1, 1, 50000.00, 0.00, 50000.00, '2026-01-15', '2026-02-15', 'pending', 1, NOW())
ON DUPLICATE KEY UPDATE receivable_code=receivable_code;

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