-- 智能项目定价模型 - 数据库初始化脚本
-- 遵循瑞小美字符标准：utf8mb4, utf8mb4_unicode_ci

-- 设置字符集
SET NAMES utf8mb4;
SET CHARACTER SET utf8mb4;

-- 使用数据库
USE pricing_model;

-- ============================================================
-- M2 核心功能 - 成本核算模块表
-- ============================================================

-- 项目成本明细表（耗材/设备）
CREATE TABLE IF NOT EXISTS project_cost_items (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    project_id BIGINT NOT NULL,
    item_type VARCHAR(20) NOT NULL COMMENT 'material-耗材, equipment-设备',
    item_id BIGINT NOT NULL,
    quantity DECIMAL(10,4) NOT NULL,
    unit_cost DECIMAL(12,4) NOT NULL,
    total_cost DECIMAL(12,2) NOT NULL COMMENT '= quantity * unit_cost',
    remark VARCHAR(200),
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_project_id (project_id),
    INDEX idx_item_type (item_type),
    FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 项目人工成本表
CREATE TABLE IF NOT EXISTS project_labor_costs (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    project_id BIGINT NOT NULL,
    staff_level_id BIGINT NOT NULL,
    duration_minutes INT NOT NULL,
    hourly_rate DECIMAL(10,2) NOT NULL COMMENT '记录时的时薪快照',
    labor_cost DECIMAL(12,2) NOT NULL COMMENT '= duration/60 * hourly_rate',
    remark VARCHAR(200),
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_project_id (project_id),
    INDEX idx_staff_level_id (staff_level_id),
    FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,
    FOREIGN KEY (staff_level_id) REFERENCES staff_levels(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 项目成本汇总表
CREATE TABLE IF NOT EXISTS project_cost_summaries (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    project_id BIGINT NOT NULL UNIQUE,
    material_cost DECIMAL(12,2) NOT NULL DEFAULT 0.00,
    equipment_cost DECIMAL(12,2) NOT NULL DEFAULT 0.00,
    labor_cost DECIMAL(12,2) NOT NULL DEFAULT 0.00,
    fixed_cost_allocation DECIMAL(12,2) NOT NULL DEFAULT 0.00,
    total_cost DECIMAL(12,2) NOT NULL DEFAULT 0.00 COMMENT '最低成本线',
    calculated_at DATETIME NOT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_project_id (project_id),
    FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ============================================================
-- M2 核心功能 - 市场行情模块表
-- ============================================================

-- 竞品机构表
CREATE TABLE IF NOT EXISTS competitors (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    competitor_name VARCHAR(100) NOT NULL,
    address VARCHAR(200),
    distance_km DECIMAL(5,2),
    positioning VARCHAR(20) NOT NULL DEFAULT 'medium' COMMENT 'high-高端, medium-中端, budget-大众',
    contact VARCHAR(50),
    is_key_competitor TINYINT(1) NOT NULL DEFAULT 0,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_positioning (positioning),
    INDEX idx_is_key (is_key_competitor)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 竞品价格表
CREATE TABLE IF NOT EXISTS competitor_prices (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    competitor_id BIGINT NOT NULL,
    project_id BIGINT COMMENT '关联本店项目',
    project_name VARCHAR(100) NOT NULL COMMENT '竞品项目名称',
    original_price DECIMAL(12,2) NOT NULL,
    promo_price DECIMAL(12,2),
    member_price DECIMAL(12,2),
    price_source VARCHAR(20) NOT NULL COMMENT 'official-官网, meituan-美团, dianping-大众点评, survey-实地调研',
    collected_at DATE NOT NULL,
    remark VARCHAR(200),
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_competitor_id (competitor_id),
    INDEX idx_project_id (project_id),
    INDEX idx_collected_at (collected_at),
    FOREIGN KEY (competitor_id) REFERENCES competitors(id) ON DELETE CASCADE,
    FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 标杆价格表
CREATE TABLE IF NOT EXISTS benchmark_prices (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    benchmark_name VARCHAR(100) NOT NULL,
    category_id BIGINT,
    min_price DECIMAL(12,2) NOT NULL,
    max_price DECIMAL(12,2) NOT NULL,
    avg_price DECIMAL(12,2) NOT NULL,
    price_tier VARCHAR(20) NOT NULL DEFAULT 'medium' COMMENT 'low-低端, medium-中端, high-高端, premium-奢华',
    effective_date DATE NOT NULL,
    remark VARCHAR(200),
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_category_id (category_id),
    INDEX idx_effective_date (effective_date),
    FOREIGN KEY (category_id) REFERENCES categories(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 市场分析结果表
CREATE TABLE IF NOT EXISTS market_analysis_results (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    project_id BIGINT NOT NULL,
    analysis_date DATE NOT NULL,
    competitor_count INT NOT NULL,
    market_min_price DECIMAL(12,2) NOT NULL,
    market_max_price DECIMAL(12,2) NOT NULL,
    market_avg_price DECIMAL(12,2) NOT NULL,
    market_median_price DECIMAL(12,2) NOT NULL,
    suggested_range_min DECIMAL(12,2) NOT NULL,
    suggested_range_max DECIMAL(12,2) NOT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_project_id (project_id),
    INDEX idx_analysis_date (analysis_date),
    FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ============================================================
-- M3 智能功能 - 智能定价与利润模拟模块表
-- ============================================================

-- 定价方案表
CREATE TABLE IF NOT EXISTS pricing_plans (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    project_id BIGINT NOT NULL,
    plan_name VARCHAR(100) NOT NULL,
    strategy_type VARCHAR(20) NOT NULL DEFAULT 'profit' COMMENT 'traffic-引流款, profit-利润款, premium-高端款',
    base_cost DECIMAL(12,2) NOT NULL COMMENT '基础成本（快照）',
    target_margin DECIMAL(5,2) NOT NULL COMMENT '目标毛利率 %',
    suggested_price DECIMAL(12,2) NOT NULL COMMENT 'AI建议价格',
    final_price DECIMAL(12,2) COMMENT '最终定价',
    ai_advice TEXT COMMENT 'AI定价建议原文',
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_by BIGINT,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_project_id (project_id),
    INDEX idx_strategy_type (strategy_type),
    INDEX idx_is_active (is_active),
    FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,
    FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 利润模拟表
CREATE TABLE IF NOT EXISTS profit_simulations (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    pricing_plan_id BIGINT NOT NULL,
    simulation_name VARCHAR(100) NOT NULL,
    price DECIMAL(12,2) NOT NULL COMMENT '模拟价格',
    estimated_volume INT NOT NULL COMMENT '预估客量',
    period_type VARCHAR(20) NOT NULL DEFAULT 'monthly' COMMENT 'daily-日, weekly-周, monthly-月, yearly-年',
    estimated_revenue DECIMAL(14,2) NOT NULL COMMENT '预估收入',
    estimated_cost DECIMAL(14,2) NOT NULL COMMENT '预估总成本',
    estimated_profit DECIMAL(14,2) NOT NULL COMMENT '预估利润',
    profit_margin DECIMAL(5,2) NOT NULL COMMENT '利润率 %',
    breakeven_volume INT NOT NULL COMMENT '盈亏平衡客量',
    created_by BIGINT,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_pricing_plan_id (pricing_plan_id),
    INDEX idx_period_type (period_type),
    FOREIGN KEY (pricing_plan_id) REFERENCES pricing_plans(id) ON DELETE CASCADE,
    FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 敏感性分析表
CREATE TABLE IF NOT EXISTS sensitivity_analyses (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    simulation_id BIGINT NOT NULL,
    price_change_rate DECIMAL(5,2) NOT NULL COMMENT '价格变动幅度 %',
    adjusted_price DECIMAL(12,2) NOT NULL COMMENT '调整后价格',
    adjusted_profit DECIMAL(14,2) NOT NULL COMMENT '调整后利润',
    profit_change_rate DECIMAL(6,2) NOT NULL COMMENT '利润变动幅度 %',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_simulation_id (simulation_id),
    FOREIGN KEY (simulation_id) REFERENCES profit_simulations(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ============================================================
-- 初始化数据
-- ============================================================

-- 初始化人员级别数据
INSERT INTO staff_levels (level_code, level_name, hourly_rate, is_active, created_at, updated_at) VALUES
('L1', '初级美容师', 30.00, 1, NOW(), NOW()),
('L2', '中级美容师', 50.00, 1, NOW(), NOW()),
('L3', '高级美容师', 80.00, 1, NOW(), NOW()),
('L4', '资深美容师', 120.00, 1, NOW(), NOW()),
('D1', '主治医师', 200.00, 1, NOW(), NOW()),
('D2', '副主任医师', 350.00, 1, NOW(), NOW()),
('D3', '主任医师', 500.00, 1, NOW(), NOW())
ON DUPLICATE KEY UPDATE updated_at = NOW();

-- 初始化项目分类数据
INSERT INTO categories (category_name, parent_id, sort_order, is_active, created_at, updated_at) VALUES
('皮肤管理', NULL, 1, 1, NOW(), NOW()),
('注射类', NULL, 2, 1, NOW(), NOW()),
('光电类', NULL, 3, 1, NOW(), NOW()),
('手术类', NULL, 4, 1, NOW(), NOW())
ON DUPLICATE KEY UPDATE updated_at = NOW();
