-- ============================================
-- 资金服务平台数据库结构
-- 数据库: fund_platform
-- 字符集: utf8mb4
-- ============================================

USE fund_platform;

-- ============================================
-- 1. 系统管理模块表结构
-- ============================================

-- 租户表
CREATE TABLE IF NOT EXISTS sys_tenant (
    tenant_id       BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '租户ID',
    tenant_code     VARCHAR(50) NOT NULL UNIQUE COMMENT '租户编码',
    tenant_name     VARCHAR(100) NOT NULL COMMENT '租户名称',
    tenant_type     TINYINT DEFAULT 1 COMMENT '租户类型：1-一库多租户，2-一库一租户',
    contact_name    VARCHAR(50) COMMENT '联系人姓名',
    contact_phone   VARCHAR(20) COMMENT '联系人电话',
    contact_email   VARCHAR(100) COMMENT '联系人邮箱',
    db_config       JSON COMMENT '数据库配置（一库一租户模式使用）',
    status          TINYINT DEFAULT 1 COMMENT '状态：0-禁用，1-启用',
    expire_time     DATETIME COMMENT '过期时间',
    created_time    DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_time    DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    INDEX idx_code (tenant_code),
    INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='租户表';

-- 部门表
CREATE TABLE IF NOT EXISTS sys_dept (
    dept_id         BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '部门ID',
    tenant_id       BIGINT NOT NULL COMMENT '租户ID',
    dept_name       VARCHAR(100) NOT NULL COMMENT '部门名称',
    parent_id       BIGINT DEFAULT 0 COMMENT '父部门ID（0表示顶级部门）',
    dept_code       VARCHAR(50) COMMENT '部门编码',
    dept_level      INT DEFAULT 1 COMMENT '部门层级',
    sort_order      INT DEFAULT 0 COMMENT '排序顺序',
    leader          VARCHAR(50) COMMENT '部门负责人',
    leader_phone    VARCHAR(20) COMMENT '负责人电话',
    status          TINYINT DEFAULT 1 COMMENT '状态：0-禁用，1-启用',
    remark          VARCHAR(500) COMMENT '备注说明',
    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-已删除',
    INDEX idx_tenant (tenant_id),
    INDEX idx_parent (parent_id),
    INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='部门表';

-- 用户表
CREATE TABLE IF NOT EXISTS sys_user (
    user_id         BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '用户ID',
    tenant_id       BIGINT NOT NULL COMMENT '租户ID',
    username        VARCHAR(50) NOT NULL COMMENT '用户名（登录账号）',
    password        VARCHAR(100) NOT NULL COMMENT '密码（加密存储）',
    real_name       VARCHAR(50) NOT NULL COMMENT '真实姓名',
    gender          TINYINT DEFAULT 0 COMMENT '性别：0-未知，1-男，2-女',
    phone           VARCHAR(20) COMMENT '手机号码',
    email           VARCHAR(100) COMMENT '邮箱地址',
    dept_id         BIGINT COMMENT '所属部门ID',
    position        VARCHAR(50) COMMENT '岗位/职位',
    avatar          VARCHAR(255) COMMENT '头像URL',
    status          TINYINT DEFAULT 1 COMMENT '状态：0-禁用，1-启用',
    last_login_time DATETIME COMMENT '最后登录时间',
    last_login_ip   VARCHAR(50) COMMENT '最后登录IP',
    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-已删除',
    UNIQUE KEY uk_tenant_username (tenant_id, username),
    INDEX idx_tenant (tenant_id),
    INDEX idx_dept (dept_id),
    INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表';

-- 角色表
CREATE TABLE IF NOT EXISTS sys_role (
    role_id         BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '角色ID',
    tenant_id       BIGINT NOT NULL COMMENT '租户ID',
    role_code       VARCHAR(50) NOT NULL COMMENT '角色编码',
    role_name       VARCHAR(100) NOT NULL COMMENT '角色名称',
    role_type       VARCHAR(20) DEFAULT 'custom' COMMENT '角色类型：system-系统角色，custom-自定义角色',
    data_scope      VARCHAR(20) DEFAULT 'self' COMMENT '数据范围：all-全部，dept-本部门，self-本人',
    sort_order      INT DEFAULT 0 COMMENT '排序顺序',
    status          TINYINT DEFAULT 1 COMMENT '状态：0-禁用，1-启用',
    remark          VARCHAR(500) COMMENT '备注说明',
    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-已删除',
    UNIQUE KEY uk_tenant_code (tenant_id, role_code),
    INDEX idx_tenant (tenant_id),
    INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='角色表';

-- 菜单表
CREATE TABLE IF NOT EXISTS sys_menu (
    menu_id         BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '菜单ID',
    tenant_id       BIGINT DEFAULT 0 COMMENT '租户ID（0表示系统通用菜单）',
    menu_name       VARCHAR(100) NOT NULL COMMENT '菜单名称',
    menu_type       VARCHAR(20) NOT NULL COMMENT '菜单类型：dir-目录，menu-菜单，button-按钮',
    parent_id       BIGINT DEFAULT 0 COMMENT '父菜单ID（0表示顶级菜单）',
    menu_level      INT DEFAULT 1 COMMENT '菜单层级',
    icon            VARCHAR(100) COMMENT '菜单图标',
    path            VARCHAR(200) COMMENT '路由路径',
    component       VARCHAR(200) COMMENT '组件路径',
    permission      VARCHAR(100) COMMENT '权限标识（如：user:list）',
    sort_order      INT DEFAULT 0 COMMENT '排序顺序',
    status          TINYINT DEFAULT 1 COMMENT '状态：0-禁用，1-启用',
    remark          VARCHAR(500) COMMENT '备注说明',
    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-已删除',
    INDEX idx_tenant (tenant_id),
    INDEX idx_parent (parent_id),
    INDEX idx_type (menu_type),
    INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='菜单表';

-- 用户角色关联表
CREATE TABLE IF NOT EXISTS sys_user_role (
    id              BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT 'ID',
    tenant_id       BIGINT NOT NULL COMMENT '租户ID',
    user_id         BIGINT NOT NULL COMMENT '用户ID',
    role_id         BIGINT NOT NULL COMMENT '角色ID',
    created_time    DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    UNIQUE KEY uk_user_role (tenant_id, user_id, role_id),
    INDEX idx_user (user_id),
    INDEX idx_role (role_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户角色关联表';

-- 角色菜单关联表
CREATE TABLE IF NOT EXISTS sys_role_menu (
    id              BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT 'ID',
    tenant_id       BIGINT NOT NULL COMMENT '租户ID',
    role_id         BIGINT NOT NULL COMMENT '角色ID',
    menu_id         BIGINT NOT NULL COMMENT '菜单ID',
    created_time    DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    UNIQUE KEY uk_role_menu (tenant_id, role_id, menu_id),
    INDEX idx_role (role_id),
    INDEX idx_menu (menu_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='角色菜单关联表';

-- 操作日志表
CREATE TABLE IF NOT EXISTS sys_operation_log (
    log_id          BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '日志ID',
    tenant_id       BIGINT NOT NULL COMMENT '租户ID',
    user_id         BIGINT COMMENT '操作人ID',
    username        VARCHAR(50) COMMENT '操作人用户名',
    operation       VARCHAR(100) COMMENT '操作描述',
    method          VARCHAR(200) COMMENT '操作方法',
    params          TEXT COMMENT '操作参数（JSON格式）',
    ip              VARCHAR(50) COMMENT '操作IP',
    user_agent      VARCHAR(500) COMMENT '用户代理',
    operation_time  DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '操作时间',
    cost_time       BIGINT DEFAULT 0 COMMENT '耗时（毫秒）',
    result          VARCHAR(20) DEFAULT 'success' COMMENT '操作结果：success-成功，fail-失败',
    error_msg       TEXT COMMENT '错误信息',
    INDEX idx_tenant (tenant_id),
    INDEX idx_user (user_id),
    INDEX idx_time (operation_time),
    INDEX idx_result (result)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='操作日志表';

-- ============================================
-- 2. 客户管理模块表结构
-- ============================================

-- 客户表
CREATE TABLE IF NOT EXISTS customer (
    customer_id         BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '客户ID',
    tenant_id           BIGINT NOT NULL COMMENT '租户ID',
    customer_code       VARCHAR(50) NOT NULL COMMENT '客户编码（唯一）',
    customer_name       VARCHAR(200) NOT NULL COMMENT '客户名称',
    customer_short      VARCHAR(100) COMMENT '客户简称',
    customer_type       VARCHAR(20) DEFAULT 'enterprise' COMMENT '客户类型：enterprise-企业，individual-个人',
    industry            VARCHAR(50) COMMENT '所属行业',
    scale               VARCHAR(20) COMMENT '企业规模：small-小型，medium-中型，large-大型',
    level               VARCHAR(20) DEFAULT 'normal' COMMENT '客户等级：A/B/C/D/normal',
    tax_no              VARCHAR(50) COMMENT '纳税人识别号',
    legal_person        VARCHAR(50) COMMENT '法定代表人',
    address             VARCHAR(500) COMMENT '公司地址',
    phone               VARCHAR(20) COMMENT '联系电话',
    email               VARCHAR(100) COMMENT '邮箱地址',
    website             VARCHAR(200) COMMENT '公司网站',
    status              TINYINT DEFAULT 1 COMMENT '状态：0-禁用，1-启用',
    remark              VARCHAR(500) COMMENT '备注说明',
    cooperation_start   DATE COMMENT '合作开始日期',
    cooperation_end     DATE COMMENT '合作结束日期',
    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-已删除',
    UNIQUE KEY uk_tenant_code (tenant_id, customer_code),
    INDEX idx_tenant (tenant_id),
    INDEX idx_status (status),
    INDEX idx_level (level)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='客户表';

-- 客户联系人表
CREATE TABLE IF NOT EXISTS customer_contact (
    contact_id      BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '联系人ID',
    tenant_id       BIGINT NOT NULL COMMENT '租户ID',
    customer_id     BIGINT NOT NULL COMMENT '客户ID',
    contact_name    VARCHAR(50) NOT NULL COMMENT '联系人姓名',
    position        VARCHAR(50) COMMENT '职位/职务',
    department      VARCHAR(100) COMMENT '所属部门',
    phone           VARCHAR(20) COMMENT '联系电话',
    mobile          VARCHAR(20) COMMENT '手机号码',
    email           VARCHAR(100) COMMENT '邮箱地址',
    wechat          VARCHAR(50) COMMENT '微信号',
    qq              VARCHAR(20) COMMENT 'QQ号',
    is_primary      TINYINT DEFAULT 0 COMMENT '是否主要联系人：0-否，1-是',
    sort_order      INT DEFAULT 0 COMMENT '排序顺序',
    status          TINYINT DEFAULT 1 COMMENT '状态：0-禁用，1-启用',
    remark          VARCHAR(200) COMMENT '备注说明',
    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-已删除',
    INDEX idx_tenant (tenant_id),
    INDEX idx_customer (customer_id),
    INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='客户联系人表';

-- ============================================
-- 3. 项目管理模块表结构
-- ============================================

-- 项目表
CREATE TABLE IF NOT EXISTS project (
    project_id          BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '项目ID',
    tenant_id           BIGINT NOT NULL COMMENT '租户ID',
    project_code        VARCHAR(50) NOT NULL COMMENT '项目编号（唯一）',
    project_name        VARCHAR(200) NOT NULL COMMENT '项目名称',
    project_short       VARCHAR(100) COMMENT '项目简称',
    customer_id         BIGINT NOT NULL COMMENT '客户ID',
    project_type        VARCHAR(20) DEFAULT 'development' COMMENT '项目类型：development-开发，maintenance-维护，consulting-咨询',
    project_manager_id  BIGINT COMMENT '项目经理ID',
    start_date          DATE COMMENT '项目开始日期',
    end_date            DATE COMMENT '项目结束日期',
    budget_amount       DECIMAL(15,2) DEFAULT 0.00 COMMENT '项目预算金额',
    contract_amount     DECIMAL(15,2) DEFAULT 0.00 COMMENT '合同金额',
    status              VARCHAR(20) DEFAULT 'preparing' COMMENT '项目状态：preparing-筹备中，ongoing-进行中，completed-已完成，archived-已归档，cancelled-已取消',
    progress            INT DEFAULT 0 COMMENT '项目进度（0-100）',
    description         TEXT COMMENT '项目描述',
    remark              VARCHAR(500) COMMENT '备注说明',
    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-已删除',
    UNIQUE KEY uk_tenant_code (tenant_id, project_code),
    INDEX idx_tenant (tenant_id),
    INDEX idx_customer (customer_id),
    INDEX idx_manager (project_manager_id),
    INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='项目表';

-- 项目成员表
CREATE TABLE IF NOT EXISTS project_member (
    member_id       BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '成员关系ID',
    tenant_id       BIGINT NOT NULL COMMENT '租户ID',
    project_id      BIGINT NOT NULL COMMENT '项目ID',
    user_id         BIGINT NOT NULL COMMENT '用户ID',
    role            VARCHAR(50) DEFAULT 'member' COMMENT '项目角色：pm-项目经理，dev-开发，test-测试，finance-财务，member-普通成员',
    join_date       DATE COMMENT '加入日期',
    leave_date      DATE COMMENT '离开日期',
    workload        DECIMAL(5,2) DEFAULT 0.00 COMMENT '工作量占比（0-100）',
    status          TINYINT DEFAULT 1 COMMENT '状态：0-已离开，1-在职',
    remark          VARCHAR(200) COMMENT '备注说明',
    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-已删除',
    UNIQUE KEY uk_project_user (tenant_id, project_id, user_id),
    INDEX idx_project (project_id),
    INDEX idx_user (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='项目成员表';

-- ============================================
-- 4. 需求工单模块表结构
-- ============================================

-- 需求工单表
CREATE TABLE IF NOT EXISTS requirement (
    requirement_id      BIGINT PRIMARY KEY 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-已删除',
    UNIQUE KEY uk_tenant_code (tenant_id, requirement_code),
    INDEX idx_tenant (tenant_id),
    INDEX idx_project (project_id),
    INDEX idx_customer (customer_id),
    INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='需求工单表';

-- ============================================
-- 5. 支出管理模块表结构
-- ============================================

-- 支出类型表
CREATE TABLE IF NOT EXISTS expense_type (
    type_id         BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '支出类型ID',
    tenant_id       BIGINT NOT NULL COMMENT '租户ID',
    type_code       VARCHAR(50) COMMENT '支出类型编码',
    type_name       VARCHAR(100) NOT NULL COMMENT '支出类型名称',
    parent_id       BIGINT DEFAULT 0 COMMENT '父类型ID（0表示一级类型）',
    type_level      INT DEFAULT 1 COMMENT '类型层级',
    sort_order      INT DEFAULT 0 COMMENT '排序顺序',
    description     VARCHAR(200) COMMENT '类型描述',
    status          TINYINT DEFAULT 1 COMMENT '状态：0-禁用，1-启用',
    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-已删除',
    INDEX idx_tenant (tenant_id),
    INDEX idx_parent (parent_id),
    INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='支出类型表';

-- 支出表
CREATE TABLE IF NOT EXISTS expense (
    expense_id      BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '支出ID',
    tenant_id       BIGINT NOT NULL COMMENT '租户ID',
    expense_code    VARCHAR(50) NOT NULL COMMENT '支出编号（唯一）',
    expense_type_id BIGINT NOT NULL COMMENT '支出类型ID',
    expense_amount  DECIMAL(15,2) DEFAULT 0.00 COMMENT '支出金额',
    expense_date    DATE COMMENT '支出日期',
    expense_reason  TEXT COMMENT '支出事由',
    project_id      BIGINT COMMENT '所属项目ID',
    applicant_id    BIGINT NOT NULL COMMENT '申请人ID',
    department_id   BIGINT COMMENT '申请部门ID',
    payment_method  VARCHAR(20) DEFAULT 'transfer' COMMENT '付款方式：transfer-转账，cash-现金，check-支票，other-其他',
    payment_account VARCHAR(100) COMMENT '付款账户',
    attachment_url  VARCHAR(500) COMMENT '附件URL',
    status          VARCHAR(20) DEFAULT 'pending' COMMENT '状态：pending-待付款，paid-已付款，completed-已完成，cancelled-已作废',
    payment_date    DATE COMMENT '付款日期',
    payment_voucher VARCHAR(255) COMMENT '付款凭证',
    remark          VARCHAR(500) COMMENT '备注说明',
    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-已删除',
    UNIQUE KEY uk_tenant_code (tenant_id, expense_code),
    INDEX idx_tenant (tenant_id),
    INDEX idx_type (expense_type_id),
    INDEX idx_project (project_id),
    INDEX idx_applicant (applicant_id),
    INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='支出表';

-- ============================================
-- 6. 应收款管理模块表结构
-- ============================================

-- 应收款表
CREATE TABLE IF NOT EXISTS receivable (
    receivable_id       BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '应收款ID',
    tenant_id           BIGINT NOT NULL COMMENT '租户ID',
    receivable_code     VARCHAR(50) NOT NULL COMMENT '应收款编号（唯一）',
    requirement_id      BIGINT NOT NULL COMMENT '需求ID',
    project_id          BIGINT NOT NULL COMMENT '项目ID',
    customer_id         BIGINT NOT NULL COMMENT '客户ID',
    receivable_amount   DECIMAL(15,2) DEFAULT 0.00 COMMENT '应收款金额',
    receivable_date     DATE COMMENT '应收款日期',
    payment_due_date    DATE COMMENT '付款截止日期',
    payment_method      VARCHAR(20) COMMENT '付款方式：transfer-转账，cash-现金，check-支票，other-其他',
    bank_account        VARCHAR(100) COMMENT '收款账户',
    status              VARCHAR(20) DEFAULT 'pending' COMMENT '状态：pending-待收款，partial-部分收款，received-已收款，overdue-逾期',
    received_amount     DECIMAL(15,2) DEFAULT 0.00 COMMENT '已收款金额',
    unpaid_amount       DECIMAL(15,2) DEFAULT 0.00 COMMENT '未收款金额',
    overdue_days        INT DEFAULT 0 COMMENT '逾期天数',
    remark              VARCHAR(500) COMMENT '备注说明',
    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-已删除',
    UNIQUE KEY uk_tenant_code (tenant_id, receivable_code),
    INDEX idx_tenant (tenant_id),
    INDEX idx_requirement (requirement_id),
    INDEX idx_project (project_id),
    INDEX idx_customer (customer_id),
    INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='应收款表';

-- 收款记录表
CREATE TABLE IF NOT EXISTS receipt (
    receipt_id      BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '收款记录ID',
    tenant_id       BIGINT NOT NULL COMMENT '租户ID',
    receipt_code    VARCHAR(50) NOT NULL COMMENT '收款编号（唯一）',
    receivable_id   BIGINT NOT NULL COMMENT '应收款ID',
    receipt_amount  DECIMAL(15,2) DEFAULT 0.00 COMMENT '收款金额',
    receipt_date    DATE COMMENT '收款日期',
    receipt_method  VARCHAR(20) DEFAULT 'transfer' COMMENT '收款方式：transfer-转账，cash-现金，check-支票，other-其他',
    receipt_account VARCHAR(100) COMMENT '收款账户',
    payer_name      VARCHAR(100) COMMENT '付款方名称',
    receipt_voucher VARCHAR(255) COMMENT '收款凭证URL',
    operator_id     BIGINT COMMENT '操作人ID',
    remark          VARCHAR(200) COMMENT '备注说明',
    created_time    DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    UNIQUE KEY uk_tenant_code (tenant_id, receipt_code),
    INDEX idx_tenant (tenant_id),
    INDEX idx_receivable (receivable_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='收款记录表';
