-- 中控参数维护模块注册及存储过程脚本
-- 执行前请确认当前数据库为 AMES_PDC。

IF NOT EXISTS (
    SELECT 1
    FROM 基础表_模块信息
    WHERE path = 'ProcessParameterMaintenance/CentralControlParameterMaintenance'
)
BEGIN
    INSERT INTO 基础表_模块信息 (path, component, name, title, icon, 是否禁用)
    VALUES ('ProcessParameterMaintenance/CentralControlParameterMaintenance', '/ProcessParameterMaintenance/CentralControlParameterMaintenance', 'CentralControlParameterMaintenance', N'中控参数维护模块', 'BasicDataManagement', 0)
END
GO

IF OBJECT_ID(N'[dbo].[中控参数维护_特殊工位定义_分页查询]', N'P') IS NOT NULL
    DROP PROCEDURE [dbo].[中控参数维护_特殊工位定义_分页查询]
GO

CREATE PROCEDURE [dbo].[中控参数维护_特殊工位定义_分页查询]
@类型       nvarchar(50) = null,
@工位列表   nvarchar(50) = null,
@PageCurrent int = null,
@PageSize    int = null,
@PageCount   int = null output,
@ItemCount   int = null output
AS
SET NOCOUNT ON

declare @tbname     sysname
declare @fieldkey   sysname
declare @where      nvarchar(1000)
declare @fieldshow  nvarchar(1000)
declare @fieldOrder nvarchar(1000)

set @tbname     = 'MES_特殊工位定义表'
set @fieldkey   = 'type'
set @fieldOrder = 'type'
set @fieldshow  = '[type],[opList]'

set @where = '1 = 1'
if @类型 != '' set @where = @where + ' and type like ''%' + @类型 + '%'''
if @工位列表 != '' set @where = @where + ' and opList like ''%' + @工位列表 + '%'''

exec sp_xt_pagesplit @tbname, @fieldkey, @where, @fieldshow, @fieldOrder, @PageCurrent, @PageSize, @PageCount out, @ItemCount out
GO

IF OBJECT_ID(N'[dbo].[中控参数维护_特殊工位定义_增加]', N'P') IS NOT NULL
    DROP PROCEDURE [dbo].[中控参数维护_特殊工位定义_增加]
GO

CREATE PROCEDURE [dbo].[中控参数维护_特殊工位定义_增加]
@类型     nvarchar(50) = null,
@工位列表 nvarchar(500) = null
AS
SET NOCOUNT ON

IF (SELECT COUNT(1) FROM MES_特殊工位定义表 WHERE [type] = @类型) > 0
BEGIN
    SELECT 2 as result, N'类型已存在' as msg
    RETURN
END

INSERT INTO MES_特殊工位定义表 ([type], [opList])
VALUES (@类型, @工位列表)

SELECT 1 as result, N'' as msg
GO

IF OBJECT_ID(N'[dbo].[中控参数维护_特殊工位定义_编辑]', N'P') IS NOT NULL
    DROP PROCEDURE [dbo].[中控参数维护_特殊工位定义_编辑]
GO

CREATE PROCEDURE [dbo].[中控参数维护_特殊工位定义_编辑]
@类型     nvarchar(50) = null,
@工位列表 nvarchar(500) = null
AS
SET NOCOUNT ON

UPDATE MES_特殊工位定义表 SET
    [opList] = @工位列表
WHERE [type] = @类型

SELECT 1 as result, N'' as msg
GO

IF OBJECT_ID(N'[dbo].[中控参数维护_特殊工位定义_删除]', N'P') IS NOT NULL
    DROP PROCEDURE [dbo].[中控参数维护_特殊工位定义_删除]
GO

CREATE PROCEDURE [dbo].[中控参数维护_特殊工位定义_删除]
@类型 nvarchar(50) = null
AS
SET NOCOUNT ON

IF (SELECT COUNT(1) FROM MES_特殊工位规则配置表 WHERE [type] = @类型) > 0
BEGIN
    SELECT 2 as result, N'该类型已配置规则，不能删除' as msg
    RETURN
END

DELETE FROM MES_特殊工位定义表 WHERE [type] = @类型

SELECT 1 as result, N'' as msg
GO

IF OBJECT_ID(N'[dbo].[中控参数维护_特殊工位规则_分页查询]', N'P') IS NOT NULL
    DROP PROCEDURE [dbo].[中控参数维护_特殊工位规则_分页查询]
