diff --git a/src/HikvisionAttendanceManager.App/Services/HrmsEmployeeService.cs b/src/HikvisionAttendanceManager.App/Services/HrmsEmployeeService.cs index 9ceb7a4..9643788 100644 --- a/src/HikvisionAttendanceManager.App/Services/HrmsEmployeeService.cs +++ b/src/HikvisionAttendanceManager.App/Services/HrmsEmployeeService.cs @@ -20,15 +20,38 @@ public sealed class HrmsEmployeeService public async Task> GetLocationSitesAsync(CancellationToken cancellationToken) { - const string sql = """ + const string inventoryTitleSql = """ SELECT DISTINCT e.location_site_id, COALESCE(ls.title, CONCAT('Site ', e.location_site_id)) AS site_title FROM hrms.employee e LEFT JOIN inventory.location_site ls ON ls.id = e.location_site_id - WHERE e.location_site_id IS NOT NULL + WHERE e.location_site_id IS NOT NULL AND e.location_site_id > 0 ORDER BY site_title """; - return await QuerySitesAsync(sql, null, cancellationToken); + try + { + var sites = await QuerySitesAsync(inventoryTitleSql, null, cancellationToken); + if (sites.Count > 0) + { + AppLogger.Info($"[HRMS] Loaded {sites.Count} location site(s) via inventory.location_site."); + return sites; + } + } + catch (Exception ex) + { + AppLogger.Warning("Location site load via inventory.location_site failed; falling back to employee-derived sites. " + ex.Message); + } + + const string employeeFallbackSql = """ + SELECT DISTINCT e.location_site_id, + CONCAT('Site ', e.location_site_id) AS site_title + FROM hrms.employee e + WHERE e.location_site_id IS NOT NULL AND e.location_site_id > 0 + ORDER BY e.location_site_id + """; + var fallbackSites = await QuerySitesAsync(employeeFallbackSql, null, cancellationToken); + AppLogger.Info($"[HRMS] Loaded {fallbackSites.Count} location site(s) from hrms.employee fallback."); + return fallbackSites; } public async Task> GetActiveDepartmentsBySiteAsync(long siteId, CancellationToken cancellationToken)