fix: 优化初始化数据库操作

This commit is contained in:
wangzhongjie 2025-06-25 13:42:01 +08:00
parent 637e41248c
commit 014c21ad81

View File

@ -110,6 +110,7 @@ export async function InitializeTheCentralTable(params: InitializeTheCentralTabl
flag = await SqlInsertTable(tableList[i], JSON.parse(data) || []) flag = await SqlInsertTable(tableList[i], JSON.parse(data) || [])
} }
if (!flag) { if (!flag) {
dConsole.error(InitTableTag, "本地文件初始化数据库失败");
Prompt.showToast({ Prompt.showToast({
message: '本地文件初始化数据库失败' message: '本地文件初始化数据库失败'
}) })
@ -129,45 +130,69 @@ export async function InitializeTheCentralTable(params: InitializeTheCentralTabl
host: params.host host: params.host
} }
const res: ResponseDataType = await initialization(str) const res: ResponseDataType = await initialization(str)
console.log("解析完成")
if (!res || !res.head || res.head.resultCode === "1") { if (!res || !res.head || res.head.resultCode === "1") {
resolve(false); resolve(false);
} }
if (res && res.body) { if (res && res.body) {
for (const key of Object.keys(res.body)) {
// 后端传递的字段首字母小写了,需要转换下
let newKey = key.charAt(0).toUpperCase() + key.slice(1)
let isExit = CenterMap.has(newKey)
console.log(InitTableTag, "-------------------表名-----------------------", newKey, isExit)
if (isExit) {
console.log("可以插入的表", newKey, CenterMap.get(newKey))
let arrList: Array<RecordType> | RecordType = []
let value: PublicInfoType = Reflect.get(res.body, key)
if (value instanceof Array) {
arrList = value
} else {
arrList.push(value)
}
const folderPath = await fileUtil.initFolder(`/config/tableList`);
fileUtil.addFile(`${folderPath}/${RemappingTableName.get(newKey)}.txt`, JSON.stringify(arrList))
try { try {
console.log(InitTableTag, "插入表操作", newKey) const tableKeys = Object.keys(res.body);
console.log(InitTableTag, "插入表别名", RemappingTableName.get(newKey)) const initPromises = tableKeys.map(async (key) => {
console.log(InitTableTag, "插入表长度", arrList.length) // 后端传递的字段首字母小写了,需要转换下
const newKey = key.charAt(0).toUpperCase() + key.slice(1);
const isExit = CenterMap.has(newKey);
dConsole.log(InitTableTag, "-------------------表名-----------------------", newKey, isExit);
if (!isExit) return false;
dConsole.log("可以插入的表", newKey, CenterMap.get(newKey));
let arrList: Array<RecordType> | RecordType = [];
const value: PublicInfoType = Reflect.get(res.body, key);
arrList = value instanceof Array ? value : [value];
const folderPath = await fileUtil.initFolder(`/config/tableList`);
await fileUtil.addFile(
`${folderPath}/${RemappingTableName.get(newKey)}.txt`,
JSON.stringify(arrList)
);
dConsole.log(InitTableTag, "插入表操作", newKey);
dConsole.log(InitTableTag, "插入表别名", RemappingTableName.get(newKey));
dConsole.log(InitTableTag, "插入表长度", arrList.length);
const result = await SqlInsertTable(RemappingTableName.get(newKey)!, arrList); const result = await SqlInsertTable(RemappingTableName.get(newKey)!, arrList);
console.log(InitTableTag, "插入表结果,表", newKey, "结果", result) dConsole.log(InitTableTag, "插入表结果,表", newKey, "结果", result);
if (!result) { if (!result) {
Prompt.showToast({ Prompt.showToast({
message: '初始化数据库失败' message: `初始化表 ${newKey} 失败`
}) });
return false;
}
dConsole.log(InitTableTag, "~~~~~~~~~~~~~~~~~~~~~~初始化成功~~~~~~~~~~~~~~~~~~~~~~", newKey);
return true;
});
const results = await Promise.all(initPromises);
const allSuccess = results.every(Boolean);
if (allSuccess) {
resolve(true);
} else {
dConsole.error(InitTableTag, "部分表初始化失败", JSON.stringify(results));
Prompt.showToast({
message: '部分表初始化失败'
});
reject(new Error('部分表初始化失败'));
} }
console.log(InitTableTag, "~~~~~~~~~~~~~~~~~~~~~~初始化成功~~~~~~~~~~~~~~~~~~~~~~", newKey)
resolve(result)
} catch (e) { } catch (e) {
dConsole.error(InitTableTag, "初始化表失败", JSON.stringify(e)) dConsole.error(InitTableTag, "初始化表失败", JSON.stringify(e));
reject(e) Prompt.showToast({
} message: '初始化数据库失败'
} });
reject(e);
} }
} }
}) })