You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

149 lines
4.8 KiB

import db from "../utils/db";
export default {
createTalbe() {
return db.executeSql(`
CREATE TABLE IF NOT EXISTS groupUserList (
create_time TEXT,
delete_time TEXT,
group_id INTEGER,
id INTEGER,
invite_id INTEGER,
is_notice INTEGER,
is_top INTEGER,
no_speak_time INTEGER,
role INTEGER,
status INTEGER,
unread INTEGER,
userInfo TEXT,
user_id INTEGER
)
`)
},
async getList({group_id}) {
try {
if (typeof group_id === 'undefined' || group_id === null) {
return [];
}
const res = await db.selectSql(`SELECT * FROM groupUserList WHERE group_id = ${group_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.group_id).filter(group_id => group_id != null);
if (ids.length === 0) return true;
const existingRecords = await db.selectSql(`SELECT * FROM groupUserList WHERE group_id = ${ids[0]}`);
// console.log('1234',existingRecords.length,existingRecords);
// 构建批量插入语句
const insertPromises = groupData.map(async (item,index) => {
// 字段值预处理
const processedItem = {
create_time: item.create_time || '',
delete_time: JSON.stringify(item.delete_time) || '',
group_id: Number(item.group_id) || '',
id: Number(item.id) || '',
invite_id: Number(item.invite_id) || '',
is_notice: Number(item.is_notice) || '',
is_top: item.is_top || '',
no_speak_time: item.no_speak_time || '',
role: Number(item.role) || '',
status: Number(item.status) || '',
unread: Number(item.unread) || '',
userInfo: item.userInfo ? JSON.stringify(item.userInfo) : '{}',
user_id: Number(item.user_id) || ''
};
// 数据完全一致时跳过
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 groupUserList WHERE group_id = ${item.group_id}`);
// 提取值数组(注意顺序要与SQL语句中的字段顺序一致)
const values = [
processedItem.create_time,
processedItem.delete_time,
processedItem.group_id,
processedItem.id,
processedItem.invite_id,
processedItem.is_notice,
processedItem.is_top,
processedItem.no_speak_time,
processedItem.role,
processedItem.status,
processedItem.unread,
processedItem.userInfo,
processedItem.user_id
];
let value_str = "'" + values.join("','") + "'"
// 构建INSERT语句
const sql = `
INSERT OR REPLACE INTO groupUserList (
create_time, delete_time, group_id, id, invite_id, is_notice, is_top, no_speak_time,
role, status, unread, userInfo, 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 = ['id','create_time','unread']; // 如果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]);
});
}
}