USE [YL_MESDB]
GO

IF OBJECT_ID(N'dbo.生产管理_手续确认报表记录', N'U') IS NULL
BEGIN
    CREATE TABLE [dbo].[生产管理_手续确认报表记录] (
        [AID] bigint IDENTITY(1,1) NOT NULL CONSTRAINT [PK_生产管理_手续确认报表记录] PRIMARY KEY,
        [TaskAID] bigint NOT NULL,
        [订单编号] int NULL,
        [手续确认] int NOT NULL CONSTRAINT [DF_生产管理_手续确认报表记录_手续确认] DEFAULT (1),
        [确认人] nvarchar(50) NULL,
        [确认时间] datetime NOT NULL CONSTRAINT [DF_生产管理_手续确认报表记录_确认时间] DEFAULT (GETDATE()),
        [备注] nvarchar(500) NULL
    );

    CREATE UNIQUE INDEX [UX_生产管理_手续确认报表记录_TaskAID]
        ON [dbo].[生产管理_手续确认报表记录] ([TaskAID]);
END
GO

IF OBJECT_ID(N'dbo.生产管理_手续确认报表_查询', N'P') IS NULL
BEGIN
    EXEC(N'CREATE PROCEDURE [dbo].[生产管理_手续确认报表_查询] AS BEGIN SET NOCOUNT ON; END');
END
GO

ALTER PROCEDURE [dbo].[生产管理_手续确认报表_查询]
    @TaskAID int = -1,
    @订单号 int = -1,
    @零件编号 nvarchar(50) = N'',
    @零件名称 nvarchar(50) = N'',
    @计划开工_Start nvarchar(50) = N'',
    @计划开工_End nvarchar(50) = N'',
    @生产状态 int = -1,
    @工位名称 nvarchar(50) = N'',
    @计划号 nvarchar(50) = N'',
    @销售订单 nvarchar(50) = N''
