From f329f504040f3608478c687a99aa36a3cf5720b7 Mon Sep 17 00:00:00 2001
From: xyc <jc_xiong@hotmail.com>
Date: 星期六, 03 十月 2026 23:45:04 +0800
Subject: [PATCH] feat(observation): 新增观测站分日调查数据导入与明细查看

---
 deploy-templates/backend/install-autostart.ps1 |  109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 109 insertions(+), 0 deletions(-)

diff --git a/deploy-templates/backend/install-autostart.ps1 b/deploy-templates/backend/install-autostart.ps1
new file mode 100644
index 0000000..7507d98
--- /dev/null
+++ b/deploy-templates/backend/install-autostart.ps1
@@ -0,0 +1,109 @@
+锘�<#
+  Register / remove / inspect a Windows Scheduled Task that starts the TrafficAudit
+  backend at machine boot, with no user session required.
+
+  Run from an ELEVATED PowerShell in the backend folder (jar + application.yml +
+  this script + start-backend-service.cmd):
+
+    powershell -NoProfile -ExecutionPolicy Bypass -File install-autostart.ps1
+    powershell -NoProfile -ExecutionPolicy Bypass -File install-autostart.ps1 -DelaySeconds 90
+    powershell -NoProfile -ExecutionPolicy Bypass -File install-autostart.ps1 -StartNow
+    powershell -NoProfile -ExecutionPolicy Bypass -File install-autostart.ps1 -Status
+    powershell -NoProfile -ExecutionPolicy Bypass -File install-autostart.ps1 -Remove
+
+  What it does:
+    - trigger  : AtStartup, delayed by -DelaySeconds (default 60s, so MySQL is up first)
+    - principal: SYSTEM, highest privileges, "run whether user is logged on or not"
+    - settings : no execution time limit, start when available, restart 3x / 1 min on failure
+    - action   : cmd.exe /c start-backend-service.cmd (logs to app.log next to the jar)
+
+  Notes:
+    - Re-running without -Remove is idempotent (-Force overwrites the task).
+    - Start/stop manually with: Start-ScheduledTask / Stop-ScheduledTask -TaskName <name>
+    - Keep this file ASCII-only: Windows PowerShell 5.1 reads a BOM-less .ps1 as ANSI.
+#>
+param(
+  [string]$TaskName = 'TrafficAuditBackend',
+  [int]$DelaySeconds = 60,
+  [switch]$Remove,
+  [switch]$Status,
+  [switch]$StartNow
+)
+
+$ErrorActionPreference = 'Stop'
+$root     = Split-Path -Parent $MyInvocation.MyCommand.Path
+$launcher = Join-Path $root 'start-backend-service.cmd'
+$logFile  = Join-Path $root 'app.log'
+
+function Assert-Admin {
+  $identity  = [Security.Principal.WindowsIdentity]::GetCurrent()
+  $principal = New-Object Security.Principal.WindowsPrincipal($identity)
+  if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
+    throw 'This script must run from an elevated (Run as Administrator) PowerShell.'
+  }
+}
+
+if ($Status) {
+  $task = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
+  if (-not $task) { Write-Host "Scheduled task '$TaskName' is not registered."; exit 1 }
+  $task | Select-Object TaskName, TaskPath, State | Format-List
+  Get-ScheduledTaskInfo -TaskName $TaskName |
+    Select-Object LastRunTime, LastTaskResult, NextRunTime, NumberOfMissedRuns | Format-List
+  exit 0
+}
+
+if ($Remove) {
+  Assert-Admin
+  if (Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue) {
+    Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
+    Write-Host "Removed scheduled task '$TaskName'."
+  } else {
+    Write-Host "Scheduled task '$TaskName' was not registered; nothing to remove."
+  }
+  exit 0
+}
+
+Assert-Admin
+if (-not (Test-Path $launcher)) { throw "Missing launcher: $launcher" }
+if (-not (Test-Path (Join-Path $root 'traffic-audit-server-1.0.0-SNAPSHOT.jar'))) {
+  Write-Host "WARN: jar not found in $root - the task will be registered, but startup will fail until the jar is there."
+}
+
+$action = New-ScheduledTaskAction -Execute 'cmd.exe' -Argument ('/c "' + $launcher + '"') -WorkingDirectory $root
+
+$trigger = New-ScheduledTaskTrigger -AtStartup
+if ($DelaySeconds -gt 0) { $trigger.Delay = 'PT' + $DelaySeconds + 'S' }
+
+$principal = New-ScheduledTaskPrincipal -UserId 'SYSTEM' -LogonType ServiceAccount -RunLevel Highest
+
+$settings = New-ScheduledTaskSettingsSet `
+  -AllowStartIfOnBatteries `
+  -DontStopIfGoingOnBatteries `
+  -StartWhenAvailable `
+  -MultipleInstances IgnoreNew `
+  -RestartCount 3 `
+  -RestartInterval (New-TimeSpan -Minutes 1) `
+  -ExecutionTimeLimit ([TimeSpan]::Zero)
+
+Register-ScheduledTask `
+  -TaskName    $TaskName `
+  -Action      $action `
+  -Trigger     $trigger `
+  -Principal   $principal `
+  -Settings    $settings `
+  -Description 'TrafficAudit backend auto-start at machine boot (script-managed).' `
+  -Force | Out-Null
+
+Write-Host "Registered scheduled task '$TaskName'."
+Write-Host "  Trigger  : At startup, +${DelaySeconds}s"
+Write-Host "  Principal: SYSTEM (highest privileges, no logon required)"
+Write-Host "  Launcher : $launcher"
+Write-Host "  Log      : $logFile"
+Write-Host ''
+Write-Host "Service address after boot: http://<server-ip>:8090"
+Write-Host "Manage it with: -Status | -StartNow | -Remove"
+
+if ($StartNow) {
+  Start-ScheduledTask -TaskName $TaskName
+  Write-Host "Started task '$TaskName' now."
+}

--
Gitblit v1.9.1