guadan
This commit is contained in:
184
web/App_Code/api/HandleBedInUsedController.cs
Normal file
184
web/App_Code/api/HandleBedInUsedController.cs
Normal file
@@ -0,0 +1,184 @@
|
||||
using Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Http;
|
||||
using System.Web.Http.Results;
|
||||
using static GuaDanStatusCode;
|
||||
|
||||
/// <summary>
|
||||
/// HandleBedInUsedController 的摘要描述
|
||||
/// </summary>
|
||||
public class HandleBedInUsedController : ApiController
|
||||
{
|
||||
private Model.ezEntities _db = new Model.ezEntities();
|
||||
public HandleBedInUsedController()
|
||||
{
|
||||
//
|
||||
// TODO: 在這裡新增建構函式邏輯
|
||||
//
|
||||
}
|
||||
[HttpPost]
|
||||
[Route("api/bed/inuse/list")]
|
||||
public IHttpActionResult Get([FromBody] UuidModel uuidModel)
|
||||
{
|
||||
//獲取已被預約或者正在入住的床位,如果有指定就會查詢指定條件,如果沒有指定就會返回所有
|
||||
var query = _db.GuaDanOrderGuest
|
||||
.Where(gd => gd.StatusCode == GuaDanStatusCode.Guadan.CheckedIn
|
||||
|| gd.StatusCode == GuaDanStatusCode.Guadan.Booked).ToList();
|
||||
if (uuidModel.bedUuid.HasValue)
|
||||
{
|
||||
// 優先按床位查詢
|
||||
query = query.Where(g => g.BedUuid == uuidModel.bedUuid.Value).ToList();
|
||||
}
|
||||
else if (uuidModel.roomUuid.HasValue)
|
||||
{
|
||||
// 如果沒有 bed,但有 room
|
||||
query = query.Where(g => g.RoomUuid == uuidModel.roomUuid.Value).ToList();
|
||||
}
|
||||
else if (uuidModel.regionUuid.HasValue)
|
||||
{
|
||||
// 如果只有 region
|
||||
//query = query.Where(g => g.Room.RegionUuid == uuidModel.regionUuid.Value);
|
||||
query = query.Where(g => IsRegionOrAncestor(g.Room, uuidModel.regionUuid.Value)).ToList();
|
||||
|
||||
}
|
||||
var data = query.Select(g => new
|
||||
{
|
||||
g.BedUuid,
|
||||
g.RoomUuid,
|
||||
g.Room.RegionUuid,
|
||||
g.GuaDanOrderNo,
|
||||
g.RegionRoomBed.Name,
|
||||
fullName = GetFullBedName(g.BedUuid.Value),
|
||||
g.followers.u_name,
|
||||
guadan_during = new { g.CheckInAt, g.CheckOutAt },
|
||||
status = new { g.StatusCode, g.RegionRoomBedStatus.Name }
|
||||
});
|
||||
return Ok(data.ToList());
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("api/bed/inuse/cancel/singlebed/booking")]
|
||||
public IHttpActionResult CancelSingleBedBooking([FromBody] UuidModel uuidModel)
|
||||
{
|
||||
if (uuidModel?.bedUuid == null)
|
||||
return BadRequest("床位ID不能為空");
|
||||
|
||||
using (var transaction = _db.Database.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
// 查詢符合條件的訂單
|
||||
var orders = _db.GuaDanOrderGuest
|
||||
.Where(g => g.BedUuid == uuidModel.bedUuid)
|
||||
.Where(g => g.StatusCode == GuaDanStatusCode.Guadan.Booked || g.StatusCode == GuaDanStatusCode.Guadan.CheckedIn)
|
||||
.ToList();
|
||||
|
||||
if (!orders.Any())
|
||||
return NotFound();
|
||||
|
||||
// 更新狀態
|
||||
foreach (var order in orders)
|
||||
{
|
||||
if (!StatusTransitionManager.CanTransition(order.StatusCode, GuaDanStatusCode.Guadan.Cancelled))
|
||||
{
|
||||
return BadRequest("當前狀態不能被取消");
|
||||
}
|
||||
|
||||
|
||||
order.StatusCode = GuaDanStatusCode.Guadan.Cancelled; // 假設Cancelled是取消狀態
|
||||
}
|
||||
var schedules = _db.RegionAndRoomAndBedSchedule
|
||||
.Where(s => s.TargetUuid == uuidModel.bedUuid)
|
||||
.Where(s => s.GuaDanOrderGuest.StatusCode == GuaDanStatusCode.Guadan.Booked
|
||||
|| s.GuaDanOrderGuest.StatusCode == GuaDanStatusCode.Guadan.CheckedIn)
|
||||
.ToList();
|
||||
foreach (var schedule in schedules)
|
||||
{
|
||||
schedule.IsCancel = true;
|
||||
}
|
||||
_db.SaveChanges();
|
||||
transaction.Commit();
|
||||
|
||||
return Ok(new { message = "取消成功", cancelledCount = orders.Count });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
transaction.Rollback();
|
||||
return InternalServerError(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("api/bed/inuse/region/list")]
|
||||
public IHttpActionResult GetRegionList()
|
||||
{
|
||||
var regions = _db.Region
|
||||
.Select(r => new
|
||||
{
|
||||
r.Uuid,
|
||||
r.Name,
|
||||
})
|
||||
.ToList();
|
||||
return Ok(regions);
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("api/bed/inuse/room/list")]
|
||||
public IHttpActionResult GetRoomList([FromUri] Guid? regionUuid = null)
|
||||
{
|
||||
var room = _db.Room.Where(r => !r.IsDeleted && r.IsActive.Value).ToList();
|
||||
if (regionUuid != null)
|
||||
{
|
||||
room = room.Where(r => IsRegionOrAncestor(r, regionUuid.Value)).ToList();
|
||||
}
|
||||
var data = room.Select(r => new
|
||||
{
|
||||
r.Uuid,
|
||||
r.Name,
|
||||
fullName = r.Region.Name + "/" + r.Name,
|
||||
}).ToList();
|
||||
return Ok(data);
|
||||
}
|
||||
public string GetFullBedName(Guid bedUuid)
|
||||
{
|
||||
var bed = _db.RegionRoomBed.Find(bedUuid);
|
||||
if (bed == null)
|
||||
return "";
|
||||
var name = bed.Name;
|
||||
var room = bed.Room;
|
||||
if (room == null)
|
||||
return name;
|
||||
name = room.Name + "/" + name;
|
||||
|
||||
var region = room?.Region;
|
||||
while (region != null)
|
||||
{
|
||||
name = region.Name + "/" + name;
|
||||
region = region.Region2; // 遞迴向上
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
bool IsRegionOrAncestor(Model.Room room, Guid regionUuid)
|
||||
{
|
||||
//判斷傳入的regionuuid是否是room的祖先
|
||||
if (room.RegionUuid == regionUuid)
|
||||
return true;
|
||||
var region = room.Region;
|
||||
while (region != null)
|
||||
{
|
||||
if (region.Uuid == regionUuid) return true;
|
||||
region = region.Region2;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public class UuidModel
|
||||
{
|
||||
public Guid? regionUuid = null;
|
||||
public Guid? roomUuid = null;
|
||||
public Guid? bedUuid = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user