SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
-- =============================================
-- Author:		蔡峰
-- Create date: 2024-07-20
-- Description:	根据用户的权限获取OEM 或者 KH的设备操作权限
-- =============================================
CREATE FUNCTION func_GetDeviceInfoByUserAuth
(
    @LoginCode VARCHAR(50)
)
RETURNS @DevRtnView TABLE
(
    AutoID INT NOT NULL,
    GUID UNIQUEIDENTIFIER NOT NULL,
    EquipmentID VARCHAR(50) NULL,
    EquipmentName NVARCHAR(200) NULL,
    Specification NVARCHAR(200) NULL,
    Manufacturer NVARCHAR(200) NULL,
    SerialNumber NVARCHAR(200) NULL,
    UsingDate VARCHAR(10) NULL,
    Totalcapacity FLOAT NULL,
    Weight FLOAT NULL,
    EquipmentCategory NVARCHAR(20) NULL,
    EquipmentOriginalvalue MONEY NULL,
    EquipmentStatus INT NULL,
    WarrantyPeriod NVARCHAR(50) NULL,
    InstallationLocation NVARCHAR(200) NULL,
    OwningUnit NVARCHAR(200) NULL,
    OperatingParameters NVARCHAR(200) NULL,
    MaintenanceFormVersion INT NULL,
    Route INT NULL,
    CreatDate DATETIME NULL,
    CreatUser INT NULL,
    ChangeDate DATETIME NULL,
    ChangeUser INT NULL,
    Remarks NVARCHAR(2000) NULL
)
AS
BEGIN
    /* 权限临时表 */
    DECLARE @AuthView TABLE
    (
        AuthCode VARCHAR(50)
    );

    INSERT INTO @AuthView
    (
        AuthCode
    )
    SELECT au.AuthCode
    FROM dbo.UserInfo us
        INNER JOIN dbo.RoleAuths ra
            ON us.RoleGroup = ra.RoleID
        INNER JOIN dbo.Auths au
            ON ra.AutoID = au.AutoID
               AND au.AuthCode IN ( 'DEVICE_KH', 'DEVICEL_OEM' )
    WHERE us.LoginCode = @LoginCode;

    /* 设备ID 表 */
    DECLARE @DevView TABLE
    (
        DevID INT
    );

    IF EXISTS (SELECT 1 FROM @AuthView WHERE AuthCode = 'DEVICE_KH')
    BEGIN
        INSERT INTO @DevView
        (
            DevID
        )
        SELECT AutoID
        FROM dbo.View_DeviceRoot
        WHERE RootName = 'KH';
    END;

    IF EXISTS (SELECT 1 FROM @AuthView WHERE AuthCode = 'DEVICEL_OEM')
    BEGIN
        INSERT INTO @DevView
        (
            DevID
        )
        SELECT AutoID
        FROM dbo.View_DeviceRoot
        WHERE RootName = 'OEM';
    END;

    INSERT INTO @DevRtnView
    (
        AutoID,
        GUID,
        EquipmentID,
        EquipmentName,
        Specification,
        Manufacturer,
        SerialNumber,
        UsingDate,
        Totalcapacity,
        Weight,
        EquipmentCategory,
        EquipmentOriginalvalue,
        EquipmentStatus,
        WarrantyPeriod,
        InstallationLocation,
        OwningUnit,
        OperatingParameters,
        MaintenanceFormVersion,
        Route,
        CreatDate,
        CreatUser,
        ChangeDate,
        ChangeUser,
        Remarks
    )
    SELECT t1.AutoID,
           t1.GUID,
           t1.EquipmentID,
           t1.EquipmentName,
           t1.Specification,
           t1.Manufacturer,
           t1.SerialNumber,
           t1.UsingDate,
           t1.Totalcapacity,
           t1.Weight,
           t1.EquipmentCategory,
           t1.EquipmentOriginalvalue,
           t1.EquipmentStatus,
           t1.WarrantyPeriod,
           t1.InstallationLocation,
           t1.OwningUnit,
           t1.OperatingParameters,
           t1.MaintenanceFormVersion,
           t1.Route,
           t1.CreatDate,
           t1.CreatUser,
           t1.ChangeDate,
           t1.ChangeUser,
           t1.Remarks
    FROM dbo.DriveInformation t1
        INNER JOIN @DevView t2
            ON t1.AutoID = t2.DevID;
    RETURN;
END;


