import db from "../utils/db"; export default { createTalbe() { return db.executeSql(` CREATE TABLE IF NOT EXISTS userInfo ( account TEXT, avatar TEXT, create_time TEXT, cs_uid INTEGER, delete_time INTEGER, email TEXT, friend TEXT, friend_limit INTEGER, group_limit INTEGER, is_auth INTEGER, last_login_ip TEXT, last_login_time INTEGER, location TEXT, login_count INTEGER, motto TEXT, name_py TEXT, password TEXT, realname TEXT, register_ip TEXT, remark TEXT, role INTEGER, salt TEXT, setting TEXT, sex INTEGER, status INTEGER, update_time TEXT, user_id INTEGER ) `) }, async getList({user_id}) { try { if (typeof user_id === 'undefined' || user_id === null) { return []; } const res = await db.selectSql(`SELECT * FROM userInfo WHERE user_id = ${user_id}`); return res; }catch (error) { console.error('查询失败:', error); return []; // 返回空数组表示查询失败 } }, // 批量插入/更新数据 async batchInsertOrUpdate(groupData) { if (!Array.isArray(groupData)) { throw new Error('数据必须为数组格式'); } if (groupData.length === 0) return true; const dbInstance = await db.createDatabase(); try { // 开启事务 await db.executeSql('BEGIN IMMEDIATE TRANSACTION'); // 2. 批量查询现有数据(基于 id) const ids = groupData.map(item => item.user_id).filter(user_id => user_id != null); if (ids.length === 0) return true; const existingRecords = await db.selectSql(`SELECT * FROM userInfo WHERE user_id = ${ids[0]}`); // console.log('1234',existingRecords.length,existingRecords); // 构建批量插入语句 const insertPromises = groupData.map(async (item,index) => { // 字段值预处理 const processedItem = { account: item.account || '', avatar: item.avatar || '', create_time: item.create_time || '', cs_uid: item.cs_uid || 0, delete_time: item.delete_time || 0, email: item.email || '', friend: item.friend ? JSON.stringify(item.friend) : '{}', friend_limit: item.friend_limit || 0, group_limit: item.group_limit || 0, is_auth: item.is_auth || 0, last_login_ip: item.last_login_ip || '', last_login_time: item.last_login_time || 0, location: item.location || '', login_count: item.login_count || 0, motto: item.motto || '', name_py: item.name_py || '', password: item.password || '', realname: item.realname || '', register_ip: item.register_ip || '', remark: item.remark || '', role: item.role || 0, salt: item.salt || '', setting: item.setting ? JSON.stringify(item.setting) : '{}', sex: item.sex || 0, status: item.status || 0, update_time: item.update_time || '', user_id: item.user_id || 0, }; // 数据完全一致时跳过 const existingItem = existingRecords[index] // console.info('123',existingItem); // console.info('1234',processedItem); // console.info(processedItem); if (existingItem && this.isDataSame(existingItem, processedItem)) { return Promise.reject(new Error('聊天信息数据已同步并且数据没有改变')); }else{ await db.executeSql(`DELETE FROM userInfo WHERE user_id = ${item.user_id}`); // 提取值数组(注意顺序要与SQL语句中的字段顺序一致) const values = [ processedItem.account, processedItem.avatar, processedItem.create_time, processedItem.cs_uid, processedItem.delete_time, processedItem.email, processedItem.friend, processedItem.friend_limit, processedItem.group_limit, processedItem.is_auth, processedItem.last_login_ip, processedItem.last_login_time, processedItem.location, processedItem.login_count, processedItem.motto, processedItem.name_py, processedItem.password, processedItem.realname, processedItem.register_ip, processedItem.remark, processedItem.role, processedItem.salt, processedItem.setting, processedItem.sex, processedItem.status, processedItem.update_time, processedItem.user_id ]; let value_str = "'" + values.join("','") + "'" // 构建INSERT语句 const sql = ` INSERT OR REPLACE INTO userInfo ( account, avatar, create_time, cs_uid, delete_time, email, friend, friend_limit, group_limit, is_auth, last_login_ip, last_login_time, location, login_count, motto, name_py, password, realname, register_ip, remark, role, salt, setting, sex, status, update_time, user_id ) VALUES (` + value_str + `) `; return db.executeSql(sql); } }); await Promise.all(insertPromises); await db.executeSql('COMMIT'); // console.log('数据同步成功,插入/更新', groupData.length, '条记录'); return true; } catch (error) { await db.executeSql('ROLLBACK'); console.error('数据同步失败:', error); throw new Error('数据同步失败: ' + error.message); } }, // 新增数据比较方法 isDataSame(existing, current) { // 排除自增字段等不需要比较的字段(根据实际表结构调整) const ignoreFields = ['create_time']; // 如果id是自增主键需要排除 return Object.keys(current).every(key => { if (ignoreFields.includes(key)) return true; // 处理JSON字段特殊比较 if (key === 'userInfo' ) { return JSON.stringify(existing[key]) === JSON.stringify(current[key]); } // 处理null值情况 if (existing[key] === null && current[key] === null) return true; if (existing[key] === null || current[key] === null) return false; // 处理数字类型(包括可能为空字符串的情况) if (typeof current[key] === 'number') { return Number(existing[key]) === current[key]; } // 默认字符串比较 return String(existing[key]) === String(current[key]); }); } }