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 
            v.订单合同号 AS 合同号,
            v.产品编码,
            v.产品名称,
            MAX(v.计划数量) AS 计划数量,
            MAX(v.完成数量) AS 完成数量,
            SUM(CASE WHEN v.操作类别 IN ('报工', '辅助结束') THEN v.操作数值 ELSE 0 END) AS 报工数量,
            MIN(CONVERT(VARCHAR(10), v.报工日期, 120)) AS 计划开始,
            MAX(CONVERT(VARCHAR(10), v.报工日期, 120)) AS 计划完成,
            CAST(SUM(v.加工工时) / 60.0 / 60.0 AS DECIMAL(10,1)) AS 总工时小时
        FROM View_生产工时视图 v
        WHERE (@开始日期 IS NULL OR @开始日期 = '' OR 结束时间 >= @开始日期)
          AND (@结束日期 IS NULL OR @结束日期 = '' OR 开始时间 <= @结束日期)
          AND (@合同号 IS NULL OR @合同号 = '' OR v.订单合同号 LIKE '%' + @合同号 + '%')
          AND (@操作人 IS NULL OR @操作人 = '' OR v.操作人 LIKE '%' + @操作人 + '%')
          AND (@产品编码 IS NULL OR @产品编码 = '' OR v.产品编码 LIKE '%' + @产品编码 + '%')
          AND (@工序名称 IS NULL OR @工序名称 = '' OR v.工序名称 LIKE '%' + @工序名称 + '%')
          AND (@订单号 IS NULL OR @订单号 = '' OR v.订单号 LIKE '%' + @订单号 + '%')
        GROUP BY v.订单合同号, v.产品编码, v.产品名称
    ),
    工序工时明细 AS (
        SELECT 
            v.订单合同号 AS 合同号,
            v.产品编码,
            v.工序名称,
            v.操作人,
			v.订单号,
            SUM(CASE WHEN v.操作类别 IN ('报工', '辅助结束') THEN v.操作数值 ELSE 0 END) AS 报工数量,
            CAST(SUM(v.加工工时) / 60.0 / 60.0 AS DECIMAL(10,1)) AS 工时小时,
            MIN(CONVERT(VARCHAR(10), v.报工日期, 120)) AS 计划开始,
            MAX(CONVERT(VARCHAR(10), v.报工日期, 120)) AS 计划完成
        FROM View_生产工时视图 v
        WHERE (@开始日期 IS NULL OR @开始日期 = '' OR 结束时间 >= @开始日期)
          AND (@结束日期 IS NULL OR @结束日期 = '' OR 开始时间 <= @结束日期)
          AND (@合同号 IS NULL OR @合同号 = '' OR v.订单合同号 LIKE '%' + @合同号 + '%')
          AND (@操作人 IS NULL OR @操作人 = '' OR v.操作人 LIKE '%' + @操作人 + '%')
          AND (@产品编码 IS NULL OR @产品编码 = '' OR v.产品编码 LIKE '%' + @产品编码 + '%')
          AND (@工序名称 IS NULL OR @工序名称 = '' OR v.工序名称 LIKE '%' + @工序名称 + '%')
          AND (@订单号 IS NULL OR @订单号 = '' OR v.订单号 LIKE '%' + @订单号 + '%')
        GROUP BY v.订单合同号, v.产品编码, v.工序名称, v.操作人,v.订单号
    )
    SELECT 
        1 AS [Level],
        合同号 AS contractNo,
        产品名称 AS materialDesc,
        产品编码 AS materialCode,
        '' AS processName,
        '' AS operator,
        CASE WHEN 计划数量 = FLOOR(计划数量) THEN CAST(CAST(计划数量 AS INT) AS VARCHAR(20)) ELSE CAST(CAST(计划数量 AS DECIMAL(10,1)) AS VARCHAR(20)) END + '/' + 
        CASE 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,
        ISNULL(计划开始, '') AS planStart,
        ISNULL(计划完成, '') AS planEnd,
        CAST(总工时小时 AS VARCHAR(20)) AS workHours,
		'' AS 订单号
    FROM 订单工时汇总
    
    UNION ALL
    
    SELECT 
        2 AS [Level],
        d.合同号 AS contractNo,
        '' AS materialDesc,
        d.产品编码 AS materialCode,
        d.工序名称 AS processName,
        d.操作人 AS operator,
        '' AS qty,
        CAST(d.报工数量 AS VARCHAR(20)) AS reportQty,
        d.计划开始 AS planStart,
        d.计划完成 AS planEnd,
        CAST(d.工时小时 AS VARCHAR(20)) AS workHours,
		d.订单号 AS 订单号
    FROM 工序工时明细 d
    
    ORDER BY contractNo, materialCode, [Level], processName
END

