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.
173 lines
5.5 KiB
173 lines
5.5 KiB
import db from "../utils/db";
|
|
|
|
export default {
|
|
createTalbe() {
|
|
return db.executeSql(`
|
|
CREATE TABLE IF NOT EXISTS groupInfo (
|
|
avatar TEXT,
|
|
create_time TEXT,
|
|
create_user INTEGER,
|
|
delete_time INTEGER,
|
|
displayName TEXT,
|
|
groupUserCount INTEGER,
|
|
group_id INTEGER,
|
|
id TEXT,
|
|
isJoin INTEGER,
|
|
is_public INTEGER,
|
|
level INTEGER,
|
|
name TEXT,
|
|
name_py TEXT,
|
|
notice TEXT,
|
|
ownerName TEXT,
|
|
owner_id INTEGER,
|
|
qrExpire TEXT,
|
|
qrUrl TEXT,
|
|
setting TEXT,
|
|
status INTEGER,
|
|
userInfo TEXT
|
|
)
|
|
`)
|
|
},
|
|
|
|
async getList({group_id}) {
|
|
try {
|
|
if (typeof group_id === 'undefined' || group_id === null) {
|
|
return [];
|
|
}
|
|
const res = await db.selectSql(`SELECT * FROM groupInfo WHERE id = ${JSON.stringify(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.id).filter(id => id != null);
|
|
if (ids.length === 0) return true;
|
|
const existingRecords = await db.selectSql(`SELECT * FROM groupInfo WHERE id = ${JSON.stringify(ids.join(','))}`);
|
|
// console.log('1234',existingRecords);
|
|
// 构建批量插入语句
|
|
const insertPromises = groupData.map(async (item) => {
|
|
// 字段值预处理
|
|
const processedItem = {
|
|
avatar: item.avatar || "",
|
|
create_time: item.create_time || "",
|
|
create_user: Number(item.create_user) || '',
|
|
delete_time: Number(item.delete_time) || '',
|
|
displayName: item.displayName || "",
|
|
groupUserCount: Number(item.groupUserCount) || '',
|
|
group_id: Number(item.group_id) || '',
|
|
id: item.id || "",
|
|
isJoin: Number(item.isJoin) || '',
|
|
is_public: Number(item.is_public) || '',
|
|
level: Number(item.level) || '',
|
|
name: item.name || "",
|
|
name_py: item.name_py || "",
|
|
notice: item.notice || "",
|
|
ownerName: item.ownerName || "",
|
|
owner_id: Number(item.owner_id) || '',
|
|
qrExpire: item.qrExpire || "",
|
|
qrUrl: item.qrUrl || "",
|
|
setting: item.setting ? JSON.stringify(item.setting) : '{}',
|
|
status: Number(item.status) || '',
|
|
userInfo: item.userInfo ? JSON.stringify(item.userInfo) : '{}'
|
|
};
|
|
|
|
// 查找现有记录
|
|
const existingItem = existingRecords.find(r => r.id === item.id);
|
|
// 数据完全一致时跳过
|
|
if (existingItem && this.isDataSame(existingItem, processedItem)) {
|
|
return Promise.reject(new Error('数据已同步过并且数据没有改变'));
|
|
}else{
|
|
await db.executeSql(`DELETE FROM groupInfo WHERE id = ${JSON.stringify(item.id)}`);
|
|
|
|
// 提取值数组(注意顺序要与SQL语句中的字段顺序一致)
|
|
const values = [
|
|
processedItem.avatar,
|
|
processedItem.create_time,
|
|
processedItem.create_user,
|
|
processedItem.delete_time,
|
|
processedItem.displayName,
|
|
processedItem.groupUserCount,
|
|
processedItem.group_id,
|
|
processedItem.id,
|
|
processedItem.isJoin,
|
|
processedItem.is_public,
|
|
processedItem.level,
|
|
processedItem.name,
|
|
processedItem.name_py,
|
|
processedItem.notice,
|
|
processedItem.ownerName,
|
|
processedItem.owner_id,
|
|
processedItem.qrExpire,
|
|
processedItem.qrUrl,
|
|
processedItem.setting,
|
|
processedItem.status,
|
|
processedItem.userInfo
|
|
];
|
|
|
|
let value_str = "'" + values.join("','") + "'"
|
|
|
|
// 构建INSERT语句
|
|
const sql = `
|
|
INSERT OR REPLACE INTO groupInfo (
|
|
avatar, create_time, create_user, delete_time, displayName, groupUserCount, group_id, id,
|
|
isJoin, is_public, level, name, name_py, notice, ownerName, owner_id,
|
|
qrExpire, qrUrl, setting, status, userInfo
|
|
) 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','qrUrl']; // 如果id是自增主键需要排除
|
|
|
|
return Object.keys(current).every(key => {
|
|
if (ignoreFields.includes(key)) return true;
|
|
|
|
// 处理JSON字段特殊比较
|
|
if (key === 'setting' || 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]);
|
|
});
|
|
}
|
|
}
|