掛單管理

This commit is contained in:
minom committed 2026-11-06 18:06:50 +08:00
1 parent 8db6019ba3
commit 9e251fc7d4
12 files changed
+2197 -116

No files matched your search

+80 -11
View File
@@ -195,6 +195,7 @@ public class regionRoomBedController : ApiController
oldBed.IsActive = bed.IsActive;
oldBed.Name = bed.Name;
oldBed.Gender = bed.Gender;
oldBed.Limits = bed.Limits;
_db.SaveChanges();
//創建床位
@@ -294,18 +295,86 @@ public class regionRoomBedController : ApiController
.Distinct()
.ToList();
// 空閒床位 = 所有床位 - 忙碌床位
var availableBeds = _db.RegionRoomBed.Select(b=>new {
b.Name ,b.Uuid,b.StatusCode,b.IsActive,
b.RoomUuid,
Room=_db.Room.Where(r=>r.Uuid==b.RoomUuid).
Select(r=>new { r.Name,r.RegionUuid,
RegionName=_db.Region.Where(g=>g.Uuid==r.RegionUuid).Select(g=>g.Name).FirstOrDefault()}).FirstOrDefault(),
})
.Where(b => !busyBedUuids.Contains(b.Uuid))
.ToList();
// 1. 先把全台/全區的 Region 載入記憶體做成字典,方便 GetParentPath 快速查找
var regionDict = _db.Region.ToDictionary(r => r.Uuid);
// 2. 修改你的 GetParentPath 邏輯(改用高效的 while 迴圈)
List<string> GetRegionPathNames(Guid? startRegionUuid)
{
var pathNames = new List<string>();
Guid? nextId = startRegionUuid;
while (nextId.HasValue && regionDict.TryGetValue(nextId.Value, out var reg))
{
pathNames.Add(reg.Name); // 收集區域名稱
nextId = reg.ParentUuid; // 假設你的欄位叫 ParentUuid,請依實際欄位修改
}
// 反轉順序讓它變成:[大區域, 中區域, 小區域]
pathNames.Reverse();
return pathNames;
}
// 3. 步驟 A:純資料庫查詢 (只撈取需要的資料,不呼叫 C# 函式)
var dbBeds = _db.RegionRoomBed
.Where(b => !busyBedUuids.Contains(b.Uuid)) // 建議過濾條件放在 ToList 之前,減少傳輸資料量
.Select(b => new
{
b.Name,
b.Uuid,
b.StatusCode,
b.IsActive,
b.RoomUuid,
b.Limits,
// 直接在 SQL 層級把 Room 撈出來
Room = _db.Room
.Where(r => r.Uuid == b.RoomUuid)
.Select(r => new
{
r.Name,
r.RegionUuid
})
.FirstOrDefault()
})
.AsEnumerable(); // 💡 關鍵:從這裡開始切換到記憶體(LINQ to Objects)
// 4. 步驟 B:在記憶體中加工,呼叫 GetRegionPathNames
var availableBeds = dbBeds.Select(b => new
{
b.Name,
b.Uuid,
b.StatusCode,
b.IsActive,
b.RoomUuid,
b.Limits,
// 💡 此時已經在記憶體中,可以安全地呼叫 C# 函式
RegionPath = GetRegionPathNames(b.Room?.RegionUuid),
Room = b.Room == null ? null : new
{
b.Room.Name,
b.Room.RegionUuid,
// 從你已經載入的字典中直接抓 RegionName,省去在 SQL 裡面重複 Join Region 表
// 💡 修正關鍵:用 is 語法自動拆箱並安全檢查,完美避開 ?? 的型態限制
RegionName = b.Room.RegionUuid is Guid rId && regionDict.TryGetValue(rId, out var reg)
? reg.Name
: null
}
}).ToList();
// 空閒床位 = 所有床位 - 忙碌床位
//var availableBeds = _db.RegionRoomBed.Select(b=>new {
// b.Name ,b.Uuid,b.StatusCode,b.IsActive,
// b.RoomUuid,b.Limits,
// resion = GetRegionPathNames(b.RoomUuid),
// Room =_db.Room.Where(r=>r.Uuid==b.RoomUuid).
// Select(r=>new { r.Name,r.RegionUuid,
// RegionName=_db.Region.Where(g=>g.Uuid==r.RegionUuid).
// Select(g=>g.Name).FirstOrDefault()}).FirstOrDefault(),
//})
// .Where(b => !busyBedUuids.Contains(b.Uuid))
// .ToList();
return Ok(new { availableBeds });
}