-- 场图查询机信息表（PLS_MapTerminal）
if not exists (select * from dbo.sysobjects where name = 'PLS_MapTerminal' and xtype = 'U')
begin
CREATE TABLE PLS_MapTerminal(
	[AutoID] [bigint] IDENTITY(1,1) NOT NULL, --自动编号
	TerminalNo varchar(20) NOT NULL, --查询机编号
	MapNo varchar(20) NOT NULL, --场图编号
	X int NOT NULL, --场图X坐标
	Y int NOT NULL, --场图Y坐标
 CONSTRAINT [PK_PLS_MapTerminal] PRIMARY KEY CLUSTERED 
(
	TerminalNo ASC,
	MapNo ASC
)
)

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[PLS_Terminal]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
begin
insert into PLS_MapTerminal(TerminalNo,MapNo,X,Y) select TerminalNo,MapNo,X,Y from PLS_Terminal
end

end
GO


-- 电梯信息表（PLS_Elevator）
if not exists (select * from dbo.sysobjects where name = 'PLS_Elevator' and xtype = 'U')
CREATE TABLE PLS_Elevator(
	ElevatorNo varchar(20) NOT NULL, --电梯编号
	FullName varchar(20) NOT NULL, --电梯名称
	StartOpenTime varchar(5) NOT NULL, --开放时间头 格式为HH:MM
	EndOpenTime varchar(5) NOT NULL, --开放时间尾 格式为HH:MM
	ValidFloors varchar(100) NOT NULL, --可达楼层 以空格分隔的楼层编号列表
 CONSTRAINT [PK_PLS_Elevator] PRIMARY KEY CLUSTERED 
(
	ElevatorNo ASC
)
)
GO

-- 场图电梯信息表（PLS_MapElevator）
if not exists (select * from dbo.sysobjects where name = 'PLS_MapElevator' and xtype = 'U')
CREATE TABLE PLS_MapElevator(
	AutoID int IDENTITY(1,1) NOT NULL, --自动编号
	ElevatorNo varchar(20) NOT NULL, --电梯编号
	MapNo varchar(20) NOT NULL, --场图编号
	X int NOT NULL, --场图X坐标
	Y int NOT NULL, --场图Y坐标
	ButtonName varchar(20) NULL, --电梯按钮名称（以楼层按钮命名）
 CONSTRAINT [PK_PLS_MapElevator] PRIMARY KEY CLUSTERED 
(
	ElevatorNo ASC,
	MapNo ASC
)
)
GO

-- 查询机到电梯表(PLS_TerminalToElevator)
if not exists (select * from dbo.sysobjects where name = 'PLS_TerminalToElevator' and xtype = 'U')
CREATE TABLE PLS_TerminalToElevator(
	PathID int IDENTITY(1,1) NOT NULL, --自动编号
	TerminalID varchar(20) NOT NULL, --查询机编号
	ElevatorNo varchar(20) NOT NULL, --电梯编号
	LinePath nvarchar(500) NOT NULL --路径
 CONSTRAINT [PK_PLS_TerminalToElevator] PRIMARY KEY CLUSTERED 
(
	PathID ASC
)
)
GO

-- 电梯到车位(PLS_ElevatorToParkLot)
if not exists (select * from dbo.sysobjects where name = 'PLS_ElevatorToParkLot' and xtype = 'U')
CREATE TABLE PLS_ElevatorToParkLot(
	PathID int IDENTITY(1,1) NOT NULL, --自动编号
	ElevatorNo varchar(20) NOT NULL, --电梯编号
	MapNo varchar(20) NOT NULL, --场图编号
	LotNoID varchar(20) NOT NULL, --车位
	LinePath nvarchar(500) NOT NULL --路径
 CONSTRAINT PK_PLS_ElevatorToParkLot PRIMARY KEY CLUSTERED 
(
	PathID ASC
)
)
GO

-- 附属查询机到车位(PLS_ExTerminalToParkLot)
if not exists (select * from dbo.sysobjects where name = 'PLS_ExTerminalToParkLot' and xtype = 'U')
CREATE TABLE PLS_ExTerminalToParkLot(
	PathID int IDENTITY(1,1) NOT NULL, --自动编号
	TerminalNo varchar(20) NOT NULL, --查询机编号
	LotNoID varchar(20) NOT NULL, --车位
	LinePath nvarchar(500) NOT NULL --路径
 CONSTRAINT PK_PLS_ExTerminalToParkLot PRIMARY KEY CLUSTERED 
(
	PathID ASC
)
)
GO

