USE [YL_MESDB]
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].[质量管理_及时检验报表_查询]
    @合同号 nvarchar(100) = N'',
    @订单号 nvarchar(100) = N'',
    @零件编号 nvarchar(100) = N'',
    @零件名称 nvarchar(100) = N'',
    @完工日期_Start nvarchar(20) = N'',
    @完工日期_End nvarchar(20) = N'',
    @检测日期_Start nvarchar(20) = N'',
    @检测日期_End nvarchar(20) = N'',
    @检测结果 nvarchar(10) = N'',
    @检测人 nvarchar(50) = N'',
    @检测类别 nvarchar(50) = N'',
    @属性 nvarchar(50) = N''
AS
BEGIN
    SET NOCOUNT ON;

    ;WITH QualitySummary AS (
        SELECT
            TaskAID,
            SUM(ISNULL(检验数量, 0)) AS 检验数量,
            SUM(ISNULL(合格数, 0)) AS 合格数,
            SUM(ISNULL(不合格数, 0)) AS 不合格数,
            MAX(检测人) AS 检测人,
            MAX(检测时间) AS 检测时间,
            MAX(检测说明) AS 检测说明
        FROM [dbo].[YL_质量检验_质检记录]
        WHERE 检测类型编号 = 6
        GROUP BY TaskAID
    ),
    OperationSummary AS (
        SELECT
            TaskAID,
            订单编号,
            物料描述,
            物料编号,
            工序名称,
            SUM(ISNULL(操作数值, 0)) AS 完成数量,
            MAX(结束时间) AS 结束时间
        FROM [dbo].[YL_加工中心_操作记录表]
        WHERE ISNULL(操作类别编号, 0) IN (3, 6)
          AND TaskAID IS NOT NULL
          AND 结束时间 IS NOT NULL
        GROUP BY
            TaskAID,
            订单编号,
            物料描述,
            物料编号,
            工序名称
    )
    SELECT TOP (500)
        o.TaskAID AS TaskAID,
        o.订单编号 AS 计划号,
        v.合同号,
        o.物料描述 AS 零件名称,
        o.物料编号 AS 零件编码,
        v.指派对象 AS 工位名称,
        o.工序名称 AS 工序名称,
        CAST(N'' AS nvarchar(100)) AS 供应商,
        CAST(N'' AS nvarchar(100)) AS 采购收货单,
        v.自制件属性,
        o.完成数量 AS 完成数量,
        ISNULL(v.加急状态, 0) AS 加急状态,
        ISNULL(v.加急总数, 0) AS 加急总数,
        ISNULL(q.检验数量, 0) AS 检验数量,
        ISNULL(q.合格数, 0) AS 合格数,
        ISNULL(q.不合格数, 0) AS 不合格数,
        ISNULL(q.检测人, N'') AS 检测人,
        ISNULL(CONVERT(nvarchar(19), q.检测时间, 120), N'') AS 检测时间,
        ISNULL(q.检测说明, N'') AS 检测说明,
        CONVERT(nvarchar(19), o.结束时间, 120) AS 结束时间,
        DATEDIFF(DAY, CONVERT(date, o.结束时间), CONVERT(date, GETDATE())) AS 超期天数,
        CASE
            WHEN q.TaskAID IS NULL THEN 0
            WHEN q.合格数 > 0 AND q.不合格数 = 0 THEN 1
            WHEN q.不合格数 > 0 THEN 2
            ELSE 0
        END AS 检测状态
    FROM OperationSummary AS o
    LEFT JOIN QualitySummary AS q ON o.TaskAID = q.TaskAID
    LEFT JOIN [dbo].[View_生产订单全部（包括已关闭）] AS v ON o.TaskAID = v.TaskAID
    WHERE ISNULL(v.单据状态, N'') = N''
      AND ISNULL(o.完成数量, 0) > ISNULL(q.检验数量, 0)
      AND DATEDIFF(DAY, CONVERT(date, o.结束时间), CONVERT(date, GETDATE())) > 2
      AND (@订单号 = N'' OR o.订单编号 = @订单号)
      AND (@合同号 = N'' OR v.合同号 LIKE N'%' + @合同号 + N'%')
      AND (@零件编号 = N'' OR o.物料编号 LIKE N'%' + @零件编号 + N'%')
      AND (@零件名称 = N'' OR o.物料描述 LIKE N'%' + @零件名称 + N'%')
      AND (
          @完工日期_Start = N''
          OR CONVERT(date, o.结束时间) >= TRY_CONVERT(date, @完工日期_Start)
      )
      AND (
          @完工日期_End = N''
          OR CONVERT(date, o.结束时间) <= TRY_CONVERT(date, @完工日期_End)
      )
      AND (
          @检测日期_Start = N''
          OR CONVERT(date, q.检测时间) >= TRY_CONVERT(date, @检测日期_Start)
      )
      AND (
          @检测日期_End = N''
          OR CONVERT(date, q.检测时间) <= TRY_CONVERT(date, @检测日期_End)
      )
      AND (
          @检测结果 = N''
          OR (@检测结果 = N'1' AND ISNULL(q.合格数, 0) > 0 AND ISNULL(q.不合格数, 0) = 0)
          OR (@检测结果 = N'0' AND ISNULL(q.不合格数, 0) > 0)
      )
      AND (@检测人 = N'' OR ISNULL(q.检测人, N'') LIKE N'%' + @检测人 + N'%')
      AND (
          @属性 = N''
          OR @属性 = N'0'
          OR (@属性 = N'1' AND ISNULL(v.自制件属性, N'') NOT LIKE N'%装配%')
          OR (@属性 = N'2' AND ISNULL(v.自制件属性, N'') LIKE N'%装配%')
      )
    ORDER BY
        ISNULL(v.加急状态, 0) DESC,
        DATEDIFF(DAY, CONVERT(date, o.结束时间), CONVERT(date, GETDATE())) DESC,
        o.结束时间 ASC,
        o.订单编号 DESC;
END
GO
