-- ========================================
-- 企业微信表格数据处理系统 - 数据库表结构
-- ========================================

-- 1. 客户数据表
DROP TABLE IF EXISTS `customer_data`;
CREATE TABLE `customer_data` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `customer_name` varchar(100) DEFAULT NULL COMMENT '客户名称',
  `description` varchar(500) DEFAULT NULL COMMENT '描述',
  `add_person` varchar(50) DEFAULT NULL COMMENT '添加人',
  `add_person_account` varchar(50) DEFAULT NULL COMMENT '添加人账号',
  `add_person_dept` varchar(100) DEFAULT NULL COMMENT '添加人所属部门',
  `add_time` datetime DEFAULT NULL COMMENT '添加时间',
  `add_date` date DEFAULT NULL COMMENT '添加日期',
  `source` varchar(50) DEFAULT NULL COMMENT '来源',
  `mobile` varchar(20) DEFAULT NULL COMMENT '手机',
  `enterprise` varchar(100) DEFAULT NULL COMMENT '企业',
  `email` varchar(100) DEFAULT NULL COMMENT '邮箱',
  `address` varchar(200) DEFAULT NULL COMMENT '地址',
  `position` varchar(50) DEFAULT NULL COMMENT '职务',
  `phone` varchar(20) DEFAULT NULL COMMENT '电话',
  `tag_group_1` varchar(50) DEFAULT NULL COMMENT '标签组1(投放)',
  `tag_group_2` varchar(50) DEFAULT NULL COMMENT '标签组2(公司孵化)',
  `tag_group_3` varchar(50) DEFAULT NULL COMMENT '标签组3(商务)',
  `tag_group_4` varchar(50) DEFAULT NULL COMMENT '标签组4(成交日期)',
  `tag_group_5` varchar(50) DEFAULT NULL COMMENT '标签组5(年级组)',
  `tag_group_6` varchar(50) DEFAULT NULL COMMENT '标签组6(客户属性)',
  `tag_group_7` varchar(50) DEFAULT NULL COMMENT '标签组7(成交)',
  `tag_group_8` varchar(50) DEFAULT NULL COMMENT '标签组8(成交品牌)',
  `tag_group_9` varchar(50) DEFAULT NULL COMMENT '标签组9(线索通标签)',
  `tag_group_10` varchar(50) DEFAULT NULL COMMENT '标签组10(A1组)',
  `tag_group_11` varchar(50) DEFAULT NULL COMMENT '标签组11(B1组)',
  `tag_group_12` varchar(50) DEFAULT NULL COMMENT '标签组12(C1组)',
  `tag_group_13` varchar(50) DEFAULT NULL COMMENT '标签组13(D1组)',
  `tag_group_14` varchar(50) DEFAULT NULL COMMENT '标签组14(E1组)',
  `tag_group_15` varchar(50) DEFAULT NULL COMMENT '标签组15(意向度)',
  `tag_group_16` varchar(50) DEFAULT NULL COMMENT '标签组16(自然流)',
  `tag_group_17` varchar(50) DEFAULT NULL COMMENT '标签组17(F1组)',
  `tag_group_18` varchar(50) DEFAULT NULL COMMENT '标签组18(G1组)',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_customer_name` (`customer_name`),
  KEY `idx_add_person_account` (`add_person_account`),
  KEY `idx_add_time` (`add_time`),
  KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='客户数据表';

-- 2. 已处理数据记录表（用于去重）
DROP TABLE IF EXISTS `processed_data_record`;
CREATE TABLE `processed_data_record` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `customer_name` varchar(100) NOT NULL COMMENT '客户名称',
  `add_person_account` varchar(50) NOT NULL COMMENT '添加人账号',
  `data_hash` varchar(64) NOT NULL COMMENT '数据哈希值(MD5)',
  `processing_status` varchar(20) DEFAULT 'PROCESSED' COMMENT '处理状态',
  `process_time` datetime DEFAULT NULL COMMENT '处理时间',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_customer_account_hash` (`customer_name`, `add_person_account`, `data_hash`),
  KEY `idx_process_time` (`process_time`),
  KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='已处理数据记录表';

-- 3. 客户标签统计表
DROP TABLE IF EXISTS `customer_tag_statistics`;
CREATE TABLE `customer_tag_statistics` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `stat_date` varchar(10) NOT NULL COMMENT '统计日期(yyyy-MM-dd)',
  `tag_name` varchar(100) NOT NULL COMMENT '标签名称',
  `tag_count` int(11) DEFAULT 0 COMMENT '标签数量',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_date_tag` (`stat_date`, `tag_name`),
  KEY `idx_stat_date` (`stat_date`),
  KEY `idx_tag_name` (`tag_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='客户标签统计表';

-- 4. 客户统计数据表（标签组维度）
DROP TABLE IF EXISTS `customer_statistics_data`;
CREATE TABLE `customer_statistics_data`  (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`cur_date` date NULL DEFAULT NULL COMMENT '统计日期',
 `indicator_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '重要指标',
`ntf_group` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'N组',
`ofh_group` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'O组(公司孵化)',
`psw_group` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'P组(商务)',
`wa1_group` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'W组(A1组)',
 `xb1_group` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'X组(B1组)',
 `yc1_group` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'Y组(C1组)',
 `zd1_group` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'Z组(D1组)',
  `aa_group` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'AA组(E1组)',
   `ac_group` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'AC组(自然流)',
   `ad_group` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'AD组(F1组)',
    `ae_group` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'AE组(G1组)',
    `sort_no` int NULL DEFAULT NULL COMMENT '排序',
   `create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
   `update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
     PRIMARY KEY (`id`) USING BTREE,
       UNIQUE INDEX `uk_date_indicator`(`cur_date` ASC, `indicator_name` ASC) USING BTREE,
      INDEX `idx_cur_date`(`cur_date` ASC) USING BTREE,
       INDEX `idx_indicator_name`(`indicator_name` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2301 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '客户统计数据表（标签组维度）' ROW_FORMAT = Dynamic;

-- 5. 部门统计数据表
DROP TABLE IF EXISTS `department_statistics_data`;
CREATE TABLE `department_statistics_data` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `stat_date` date NOT NULL COMMENT '统计日期',
  `department_path` varchar(200) NOT NULL COMMENT '部门路径（如：苏州曼普/销售部/一组 或 苏州曼普/销售部/一组/盛宇婷）',
  `indicator_name` varchar(100) NOT NULL COMMENT '指标名称',
  `indicator_value` varchar(50) DEFAULT NULL COMMENT '指标值',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_date_dept_indicator` (`stat_date`, `department_path`, `indicator_name`),
  KEY `idx_stat_date` (`stat_date`),
  KEY `idx_department_path` (`department_path`),
  KEY `idx_indicator_name` (`indicator_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='部门统计数据表';

-- ========================================
-- 初始化数据（可选）
-- ========================================

-- 插入测试数据示例（可根据需要删除）
-- INSERT INTO `customer_data` (`customer_name`, `add_person_account`, `add_time`)
-- VALUES ('测试客户', 'test_account', NOW());
