-- =====================================================
-- CRM经销商表初始化SQL
-- =====================================================

-- 经销商表
CREATE TABLE IF NOT EXISTS crm_dealer (
    dealer_id BIGINT NOT NULL COMMENT '经销商ID（主键）',
    tenant_id VARCHAR(20) DEFAULT '000000' COMMENT '租户ID',
    customer_code VARCHAR(100) DEFAULT NULL COMMENT 'ERP客户编码（关联）',
    dealer_name VARCHAR(200) NOT NULL COMMENT '经销商名称',
    dealer_code VARCHAR(100) NOT NULL COMMENT '经销商编码',
    contact_name VARCHAR(100) DEFAULT NULL COMMENT '联系人',
    mobile VARCHAR(50) DEFAULT NULL COMMENT '手机',
    province VARCHAR(50) DEFAULT NULL COMMENT '省',
    city VARCHAR(50) DEFAULT NULL COMMENT '市',
    level VARCHAR(50) DEFAULT 'C' COMMENT '经销商等级（字典：crm_dealer_level）',
    lifecycle VARCHAR(50) DEFAULT 'active' COMMENT '生命周期（字典：crm_lifecycle）',
    signed_at DATETIME DEFAULT NULL COMMENT '签约时间',
    store_count INT DEFAULT 0 COMMENT '门店数',
    team_size INT DEFAULT 0 COMMENT '团队规模',
    total_order_amount DECIMAL(18,2) DEFAULT 0 COMMENT '累计订单金额',
    total_payment_amount DECIMAL(18,2) DEFAULT 0 COMMENT '累计回款金额',
    activity_score DECIMAL(5,2) DEFAULT 0 COMMENT '活跃评分',
    risk_score DECIMAL(5,2) DEFAULT 0 COMMENT '风险评分',
    owner_user_id BIGINT DEFAULT NULL COMMENT '负责人（关联 sys_user）',
    source_lead_id BIGINT DEFAULT NULL COMMENT '来源线索ID',
    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 (dealer_id),
    KEY idx_tenant_id (tenant_id),
    KEY idx_customer_code (customer_code),
    KEY idx_dealer_code (dealer_code),
    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经销商表';

-- 数据字典：经销商等级
INSERT INTO sys_dict_type (dict_id, tenant_id, dict_name, dict_type,  create_dept, create_by, create_time, update_by, update_time, remark) VALUES
(18, '000000', '经销商等级', 'crm_dealer_level',  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,  create_dept, create_by, create_time, update_by, update_time, remark) VALUES
(61, '000000', 1, 'A级经销商', 'A', 'crm_dealer_level', '', 'primary', 'N',  103, 1, sysdate(), NULL, NULL, 'A级经销商'),
(62, '000000', 2, 'B级经销商', 'B', 'crm_dealer_level', '', 'success', 'N',  103, 1, sysdate(), NULL, NULL, 'B级经销商'),
(63, '000000', 3, 'C级经销商', 'C', 'crm_dealer_level', '', 'info', 'Y',  103, 1, sysdate(), NULL, NULL, 'C级经销商');

-- 数据字典：生命周期
INSERT INTO sys_dict_type (dict_id, tenant_id, dict_name, dict_type,  create_dept, create_by, create_time, update_by, update_time, remark) VALUES
(19, '000000', '经销商生命周期', 'crm_lifecycle',  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, create_dept, create_by, create_time, update_by, update_time, remark) VALUES
(64, '000000', 1, '活跃期', 'active', 'crm_lifecycle', '', 'success', 'Y',  103, 1, sysdate(), NULL, NULL, '活跃期'),
(65, '000000', 2, '稳定期', 'stable', 'crm_lifecycle', '', 'primary', 'N',  103, 1, sysdate(), NULL, NULL, '稳定期'),
(66, '000000', 3, '衰退期', 'decline', 'crm_lifecycle', '', 'warning', 'N',  103, 1, sysdate(), NULL, NULL, '衰退期'),
(67, '000000', 4, '流失期', 'churn', 'crm_lifecycle', '', 'danger', 'N',  103, 1, sysdate(), NULL, NULL, '流失期');