﻿-- 整线PDF：表 + SP + 原查询/查看改造
IF OBJECT_ID(N'dbo.基础数据_操作指南整线指导文件', N'U') IS NULL
BEGIN
  CREATE TABLE [dbo].[基础数据_操作指南整线指导文件](
    [ID] int IDENTITY(1,1) NOT NULL PRIMARY KEY,
    [指导文件] varbinary(MAX) NULL,
    [文件名称] nvarchar(200) NULL,
    [操作时间] datetime NULL DEFAULT (getdate())
  )
END
GO

CREATE OR ALTER PROCEDURE [dbo].[基础数据_操作指南整线指导文件_查询]
AS
SET NOCOUNT ON
BEGIN
  SELECT [ID], [文件名称], [操作时间]
  FROM [dbo].[基础数据_操作指南整线指导文件]
  ORDER BY [操作时间] DESC
END
GO

CREATE OR ALTER PROCEDURE [dbo].[基础数据_操作指南整线指导文件_保存文件]
  @文件名称 nvarchar(200) = NULL,
  @文件后缀 nvarchar(50) = NULL,
  @文件内容 varbinary(MAX) = NULL
AS
SET NOCOUNT ON
BEGIN
  INSERT INTO [dbo].[基础数据_操作指南整线指导文件] ([指导文件], [文件名称])
  VALUES (@文件内容, @文件名称)
  SELECT '1' AS result
END
GO

CREATE OR ALTER PROCEDURE [dbo].[基础数据_操作指南整线指导文件_查看文件]
  @ID int = 1
AS
SET NOCOUNT ON
BEGIN
  SELECT TOP 1 *
  FROM [dbo].[基础数据_操作指南整线指导文件]
  WHERE ID = @ID
END
GO

CREATE OR ALTER PROCEDURE [dbo].[基础数据_操作指南整线指导文件_删除]
  @ID int = 1
AS
SET NOCOUNT ON
BEGIN
  DELETE FROM [dbo].[基础数据_操作指南整线指导文件]
  WHERE ID = @ID
  SELECT '1' AS result
END
GO

CREATE OR ALTER PROCEDURE [dbo].[基础数据_操作指南指导文件_查询]
  @工位号 nvarchar(50) = 'OP010'
AS
SET NOCOUNT ON
BEGIN
  SELECT [ID], [文件名称], [工位号], [操作时间]
  FROM [dbo].[基础数据_操作指南指导文件]
  WHERE 工位号 = @工位号
  UNION ALL
  SELECT [ID] + 100000000, [文件名称], N'整线', [操作时间]
  FROM [dbo].[基础数据_操作指南整线指导文件]
  ORDER BY [操作时间] DESC
END
GO

CREATE OR ALTER PROCEDURE [dbo].[基础数据_操作指南指导文件_查看文件]
  @ID int = 1
AS
SET NOCOUNT ON
BEGIN
  IF @ID >= 100000000
    SELECT TOP 1 * FROM [dbo].[基础数据_操作指南整线指导文件] WHERE ID = @ID - 100000000
  ELSE
    SELECT TOP 1 * FROM [dbo].[基础数据_操作指南指导文件] WHERE ID = @ID
END
GO
