-- 现场总装数据库：MESBasicDB_XD_ZZ
-- 用途：支持 AGV lower/raise 回调与 PLC 允许下降/举升放行查询。
-- 说明：请在目标库手工执行。本脚本不直接修改 AGV_光栅请求记录 表结构。

SET NOCOUNT ON;
GO

-- 1. 增加变量类型代码。500/501 是 MES 写 PLC 请求位，550/551 是 PLC 允许位。
IF NOT EXISTS (SELECT 1 FROM [dbo].[MES_基本变量变量类型代码] WHERE [变量类型代码] = 500)
BEGIN
    INSERT INTO [dbo].[MES_基本变量变量类型代码]
        ([变量类型], [TagTypeDataType], [TagName], [变量类型代码], [TagTypeChange], [MesTagTypeChange], [WebTagTypeChange], [信号点名称], [变量类型名称])
    VALUES
        (N'X', 11, N'AGV请求下降', 500, 1, 0, 1, N'AGV请求下降', N'AGV请求下降');
END

IF NOT EXISTS (SELECT 1 FROM [dbo].[MES_基本变量变量类型代码] WHERE [变量类型代码] = 501)
BEGIN
    INSERT INTO [dbo].[MES_基本变量变量类型代码]
        ([变量类型], [TagTypeDataType], [TagName], [变量类型代码], [TagTypeChange], [MesTagTypeChange], [WebTagTypeChange], [信号点名称], [变量类型名称])
    VALUES
        (N'X', 11, N'AGV请求举升', 501, 1, 0, 1, N'AGV请求举升', N'AGV请求举升');
END

IF NOT EXISTS (SELECT 1 FROM [dbo].[MES_基本变量变量类型代码] WHERE [变量类型代码] = 550)
BEGIN
    INSERT INTO [dbo].[MES_基本变量变量类型代码]
        ([变量类型], [TagTypeDataType], [TagName], [变量类型代码], [TagTypeChange], [MesTagTypeChange], [WebTagTypeChange], [信号点名称], [变量类型名称])
    VALUES
        (N'X', 11, N'允许AGV下降', 550, 1, 0, 1, N'允许AGV下降', N'允许AGV下降');
END

IF NOT EXISTS (SELECT 1 FROM [dbo].[MES_基本变量变量类型代码] WHERE [变量类型代码] = 551)
BEGIN
    INSERT INTO [dbo].[MES_基本变量变量类型代码]
        ([变量类型], [TagTypeDataType], [TagName], [变量类型代码], [TagTypeChange], [MesTagTypeChange], [WebTagTypeChange], [信号点名称], [变量类型名称])
    VALUES
        (N'X', 11, N'允许AGV举升', 551, 1, 0, 1, N'允许AGV举升', N'允许AGV举升');
END
GO

-- 2. 扩展 AGV continueTask 放行查询。
-- apply/release 保持原有 XD_库位Moby 查询。
-- lower/raise 从 AGV_光栅请求记录 取最新未允许或最近请求，避免改动 XD_库位Moby 与历史表列顺序。
ALTER PROCEDURE [dbo].[AGV_允许进入离开_查询]
    @opName nvarchar(50) = 'AC0901-02',
    @engineID nvarchar(50) = '0',
    @type nvarchar(50) = 'apply'
AS
BEGIN
    SET NOCOUNT ON;

    IF (@type = 'apply')
    BEGIN
        SELECT [进入req] AS reqCode, [进入task] AS taskCode
        FROM [dbo].[XD_库位Moby]
        WHERE [库位号] = @opName
          AND [AGV请求进入时间] IS NOT NULL;
    END

    IF (@type = 'release')
    BEGIN
        SELECT [离开req] AS reqCode, [离开task] AS taskCode
        FROM [dbo].[XD_库位Moby]
        WHERE [库位号] = @opName
          AND [AGV请求离开时间] IS NOT NULL;
    END

    IF (@type = 'lower')
    BEGIN
        SELECT TOP 1 [reqCode], [taskCode]
        FROM [dbo].[AGV_光栅请求记录]
        WHERE [method] = 'lower'
          AND (
                [currentPositionCode] = @opName
                OR [currentPositionCode] LIKE @opName + N'[_]%'
                OR [wbCode] = @opName
              )
        ORDER BY ISNULL([是否允许], 0) ASC, [创建时间] DESC, [ID] DESC;
    END

    IF (@type = 'raise')
    BEGIN
        SELECT TOP 1 [reqCode], [taskCode]
        FROM [dbo].[AGV_光栅请求记录]
        WHERE [method] = 'raise'
          AND (
                [currentPositionCode] = @opName
                OR [currentPositionCode] LIKE @opName + N'[_]%'
                OR [wbCode] = @opName
              )
        ORDER BY ISNULL([是否允许], 0) ASC, [创建时间] DESC, [ID] DESC;
    END
END
GO
