-- 修复客服联系方式分组表
-- 检查表是否存在，如果不存在则创建

-- 先检查表是否存在
SELECT COUNT(*) as table_exists FROM information_schema.tables 
WHERE table_schema = DATABASE() AND table_name = 'eb_customer_service_group';

-- 如果表存在但字段名不对，需要修改
-- 检查是否有 name 字段
SET @has_name := (SELECT COUNT(*) FROM information_schema.columns 
                  WHERE table_schema = DATABASE() 
                  AND table_name = 'eb_customer_service_group' 
                  AND column_name = 'name');

-- 检查是否有 group_name 字段
SET @has_group_name := (SELECT COUNT(*) FROM information_schema.columns 
                        WHERE table_schema = DATABASE() 
                        AND table_name = 'eb_customer_service_group' 
                        AND column_name = 'group_name');

-- 如果有 name 但没有 group_name，重命名字段
-- ALTER TABLE eb_customer_service_group CHANGE COLUMN `name` `group_name` varchar(64) NOT NULL DEFAULT '' COMMENT '用户组名称';

-- 或者直接重建表（更安全）
DROP TABLE IF EXISTS `eb_customer_service_group`;
CREATE TABLE `eb_customer_service_group` (
  `id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `group_name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户组名称',
  `avatar` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '用户头像',
  `contact` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '联系电话或微信',
  `contact_type` tinyint NOT NULL DEFAULT 1 COMMENT '联系类型 1=电话 2=微信',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '客服联系方式分组表' ROW_FORMAT = DYNAMIC;

-- 插入测试数据
INSERT INTO `eb_customer_service_group` (`group_name`, `avatar`, `contact`, `contact_type`, `create_time`) VALUES
('官方客服', 'https://img.zcool.cn/community/01a9a65d143edaa8012187f447cfef.jpg', '400-888-8888', 1, NOW()),
('技术支持', 'https://img.zcool.cn/community/01b72057a7e0790000018c1bf4fce0.png', 'tech_support_wx', 2, NOW()),
('商务合作', '', '13800138000', 1, NOW()),
('投诉建议', '', 'complaint_wx', 2, NOW());

-- 查看数据
SELECT * FROM eb_customer_service_group;
