-- 商机管理模块数据库表结构

-- 1. 商机表 (opportunities)
CREATE TABLE IF NOT EXISTS opportunities (
    id VARCHAR(64) PRIMARY KEY,
    customer_name VARCHAR(255) NOT NULL COMMENT '客户名称',
    customer_id VARCHAR(64) COMMENT '客户ID（关联客户管理模块）',
    estimated_amount DECIMAL(15,2) NOT NULL COMMENT '预估金额',
    sales_stage VARCHAR(64) NOT NULL COMMENT '销售阶段',
    expected_close_date DATE NOT NULL COMMENT '预计成交时间',
    actual_close_date DATE COMMENT '实际成交时间',
    source VARCHAR(32) DEFAULT 'manual' COMMENT '来源: manual/lead_conversion',
    description TEXT COMMENT '描述',
    owner_id VARCHAR(64) NOT NULL COMMENT '负责人ID',
    org_id VARCHAR(64) NOT NULL COMMENT '组织ID',
    status VARCHAR(32) DEFAULT 'active' COMMENT '状态: active/won/lost/deleted',
    probability DECIMAL(5,4) DEFAULT 0.1000 COMMENT '成交概率',
    next_action_date DATE COMMENT '下次行动日期',
    next_action_description VARCHAR(255) COMMENT '下次行动描述',
    tags VARCHAR(255) COMMENT '标签',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_customer_id (customer_id),
    INDEX idx_sales_stage (sales_stage),
    INDEX idx_owner_id (owner_id),
    INDEX idx_org_id (org_id),
    INDEX idx_status (status),
    INDEX idx_expected_close_date (expected_close_date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 2. 商机阶段变更历史表 (opportunity_stage_history)
CREATE TABLE IF NOT EXISTS opportunity_stage_history (
    id VARCHAR(64) PRIMARY KEY,
    opportunity_id VARCHAR(64) NOT NULL,
    old_stage VARCHAR(64) NOT NULL COMMENT '原阶段',
    new_stage VARCHAR(64) NOT NULL COMMENT '新阶段',
    change_reason TEXT NOT NULL COMMENT '变更原因',
    changed_by VARCHAR(64) NOT NULL COMMENT '变更人ID',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_opportunity_id (opportunity_id),
    INDEX idx_changed_by (changed_by),
    FOREIGN KEY (opportunity_id) REFERENCES opportunities(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 3. 销售漏斗配置表 (sales_funnel_config)
CREATE TABLE IF NOT EXISTS sales_funnel_config (
    id VARCHAR(64) PRIMARY KEY,
    org_id VARCHAR(64) NOT NULL,
    stage_name VARCHAR(64) NOT NULL COMMENT '阶段名称',
    stage_order INT NOT NULL COMMENT '阶段顺序',
    default_probability DECIMAL(5,4) NOT NULL COMMENT '默认成交概率',
    color_code VARCHAR(16) COMMENT '颜色代码',
    is_active TINYINT(1) DEFAULT 1 COMMENT '是否激活',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_org_stage (org_id, stage_name),
    INDEX idx_org_id (org_id),
    INDEX idx_stage_order (stage_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 插入默认销售漏斗配置
INSERT IGNORE INTO sales_funnel_config (id, org_id, stage_name, stage_order, default_probability, color_code) VALUES
(REPLACE(UUID(), '-', ''), 'default', '初步接洽', 1, 0.1000, '#FF6B6B'),
(REPLACE(UUID(), '-', ''), 'default', '需求确认', 2, 0.3000, '#4ECDC4'),
(REPLACE(UUID(), '-', ''), 'default', '方案报价', 3, 0.5000, '#45B7D1'),
(REPLACE(UUID(), '-', ''), 'default', '合同谈判', 4, 0.7000, '#96CEB4'),
(REPLACE(UUID(), '-', ''), 'default', '成交', 5, 1.0000, '#FFEAA7');