更新
This commit is contained in:
1 parent
977ce66110
commit
c4b1f8fd1b
17 files changed
+720
-48
No files matched your search
@@ -66,6 +66,7 @@ class SecHubDatabase {
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
json_filename TEXT UNIQUE NOT NULL,
|
||||
table_name TEXT NOT NULL,
|
||||
section_no INTEGER DEFAULT 0,
|
||||
last_sync_time DATETIME NOT NULL,
|
||||
json_file_mtime INTEGER NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
@@ -115,6 +116,9 @@ class SecHubDatabase {
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取排序号(从第一个数据项的 no 字段)
|
||||
$sectionNo = $data[0]['no'] ?? 0;
|
||||
|
||||
// 创建或更新表
|
||||
$this->createTable($tableName, $data[0]);
|
||||
|
||||
@@ -127,14 +131,14 @@ class SecHubDatabase {
|
||||
$this->insertItem($tableName, $item, $data[0]['section'] ?? $tableName);
|
||||
}
|
||||
|
||||
// 更新同步日志
|
||||
$this->updateSyncLog($filename, $tableName);
|
||||
// 更新同步日志(包含排序号)
|
||||
$this->updateSyncLog($filename, $tableName, $sectionNo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新同步日志
|
||||
*/
|
||||
private function updateSyncLog($filename, $tableName) {
|
||||
private function updateSyncLog($filename, $tableName, $sectionNo = 0) {
|
||||
$jsonFile = $this->jsonDir . $filename;
|
||||
$jsonModified = filemtime($jsonFile);
|
||||
$syncTime = date('Y-m-d H:i:s');
|
||||
@@ -148,20 +152,22 @@ class SecHubDatabase {
|
||||
if ($exists) {
|
||||
// 更新现有记录
|
||||
$sql = "UPDATE json_sync_log
|
||||
SET table_name = :table_name,
|
||||
SET table_name = :table_name,
|
||||
section_no = :section_no,
|
||||
last_sync_time = :sync_time,
|
||||
json_file_mtime = :mtime
|
||||
WHERE json_filename = :filename";
|
||||
} else {
|
||||
// 插入新记录
|
||||
$sql = "INSERT INTO json_sync_log (json_filename, table_name, last_sync_time, json_file_mtime)
|
||||
VALUES (:filename, :table_name, :sync_time, :mtime)";
|
||||
$sql = "INSERT INTO json_sync_log (json_filename, table_name, section_no, last_sync_time, json_file_mtime)
|
||||
VALUES (:filename, :table_name, :section_no, :sync_time, :mtime)";
|
||||
}
|
||||
|
||||
$stmt = $this->db->prepare($sql);
|
||||
$stmt->execute([
|
||||
':filename' => $filename,
|
||||
':table_name' => $tableName,
|
||||
':section_no' => $sectionNo,
|
||||
':sync_time' => $syncTime,
|
||||
':mtime' => $jsonModified
|
||||
]);
|
||||
@@ -344,6 +350,15 @@ class SecHubDatabase {
|
||||
|
||||
foreach ($tables as $table) {
|
||||
$tableName = $table['name'];
|
||||
|
||||
// 从同步日志中获取排序号
|
||||
$sql = "SELECT section_no FROM json_sync_log WHERE table_name = :table_name LIMIT 1";
|
||||
$stmt = $this->db->prepare($sql);
|
||||
$stmt->execute([':table_name' => $tableName]);
|
||||
$log = $stmt->fetch();
|
||||
$sectionNo = $log ? $log['section_no'] : 0;
|
||||
|
||||
// 获取栏目名称
|
||||
$sql = "SELECT DISTINCT section FROM {$tableName} LIMIT 1";
|
||||
$stmt = $this->db->query($sql);
|
||||
$row = $stmt->fetch();
|
||||
@@ -351,11 +366,17 @@ class SecHubDatabase {
|
||||
if ($row) {
|
||||
$sections[$tableName] = [
|
||||
'title' => $row['section'],
|
||||
'table' => $tableName
|
||||
'table' => $tableName,
|
||||
'no' => $sectionNo
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// 按 no 字段排序
|
||||
uasort($sections, function($a, $b) {
|
||||
return ($a['no'] ?? 0) - ($b['no'] ?? 0);
|
||||
});
|
||||
|
||||
return $sections;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user