USE [17168erp_t2]
GO
/****** Object:  Table [dbo].[AncestralTabletArea]    Script Date: 2025/10/29 下午 01:32:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[AncestralTabletArea](
	[AreaId] [int] IDENTITY(1,1) NOT NULL,
	[AreaName] [nvarchar](10) NOT NULL,
	[AreaCode] [nvarchar](20) NOT NULL,
	[ParentAreaId] [int] NULL,
	[AreaType] [nvarchar](10) NULL,
	[Price] [int] NULL,
	[SortOrder] [int] NULL,
	[IsDisabled] [bit] NOT NULL,
	[Description] [nvarchar](200) NULL,
PRIMARY KEY CLUSTERED 
(
	[AreaId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[AncestralTabletPosition]    Script Date: 2025/10/29 下午 01:32:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[AncestralTabletPosition](
	[PositionId] [int] IDENTITY(1,1) NOT NULL,
	[AreaId] [int] NOT NULL,
	[PositionCode] [nvarchar](20) NOT NULL,
	[PositionName] [nvarchar](50) NULL,
	[Price] [int] NULL,
	[StatusCode] [nvarchar](20) NULL,
	[Description] [nvarchar](200) NULL,
	[RowNo] [int] NULL,
	[ColumnNo] [int] NULL,
PRIMARY KEY CLUSTERED 
(
	[PositionId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY],
 CONSTRAINT [UQ_Position_Area_Code] UNIQUE NONCLUSTERED 
(
	[AreaId] ASC,
	[PositionCode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[AncestralTabletPositionRecord]    Script Date: 2025/10/29 下午 01:32:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[AncestralTabletPositionRecord](
	[RecordId] [int] IDENTITY(1,1) NOT NULL,
	[RegistrantCode] [nvarchar](20) NOT NULL,
	[NPTitle] [nvarchar](30) NULL,
	[NPStandDate] [date] NOT NULL,
	[NPYangShang] [nvarchar](20) NULL,
	[WPContent] [nvarchar](1000) NULL,
	[CreatedAt] [datetime] NOT NULL,
	[UpdatedAt] [datetime] NULL,
PRIMARY KEY CLUSTERED 
(
	[RecordId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[AncestralTabletRegistrant]    Script Date: 2025/10/29 下午 01:32:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[AncestralTabletRegistrant](
	[RegistrantCode] [nvarchar](20) NOT NULL,
	[Name] [nvarchar](50) NOT NULL,
	[Phone] [nvarchar](50) NULL,
	[Address] [nvarchar](60) NULL,
	[RegisterDate] [date] NOT NULL,
	[Price] [int] NULL,
	[PositionId] [int] NULL,
	[StartDate] [date] NOT NULL,
	[EndDate] [date] NULL,
	[IsLongTerm] [bit] NOT NULL,
	[IsActive] [bit] NOT NULL,
	[CreatedAt] [datetime] NOT NULL,
	[UpdatedAt] [datetime] NULL,
	[IsEnd] [bit] NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[RegistrantCode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[AncestralTabletStatus]    Script Date: 2025/10/29 下午 01:32:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[AncestralTabletStatus](
	[StatusCode] [nvarchar](20) NOT NULL,
	[StatusName] [nvarchar](20) NOT NULL,
	[StatusType] [nvarchar](20) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[StatusCode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[AncestralTabletArea] ADD  DEFAULT ((0)) FOR [IsDisabled]
GO
ALTER TABLE [dbo].[AncestralTabletPositionRecord] ADD  DEFAULT (getdate()) FOR [CreatedAt]
GO
ALTER TABLE [dbo].[AncestralTabletRegistrant] ADD  DEFAULT ((0)) FOR [IsLongTerm]
GO
ALTER TABLE [dbo].[AncestralTabletRegistrant] ADD  DEFAULT ((1)) FOR [IsActive]
GO
ALTER TABLE [dbo].[AncestralTabletRegistrant] ADD  DEFAULT (getdate()) FOR [CreatedAt]
GO
ALTER TABLE [dbo].[AncestralTabletRegistrant] ADD  DEFAULT ((0)) FOR [IsEnd]
GO
ALTER TABLE [dbo].[AncestralTabletArea]  WITH CHECK ADD  CONSTRAINT [FK_AncestralTabletArea_Parent] FOREIGN KEY([ParentAreaId])
REFERENCES [dbo].[AncestralTabletArea] ([AreaId])
GO
ALTER TABLE [dbo].[AncestralTabletArea] CHECK CONSTRAINT [FK_AncestralTabletArea_Parent]
GO
ALTER TABLE [dbo].[AncestralTabletPosition]  WITH CHECK ADD  CONSTRAINT [FK_Position_Area] FOREIGN KEY([AreaId])
REFERENCES [dbo].[AncestralTabletArea] ([AreaId])
GO
ALTER TABLE [dbo].[AncestralTabletPosition] CHECK CONSTRAINT [FK_Position_Area]
GO
ALTER TABLE [dbo].[AncestralTabletPosition]  WITH CHECK ADD  CONSTRAINT [FK_Position_Status] FOREIGN KEY([StatusCode])
REFERENCES [dbo].[AncestralTabletStatus] ([StatusCode])
GO
ALTER TABLE [dbo].[AncestralTabletPosition] CHECK CONSTRAINT [FK_Position_Status]
GO
ALTER TABLE [dbo].[AncestralTabletPositionRecord]  WITH CHECK ADD FOREIGN KEY([RegistrantCode])
REFERENCES [dbo].[AncestralTabletRegistrant] ([RegistrantCode])
GO
ALTER TABLE [dbo].[AncestralTabletRegistrant]  WITH CHECK ADD  CONSTRAINT [FK_Registrant_Position] FOREIGN KEY([PositionId])
REFERENCES [dbo].[AncestralTabletPosition] ([PositionId])
GO
ALTER TABLE [dbo].[AncestralTabletRegistrant] CHECK CONSTRAINT [FK_Registrant_Position]
GO

INSERT [dbo].[AncestralTabletStatus] ([StatusCode], [StatusName], [StatusType]) VALUES (N'available', N'可用', N'Position')
INSERT [dbo].[AncestralTabletStatus] ([StatusCode], [StatusName], [StatusType]) VALUES (N'maintenance', N'維護中', N'Position')
INSERT [dbo].[AncestralTabletStatus] ([StatusCode], [StatusName], [StatusType]) VALUES (N'used', N'已使用', N'Position')
GO
-- 1. 如果不存在 OrderUuid 栏位，则新增
IF NOT EXISTS (
    SELECT 1
    FROM sys.columns
    WHERE Name = N'OrderUuid'
      AND Object_ID = Object_ID(N'dbo.GuaDanOrderGuest')
)
BEGIN
    ALTER TABLE [dbo].[GuaDanOrderGuest]
    ADD [OrderUuid] UNIQUEIDENTIFIER NULL;
END
GO

-- 2. 如果不存在 FK_GuaDanOrderGuest_Order 外键，则新增
IF NOT EXISTS (
    SELECT 1
    FROM sys.foreign_keys
    WHERE Name = N'FK_GuaDanOrderGuest_Order'
      AND parent_object_id = OBJECT_ID(N'dbo.GuaDanOrderGuest')
)
BEGIN
    ALTER TABLE [dbo].[GuaDanOrderGuest]
    ADD CONSTRAINT [FK_GuaDanOrderGuest_Order]
    FOREIGN KEY ([OrderUuid]) REFERENCES [dbo].[GuaDanOrder] ([Uuid]);
END
GO
