
CREATE PROCEDURE [dbo].[工时统计_设备工时_查询]
    @合同号 NVARCHAR(50) = NULL,
    @操作人 NVARCHAR(50) = NULL,
    @产品编码 NVARCHAR(100) = NULL,
    @工序名称 NVARCHAR(50) = NULL,
    @订单号 NVARCHAR(50) = NULL,
    @开始日期 DATE = NULL,
    @结束日期 DATE = NULL
AS
BEGIN
    SET NOCOUNT ON;

    ;WITH 操作人汇总 AS (
        SELECT 
            工位名称,
            COUNT(*) AS 报工次数,
            SUM(CASE WHEN 操作类别 IN ('报工', '辅助结束') THEN 操作数值 ELSE 0 END) AS 报工数量,
            CAST(SUM(加工工时) / 60.0 / 60.0 AS DECIMAL(10,1)) AS 总工时小时
        FROM View_生产工时视图
        WHERE (@开始日期 IS NULL OR @开始日期 = '' OR 结束时间 >= @开始日期)
          AND (@结束日期 IS NULL OR @结束日期 = '' OR 开始时间 <= @结束日期)
          AND 操作人 IS NOT NULL AND 操作人 != ''
          AND (@合同号 IS NULL OR @合同号 = '' OR 订单合同号 LIKE '%' + @合同号 + '%')
          AND (@操作人 IS NULL OR @操作人 = '' OR 工位名称 LIKE '%' + @操作人 + '%')
          AND (@产品编码 IS NULL OR @产品编码 = '' OR 产品编码 LIKE '%' + @产品编码 + '%')
          AND (@工序名称 IS NULL OR @工序名称 = '' OR 工序名称 LIKE '%' + @工序名称 + '%')
          AND (@订单号 IS NULL OR @订单号 = '' OR 订单号 LIKE '%' + @订单号 + '%')
		  and isNull(阶段标识,0) = -1
        GROUP BY 工位名称
    ),
    日期汇总 AS (
        SELECT 
            工位名称,
            CONVERT(VARCHAR(10), 开始时间, 120) AS 工作日期,
            COUNT(*) AS 报工次数,
            SUM(CASE WHEN 操作类别 IN ('报工', '辅助结束') THEN 操作数值 ELSE 0 END) AS 报工数量,
            CAST(SUM(加工工时) / 60.0 / 60.0 AS DECIMAL(10,1)) AS 工时小时
        FROM View_生产工时视图
        WHERE (@开始日期 IS NULL OR @开始日期 = '' OR 结束时间 >= @开始日期)
          AND (@结束日期 IS NULL OR @结束日期 = '' OR 开始时间 <= @结束日期)
          AND 工位名称 IS NOT NULL AND 工位名称 != ''
          AND (@合同号 IS NULL OR @合同号 = '' OR 订单合同号 LIKE '%' + @合同号 + '%')
          AND (@操作人 IS NULL OR @操作人 = '' OR 工位名称 LIKE '%' + @操作人 + '%')
          AND (@产品编码 IS NULL OR @产品编码 = '' OR 产品编码 LIKE '%' + @产品编码 + '%')
          AND (@工序名称 IS NULL OR @工序名称 = '' OR 工序名称 LIKE '%' + @工序名称 + '%')
          AND (@订单号 IS NULL OR @订单号 = '' OR 订单号 LIKE '%' + @订单号 + '%')
		  and isNull(阶段标识,0) = -1
        GROUP BY 工位名称, CONVERT(VARCHAR(10), 开始时间, 120)
    ),
    报工明细 AS (
        SELECT 
            工位名称,
            CONVERT(VARCHAR(10), 开始时间, 120) AS 工作日期,
            订单合同号 AS 合同号,
            订单号,
            产品编码,
            产品名称,
            工序名称,
            CASE WHEN 操作类别 IN ('报工', '辅助结束') THEN 操作数值 ELSE 0 END AS 报工数量,
            CONVERT(VARCHAR(19), 开始时间, 120) AS 开始时间,
            CONVERT(VARCHAR(19), 结束时间, 120) AS 结束时间,
            CAST(加工工时 / 60.0 / 60.0 AS DECIMAL(10,2)) AS 工时小时
        FROM View_生产工时视图
        WHERE (@开始日期 IS NULL OR @开始日期 = '' OR 结束时间 >= @开始日期)
          AND (@结束日期 IS NULL OR @结束日期 = '' OR 开始时间 <= @结束日期)
          AND 操作人 IS NOT NULL AND 操作人 != ''
          AND (@合同号 IS NULL OR @合同号 = '' OR 订单合同号 LIKE '%' + @合同号 + '%')
          AND (@操作人 IS NULL OR @操作人 = '' OR 工位名称 LIKE '%' + @操作人 + '%')
          AND (@产品编码 IS NULL OR @产品编码 = '' OR 产品编码 LIKE '%' + @产品编码 + '%')
          AND (@工序名称 IS NULL OR @工序名称 = '' OR 工序名称 LIKE '%' + @工序名称 + '%')
          AND (@订单号 IS NULL OR @订单号 = '' OR 订单号 LIKE '%' + @订单号 + '%')
		  and isNull(阶段标识,0) = -1
    )
    SELECT 
        1 AS [Level],
        工位名称 AS operator,
        '' AS dateLabel,
        CAST(报工次数 AS VARCHAR(20)) AS reportCount,
        CAST(报工数量 AS VARCHAR(20)) AS reportQty,
        '' AS contractNo,
        '' AS orderNo,
        '' AS productCode,
        '' AS productName,
        '' AS processName,
        '' AS startTime,
        '' AS endTime,
        CAST(总工时小时 AS VARCHAR(20)) AS workHours
    FROM 操作人汇总

    UNION ALL

    SELECT 
        2 AS [Level],
        工位名称 AS operator,
        工作日期 AS dateLabel,
        CAST(报工次数 AS VARCHAR(20)) AS reportCount,
        CAST(报工数量 AS VARCHAR(20)) AS reportQty,
        '' AS contractNo,
        '' AS orderNo,
        '' AS productCode,
        '' AS productName,
        '' AS processName,
        '' AS startTime,
        '' AS endTime,
        CAST(工时小时 AS VARCHAR(20)) AS workHours
    FROM 日期汇总

    UNION ALL

    SELECT 
        3 AS [Level],
        工位名称 AS operator,
        工作日期 AS dateLabel,
        '' AS reportCount,
        CAST(报工数量 AS VARCHAR(20)) AS reportQty,
        ISNULL(合同号, '') AS contractNo,
        ISNULL(订单号, '') AS orderNo,
        ISNULL(产品编码, '') AS productCode,
        ISNULL(产品名称, '') AS productName,
        ISNULL(工序名称, '') AS processName,
        ISNULL(开始时间, '') AS startTime,
        ISNULL(结束时间, '') AS endTime,
        CAST(工时小时 AS VARCHAR(20)) AS workHours
    FROM 报工明细

    ORDER BY operator, dateLabel, [Level]
END