AS
BEGIN
    SET NOCOUNT ON;

    SELECT TOP (500)
        main.TaskAID,
        main.[订单编号] AS 订单号,
        main.合同号,
        main.自制件属性,
        main.加急总数,
        main.派工特殊说明,
        main.[订单拆分内码],
        main.[订单行号],
        main.[开工时间],
        main.[物料编号] AS 零件编码,
        main.[物料描述] AS 零件名称,
        CASE WHEN ISNULL(main.[加急状态], 0) = 1 THEN N'加急' ELSE N'否' END AS [加急],
        main.[优先级] AS [优先],
        main.[指派对象编号],
        main.[指派对象],
        main.[工序名称],
        main.[二次派工],
        main.[计划号],
        ISNULL(main.[指派数量], 0) AS 分配数,
        main.[计划开始时间] AS 计划开始,
        main.[计划完成时间] AS 计划完成,
        main.[最晚开始时间] AS 最晚开始,
        main.[齐套时间] AS 发料齐套时间,
        ISNULL(main.[收料数量], 0) AS 收料,
        ISNULL(main.[完成数量], 0) AS 完成数量,
        ISNULL(main.[入库], 0) AS 入库,
        ISNULL(main.[入库数量], 0) AS 入库数量,
        main.[订单状态],
        main.[单据状态],
        CASE
            WHEN ISNULL(main.齐套, 0) = 1 THEN N'是'
            WHEN ISNULL(main.齐套, 0) = 0 THEN N'否'
            ELSE N''
        END AS [齐套],
        CASE
            WHEN ISNULL(main.发料状态, 0) = 1 THEN N'发料齐套'
            WHEN ISNULL(main.发料状态, 0) = 2 THEN N'发料缺件'
            ELSE N''
        END AS [发料状态],
        CASE
            WHEN ISNULL(main.拣配状态, 0) = 1 THEN N'已拣配'
            WHEN ISNULL(main.拣配状态, 0) = 0 THEN N'未拣配'
            ELSE N''
        END AS [拣配状态],
        CASE
            WHEN ISNULL(main.任务状态, 0) = 4 THEN N'已完成'
            WHEN ISNULL(main.任务状态, 0) = 2 THEN N'已开工'
            WHEN ISNULL(main.任务状态, 0) = 0 THEN N'待执行'
            WHEN ISNULL(main.任务状态, 0) = 1 THEN N'可执行'
            ELSE N''
        END AS [任务状态],
        CASE
            WHEN ISNULL(main.加工状态, 0) = 2 THEN N'辅助开始'
            WHEN ISNULL(main.加工状态, 0) = 3 THEN N'辅助结束'
            WHEN ISNULL(main.加工状态, 0) = 4 THEN N'开始加工'
            WHEN ISNULL(main.加工状态, 0) = 5 THEN N'暂停'
            WHEN ISNULL(main.加工状态, 0) = 6 THEN N'报工'
            ELSE N''
        END AS [加工状态],
        ISNULL(main.加工状态, 0) AS 加工状态编号,
        CASE WHEN ISNULL(main.[首检], -1) = 1 THEN N'OK' WHEN ISNULL(main.[首检], -1) = 0 THEN N'NG' ELSE N'' END AS [首检],
        CASE WHEN ISNULL(main.[序检], -1) = 1 THEN N'OK' WHEN ISNULL(main.[序检], -1) = 0 THEN N'NG' ELSE N'' END AS [序检],
        next_task.[工序名称] AS 下序名称,
        ISNULL(prev_quality.上序合格数, 0) AS 上序合格数,
        second_worker.二次派工名称,
        CASE WHEN confirm.TaskAID IS NULL THEN N'未完成' ELSE N'已完成' END AS 手续确认,
        confirm.确认人 AS 手续确认人,
        confirm.确认时间 AS 手续确认时间
    FROM [dbo].[View_生产订单_MES] AS main
    OUTER APPLY (
        SELECT TOP (1) next_task.[工序名称]
        FROM [dbo].[View_生产订单_MES] AS next_task
        WHERE next_task.[订单编号] = main.[订单编号]
          AND next_task.[订单行号] = main.[订单行号] + 1
          AND next_task.[排产状态] = 1
        ORDER BY next_task.[订单行号]
    ) AS next_task
    OUTER APPLY (
        SELECT ISNULL(SUM(ISNULL(a.合格数, 0)), 0) AS 上序合格数
        FROM [dbo].[YL_质量检验_质检记录] AS a
        INNER JOIN [dbo].[MES_接口_生产计划_生产任务] AS b ON a.TaskAID = b.AID
        WHERE b.订单编号 = main.[订单编号]
          AND b.订单行号 = main.[订单行号] - 1
          AND a.检测类型编号 = 6
    ) AS prev_quality
    OUTER APPLY (
        SELECT STRING_AGG(a.Personnel_Name, N',') AS 二次派工名称
        FROM [dbo].[登录基础数据_人员信息] AS a
        WHERE EXISTS (
            SELECT 1
            FROM STRING_SPLIT(ISNULL(main.二次派工, N''), N',') AS ids
            WHERE LTRIM(RTRIM(ids.value)) = CAST(a.Personnel_Number AS varchar(50))
        )
    ) AS second_worker
    LEFT JOIN [dbo].[生产管理_手续确认报表记录] AS confirm ON confirm.TaskAID = main.TaskAID
    WHERE main.[排产状态] = 1
      AND ISNULL(main.[指派对象], N'') <> N'外协'
      AND ISNULL(main.[指派对象], N'') LIKE N'%装配%'
      AND confirm.TaskAID IS NULL
      AND ISNULL(main.[入库], 0) <> 1
      AND ISNULL(main.[入库数量], 0) <= 0
      AND ISNULL(main.[订单状态], N'') NOT LIKE N'%取消%'
      AND ISNULL(main.[订单状态], N'') NOT LIKE N'%关闭%'
      AND ISNULL(main.[单据状态], N'') = N''
      AND (@订单号 <= 0 OR main.[订单编号] = @订单号)
      AND (@计划号 = N'' OR main.[计划号] = @计划号)
      AND (@销售订单 = N'' OR main.[合同号] LIKE N'%' + @销售订单 + N'%')
      AND (@TaskAID <= 0 OR main.[TaskAID] = @TaskAID)
      AND (@零件编号 = N'' OR main.[物料编号] LIKE N'%' + @零件编号 + N'%')
      AND (@零件名称 = N'' OR main.[物料描述] LIKE N'%' + @零件名称 + N'%')
      AND (@计划开工_Start = N'' OR main.[计划开始时间] >= TRY_CONVERT(datetime, @计划开工_Start))
      AND (@计划开工_End = N'' OR main.[计划开始时间] <= TRY_CONVERT(datetime, @计划开工_End))
      AND (@工位名称 = N'' OR main.[指派对象] IN (SELECT LTRIM(RTRIM(value)) FROM STRING_SPLIT(@工位名称, N',')))
      AND (
          @生产状态 IS NULL
          OR @生产状态 = -1
          OR @生产状态 = 10
          OR ISNULL(main.任务状态, 0) = @生产状态
      )
    ORDER BY
        CASE WHEN ISNULL(main.[加急状态], 0) = 1 THEN 0 ELSE 1 END,
        CASE WHEN ISNULL(main.齐套, 0) = 1 THEN 0 ELSE 1 END,
        main.[计划开始时间],
        main.[订单编号],
        main.[阶段标识];
END
GO

IF OBJECT_ID(N'dbo.生产管理_手续确认报表_确认', N'P') IS NULL
BEGIN
    EXEC(N'CREATE PROCEDURE [dbo].[生产管理_手续确认报表_确认] AS BEGIN SET NOCOUNT ON; END');
END
GO

ALTER PROCEDURE [dbo].[生产管理_手续确认报表_确认]
    @TaskAID bigint,
    @订单号 int = NULL,
    @确认人 nvarchar(50) = N'',
    @备注 nvarchar(500) = N''
AS
BEGIN
    SET NOCOUNT ON;

    IF @TaskAID IS NULL OR @TaskAID <= 0
    BEGIN
        SELECT 0 AS result, N'TaskAID不能为空' AS msg;
        RETURN;
    END;

    IF EXISTS (SELECT 1 FROM [dbo].[生产管理_手续确认报表记录] WHERE TaskAID = @TaskAID)
    BEGIN
        UPDATE [dbo].[生产管理_手续确认报表记录]
        SET
            [手续确认] = 1,
            [确认人] = @确认人,
            [确认时间] = GETDATE(),
            [备注] = @备注
        WHERE TaskAID = @TaskAID;
    END
    ELSE
    BEGIN
        INSERT INTO [dbo].[生产管理_手续确认报表记录]
            ([TaskAID], [订单编号], [手续确认], [确认人], [确认时间], [备注])
        VALUES
            (@TaskAID, @订单号, 1, @确认人, GETDATE(), @备注);
    END;

    SELECT 1 AS result, N'确认成功' AS msg;
END
GO