-- 电梯到电梯(PLS_ElevatorToElevator)
if not exists (select * from dbo.sysobjects where name = 'PLS_ElevatorToElevator' and xtype = 'U')
CREATE TABLE PLS_ElevatorToElevator(
	PathID int IDENTITY(1,1) NOT NULL, --自动编号
	SrcElevatorNo varchar(20) NOT NULL, --源电梯编号
	TagElevatorNo varchar(20) NOT NULL, --目的电梯编号
	LinePath nvarchar(500) NOT NULL --路径
 CONSTRAINT PK_PLS_ElevatorToElevator PRIMARY KEY CLUSTERED 
(
	PathID ASC
)
)
GO

-- 寻车路线
if not exists (select * from dbo.sysobjects where name = 'Park_SeIfPath' and xtype = 'U')
CREATE TABLE [dbo].[Park_SeIfPath](
	[PathID] [bigint] IDENTITY(1,1) NOT NULL,
	[TerminalID] [nchar](50) NULL,
	[LotNoID] [nchar](50) NULL,
	[Path] [nvarchar](500) NULL
) ON [PRIMARY]
GO

if not exists(select top 1 1 from sys.columns where object_id = object_id(N'park_seifpath') and name = 'MapNo')
begin
alter table park_seifpath add MapNo varchar(20) NOT NULL default('')--增加场图（查询机和附属查询机编号相同，但场图不同）
end
go

if exists(select top 1 1 from sys.columns where object_id = object_id(N'park_seifpath') and name = 'MapNo')
begin
update park_seifpath set mapno=t.mapno from park_seifpath sp inner join pls_terminal t on sp.terminalid = t.terminalno--(同时更新场图)
end
go

--v2.0.0.7
if exists(select top 1 1 from sys.columns where object_id = object_id(N'PLS_ExTerminalToParkLot'))
begin
drop table PLS_ExTerminalToParkLot
end
go


if not exists(select top 1 1 from sys.columns where object_id = object_id(N'PLS_TerminalToElevator') and name = 'MapNo')
begin
alter table PLS_TerminalToElevator add MapNo varchar(20) NOT NULL default('')--增加场图（查询机和附属查询机编号相同，但场图不同）
end
go

if exists(select top 1 1 from sys.columns where object_id = object_id(N'PLS_TerminalToElevator') and name = 'MapNo')
begin
update PLS_TerminalToElevator set mapno=t.mapno from PLS_TerminalToElevator sp inner join PLS_MapTerminal t on sp.terminalid = t.terminalno--(同时更新场图)
end
go

if not exists(select top 1 1 from sys.columns where object_id = object_id(N'PLS_ElevatorToElevator') and name = 'MapNo')
begin
alter table PLS_ElevatorToElevator add MapNo varchar(20) NOT NULL default('')--增加场图
end
go

--v2.0.0.8 增加寻车码寻车

-- 寻车码机到车位(PLS_BarcodeToParkLot)
if not exists (select * from dbo.sysobjects where name = 'PLS_BarcodeToParkLot' and xtype = 'U')
CREATE TABLE PLS_BarcodeToParkLot(
	PathID int IDENTITY(1,1) NOT NULL, --自动编号
	BarcodeNo varchar(20) NOT NULL, --寻车码编号
	LotNoID varchar(20) NOT NULL, --车位
	MapNo varchar(20) NOT NULL, --场图编号
	LinePath nvarchar(500) NOT NULL --路径
 CONSTRAINT PK_PLS_BarcodeToParkLot PRIMARY KEY CLUSTERED 
(
	PathID ASC
)
)
GO

-- 寻车码到电梯表(PLS_BarcodeToElevator)
if not exists (select * from dbo.sysobjects where name = 'PLS_BarcodeToElevator' and xtype = 'U')
CREATE TABLE PLS_BarcodeToElevator(
	PathID int IDENTITY(1,1) NOT NULL, --自动编号
	BarcodeNo varchar(20) NOT NULL, --寻车码编号
	ElevatorNo varchar(20) NOT NULL, --电梯编号
	MapNo varchar(20) NOT NULL, --场图编号
	LinePath nvarchar(500) NOT NULL --路径
 CONSTRAINT [PK_PLS_BarcodeToElevator] PRIMARY KEY CLUSTERED 
(
	PathID ASC
)
)
GO

