
CREATE 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)
                WHEN v.操作类别 = '异常' THEN -ISNULL(v.总时长, 0)
                ELSE 0
            END AS 有效工时秒
        FROM [dbo].[View_生产工时视图全部] v
        LEFT JOIN 人员班组 p ON LTRIM(RTRIM(v.操作人)) = p.Personnel_Name
    ),
    筛选明细 AS (
        SELECT *
        FROM 工时基础
        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 (@工时类型 IS NULL OR @工时类型 = '' OR 工时类型 = @工时类型)
          AND ISNULL(阶段标识, 0) != -1
    ),
    TaskStart报工数量 AS (
        SELECT
            TaskAID,
            CONVERT(VARCHAR(19), 开始时间, 120) AS 开始时间,
            MAX(CASE
                WHEN 操作类别 NOT IN ('异常') THEN ISNULL(操作数值, 0)
                WHEN 操作类别 = '异常' THEN -ISNULL(操作数值, 0)
                ELSE 0
            END) AS 报工数量
        FROM [dbo].[View_生产工时视图全部]
        WHERE 操作类别编号 = 6
        GROUP BY TaskAID, CONVERT(VARCHAR(19), 开始时间, 120)
    ),
    报工明细 AS (
        SELECT
            b.操作人,
            CONVERT(VARCHAR(10), b.开始时间, 120) AS 工作日期,
            b.TaskAID,
            b.订单合同号 AS 合同号,
            b.订单号,
            b.产品编码,
            b.产品名称,
            b.工序名称,
            b.工时类型,
            b.计划数量,
            b.工位名称 AS 指派对象,
            ISNULL(q.报工数量, CASE
                WHEN b.操作类别 NOT IN ('异常') THEN ISNULL(b.操作数值, 0)
                WHEN b.操作类别 = '异常' THEN -ISNULL(b.操作数值, 0)
                ELSE 0
            END) AS 报工数量,
            CONVERT(VARCHAR(19), b.开始时间, 120) AS 开始时间,
            CONVERT(VARCHAR(19), b.结束时间, 120) AS 结束时间,
            CAST(b.有效工时秒 / 60.0 / 60.0 AS DECIMAL(10,2)) AS 工时小时,
            b.异常说明,
            b.工序说明,
            b.操作类别
        FROM 筛选明细 b
        LEFT JOIN TaskStart报工数量 q
          ON q.TaskAID = b.TaskAID
         AND q.开始时间 = CONVERT(VARCHAR(19), b.开始时间, 120)
    ),
    操作人报工数量 AS (
        SELECT 操作人, SUM(报工数量) AS 报工数量
        FROM (
            SELECT 操作人, TaskAID, 开始时间, MAX(报工数量) AS 报工数量
            FROM 报工明细
            WHERE TaskAID IS NOT NULL AND 开始时间 IS NOT NULL AND 开始时间 != ''
            GROUP BY 操作人, TaskAID, 开始时间
        ) x
        GROUP BY 操作人
    ),
    日期报工数量 AS (
        SELECT 操作人, 工作日期, SUM(报工数量) AS 报工数量
        FROM (
            SELECT 操作人, 工作日期, TaskAID, 开始时间, MAX(报工数量) AS 报工数量
            FROM 报工明细
            WHERE TaskAID IS NOT NULL AND 开始时间 IS NOT NULL AND 开始时间 != ''
            GROUP BY 操作人, 工作日期, TaskAID, 开始时间
        ) x
        GROUP BY 操作人, 工作日期
    ),
    操作人汇总 AS (
        SELECT
            操作人,
            COUNT(*) AS 报工次数,
            CAST(SUM(有效工时秒) / 60.0 / 60.0 AS DECIMAL(10,1)) AS 总工时小时
        FROM 筛选明细
        GROUP BY 操作人
    ),
    日期汇总 AS (
        SELECT
            操作人,
            CONVERT(VARCHAR(10), 开始时间, 120) AS 工作日期,
            COUNT(*) AS 报工次数,
            CAST(SUM(有效工时秒) / 60.0 / 60.0 AS DECIMAL(10,1)) AS 工时小时
        FROM 筛选明细
        GROUP BY 操作人, CONVERT(VARCHAR(10), 开始时间, 120)
    )
    SELECT
        1 AS [Level],
        s.操作人 AS operator,
        '' AS dateLabel,
        CAST(s.报工次数 AS VARCHAR(20)) AS reportCount,
        CAST(ISNULL(q.报工数量, 0) AS VARCHAR(20)) AS reportQty,
        '' AS contractNo,
        '' AS orderNo,
        '' AS productCode,
        '' AS productName,
        '' AS processName,
        CAST(NULL AS DECIMAL(19,6)) AS 计划数量,
        '' AS 工时类型,
        '' AS 指派对象,
        '' AS startTime,
        '' AS endTime,
        '' AS 异常说明,
        '' AS 工序说明,
        '' AS 操作类别,
        CAST(s.总工时小时 AS VARCHAR(20)) AS workHours,
        '' AS TaskAID
    FROM 操作人汇总 s
    LEFT JOIN 操作人报工数量 q ON q.操作人 = s.操作人

    UNION ALL

    SELECT
        2 AS [Level],
        s.操作人 AS operator,
        s.工作日期 AS dateLabel,
        CAST(s.报工次数 AS VARCHAR(20)) AS reportCount,
        CAST(ISNULL(q.报工数量, 0) AS VARCHAR(20)) AS reportQty,
        '' AS contractNo,
        '' AS orderNo,
        '' AS productCode,
        '' AS productName,
        '' AS processName,
        CAST(NULL AS DECIMAL(19,6)) AS 计划数量,
        '' AS 工时类型,
        '' AS 指派对象,
        '' AS startTime,
        '' AS endTime,
        '' AS 异常说明,
        '' AS 工序说明,
        '' AS 操作类别,
        CAST(s.工时小时 AS VARCHAR(20)) AS workHours,
        '' AS TaskAID
    FROM 日期汇总 s
    LEFT JOIN 日期报工数量 q ON q.操作人 = s.操作人 AND q.工作日期 = s.工作日期

    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,
        计划数量 AS 计划数量,
        ISNULL(工时类型, '') AS 工时类型,
        ISNULL(指派对象, '') AS 指派对象,
        ISNULL(开始时间, '') AS startTime,
        ISNULL(结束时间, '') AS endTime,
        ISNULL(异常说明, '') AS 异常说明,
        ISNULL(工序说明, '') AS 工序说明,
        ISNULL(操作类别, '') AS 操作类别,
        CAST(工时小时 AS VARCHAR(20)) AS workHours,
        CAST(TaskAID AS VARCHAR(50)) AS TaskAID
    FROM 报工明细

    ORDER BY operator, dateLabel, [Level], startTime
END

