-- =============================================
-- 预约系统按时段优化 - 数据库迁移脚本
-- 创建时间: 2025-12-06
-- 说明: 新增房间时段价格表，修改现有表结构
-- =============================================

USE [您的数据库名称]
GO

-- =============================================
-- 1. 创建房间时段价格表 SQRoomPricing
-- =============================================
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SQRoomPricing]') AND type in (N'U'))
BEGIN
    CREATE TABLE [dbo].[SQRoomPricing](
        [id] [int] IDENTITY(1,1) NOT NULL,
        [room_id] [int] NOT NULL,
        [time_slot_type] [int] NOT NULL,
        [standard_price] [decimal](10, 2) NOT NULL,
        [member_price] [decimal](10, 2) NOT NULL,
        [price_desc_standard] [nvarchar](100) NULL,
        [price_desc_member] [nvarchar](100) NULL,
        [effective_date_start] [date] NULL,
        [effective_date_end] [date] NULL,
        [is_active] [bit] NOT NULL DEFAULT 1,
        [created_at] [datetime] NOT NULL DEFAULT GETDATE(),
        [updated_at] [datetime] NOT NULL DEFAULT GETDATE(),
        CONSTRAINT [PK_SQRoomPricing] PRIMARY KEY CLUSTERED ([id] ASC)
    )
    
    -- 创建索引
    CREATE NONCLUSTERED INDEX [IX_SQRoomPricing_RoomSlot] ON [dbo].[SQRoomPricing]
    (
        [room_id] ASC,
        [time_slot_type] ASC
    )
    
    CREATE NONCLUSTERED INDEX [IX_SQRoomPricing_EffectiveDate] ON [dbo].[SQRoomPricing]
    (
        [effective_date_start] ASC,
        [effective_date_end] ASC
    )
    
    PRINT '✓ 表 SQRoomPricing 创建成功'
END
ELSE
BEGIN
    PRINT '× 表 SQRoomPricing 已存在'
END
GO

-- =============================================
-- 2. 修改 SQRooms 表，添加新字段
-- =============================================

-- 添加 room_type_name 字段
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[SQRooms]') AND name = 'room_type_name')
BEGIN
    ALTER TABLE [dbo].[SQRooms] ADD [room_type_name] [nvarchar](50) NULL
    PRINT '✓ SQRooms.room_type_name 字段添加成功'
END
ELSE
BEGIN
    PRINT '× SQRooms.room_type_name 字段已存在'
END
GO

-- 添加 sort_order 字段
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[SQRooms]') AND name = 'sort_order')
BEGIN
    ALTER TABLE [dbo].[SQRooms] ADD [sort_order] [int] NULL DEFAULT 0
    PRINT '✓ SQRooms.sort_order 字段添加成功'
END
ELSE
BEGIN
    PRINT '× SQRooms.sort_order 字段已存在'
END
GO

-- =============================================
-- 3. 修改 SQRoomUnavailableTimes 表，添加新字段
-- =============================================

-- 添加 time_slot_type 字段
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[SQRoomUnavailableTimes]') AND name = 'time_slot_type')
BEGIN
    ALTER TABLE [dbo].[SQRoomUnavailableTimes] ADD [time_slot_type] [int] NULL
    PRINT '✓ SQRoomUnavailableTimes.time_slot_type 字段添加成功'
END
ELSE
BEGIN
    PRINT '× SQRoomUnavailableTimes.time_slot_type 字段已存在'
END
GO

-- 添加 created_by 字段
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[SQRoomUnavailableTimes]') AND name = 'created_by')
BEGIN
    ALTER TABLE [dbo].[SQRoomUnavailableTimes] ADD [created_by] [int] NULL
    PRINT '✓ SQRoomUnavailableTimes.created_by 字段添加成功'
END
ELSE
BEGIN
    PRINT '× SQRoomUnavailableTimes.created_by 字段已存在'
END
GO

-- =============================================
-- 4. 修改 SQReservations 表，添加新字段
-- =============================================

-- 添加 time_slot_type 字段
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[SQReservations]') AND name = 'time_slot_type')
BEGIN
    ALTER TABLE [dbo].[SQReservations] ADD [time_slot_type] [int] NULL
    PRINT '✓ SQReservations.time_slot_type 字段添加成功'
END
ELSE
BEGIN
    PRINT '× SQReservations.time_slot_type 字段已存在'
END
GO

-- 添加 latest_arrival_time 字段
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[SQReservations]') AND name = 'latest_arrival_time')
BEGIN
    ALTER TABLE [dbo].[SQReservations] ADD [latest_arrival_time] [datetime] NULL
    PRINT '✓ SQReservations.latest_arrival_time 字段添加成功'
