-- =============================================
-- 资金服务平台 - 客户中心数据库初始化脚本
-- Database: fund_cust
-- Version: 1.0
-- Created: 2026-02-17
-- =============================================

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

USE fund_cust;

-- =============================================
-- 1. 客户表 (customer)
-- =============================================
CREATE TABLE IF NOT EXISTS customer (
    id BIGINT NOT NULL AUTO_INCREMENT COMMENT '客户ID',
    tenant_id BIGINT NOT NULL DEFAULT 0 COMMENT '租户ID',
    customer_code VARCHAR(64) NOT NULL COMMENT '客户编码',
    customer_name VARCHAR(128) NOT NULL COMMENT '客户名称',
    contact VARCHAR(64) COMMENT '联系人',
    phone VARCHAR(20) COMMENT '联系电话',
    email VARCHAR(128) COMMENT '邮箱',
    address VARCHAR(255) COMMENT '地址',
    status TINYINT NOT NULL DEFAULT 1 COMMENT '状态: 0-禁用, 1-启用',
    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, customer_code, deleted),
    KEY idx_tenant_id (tenant_id),
    KEY idx_customer_name (customer_name),
    KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='客户表';

-- =============================================
-- 2. 联系人表 (customer_contact)
-- =============================================
CREATE TABLE IF NOT EXISTS customer_contact (
    id BIGINT NOT NULL AUTO_INCREMENT COMMENT '联系人ID',
    tenant_id BIGINT NOT NULL DEFAULT 0 COMMENT '租户ID',
    customer_id BIGINT NOT NULL COMMENT '客户ID',
    contact_name VARCHAR(64) NOT NULL COMMENT '联系人姓名',
    phone VARCHAR(20) COMMENT '手机号',
    email VARCHAR(128) COMMENT '邮箱',
    position VARCHAR(64) COMMENT '职位',
    is_primary TINYINT NOT NULL DEFAULT 0 COMMENT '是否主要联系人: 0-否, 1-是',
    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),
    KEY idx_tenant_id (tenant_id),
    KEY idx_customer_id (customer_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='客户联系人表';

-- =============================================
-- 初始化测试数据(租户ID=1)
-- =============================================
INSERT INTO customer (id, tenant_id, customer_code, customer_name, contact, phone, status, created_by, created_time)
VALUES (1, 1, 'CUST001', '测试客户A', '张三', '13800138001', 1, 1, NOW())
ON DUPLICATE KEY UPDATE customer_code=customer_code;

INSERT INTO customer_contact (tenant_id, customer_id, contact_name, phone, position, is_primary, status, created_by, created_time)
VALUES (1, 1, '张三', '13800138001', '总经理', 1, 1, 1, NOW())
ON DUPLICATE KEY UPDATE contact_name=contact_name;
