using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Threading.Tasks; using System.Web; /// /// RegionAndRoomAndBedSchedule 的摘要描述 /// namespace Model { public partial class RegionAndRoomAndBedSchedule { public enum SchedulePurpose { None = -1, // 無特定用途 // 區域相關 Region_Repair = 10, // 區域修繕 Region_Cleaning = 11, // 區域清潔 // 客房相關 Room_Reservation = 20, // 客房預訂 Room_Cleaning = 21, // 客房清潔 Room_Disinfection = 22, // 客房消毒 Room_Repair = 23, // 客房維修 // 床位相關 Bed_Reservation = 30, // 床位預訂 Bed_Cleaning = 31, // 床位清潔 Bed_Repair = 32, // 床位維修 Bed_Disinfection = 33, // 床位消毒 // 其他用途 Event_Use = 90, // 活動使用 Other = 99 // 其他 } public enum StatusEnum { None = 0, Normal = 1, Cancel = 2, } /// /// 查找指定时间段内可用床位(无排程占用) /// /// 数据库上下文 /// 查询开始时间 /// 查询结束时间 /// 可用床位列表 public static async Task> GetAvailableBedsAsync(ezEntities db, DateTime start, DateTime? end) { start = start.Date; end = end?.Date; // 找出在日期範圍內被占用的床位 Uuid(包括長期占用 ScheduleDate = null) var busyBedUuidsQuery = db.RegionAndRoomAndBedSchedule .Where(s => s.IsDeleted == false && !s.IsCancel && (s.ScheduleDate == null || (end.HasValue && s.ScheduleDate >= start && s.ScheduleDate <= end.Value))); var busyBedUuids = await busyBedUuidsQuery .Select(s => s.TargetUuid) .Distinct() .ToListAsync(); // 空閒床位 = 所有床位 - 忙碌床位 var availableBeds = await db.RegionRoomBed .Where(b => !busyBedUuids.Contains(b.Uuid)) .ToListAsync(); return availableBeds; } public static async Task IsBedAvailableAsync(ezEntities db, Guid targetUuid, DateTime start, DateTime? end) { // 如果 end 為 null,表示長期占用,直接判斷是否已有長期占用 //不包含结束时间那一天 if (end == null) { var hasLongTerm = await db.RegionAndRoomAndBedSchedule .AnyAsync(s => s.IsDeleted == false && !s.IsCancel && s.TargetUuid == targetUuid && s.ScheduleDate == null); return !hasLongTerm; } // 短期占用,查詢每日排程中有無衝突 var totalDays = (end.Value.Date - start.Date).Days; for (int i = 0; i < totalDays; i++) { var date = start.Date.AddDays(i); var conflict = await db.RegionAndRoomAndBedSchedule .Where(s => s.GuaDanOrderGuest.StatusCode != GuaDanOrderGuest.STATUS_CANCELLED) .Where(s => s.GuaDanOrderGuest.StatusCode != GuaDanOrderGuest.STATUS_CHECKED_OUT) .AnyAsync(s => s.IsDeleted == false && !s.IsCancel && s.TargetUuid == targetUuid && s.ScheduleDate == date); if (conflict) return false; } return true; } public static async Task GetAvailableBedCountsAsync(ezEntities db, DateTime start, DateTime end) { start = start.Date; end = end.Date; // 找出所有在日期範圍內被占用的床位 var busyBedUuids = await db.RegionAndRoomAndBedSchedule .Where(s => s.GuaDanOrderGuest.StatusCode != "403") .Where(s => s.GuaDanOrderGuest.StatusCode != "404") .Where(a => a.IsCancel == false) .Where(s => s.IsDeleted == false && (s.ScheduleDate == null // 長期占用 || (s.ScheduleDate >= start && s.ScheduleDate <= end))) .Select(s => s.TargetUuid) .Distinct() .ToListAsync(); // 可用床位 = 所有床位 - 忙碌床位 var availableBeds = db.RegionRoomBed .Where(b => !busyBedUuids.Contains(b.Uuid)); var result = await availableBeds .GroupBy(b => b.Gender) .Select(g => new { Gender = g.Key, Count = g.Count() }) .ToListAsync(); var male = result.Where(r => r.Gender == true).Select(r => r.Count).FirstOrDefault(); var female = result.Where(r => r.Gender == false).Select(r => r.Count).FirstOrDefault(); return new { male, female }; } } }