GO

CREATE PROCEDURE [dbo].[中控参数维护_特殊工位规则_分页查询]
@类型       nvarchar(50) = null,
@工位       nvarchar(50) = null,
@规则键     nvarchar(50) = null,
@启用       nvarchar(10) = null,
@PageCurrent int = null,
@PageSize    int = null,
@PageCount   int = null output,
@ItemCount   int = null output
AS
SET NOCOUNT ON

declare @tbname     sysname
declare @fieldkey   sysname
declare @where      nvarchar(1000)
declare @fieldshow  nvarchar(1000)
declare @fieldOrder nvarchar(1000)

set @tbname     = 'MES_特殊工位规则配置表'
set @fieldkey   = 'id'
set @fieldOrder = 'id'
set @fieldshow  = '[id],[type],[opName],[ruleKey],[ruleValue],[enable],[remark]'

set @where = '1 = 1'
if @类型 != '' set @where = @where + ' and type = ''' + @类型 + ''''
if @工位 != '' set @where = @where + ' and opName like ''%' + @工位 + '%'''
if @规则键 != '' set @where = @where + ' and ruleKey like ''%' + @规则键 + '%'''
if @启用 != '' set @where = @where + ' and enable = ' + @启用

exec sp_xt_pagesplit @tbname, @fieldkey, @where, @fieldshow, @fieldOrder, @PageCurrent, @PageSize, @PageCount out, @ItemCount out
GO

IF OBJECT_ID(N'[dbo].[中控参数维护_特殊工位规则_增加]', N'P') IS NOT NULL
    DROP PROCEDURE [dbo].[中控参数维护_特殊工位规则_增加]
GO

CREATE PROCEDURE [dbo].[中控参数维护_特殊工位规则_增加]
@ID      nvarchar(50) = null,
@类型    nvarchar(50) = null,
@工位    nvarchar(50) = null,
@规则键  nvarchar(50) = null,
@规则值  nvarchar(500) = null,
@启用    bit = null,
@备注    nvarchar(200) = null
AS
SET NOCOUNT ON

IF (SELECT COUNT(1) FROM MES_特殊工位规则配置表 WHERE [type] = @类型 AND [opName] = @工位 AND [ruleKey] = @规则键) > 0
BEGIN
    SELECT 2 as result, N'该类型、工位、规则键已存在' as msg
    RETURN
END

INSERT INTO MES_特殊工位规则配置表 ([type], [opName], [ruleKey], [ruleValue], [enable], [remark])
VALUES (@类型, @工位, @规则键, @规则值, ISNULL(@启用, 1), @备注)

SELECT 1 as result, N'' as msg
GO

IF OBJECT_ID(N'[dbo].[中控参数维护_特殊工位规则_编辑]', N'P') IS NOT NULL
    DROP PROCEDURE [dbo].[中控参数维护_特殊工位规则_编辑]
GO

CREATE PROCEDURE [dbo].[中控参数维护_特殊工位规则_编辑]
@ID      int = null,
@类型    nvarchar(50) = null,
@工位    nvarchar(50) = null,
@规则键  nvarchar(50) = null,
@规则值  nvarchar(500) = null,
@启用    bit = null,
@备注    nvarchar(200) = null
AS
SET NOCOUNT ON

IF (SELECT COUNT(1) FROM MES_特殊工位规则配置表 WHERE [type] = @类型 AND [opName] = @工位 AND [ruleKey] = @规则键 AND [id] != @ID) > 0
BEGIN
    SELECT 2 as result, N'该类型、工位、规则键已存在' as msg
    RETURN
END

UPDATE MES_特殊工位规则配置表 SET
    [type] = @类型,
    [opName] = @工位,
    [ruleKey] = @规则键,
    [ruleValue] = @规则值,
    [enable] = ISNULL(@启用, 1),
    [remark] = @备注
WHERE [id] = @ID

SELECT 1 as result, N'' as msg
GO

IF OBJECT_ID(N'[dbo].[中控参数维护_特殊工位规则_删除]', N'P') IS NOT NULL
    DROP PROCEDURE [dbo].[中控参数维护_特殊工位规则_删除]
GO

CREATE PROCEDURE [dbo].[中控参数维护_特殊工位规则_删除]
@ID int = null
AS
SET NOCOUNT ON

DELETE FROM MES_特殊工位规则配置表 WHERE [id] = @ID

SELECT 1 as result, N'' as msg
GO