END
ELSE
BEGIN
    PRINT '× SQReservations.latest_arrival_time 字段已存在'
END
GO

-- 添加 is_solo_mode 字段
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[SQReservations]') AND name = 'is_solo_mode')
BEGIN
    ALTER TABLE [dbo].[SQReservations] ADD [is_solo_mode] [bit] NOT NULL DEFAULT 0
    PRINT '✓ SQReservations.is_solo_mode 字段添加成功'
END
ELSE
BEGIN
    PRINT '× SQReservations.is_solo_mode 字段已存在'
END
GO

-- 添加 actual_price 字段
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[dbo].[SQReservations]') AND name = 'actual_price')
BEGIN
    ALTER TABLE [dbo].[SQReservations] ADD [actual_price] [decimal](10, 2) NULL
    PRINT '✓ SQReservations.actual_price 字段添加成功'
END
ELSE
BEGIN
    PRINT '× SQReservations.actual_price 字段已存在'
END
GO

-- =============================================
-- 5. 数据初始化：为现有房间创建默认价格配置
-- =============================================

PRINT ''
PRINT '开始初始化房间价格数据...'
GO

-- 为每个房间创建4个时段的默认价格（如果还没有的话）
INSERT INTO [dbo].[SQRoomPricing] 
    ([room_id], [time_slot_type], [standard_price], [member_price], [price_desc_standard], [price_desc_member], [is_active], [created_at], [updated_at])
SELECT 
    r.id as room_id,
    slots.time_slot_type,
    r.price_per_hour * 6 as standard_price,  -- 6小时时段
    r.price_per_hour * 6 * 0.8 as member_price,  -- 会员价8折
    CAST(CAST(r.price_per_hour * 6 AS INT) AS NVARCHAR) + '元/时段' as price_desc_standard,
    CAST(CAST(r.price_per_hour * 6 * 0.8 AS INT) AS NVARCHAR) + '元/时段' as price_desc_member,
    1 as is_active,
    GETDATE() as created_at,
    GETDATE() as updated_at
FROM [dbo].[SQRooms] r
CROSS JOIN (
    SELECT 0 as time_slot_type UNION ALL  -- 凌晨
    SELECT 1 UNION ALL                     -- 上午
    SELECT 2 UNION ALL                     -- 下午
    SELECT 3                               -- 晚上
) slots
WHERE NOT EXISTS (
    SELECT 1 FROM [dbo].[SQRoomPricing] p 
    WHERE p.room_id = r.id 
    AND p.time_slot_type = slots.time_slot_type
    AND p.effective_date_start IS NULL
    AND p.effective_date_end IS NULL
)

PRINT '✓ 房间价格数据初始化完成'
GO

-- =============================================
-- 6. 数据迁移：为历史预约记录填充 time_slot_type
-- =============================================

PRINT ''
PRINT '开始迁移历史预约数据...'
GO

UPDATE [dbo].[SQReservations]
SET [time_slot_type] = 
    CASE 
        WHEN DATEPART(HOUR, start_time) >= 0 AND DATEPART(HOUR, start_time) < 6 THEN 0   -- 凌晨
        WHEN DATEPART(HOUR, start_time) >= 6 AND DATEPART(HOUR, start_time) < 12 THEN 1  -- 上午
        WHEN DATEPART(HOUR, start_time) >= 12 AND DATEPART(HOUR, start_time) < 18 THEN 2 -- 下午
        ELSE 3  -- 晚上
    END
WHERE [time_slot_type] IS NULL

PRINT '✓ 历史预约数据迁移完成'
GO

-- =============================================
-- 7. 更新 room_type_name 字段（如果 room_type 有值）
-- =============================================

UPDATE [dbo].[SQRooms]
SET [room_type_name] = [room_type]
WHERE [room_type] IS NOT NULL AND [room_type_name] IS NULL

PRINT '✓ room_type_name 数据填充完成'
GO

PRINT ''
PRINT '========================================='
PRINT '数据库迁移脚本执行完成！'
PRINT '========================================='
PRINT ''
PRINT '说明：'
PRINT '1. 时段类型定义：'
PRINT '   0 = 凌晨 (00:00-05:59)'
PRINT '   1 = 上午 (06:00-11:59)'
PRINT '   2 = 下午 (12:00-17:59)'
PRINT '   3 = 晚上 (18:00-23:59)'
PRINT ''
PRINT '2. 默认价格策略：'
PRINT '   标准价 = 原price_per_hour × 6'
PRINT '   会员价 = 标准价 × 0.8'
PRINT ''
PRINT '3. 请在后台管理界面调整各时段的实际价格'
PRINT '========================================='
GO

