
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 
            订单合同号 AS 合同号,
            订单号,
            产品编码,
            产品名称,
            工序名称,
			操作类别,
            MAX(计划数量) AS 计划数量,
            MAX(完成数量) 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 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 '%' + @订单号 + '%')
        GROUP BY 订单合同号, 订单号, 产品编码, 产品名称, 工序名称,操作类别
    ),
    报工明细 AS (
        SELECT 
            订单合同号 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 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 '%' + @订单号 + '%')
    )
    SELECT 
        1 AS [Level],
        ISNULL(合同号, '') AS contractNo,
        ISNULL(订单号, '') AS orderNo,
        ISNULL(产品编码, '') AS productCode,
        ISNULL(产品名称, '') AS productName,
        ISNULL(工序名称, '') AS processName,
        '' AS operator,
        CASE WHEN 计划数量 IS NULL THEN '0' WHEN 计划数量 = FLOOR(计划数量) THEN CAST(CAST(计划数量 AS INT) AS VARCHAR(20)) ELSE CAST(CAST(计划数量 AS DECIMAL(10,1)) AS VARCHAR(20)) END + '/' + 
        CASE WHEN 完成数量 IS NULL THEN '0' WHEN 完成数量 = FLOOR(完成数量) THEN CAST(CAST(完成数量 AS INT) AS VARCHAR(20)) ELSE CAST(CAST(完成数量 AS DECIMAL(10,1)) AS VARCHAR(20)) END AS qty,
        CAST(报工数量 AS VARCHAR(20)) AS reportQty,
        '' AS startTime,
        '' AS endTime,
        CAST(总工时小时 AS VARCHAR(20)) AS workHours,
		操作类别
    FROM 任务汇总

    UNION ALL

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

    ORDER BY contractNo, orderNo, productCode, processName, [Level], startTime
END

