USE [YL_MESDB]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER 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;

    IF @开始日期 = CONVERT(DATE, '19000101', 112) SET @开始日期 = NULL;
    IF @结束日期 = CONVERT(DATE, '19000101', 112) SET @结束日期 = NULL;

    ;WITH 操作人汇总 AS (
        SELECT 
            工位名称,
            COUNT(*) AS 报工次数,
            SUM(CASE WHEN 操作类别 IN (N'报工', N'辅助结束') THEN 操作数值 ELSE 0 END) AS 报工数量,
            CAST(SUM(加工工时) / 60.0 / 60.0 AS DECIMAL(10,1)) AS 总工时小时
        FROM View_生产工时视图全部
        WHERE (@开始日期 IS NULL OR 结束时间 >= @开始日期)
          AND (@结束日期 IS NULL 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 (N'报工', N'辅助结束') THEN 操作数值 ELSE 0 END) AS 报工数量,
            CAST(SUM(加工工时) / 60.0 / 60.0 AS DECIMAL(10,1)) AS 工时小时
        FROM View_生产工时视图全部
        WHERE (@开始日期 IS NULL OR 结束时间 >= @开始日期)
          AND (@结束日期 IS NULL 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 合同号,
            订单号,
            产品编码,
            产品名称,
            CAST(计划数量 AS DECIMAL(19,6)) AS 计划数量,
            工序名称,
            工位名称 AS 指派对象,
            CASE
                WHEN 操作描述 LIKE N'%白班%' THEN N'白班'
                WHEN 操作描述 LIKE N'%夜班%' THEN N'夜班'
                ELSE N''
            END AS 班次,
            操作类别 AS 报工类型,
            CASE WHEN 操作类别 IN (N'报工', N'辅助结束') 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 结束时间 >= @开始日期)
          AND (@结束日期 IS NULL 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,
        CAST(NULL AS DECIMAL(19,6)) AS 计划数量,
        '' AS processName,
        '' AS 班次,
        '' AS 报工类型,
        '' AS 指派对象,
        '' 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,
        CAST(NULL AS DECIMAL(19,6)) AS 计划数量,
        '' AS processName,
        '' AS 班次,
        '' AS 报工类型,
        '' AS 指派对象,
        '' 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,
        计划数量 AS 计划数量,
        ISNULL(工序名称, '') AS processName,
        ISNULL(班次, '') AS 班次,
        ISNULL(报工类型, '') AS 报工类型,
        ISNULL(指派对象, '') AS 指派对象,
        ISNULL(开始时间, '') AS startTime,
        ISNULL(结束时间, '') AS endTime,
        CAST(工时小时 AS VARCHAR(20)) AS workHours
    FROM 报工明细

    ORDER BY operator, dateLabel, [Level]
END
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;

    IF @开始日期 = CONVERT(DATE, '19000101', 112) SET @开始日期 = NULL;
    IF @结束日期 = CONVERT(DATE, '19000101', 112) SET @结束日期 = NULL;

    ;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 结束时间 >= @开始日期)
          AND (@结束日期 IS NULL 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.工序名称,
            MAX(v.工序说明) AS 工序说明,
            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 结束时间 >= @开始日期)
          AND (@结束日期 IS NULL 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 工时类型,
        '' 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,
        ISNULL(d.工序说明, '') AS 工序说明,
        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

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;

    IF @开始日期 = CONVERT(DATE, '19000101', 112) SET @开始日期 = NULL;
    IF @结束日期 = CONVERT(DATE, '19000101', 112) SET @结束日期 = NULL;

    ;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 
            订单合同号 AS 合同号,
            订单号,
            产品编码,
            产品名称,
            工序名称,
            MAX(工序说明) 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 工时基础
        WHERE (@开始日期 IS NULL OR 结束时间 >= @开始日期)
          AND (@结束日期 IS NULL 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 '%' + @订单号 + '%')
          AND (@工时类型 IS NULL OR @工时类型 = '' OR 工时类型 = @工时类型)
        GROUP BY 订单合同号, 订单号, 产品编码, 产品名称, 工序名称, 工时类型, 操作类别
    ),
    报工明细 AS (
        SELECT 
            订单合同号 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 工时基础
        WHERE (@开始日期 IS NULL OR 结束时间 >= @开始日期)
          AND (@结束日期 IS NULL 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 '%' + @订单号 + '%')
          AND (@工时类型 IS NULL OR @工时类型 = '' OR 工时类型 = @工时类型)
    )
    SELECT 
        1 AS [Level],
        ISNULL(合同号, '') AS contractNo,
        ISNULL(订单号, '') AS orderNo,
        ISNULL(产品编码, '') AS productCode,
        ISNULL(产品名称, '') AS productName,
        ISNULL(工序名称, '') AS processName,
        ISNULL(工序说明, '') AS 工序说明,
        CAST(计划数量 AS DECIMAL(19,6)) AS 计划数量,
        ISNULL(工时类型, '') AS 工时类型,
        '' AS operator,
        '' AS 指派对象,
        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 工序说明,
        CAST(计划数量 AS DECIMAL(19,6)) AS 计划数量,
        ISNULL(工时类型, '') AS 工时类型,
        ISNULL(操作人, '') AS operator,
        ISNULL(指派对象, '') AS 指派对象,
        '' 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
GO
