const db = require('../plugin/DataBase/db') const { normalizeSocialType } = require('./UniLoginClient') async function getBindingByIdentity(socialType, socialUid) { const type = normalizeSocialType(socialType) if (!type || !socialUid) return null const rows = await db.query( 'SELECT user_uuid, social_type, social_uid, social_nickname, social_avatar FROM user_social_bindings WHERE social_type = ? AND social_uid = ? LIMIT 1', [type, socialUid] ) return rows?.[0] || null } async function getLegacyUserByIdentity(socialType, socialUid) { const type = normalizeSocialType(socialType) if (!type || !socialUid) return null const rows = await db.query( 'SELECT uuid, username, permission, avatar, nickname FROM users WHERE social_uid = ? AND social_type = ? LIMIT 1', [socialUid, type] ) return rows?.[0] || null } async function getUserSocialBindings(userUuid) { const rows = await db.query( 'SELECT social_type, social_uid, social_nickname, social_avatar FROM user_social_bindings WHERE user_uuid = ? ORDER BY FIELD(social_type, "qq", "wx")', [userUuid] ) return rows || [] } async function insertSocialBinding(userUuid, socialType, socialUid, socialNickname = null, socialAvatar = null) { const type = normalizeSocialType(socialType) if (!type || !socialUid) return null const now = new Date().getTime() return await db.query( 'INSERT IGNORE INTO user_social_bindings (user_uuid, social_type, social_uid, social_nickname, social_avatar, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)', [userUuid, type, socialUid, socialNickname, socialAvatar, now, now] ) } async function updateSocialBindingProfile(socialType, socialUid, socialNickname = null, socialAvatar = null) { const type = normalizeSocialType(socialType) if (!type || !socialUid) return null const now = new Date().getTime() return await db.query( 'UPDATE user_social_bindings SET social_nickname = ?, social_avatar = ?, updated_at = ? WHERE social_type = ? AND social_uid = ?', [socialNickname, socialAvatar, now, type, socialUid] ) } async function syncLegacySocialMirror(userUuid, preferredSocialType = null) { const bindings = await getUserSocialBindings(userUuid) const preferredType = normalizeSocialType(preferredSocialType) const binding = bindings.find(item => item.social_type === preferredType) || bindings[0] if (!binding) { return await db.query( 'UPDATE users SET social_uid = NULL, social_type = NULL WHERE uuid = ?', [userUuid] ) } return await db.query( 'UPDATE users SET social_uid = ?, social_type = ? WHERE uuid = ?', [binding.social_uid, binding.social_type, userUuid] ) } async function removeSocialBinding(userUuid, socialType) { const type = normalizeSocialType(socialType) if (!type) return null return await db.query( 'DELETE FROM user_social_bindings WHERE user_uuid = ? AND social_type = ?', [userUuid, type] ) } function toSocialBindingSummary(bindings = []) { return ['qq', 'wx'].map(type => { const binding = bindings.find(item => item.social_type === type) return { type, bound: !!binding, social_uid: binding?.social_uid || '', nickname: binding?.social_nickname || '', avatar: binding?.social_avatar || '' } }) } module.exports = { getBindingByIdentity, getLegacyUserByIdentity, getUserSocialBindings, insertSocialBinding, updateSocialBindingProfile, syncLegacySocialMirror, removeSocialBinding, toSocialBindingSummary }