-- =====================================================
-- CRM商机表初始化SQL
-- =====================================================

-- 商机表
CREATE TABLE IF NOT EXISTS crm_opportunity (
    opportunity_id BIGINT NOT NULL COMMENT '商机ID（主键）',
    tenant_id VARCHAR(20) DEFAULT '000000' COMMENT '租户ID',
    dealer_id BIGINT NOT NULL COMMENT '经销商ID（关联 crm_dealer）',
    opportunity_name VARCHAR(200) NOT NULL COMMENT '商机名称',
    stage VARCHAR(50) DEFAULT 'lead' COMMENT '商机阶段（字典：crm_opportunity_stage）',
    amount DECIMAL(18,2) DEFAULT 0 COMMENT '商机金额',
    probability INT DEFAULT 0 COMMENT '成功概率（百分比）',
    expected_close_date DATE DEFAULT NULL COMMENT '预计成交日期',
    actual_close_date DATE DEFAULT NULL COMMENT '实际成交日期',
    owner_user_id BIGINT DEFAULT NULL COMMENT '负责人（关联 sys_user）',
    product_name VARCHAR(200) DEFAULT NULL COMMENT '产品名称',
    description TEXT DEFAULT NULL COMMENT '商机描述',
    source_lead_id BIGINT DEFAULT NULL COMMENT '来源线索ID',
    status VARCHAR(50) DEFAULT 'active' COMMENT '状态（字典：crm_opportunity_status）',
    create_dept BIGINT DEFAULT NULL COMMENT '创建部门',
    create_by BIGINT DEFAULT NULL COMMENT '创建人',
    create_time DATETIME DEFAULT NULL COMMENT '创建时间',
    update_by BIGINT DEFAULT NULL COMMENT '更新人',
    update_time DATETIME DEFAULT NULL COMMENT '更新时间',
    del_flag INT DEFAULT 0 COMMENT '删除标志',
    PRIMARY KEY (opportunity_id),
    KEY idx_tenant_id (tenant_id),
    KEY idx_dealer_id (dealer_id),
    KEY idx_stage (stage),
    KEY idx_owner_user_id (owner_user_id),
    KEY idx_source_lead_id (source_lead_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='CRM商机表';

-- 商机跟进记录表
CREATE TABLE IF NOT EXISTS crm_opportunity_follow (
    follow_id BIGINT NOT NULL COMMENT '跟进记录ID（主键）',
    tenant_id VARCHAR(20) DEFAULT '000000' COMMENT '租户ID',
    opportunity_id BIGINT NOT NULL COMMENT '商机ID',
    follow_type VARCHAR(50) DEFAULT NULL COMMENT '跟进方式（字典：crm_follow_type）',
    content TEXT DEFAULT NULL COMMENT '跟进内容',
    ai_summary TEXT DEFAULT NULL COMMENT 'AI摘要',
    next_follow_time DATETIME DEFAULT NULL COMMENT '下次跟进时间',
    follow_user_id BIGINT DEFAULT NULL COMMENT '跟进人（关联 sys_user）',
    create_dept BIGINT DEFAULT NULL COMMENT '创建部门',
    create_by BIGINT DEFAULT NULL COMMENT '创建人',
    create_time DATETIME DEFAULT NULL COMMENT '创建时间',
    update_by BIGINT DEFAULT NULL COMMENT '更新人',
    update_time DATETIME DEFAULT NULL COMMENT '更新时间',
    del_flag INT DEFAULT 0 COMMENT '删除标志',
    PRIMARY KEY (follow_id),
    KEY idx_tenant_id (tenant_id),
    KEY idx_opportunity_id (opportunity_id),
    KEY idx_follow_user_id (follow_user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='CRM商机跟进记录表';

-- 数据字典：商机阶段
INSERT INTO sys_dict_type (dict_id, tenant_id, dict_name, dict_type, status, create_dept, create_by, create_time, update_by, update_time, remark) VALUES
(102, '000000', '商机阶段', 'crm_opportunity_stage', '0', 103, 1, sysdate(), NULL, NULL, '商机管道阶段列表');

INSERT INTO sys_dict_data (dict_code, tenant_id, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_dept, create_by, create_time, update_by, update_time, remark) VALUES
(107, '000000', 1, '线索', 'lead', 'crm_opportunity_stage', '', 'info', 'Y', '0', 103, 1, sysdate(), NULL, NULL, '线索阶段'),
(108, '000000', 2, '谈判中', 'negotiation', 'crm_opportunity_stage', '', 'warning', 'N', '0', 103, 1, sysdate(), NULL, NULL, '谈判阶段'),
(109, '000000', 3, '方案', 'proposal', 'crm_opportunity_stage', '', 'primary', 'N', '0', 103, 1, sysdate(), NULL, NULL, '方案阶段'),
(110, '000000', 4, '赢单', 'closing', 'crm_opportunity_stage', '', 'success', 'N', '0', 103, 1, sysdate(), NULL, NULL, '赢单阶段'),
(111, '000000', 5, '输单', 'lost', 'crm_opportunity_stage', '', 'danger', 'N', '0', 103, 1, sysdate(), NULL, NULL, '输单阶段');

-- 数据字典：商机状态
INSERT INTO sys_dict_type (dict_id, tenant_id, dict_name, dict_type, status, create_dept, create_by, create_time, update_by, update_time, remark) VALUES
(103, '000000', '商机状态', 'crm_opportunity_status', '0', 103, 1, sysdate(), NULL, NULL, '商机状态列表');

INSERT INTO sys_dict_data (dict_code, tenant_id, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status, create_dept, create_by, create_time, update_by, update_time, remark) VALUES
(112, '000000', 1, '进行中', 'active', 'crm_opportunity_status', '', 'primary', 'Y', '0', 103, 1, sysdate(), NULL, NULL, '进行中'),
(113, '000000', 2, '已成交', 'won', 'crm_opportunity_status', '', 'success', 'N', '0', 103, 1, sysdate(), NULL, NULL, '已成交'),
(114, '000000', 3, '已失败', 'lost', 'crm_opportunity_status', '', 'danger', 'N', '0', 103, 1, sysdate(), NULL, NULL, '已失败'),
(115, '000000', 4, '已暂停', 'paused', 'crm_opportunity_status', '', 'info', 'N', '0', 103, 1, sysdate(), NULL, NULL, '已暂停');