-- 合同管理模块 - 增强版数据库表结构

-- 1. 合同表 (contract) - 添加商机关联字段
ALTER TABLE contract 
ADD COLUMN IF NOT EXISTS opportunity_id VARCHAR(64) COMMENT '关联的商机ID',
ADD COLUMN IF NOT EXISTS customer_id VARCHAR(64) COMMENT '客户ID',
ADD COLUMN IF NOT EXISTS payment_terms TEXT COMMENT '付款条款',
ADD COLUMN IF NOT EXISTS credit_period VARCHAR(32) COMMENT '账期',
ADD COLUMN IF NOT EXISTS penalty_clause TEXT COMMENT '违约金条款',
ADD COLUMN IF NOT EXISTS tax_rate DECIMAL(5,4) DEFAULT 0.1300 COMMENT '税率';

-- 2. 合同版本表 (contract_versions) - 已存在，无需修改

-- 3. 合同里程碑表 (contract_milestones)
CREATE TABLE IF NOT EXISTS contract_milestones (
    id VARCHAR(64) PRIMARY KEY,
    contract_id VARCHAR(64) NOT NULL,
    milestone_name VARCHAR(255) NOT NULL COMMENT '里程碑名称',
    milestone_type VARCHAR(32) NOT NULL COMMENT '里程碑类型: payment/delivery/acceptance',
    planned_date DATE NOT NULL COMMENT '计划日期',
    actual_date DATE COMMENT '实际完成日期',
    status VARCHAR(32) DEFAULT 'pending' COMMENT '状态: pending/completed/delayed',
    description TEXT COMMENT '描述',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_contract_id (contract_id),
    INDEX idx_status (status),
    FOREIGN KEY (contract_id) REFERENCES contract(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 4. 订单表 (orders)
CREATE TABLE IF NOT EXISTS orders (
    id VARCHAR(64) PRIMARY KEY,
    order_number VARCHAR(64) UNIQUE NOT NULL COMMENT '订单编号',
    contract_id VARCHAR(64) NOT NULL,
    customer_id VARCHAR(64) COMMENT '客户ID',
    order_type VARCHAR(32) NOT NULL COMMENT '订单类型: advance/progress/final/manual',
    delivery_batch VARCHAR(255) COMMENT '交付批次',
    acceptance_deadline DATE COMMENT '验收截止日期',
    amount DECIMAL(15,2) NOT NULL COMMENT '订单金额',
    tax_rate DECIMAL(5,4) DEFAULT 0.1300 COMMENT '税率',
    credit_period VARCHAR(32) COMMENT '账期',
    status VARCHAR(32) DEFAULT 'active' COMMENT '状态: active/completed/cancelled',
    description TEXT COMMENT '描述',
    owner_id VARCHAR(64) NOT NULL COMMENT '负责人ID',
    org_id VARCHAR(64) NOT NULL COMMENT '组织ID',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_contract_id (contract_id),
    INDEX idx_order_number (order_number),
    INDEX idx_status (status),
    INDEX idx_org_id (org_id),
    FOREIGN KEY (contract_id) REFERENCES contract(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 5. 订单付款记录表 (order_payments)
CREATE TABLE IF NOT EXISTS order_payments (
    id VARCHAR(64) PRIMARY KEY,
    order_id VARCHAR(64) NOT NULL,
    payment_amount DECIMAL(15,2) NOT NULL COMMENT '付款金额',
    payment_date DATE NOT NULL COMMENT '付款日期',
    payment_method VARCHAR(32) NOT NULL COMMENT '付款方式: bank_transfer/check/cash/online_payment',
    transaction_id VARCHAR(255) COMMENT '交易ID',
    payer_info VARCHAR(255) COMMENT '付款方信息',
    status VARCHAR(32) DEFAULT 'pending' COMMENT '状态: pending/confirmed/rejected',
    confirmed_by VARCHAR(64) COMMENT '确认人ID',
    confirmed_at DATETIME COMMENT '确认时间',
    notes TEXT COMMENT '备注',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_order_id (order_id),
    INDEX idx_status (status),
    INDEX idx_payment_date (payment_date),
    FOREIGN KEY (order_id) REFERENCES orders(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 6. AI分析结果表 (ai_analysis_results)
CREATE TABLE IF NOT EXISTS ai_analysis_results (
    id VARCHAR(64) PRIMARY KEY,
    contract_id VARCHAR(64) NOT NULL,
    analysis_type VARCHAR(32) NOT NULL COMMENT '分析类型: compliance/risk/key_dates/terms',
    analysis_result JSON NOT NULL COMMENT '分析结果JSON',
    severity VARCHAR(32) COMMENT '严重程度: low/medium/high/critical',
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_contract_id (contract_id),
    INDEX idx_analysis_type (analysis_type),
    FOREIGN KEY (contract_id) REFERENCES contract(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;