guadan
This commit is contained in:
@@ -8,6 +8,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Entity;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using System.Web.Http;
|
||||
@@ -66,7 +67,7 @@ public class regionRoomBedController : ApiController
|
||||
.ToList();
|
||||
|
||||
bool canUsed = !bedSchedules.Any();
|
||||
|
||||
bool bedIsStop = IsBedStopped(a);
|
||||
return new
|
||||
{
|
||||
a.Uuid,
|
||||
@@ -76,6 +77,7 @@ public class regionRoomBedController : ApiController
|
||||
a.StatusCode,
|
||||
a.RoomUuid,
|
||||
canUsed,
|
||||
bedIsStop,
|
||||
schedule = bedSchedules
|
||||
};
|
||||
});
|
||||
@@ -83,6 +85,33 @@ public class regionRoomBedController : ApiController
|
||||
|
||||
return Ok(data);
|
||||
}
|
||||
public bool IsBedStopped(RegionRoomBed bed)
|
||||
{
|
||||
// 1️⃣ 床位本身不可用
|
||||
if (!bed.IsActive || bed.IsDeleted)
|
||||
return true;
|
||||
|
||||
// 2️⃣ 所属房间不可用
|
||||
var room = bed.Room;
|
||||
if (room == null || !room.IsActive.Value || room.IsDeleted)
|
||||
return true;
|
||||
|
||||
// 3️⃣ 所属区域不可用
|
||||
var region = room.Region;
|
||||
while (region != null)
|
||||
{
|
||||
if (!region.IsActive || region.IsDeleted)
|
||||
return true; // 有任意一级区域不可用就返回 true
|
||||
|
||||
if (!region.ParentUuid.HasValue)
|
||||
break; // 到顶层了
|
||||
|
||||
region = _db.Region.FirstOrDefault(r => r.Uuid == region.ParentUuid.Value);
|
||||
}
|
||||
|
||||
// 4️⃣ 全部检查通过 → 床位可用
|
||||
return false;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("api/region/bed/create")]
|
||||
@@ -147,6 +176,20 @@ public class regionRoomBedController : ApiController
|
||||
{
|
||||
return BadRequest("床為性別和房間性別必須一致");
|
||||
}
|
||||
if (bed.IsActive == false)
|
||||
{
|
||||
var hasPendingBeds = oldBed.GuaDanOrderGuest.Any(g => !g.IsDeleted &&
|
||||
(g.RegionRoomBedStatus.Code == GuaDanOrderGuest.STATUS_BOOKED || // 预约中
|
||||
g.RegionRoomBedStatus.Code == GuaDanOrderGuest.STATUS_CHECKED_IN));
|
||||
if (hasPendingBeds)
|
||||
{
|
||||
return Content(HttpStatusCode.BadRequest, new
|
||||
{
|
||||
code = "BED_IS_USED",
|
||||
message = "該床位正在掛單中,請先處理"
|
||||
});
|
||||
}
|
||||
}
|
||||
oldBed.StatusCode = bed.StatusCode;
|
||||
oldBed.IsActive = bed.IsActive;
|
||||
oldBed.Name = bed.Name;
|
||||
@@ -158,18 +201,38 @@ public class regionRoomBedController : ApiController
|
||||
}
|
||||
[HttpPost]
|
||||
[Route("api/region/bed/delete")]
|
||||
public IHttpActionResult delete([FromUri] Guid uuid)
|
||||
public IHttpActionResult Delete([FromUri] Guid uuid)
|
||||
{
|
||||
var bed = _db.RegionRoomBed.Find(uuid);
|
||||
if (bed == null)
|
||||
{
|
||||
return BadRequest("未找到床位");
|
||||
}
|
||||
_db.RegionRoomBed.Remove(bed);
|
||||
_db.SaveChanges();
|
||||
return Ok(new { message = "刪除成功" });
|
||||
|
||||
try
|
||||
{
|
||||
_db.RegionRoomBed.Remove(bed);
|
||||
_db.SaveChanges();
|
||||
return Ok(new { message = "刪除成功" });
|
||||
}
|
||||
catch (System.Data.Entity.Infrastructure.DbUpdateException ex)
|
||||
{
|
||||
// 判斷是否為外鍵關聯錯誤
|
||||
if (ex.InnerException?.InnerException is System.Data.SqlClient.SqlException sqlEx &&
|
||||
(sqlEx.Number == 547)) // 547 = SQL Server 外鍵違反錯誤碼
|
||||
{
|
||||
return BadRequest("刪除失敗:該床位已被使用或存在關聯資料,無法刪除。");
|
||||
}
|
||||
|
||||
return InternalServerError(ex); // 其他資料庫錯誤
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return InternalServerError(ex); // 其他未預期錯誤
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[HttpGet]
|
||||
[Route("api/region/bed/getavailablebedcountbytime")]
|
||||
public async Task<IHttpActionResult> GetCanUseBedCountByTime(DateTime startTime, DateTime endTime)
|
||||
@@ -192,6 +255,7 @@ public class regionRoomBedController : ApiController
|
||||
|
||||
// 可用床位 = 所有床位 - 忙碌床位
|
||||
var availableBeds = _db.RegionRoomBed
|
||||
.Where(b => b.IsActive)
|
||||
.Where(b => !busyBedUuids.Contains(b.Uuid));
|
||||
|
||||
var result = await availableBeds
|
||||
|
||||
Reference in New Issue
Block a user