-- 确保 users 表包含立即入驻功能所需的所有字段
-- 此脚本用于修复或添加入驻流程可能需要的数据库字段

USE wechat_app;

-- 检查并添加缺失的字段（如果不存在）

-- 1. 入驻相关字段
-- 检查 collaborationid 字段（合作商身份）
IF NOT EXISTS (
    SELECT * FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = 'wechat_app'
    AND TABLE_NAME = 'users'
    AND COLUMN_NAME = 'collaborationid'
) THEN
    ALTER TABLE users ADD COLUMN collaborationid TEXT COMMENT '合作商身份' AFTER demand;
END IF;

-- 检查 cooperation 字段（合作模式）
IF NOT EXISTS (
    SELECT * FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = 'wechat_app'
    AND TABLE_NAME = 'users'
    AND COLUMN_NAME = 'cooperation'
) THEN
    ALTER TABLE users ADD COLUMN cooperation VARCHAR(255) COMMENT '合作模式' AFTER collaborationid;
END IF;

-- 检查 businesslicenseurl 字段（营业执照）
IF NOT EXISTS (
    SELECT * FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = 'wechat_app'
    AND TABLE_NAME = 'users'
    AND COLUMN_NAME = 'businesslicenseurl'
) THEN
    ALTER TABLE users ADD COLUMN businesslicenseurl TEXT COMMENT '营业执照URL' AFTER cooperation;
END IF;

-- 检查 proofurl 字段（证明材料）
IF NOT EXISTS (
    SELECT * FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = 'wechat_app'
    AND TABLE_NAME = 'users'
    AND COLUMN_NAME = 'proofurl'
) THEN
    ALTER TABLE users ADD COLUMN proofurl TEXT COMMENT '证明材料URL' AFTER businesslicenseurl;
END IF;

-- 检查 brandurl 字段（品牌授权链）
IF NOT EXISTS (
    SELECT * FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = 'wechat_app'
    AND TABLE_NAME = 'users'
    AND COLUMN_NAME = 'brandurl'
) THEN
    ALTER TABLE users ADD COLUMN brandurl TEXT COMMENT '品牌授权链文件URL' AFTER proofurl;
END IF;

-- 检查 partnerstatus 字段（合作商状态）
IF NOT EXISTS (
    SELECT * FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = 'wechat_app'
    AND TABLE_NAME = 'users'
    AND COLUMN_NAME = 'partnerstatus'
) THEN
    ALTER TABLE users ADD COLUMN partnerstatus VARCHAR(255) COMMENT '合作商状态' AFTER brandurl;
END IF;

-- 2. 详细地址相关字段
-- 检查 detailedaddress 字段
IF NOT EXISTS (
    SELECT * FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = 'wechat_app'
    AND TABLE_NAME = 'users'
    AND COLUMN_NAME = 'detailedaddress'
) THEN
    ALTER TABLE users ADD COLUMN detailedaddress VARCHAR(255) COMMENT '详细地址' AFTER district;
END IF;

-- 3. 其他可能需要的字段
-- 检查 reasonforfailure 字段（审核失败原因）
IF NOT EXISTS (
    SELECT * FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = 'wechat_app'
    AND TABLE_NAME = 'users'
    AND COLUMN_NAME = 'reasonforfailure'
) THEN
    ALTER TABLE users ADD COLUMN reasonforfailure TEXT COMMENT '审核失败原因' AFTER partnerstatus;
END IF;

-- 检查 audit_time 字段（审核时间）
IF NOT EXISTS (
    SELECT * FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = 'wechat_app'
    AND TABLE_NAME = 'users'
    AND COLUMN_NAME = 'audit_time'
) THEN
    ALTER TABLE users ADD COLUMN audit_time DATETIME COMMENT '审核时间' AFTER reasonforfailure;
END IF;

-- 4. 修改字段属性以允许空值（如果需要）
-- 修改 collaborationid 允许为空（入驻流程中可能未选择）
ALTER TABLE users MODIFY COLUMN collaborationid TEXT NULL COMMENT '合作商身份';

-- 修改 cooperation 允许为空
ALTER TABLE users MODIFY COLUMN cooperation VARCHAR(255) NULL COMMENT '合作模式';

-- 修改 province/city/district 允许为空（地址选择可能不完整）
ALTER TABLE users MODIFY COLUMN province VARCHAR(50) NULL COMMENT '省份';
ALTER TABLE users MODIFY COLUMN city VARCHAR(50) NULL COMMENT '城市';
ALTER TABLE users MODIFY COLUMN district VARCHAR(255) NULL COMMENT '区域';

-- 5. 添加索引以提高查询性能
-- 为 openid 添加唯一索引（如果尚未存在）
IF NOT EXISTS (
    SELECT * FROM information_schema.STATISTICS
    WHERE TABLE_SCHEMA = 'wechat_app'
    AND TABLE_NAME = 'users'
    AND INDEX_NAME = 'idx_openid'
) THEN
    CREATE INDEX idx_openid ON users(openid);
END IF;

-- 为 partnerstatus 添加索引（用于查询审核状态）
IF NOT EXISTS (
    SELECT * FROM information_schema.STATISTICS
    WHERE TABLE_SCHEMA = 'wechat_app'
    AND TABLE_NAME = 'users'
    AND INDEX_NAME = 'idx_partnerstatus'
) THEN
    CREATE INDEX idx_partnerstatus ON users(partnerstatus);
END IF;

-- 为 userId 添加索引（如果尚未存在唯一索引）
IF NOT EXISTS (
    SELECT * FROM information_schema.STATISTICS
    WHERE TABLE_SCHEMA = 'wechat_app'
    AND TABLE_NAME = 'users'
    AND INDEX_NAME = 'idx_userId'
) THEN
    CREATE INDEX idx_userId ON users(userId);
END IF;

SELECT '数据库表结构修复完成！' AS status;
