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.

117 lines
3.3 KiB

import 'dart:convert';
import 'package:get_storage/get_storage.dart';
import '../../data/services/db/sqflite_api.dart';
import '../../data/services/network/api.dart';
class Synchrodata {
static final GetStorage _storage = GetStorage();
static List<Map<String, dynamic>> _records =
(_storage.read("operation_records") as List?)
?.map((e) => Map<String, dynamic>.from(e as Map))
.toList() ??
[];
static List _delids = _storage.read("operation_delids") ?? [];
static bool _isSynchro = false;
// 保存操作记录
static Future saveOperationRecord(
String method,
dynamic params,
) async {
String userId = '';
Map? userInfo = _storage.read("user_info");
if (userInfo != null) {
userId = userInfo['user']['uid'];
}
String id = '${userId}_${params['id']}';
if (params['type'] != null) {
params['rtype'] = params['type'];
}
switch (method) {
case 'insert':
_records.add({
...Map<String, dynamic>.from(params),
'id': id,
'number': params['id'],
'uid': userId,
});
_storage.write("operation_records", _records);
break;
case 'update':
Map<String, dynamic>? foundElement = _records.firstWhere(
(element) => element['id'] == id,
orElse: () => {},
);
if (foundElement.isNotEmpty) {
foundElement.addAll({
...Map<String, dynamic>.from(params),
'id': id,
'number': params['id'],
});
} else {
_records.add({
...Map<String, dynamic>.from(params),
'id': id,
'number': params['id'],
'uid': userId,
});
}
_storage.write("operation_records", _records);
break;
case 'delete':
_delids.add(id);
_storage.write("operation_delids", _delids);
break;
default:
break;
}
}
// 上传操作记录
static Future uploadOperationRecords({bool isSynchro = false}) async {
if (_isSynchro) return;
_isSynchro = true;
if (_records.isNotEmpty || _delids.isNotEmpty) {
// 获取需要修改的全部数据
List putRecords = [];
for (var element in _records) {
Map result = {'details': {}};
List speakerList = [];
if (!_delids.contains(element['id'])) {
result = await SqfliteApi.getMeetingDetails(element['number']);
speakerList = await SqfliteApi.getMeetingSpeaker(element['number']);
}
putRecords.add({
...Map<String, dynamic>.from(result),
...Map<String, dynamic>.from(result['details']),
'rtype': result['type'],
'original': jsonEncode(speakerList),
...Map<String, dynamic>.from(element),
});
}
var result = await Api.putOperationRecord({
'delids': _delids,
'records': putRecords,
});
if (result != null) {
_records = [];
_delids = [];
_storage.write("operation_records", _records);
_storage.write("operation_delids", _delids);
}
}
if (isSynchro) {
await _synchrodata();
}
_isSynchro = false;
}
// 同步数据
static Future _synchrodata() async {
var result = await Api.getOperationRecord();
}
}