CREATE OR ALTER PROCEDURE [dbo].[MES_Kanban_ProductionInfo]
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @today date = CONVERT(date, GETDATE());
    DECLARE @tomorrow date = DATEADD(day, 1, @today);
    DECLARE @monthStart date = DATEFROMPARTS(YEAR(@today), MONTH(@today), 1);

    DECLARE @currentPlan decimal(18, 2) = 0;
    DECLARE @currentOutput decimal(18, 2) = 0;
    DECLARE @completionRate decimal(18, 2) = 0;
    DECLARE @workHours decimal(18, 1) = 0;
    DECLARE @kitRate decimal(18, 2) = 0;
    DECLARE @qualityRate decimal(18, 2) = 0;

    ;WITH AssemblyOrders AS
    (
        SELECT
            CONVERT(nvarchar(100), [订单编号]) AS [订单编号],
            MAX(ISNULL(CONVERT(decimal(18, 4), [计划数量订单]), 0)) AS [计划数量],
            MAX(ISNULL(CONVERT(decimal(18, 4), [完成数量]), 0)) AS [完成数量]
        FROM [dbo].[View_生产订单_MES]
        WHERE [计划完成] >= @monthStart
          AND [计划完成] < @tomorrow
          AND [自制件属性] LIKE N'%装配%'
        GROUP BY CONVERT(nvarchar(100), [订单编号])
    )
    SELECT
        @currentPlan = ISNULL(SUM([计划数量]), 0),
        @currentOutput = ISNULL(SUM([完成数量]), 0)
    FROM AssemblyOrders;

    SET @completionRate = CAST(
        CASE WHEN @currentPlan > 0
             THEN @currentOutput * 100.0 / @currentPlan
             ELSE 0
        END AS decimal(18, 2)
    );

    SELECT @workHours = CAST(ISNULL(SUM(ISNULL([加工工时], 0)), 0) / 3600.0 AS decimal(18, 1))
    FROM [dbo].[View_生产工时视图全部]
    WHERE [结束时间] >= @monthStart
      AND [结束时间] < @tomorrow
      AND ISNULL([阶段标识], 0) = -1
      AND ISNULL([加工工时], 0) > 0;

    SELECT @kitRate = CAST(
        CASE WHEN COUNT_BIG(1) > 0
             THEN SUM(CASE WHEN ISNULL([齐套], 0) = 1 THEN 1 ELSE 0 END) * 100.0 / COUNT_BIG(1)
             ELSE 0
        END AS decimal(18, 2)
    )
    FROM [dbo].[View_生产订单_MES]
    WHERE [计划开始时间] >= @monthStart
      AND [计划开始时间] < @tomorrow
      AND [自制件属性] LIKE N'%装配%'
      AND [物料编码] NOT LIKE N'%3420-HT2%'
      AND [物料编码] NOT LIKE N'%3420-YT2%'
      AND [物料编码] NOT LIKE N'%3420-YF2%'
      AND [物料编码] NOT LIKE N'%3420-YP2%';

    SELECT @qualityRate = CAST(
        CASE WHEN ISNULL(SUM([检验数量]), 0) > 0
             THEN ISNULL(SUM([合格数]), 0) * 100.0 / SUM([检验数量])
             ELSE 0
        END AS decimal(18, 2)
    )
    FROM [dbo].[View_质量检验_质检记录]
    WHERE [工序名称] = N'装配'
      AND [检测时间] >= @monthStart
      AND [检测时间] < @tomorrow;

    SELECT [Zone], [Sort], [TagName], [TagValue], [TagUnit], [Icon], [color]
    FROM
    (
        SELECT N'生产' AS [Zone], 1 AS [Sort], N'当前计划' AS [TagName],
               CONVERT(nvarchar(50), @currentPlan) AS [TagValue], N'台' AS [TagUnit],
               N'AssemblyPlan' AS [Icon], N'#0678b4' AS [color]
        UNION ALL
        SELECT N'生产', 2, N'当前产量', CONVERT(nvarchar(50), @currentOutput), N'台',
               N'ManufacturingExecution', N'#68b720'
        UNION ALL
        SELECT N'生产', 3, N'计划完成', CONVERT(nvarchar(50), @completionRate), N'%',
               N'ProductionCheck', N'#e97378'
        UNION ALL
        SELECT N'生产', 4, N'工时消耗', CONVERT(nvarchar(50), @workHours), N'h',
               N'ManHourStatistics', N'#9d6dd5'
        UNION ALL
        SELECT N'生产', 5, N'齐套率', CONVERT(nvarchar(50), @kitRate), N'%',
               N'GroupMatching', N'#dc7e86'
        UNION ALL
        SELECT N'生产', 6, N'合格率', CONVERT(nvarchar(50), @qualityRate), N'%',
               N'QualityBasicData', N'#fafc80'
    ) AS ProductionInfo
    ORDER BY [Sort];
END;
