-- 企业微信外部联系人（客户）表
CREATE TABLE IF NOT EXISTS `zyt_qywx_external_contact` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `external_userid` varchar(64) NOT NULL DEFAULT '' COMMENT '外部联系人ID',
  `name` varchar(64) NOT NULL DEFAULT '' COMMENT '客户名称',
  `avatar` varchar(255) NOT NULL DEFAULT '' COMMENT '头像URL',
  `type` tinyint(1) unsigned NOT NULL DEFAULT 1 COMMENT '类型：1=微信用户，2=企业微信用户',
  `gender` tinyint(1) unsigned NOT NULL DEFAULT 0 COMMENT '性别：0=未知，1=男，2=女',
  `unionid` varchar(64) NOT NULL DEFAULT '' COMMENT '微信unionid',
  `position` varchar(64) NOT NULL DEFAULT '' COMMENT '职位',
  `corp_name` varchar(128) NOT NULL DEFAULT '' COMMENT '企业名称',
  `corp_full_name` varchar(255) NOT NULL DEFAULT '' COMMENT '企业全称',
  `external_profile` text COMMENT '外部联系人扩展信息JSON',
  `follow_users` text COMMENT '跟进人列表JSON',
  `follow_admin_ids` text NULL COMMENT '跟进人对应后台管理员ID列表JSON',
  `external_first_add_time` int(11) unsigned NOT NULL DEFAULT 0 COMMENT '企微侧首次添加外部联系人时间(Unix秒)',
  `create_time` int(11) unsigned NOT NULL DEFAULT 0 COMMENT '添加时间',
  `update_time` int(11) unsigned NOT NULL DEFAULT 0 COMMENT '更新时间',
  `delete_time` int(11) unsigned DEFAULT NULL COMMENT '删除时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_external_userid` (`external_userid`),
  KEY `idx_name` (`name`),
  KEY `idx_unionid` (`unionid`),
  KEY `idx_external_first_add_time` (`external_first_add_time`),
  KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企业微信外部联系人表';

-- 企业微信同步设置表
CREATE TABLE IF NOT EXISTS `zyt_qywx_sync_settings` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `setting_key` varchar(64) NOT NULL DEFAULT '' COMMENT '设置键',
  `setting_value` text COMMENT '设置值JSON',
  `create_time` int(11) unsigned NOT NULL DEFAULT 0 COMMENT '创建时间',
  `update_time` int(11) unsigned NOT NULL DEFAULT 0 COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_setting_key` (`setting_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企业微信同步设置表';

-- 插入默认同步设置
INSERT INTO `zyt_qywx_sync_settings` (`setting_key`, `setting_value`, `create_time`, `update_time`)
VALUES ('customer_sync', '{"auto_sync":false,"interval":3600,"last_sync_time":0,"sync_status":"idle"}', UNIX_TIMESTAMP(), UNIX_TIMESTAMP())
ON DUPLICATE KEY UPDATE `update_time` = UNIX_TIMESTAMP();