--v2.0.0.9 寻车码到车位组
if not exists (select * from dbo.sysobjects where name = 'PLS_BarcodeToGroup' and xtype = 'U')
CREATE TABLE PLS_BarcodeToGroup(
	PathID int IDENTITY(1,1) NOT NULL, --自动编号
	BarcodeNo varchar(20) NOT NULL, --寻车码编号
	GroupNo varchar(20) NOT NULL, --车位组编号
	MapNo varchar(20) NOT NULL, --场图编号
	LinePath nvarchar(500) NOT NULL --路径
 CONSTRAINT PK_PLS_BarcodeToGroup PRIMARY KEY CLUSTERED 
(
	PathID ASC
)
)
GO

if not exists (select * from dbo.sysobjects where name = 'PLS_ElevatorToGroup' and xtype = 'U')
CREATE TABLE PLS_ElevatorToGroup(
	PathID int IDENTITY(1,1) NOT NULL, --自动编号
	ElevatorNo varchar(20) NOT NULL, --电梯编号
	MapNo varchar(20) NOT NULL, --场图编号
	GroupNo varchar(20) NOT NULL, --车位组编号
	LinePath nvarchar(500) NOT NULL --路径
 CONSTRAINT PK_PLS_ElevatorToGroup PRIMARY KEY CLUSTERED 
(
	PathID ASC
)
)
GO

--v2.0.0.10 查询机到车位组
if not exists (select * from dbo.sysobjects where name = 'PLS_TerminalToGroup' and xtype = 'U')
CREATE TABLE PLS_TerminalToGroup(
	PathID int IDENTITY(1,1) NOT NULL, --自动编号
	TerminalNo varchar(20) NOT NULL, --查询机编号
	GroupNo varchar(20) NOT NULL, --车位组编号
	MapNo varchar(20) NOT NULL, --场图编号
	LinePath nvarchar(500) NOT NULL --路径
 CONSTRAINT PK_PLS_TerminalToGroup PRIMARY KEY CLUSTERED 
(
	PathID ASC
)
)
GO

--寻车码到车位 把路线长度改到最大
if not exists (select top 1 1 from sys.columns where object_id = object_id(N'PLS_BarcodeToParkLot') and name = N'LinePath' and max_length=-1)
    alter table  PLS_BarcodeToParkLot  alter column LinePath varchar(max);
GO

--寻车码到电梯表 把路线长度改到最大
if not exists (select top 1 1 from sys.columns where object_id = object_id(N'PLS_BarcodeToElevator') and name = N'LinePath' and max_length=-1)
    alter table  PLS_BarcodeToElevator  alter column LinePath varchar(max);
GO

--寻车码到车位组 把路线长度改到最大
if not exists (select top 1 1 from sys.columns where object_id = object_id(N'PLS_BarcodeToGroup') and name = N'LinePath' and max_length=-1)
    alter table  PLS_BarcodeToGroup  alter column LinePath varchar(max);
GO

--电梯到车位把路线长度改到最大
if not exists (select top 1 1 from sys.columns where object_id = object_id(N'PLS_ElevatorToParkLot') and name = N'LinePath' and max_length=-1)
    alter table  PLS_ElevatorToParkLot  alter column LinePath varchar(max);
GO

--电梯到电梯表 把路线长度改到最大
if not exists (select top 1 1 from sys.columns where object_id = object_id(N'PLS_ElevatorToElevator') and name = N'LinePath' and max_length=-1)
    alter table  PLS_ElevatorToElevator  alter column LinePath varchar(max);
GO

--电梯到车位组 把路线长度改到最大
if not exists (select top 1 1 from sys.columns where object_id = object_id(N'PLS_ElevatorToGroup') and name = N'LinePath' and max_length=-1)
    alter table  PLS_ElevatorToGroup  alter column LinePath varchar(max);
GO

--查询机到电梯表 把路线长度改到最大
if not exists (select top 1 1 from sys.columns where object_id = object_id(N'PLS_TerminalToElevator') and name = N'LinePath' and max_length=-1)
    alter table  PLS_TerminalToElevator  alter column LinePath varchar(max);
GO

--查询机到车位组 把路线长度改到最大
if not exists (select top 1 1 from sys.columns where object_id = object_id(N'PLS_TerminalToGroup') and name = N'LinePath' and max_length=-1)
    alter table  PLS_TerminalToGroup  alter column LinePath varchar(max);
GO

--查询机到车位 把路线长度改到最大
if not exists(select top 1 1 from sys.columns where object_id = object_id(N'Park_SeIfPath') and name = 'Path' and max_length=-1)
	alter table  Park_SeIfPath  alter column Path varchar(max);
go