USE [YL_MESDB]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO

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

    ;WITH 人员班组 AS (
        SELECT
            LTRIM(RTRIM(Personnel_Name)) AS Personnel_Name,
            MAX(NULLIF(LTRIM(RTRIM(班组)), '')) AS 班组
        FROM [YL_MESDB].[dbo].[登录基础数据_人员信息]
        WHERE Personnel_Name IS NOT NULL
        GROUP BY LTRIM(RTRIM(Personnel_Name))
    ),
    工时基础 AS (
        SELECT
            v.*,
            ISNULL(p.班组, '未分类') AS 工时类型,
            CASE
                WHEN v.操作类别 IN ('报工', '辅助结束') AND ISNULL(v.加工工时, 0) > 0 THEN ISNULL(v.加工工时, 0)
                WHEN v.操作类别 IN ('报工', '辅助结束') AND ISNULL(v.总时长, 0) > 0 AND ISNULL(v.加工工时, 0) = 0 THEN ISNULL(v.总时长, 0)
                WHEN v.操作类别 LIKE '%加班%' AND ISNULL(v.总时长, 0) > 0 THEN ISNULL(v.总时长, 0)
                WHEN v.操作类别 = '开始加工' AND ISNULL(v.总时长, 0) > 0 THEN ISNULL(v.总时长, 0)
                ELSE 0
            END AS 有效工时秒
        FROM View_生产工时视图 v
        LEFT JOIN 人员班组 p ON LTRIM(RTRIM(v.操作人)) = p.Personnel_Name
    ),
    订单工时汇总 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 工时基础 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 '%' + @订单号 + '%')
          AND (@工时类型 IS NULL OR @工时类型 = '' OR v.工时类型 = @工时类型)
        GROUP BY v.订单合同号, v.产品编码, v.产品名称
    ),
    工序工时明细 AS (
        SELECT 
            v.订单合同号 AS 合同号,
            v.产品编码,
            v.工序名称,
            v.工时类型,
            v.操作人,
            MAX(v.工位名称) AS 指派对象,
            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 计划完成,
            MAX(v.计划数量) AS 计划数量
        FROM 工时基础 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 '%' + @订单号 + '%')
          AND (@工时类型 IS NULL OR @工时类型 = '' OR v.工时类型 = @工时类型)
        GROUP BY v.订单合同号, v.产品编码, v.工序名称, v.工时类型, v.操作人, v.订单号
    )
    SELECT 
        1 AS [Level],
        合同号 AS contractNo,
        产品名称 AS materialDesc,
        产品编码 AS materialCode,
        CAST(NULL AS DECIMAL(19,6)) AS 计划数量,
        '' AS processName,
        '' AS 工时类型,
        '' AS operator,
        '' AS 指派对象,
        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 计划数量,
        d.工序名称 AS processName,
        d.工时类型 AS 工时类型,
        d.操作人 AS operator,
        ISNULL(d.指派对象, '') AS 指派对象,
        '' 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
GO
