-- =============================================
-- 资金服务平台 - 支出管理数据库初始化脚本
-- Database: fund_exp
-- Version: 2.0
-- Created: 2026-02-22
-- Updated: 2026-03-02 (主键类型改为VARCHAR雪花ID)
-- =============================================

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

USE fund_exp;

-- =============================================
-- 1. 支出类型表 (expense_type)
-- =============================================
CREATE TABLE IF NOT EXISTS expense_type (
    id VARCHAR(32) NOT NULL COMMENT '主键ID（雪花算法）',
    tenant_id VARCHAR(32) NOT NULL COMMENT '租户ID',
    type_code VARCHAR(64) COMMENT '支出类型编码',
    type_name VARCHAR(128) NOT NULL COMMENT '支出类型名称',
    parent_id VARCHAR(32) NOT NULL DEFAULT '0' COMMENT '父类型ID, 0表示一级类型',
    type_level INT NOT NULL DEFAULT 1 COMMENT '类型层级',
    sort_order INT DEFAULT 0 COMMENT '排序号',
    description VARCHAR(500) COMMENT '类型描述',
    status TINYINT NOT NULL DEFAULT 1 COMMENT '状态: 0-禁用, 1-启用',
    remark VARCHAR(500) COMMENT '备注',
    created_by VARCHAR(32) COMMENT '创建人',
    created_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_by VARCHAR(32) 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, type_code, deleted),
    KEY idx_tenant_id (tenant_id),
    KEY idx_parent_id (parent_id),
    KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='支出类型表';

-- =============================================
-- 2. 支出表 (fund_expense)
-- =============================================
CREATE TABLE IF NOT EXISTS fund_expense (
    id VARCHAR(32) NOT NULL COMMENT '主键ID（雪花算法）',
    tenant_id VARCHAR(32) NOT NULL COMMENT '租户ID',
    expense_no VARCHAR(64) NOT NULL COMMENT '支出单号',
    title VARCHAR(200) COMMENT '支出标题',
    amount DECIMAL(18,2) NOT NULL COMMENT '支出金额',
    currency VARCHAR(16) DEFAULT 'CNY' COMMENT '币种',
    expense_type VARCHAR(32) COMMENT '支出类型ID',
    payee_name VARCHAR(128) COMMENT '收款单位',
    payee_bank VARCHAR(128) COMMENT '收款银行',
    payee_account VARCHAR(64) COMMENT '收款账号',
    expense_date DATETIME COMMENT '支出日期',
    purpose VARCHAR(500) COMMENT '用途说明',
    request_id VARCHAR(32) COMMENT '关联用款申请ID',
    project_id VARCHAR(32) COMMENT '所属项目ID',
    customer_id VARCHAR(32) COMMENT '客户ID',
    pay_status INT DEFAULT 0 COMMENT '支付状态: 0-待支付, 1-已支付, 2-支付失败',
    pay_time DATETIME COMMENT '支付时间',
    pay_channel VARCHAR(32) COMMENT '支付渠道',
    pay_voucher VARCHAR(255) COMMENT '支付凭证',
    approval_status INT DEFAULT 0 COMMENT '审批状态',
    approver_id VARCHAR(32) COMMENT '审批人ID',
    approval_time DATETIME COMMENT '审批时间',
    approval_comment VARCHAR(500) COMMENT '审批意见',
    attachments VARCHAR(1000) COMMENT '附件URL',
    remark VARCHAR(500) COMMENT '备注',
    created_by VARCHAR(32) COMMENT '创建人',
    created_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    updated_by VARCHAR(32) 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, expense_no, deleted),
    KEY idx_tenant_id (tenant_id),
    KEY idx_expense_type_id (expense_type),
    KEY idx_project_id (project_id),
    KEY idx_customer_id (customer_id),
    KEY idx_request_id (request_id),
    KEY idx_pay_status (pay_status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='支出表';

-- =============================================
-- 初始化支出类型数据(租户ID=1)
-- =============================================
-- 一级支出类型
INSERT INTO expense_type (id, tenant_id, type_code, type_name, parent_id, type_level, sort_order, status, created_by, created_time)
VALUES 
('1', '1', 'LABOR', '人力成本', '0', 1, 1, 1, '1', NOW()),
('2', '1', 'OFFICE', '办公费用', '0', 1, 2, 1, '1', NOW()),
('3', '1', 'TRAVEL', '差旅费用', '0', 1, 3, 1, '1', NOW()),
('4', '1', 'PURCHASE', '采购费用', '0', 1, 4, 1, '1', NOW()),
('5', '1', 'OTHER', '其他费用', '0', 1, 5, 1, '1', NOW())
ON DUPLICATE KEY UPDATE type_code=type_code;

-- 二级支出类型
INSERT INTO expense_type (id, tenant_id, type_code, type_name, parent_id, type_level, sort_order, status, created_by, created_time)
VALUES 
('11', '1', 'SALARY', '工资', '1', 2, 1, 1, '1', NOW()),
('12', '1', 'BONUS', '奖金', '1', 2, 2, 1, '1', NOW()),
('13', '1', 'SOCIAL_INSURANCE', '社保', '1', 2, 3, 1, '1', NOW()),
('21', '1', 'RENT', '房租', '2', 2, 1, 1, '1', NOW()),
('22', '1', 'UTILITIES', '水电费', '2', 2, 2, 1, '1', NOW()),
('23', '1', 'SUPPLIES', '办公用品', '2', 2, 3, 1, '1', NOW()),
('31', '1', 'TRANSPORT', '交通费', '3', 2, 1, 1, '1', NOW()),
('32', '1', 'ACCOMMODATION', '住宿费', '3', 2, 2, 1, '1', NOW()),
('33', '1', 'MEALS', '餐饮费', '3', 2, 3, 1, '1', NOW()),
('41', '1', 'EQUIPMENT', '设备采购', '4', 2, 1, 1, '1', NOW()),
('42', '1', 'SOFTWARE', '软件采购', '4', 2, 2, 1, '1', NOW()),
('43', '1', 'SERVICE', '服务采购', '4', 2, 3, 1, '1', NOW())
ON DUPLICATE KEY UPDATE type_code=type_code;

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