ALTER PROCEDURE [dbo].[看板_主页六个关键数据_查询]
AS
BEGIN
  SET NOCOUNT ON;
  
  DECLARE @nowDateTime DATETIME = GETDATE()
  
  -- 计算6个关键指标
  DECLARE @当前计划 FLOAT = 0
  SELECT @当前计划 = ISNULL(SUM(ISNULL(指派数量, 0)), 0)
  FROM View_生产订单_MES 
  WHERE YEAR(计划完成) = YEAR(@nowDateTime) AND MONTH(计划完成) = MONTH(@nowDateTime)
  
  DECLARE @当前产量 FLOAT = 0
  SELECT @当前产量 = ISNULL(SUM(ISNULL(完成数量, 0)), 0)
  FROM View_生产订单_MES 
  WHERE YEAR(计划完成) = YEAR(@nowDateTime) AND MONTH(计划完成) = MONTH(@nowDateTime)
  
  DECLARE @计划完成 FLOAT = 0
  IF @当前计划 > 0
    SET @计划完成 = ROUND((@当前产量 / @当前计划) * 100, 1)
  
  DECLARE @工时消耗 FLOAT = 0
  SELECT @工时消耗 = ISNULL(SUM(ISNULL(加工工时, 0)), 0)
  FROM View_生产工时视图 
  WHERE YEAR(报工日期) = YEAR(@nowDateTime) AND MONTH(报工日期) = MONTH(@nowDateTime)
  
  DECLARE @齐套订单数 FLOAT = 0, @总订单数 FLOAT = 0, @当前齐套率 FLOAT = 0
  SELECT @齐套订单数 = COUNT(DISTINCT CASE WHEN 齐套 = 1 THEN 订单编号 END),
         @总订单数 = COUNT(DISTINCT 订单编号)
  FROM View_生产订单_MES
  WHERE YEAR(计划完成) = YEAR(@nowDateTime) AND MONTH(计划完成) = MONTH(@nowDateTime)
  IF @总订单数 > 0
    SET @当前齐套率 = ROUND((@齐套订单数 / @总订单数) * 100, 1)
  
  DECLARE @当前合格率 FLOAT = 0, @不合格总数 FLOAT = 0
  SELECT @不合格总数 = ISNULL(SUM(ISNULL(不合格数量, 0)), 0)
  FROM View_生产订单_MES
  WHERE YEAR(计划完成) = YEAR(@nowDateTime) AND MONTH(计划完成) = MONTH(@nowDateTime)
  IF @当前产量 > 0
    SET @当前合格率 = ROUND(((@当前产量 - @不合格总数) / @当前产量) * 100, 1)
  ELSE
    SET @当前合格率 = 100

  -- 统一格式返回所有数据
  SELECT Zone, Sort, TagName, TagValue, TagUnit, Icon, color FROM (
    -- 生产信息 6项
    SELECT '生产' AS Zone, 1 AS Sort, '当前计划' AS TagName, CAST(@当前计划 AS NVARCHAR(50)) AS TagValue, '台' AS TagUnit, 'AssemblyPlan' AS Icon, '#0678b4' AS color
    UNION ALL SELECT '生产', 2, '当前产量', CAST(@当前产量 AS NVARCHAR(50)), '台', 'ManufacturingExecution', '#68b720'
    UNION ALL SELECT '生产', 3, '计划完成', CAST(@计划完成 AS NVARCHAR(50)), '%', 'ProductionCheck', '#e97378'
    UNION ALL SELECT '生产', 4, '工时消耗', CAST(@工时消耗 AS NVARCHAR(50)), 'h', 'ManHourStatistics', '#9d6dd5'
    UNION ALL SELECT '生产', 5, '齐套率', CAST(@当前齐套率 AS NVARCHAR(50)), '%', 'GroupMatching', '#dc7e86'
    UNION ALL SELECT '生产', 6, '合格率', CAST(@当前合格率 AS NVARCHAR(50)), '%', 'QualityBasicData', '#fafc80'
    -- 环境监测 8项
    UNION ALL SELECT '环境', 1, '二氧化碳', '420', 'ppm', 'CloudData', ''
    UNION ALL SELECT '环境', 2, '甲醛', '0.08', 'mg/m³', 'SafetyMargin', ''
    UNION ALL SELECT '环境', 3, 'TVOC', '0.15', 'mg/m³', 'QualityBasicData', ''
    UNION ALL SELECT '环境', 4, 'PM2.5', '35', 'μg/m³', 'OqcInformation', ''
    UNION ALL SELECT '环境', 5, 'PM10', '65', 'μg/m³', 'OqcInformation', ''
    UNION ALL SELECT '环境', 6, '温度', '24.5', '℃', 'MeasurementRangeMaintenance', ''
    UNION ALL SELECT '环境', 7, '湿度', '45', '%RH', 'Maintenancebasicequipmentinformation', ''
    UNION ALL SELECT '环境', 8, '噪声', '55', 'dB', 'EquipmentAlarmStatistics', ''
    -- 水 3项
    UNION ALL SELECT '水', 1, '瞬时流量', '2.5', 'm³/min', 'MaterialPull', ''
    UNION ALL SELECT '水', 2, '累计流量', '1580', 'm³', 'InventoryManagement', ''
    UNION ALL SELECT '水', 3, '压力', '0.35', 'Mpa', 'EquipmentStatusMonitoring', ''
    -- 电 4项
    UNION ALL SELECT '电', 1, '电压A', '380', 'V', 'ElectricalComponentParameters', ''
    UNION ALL SELECT '电', 2, '电压B', '382', 'V', 'ElectricalComponentParameters', ''
    UNION ALL SELECT '电', 3, '电压C', '378', 'V', 'ElectricalComponentParameters', ''
    UNION ALL SELECT '电', 4, '电能', '1250', 'kwh', 'RealTimeStatusEquipment', ''
    -- 气 4项
    UNION ALL SELECT '气', 1, '瞬时流量', '15.8', 'm³/min', 'MaterialPull', ''
    UNION ALL SELECT '气', 2, '累计流量', '8520', 'm³', 'InventoryManagement', ''
    UNION ALL SELECT '气', 3, '温度', '22', '℃', 'MeasurementRangeMaintenance', ''
    UNION ALL SELECT '气', 4, '压力', '0.65', 'Mpa', 'EquipmentStatusMonitoring', ''
  ) t
  ORDER BY 
    CASE Zone WHEN '生产' THEN 0 WHEN '环境' THEN 1 WHEN '水' THEN 2 WHEN '电' THEN 3 WHEN '气' THEN 4 END,
    Sort
END
GO

ALTER PROCEDURE [dbo].[看板_左上订单看板_表格_查询]
AS
BEGIN
  SET NOCOUNT ON;
  
  SELECT TOP 50
    t.合同号 AS 内部合同号,
    CONVERT(nvarchar(10), t.最大计划完成, 23) AS 交货日期,
    t.工艺部,
    CAST(0 AS DECIMAL(5,1)) AS 采购部,
    ISNULL(lp.生产进度, 0) AS 生产部,
    t.装配部
  FROM (
    SELECT 
      订单编号,
      MAX(合同号) AS 合同号,
      MAX(计划完成) AS 最大计划完成,
      -- 工艺部：有生产工艺的任务占比
      ISNULL(CAST(ROUND(
        SUM(CASE WHEN 生产工艺 = 1 THEN 1.0 ELSE 0 END) / NULLIF(COUNT(*), 0) * 100
      , 1) AS DECIMAL(5,1)), 0) AS 工艺部,
      -- 装配部：自制件属性包含"装配"的工序完成进度
      ISNULL(CAST(ROUND(
        SUM(CASE WHEN 自制件属性 LIKE '%装配%' THEN ISNULL(完成数量, 0) ELSE 0 END) * 100.0 / 
        NULLIF(SUM(CASE WHEN 自制件属性 LIKE '%装配%' THEN ISNULL(指派数量, 0) ELSE 0 END), 0)
      , 1) AS DECIMAL(5,1)), 0) AS 装配部
    FROM View_生产订单_MES
    WHERE 合同号 IS NOT NULL AND 合同号 <> ''
    GROUP BY 订单编号
  ) t
  OUTER APPLY (
    -- 生产部：取当前订单中顺序编号最大的那道工序的进度
    SELECT TOP 1
      CAST(ROUND(
        SUM(ISNULL(完成数量, 0)) * 100.0 / NULLIF(SUM(ISNULL(指派数量, 0)), 0)
      , 1) AS DECIMAL(5,1)) AS 生产进度
    FROM View_生产订单_MES
    WHERE 订单编号 = t.订单编号
      AND 顺序编号 = (SELECT MAX(顺序编号) FROM View_生产订单_MES WHERE 订单编号 = t.订单编号)
    GROUP BY 顺序编号
  ) lp
  ORDER BY t.最大计划完成 DESC
END
GO

ALTER PROCEDURE [dbo].[看板_左中装配看板_表格_查询]
AS
BEGIN
  SET NOCOUNT ON;

  SELECT TOP 50
    合同号 AS 内部合同号,
    物料描述 AS 产品名称,
    CONVERT(nvarchar(10), ISNULL(要求发货日期, 计划完成), 23) AS 要求发货时间,
    CASE 
      WHEN 发货状态 = '1' THEN '已出库'
      WHEN 发货状态 = '0' THEN '未出库'
      ELSE '未出库'
    END AS 出库状态
  FROM View_生产订单_MES
  WHERE 自制件属性 IN ('机械装配', '电气装配')
    AND 合同号 IS NOT NULL AND 合同号 <> ''
  ORDER BY ISNULL(要求发货日期, 计划完成) DESC
END
GO

ALTER PROCEDURE [dbo].[看板_左下未完成看板_表格_查询]
AS
BEGIN
  SET NOCOUNT ON;

  SELECT TOP 50
    m.合同号 AS 内部合同号,
    m.物料描述 AS 零件名称,
    CONVERT(nvarchar(10), m.计划完成时间, 23) AS 要求完工日,
    m.工序名称,
    CASE m.自制件属性 
      WHEN '机加件' THEN '自制'
      WHEN '机械装配' THEN '自制'
      WHEN '电气装配' THEN '自制'
      ELSE ISNULL(m.自制件属性, '自制')
    END AS 性质,
    CAST(ISNULL(h.完工工时合计, 0) AS DECIMAL(10,1)) AS 完工工时,
    CAST(
      CASE 
        WHEN ISNULL(p.标准工时, 0) - ISNULL(h.完工工时合计, 0) < 0 THEN 0
        ELSE ISNULL(p.标准工时, 0) - ISNULL(h.完工工时合计, 0) 
      END AS DECIMAL(10,1)
    ) AS 未完工工时
  FROM View_生产订单_MES m
  LEFT JOIN (
    -- 按TaskAID汇总已完成工时
    SELECT TaskAID, SUM(ISNULL(加工工时, 0)) AS 完工工时合计
    FROM View_生产工时视图
    GROUP BY TaskAID
  ) h ON m.TaskAID = h.TaskAID
  LEFT JOIN View_生产工艺视图 p ON m.订单编号 = p.订单编号 AND m.顺序编号 = p.工艺顺序
  WHERE m.任务状态 IN (0, 1, 2)  -- 未完工状态
    AND m.合同号 IS NOT NULL AND m.合同号 <> ''
  ORDER BY m.计划完成时间 DESC
END
GO

ALTER PROCEDURE [dbo].[看板_左上计划执行情况_曲线_查询]
AS
BEGIN
  SET NOCOUNT ON;

  DECLARE @date DATE = CAST(GETDATE() AS DATE)

  SELECT 
    RIGHT(CONVERT(nvarchar(10), MAX(计划完成), 23), 5) AS 交货日期,
    ISNULL(CAST(ROUND(AVG(
      CASE WHEN 生产工艺 = 1 THEN 100.0 ELSE 0 END
    ), 1) AS DECIMAL(5,1)), 0) AS 工艺部,
    CAST(0 AS DECIMAL(5,1)) AS 采购部,
    ISNULL(CAST(ROUND(AVG(
      CASE WHEN ISNULL(指派数量, 0) > 0 THEN ISNULL(完成数量, 0) * 100.0 / 指派数量 ELSE 0 END
    ), 1) AS DECIMAL(5,1)), 0) AS 生产部,
    -- 装配部：自制件属性包含“装配”的工序完成进度
    ISNULL(CAST(ROUND(AVG(
      CASE WHEN 自制件属性 LIKE '%装配%' AND ISNULL(指派数量, 0) > 0 
        THEN ISNULL(完成数量, 0) * 100.0 / 指派数量 
        ELSE NULL END
    ), 1) AS DECIMAL(5,1)), 0) AS 装配部,
    COUNT(1) AS 条数
  FROM View_生产订单_MES
  WHERE 合同号 IS NOT NULL AND 合同号 <> ''
    AND CAST(计划完成 AS DATE) >= DATEADD(DAY, -10, @date)
    AND CAST(计划完成 AS DATE) <= @date
  GROUP BY CAST(计划完成 AS DATE)
  ORDER BY CAST(计划完成 AS DATE)
END
GO

ALTER PROCEDURE [dbo].[看板_中间主体订单_表格_查询]
AS
BEGIN
  SET NOCOUNT ON;

  SELECT TOP 100
    t.合同号 AS 内部合同号,
    '' AS 签订日期,
    '' AS 交货日期,
    t.工艺部,
    CAST(0 AS DECIMAL(5,1)) AS 采购部,
    ISNULL(lp.生产进度, 0) AS 生产部,
    t.装配部
  FROM (
    SELECT 
      订单编号,
      MAX(合同号) AS 合同号,
      MAX(计划完成) AS 最大计划完成,
      ISNULL(CAST(ROUND(
        SUM(CASE WHEN 生产工艺 = 1 THEN 1.0 ELSE 0 END) / NULLIF(COUNT(*), 0) * 100
      , 1) AS DECIMAL(5,1)), 0) AS 工艺部,
      -- 装配部：自制件属性包含“装配”的工序完成进度
      ISNULL(CAST(ROUND(
        SUM(CASE WHEN 自制件属性 LIKE '%装配%' THEN ISNULL(完成数量, 0) ELSE 0 END) * 100.0 / 
        NULLIF(SUM(CASE WHEN 自制件属性 LIKE '%装配%' THEN ISNULL(指派数量, 0) ELSE 0 END), 0)
      , 1) AS DECIMAL(5,1)), 0) AS 装配部
    FROM View_生产订单_MES
    WHERE 合同号 IS NOT NULL AND 合同号 <> ''
    GROUP BY 订单编号
  ) t
  OUTER APPLY (
    SELECT TOP 1
      CAST(ROUND(
        SUM(ISNULL(完成数量, 0)) * 100.0 / NULLIF(SUM(ISNULL(指派数量, 0)), 0)
      , 1) AS DECIMAL(5,1)) AS 生产进度
    FROM View_生产订单_MES
    WHERE 订单编号 = t.订单编号
      AND 顺序编号 = (SELECT MAX(顺序编号) FROM View_生产订单_MES WHERE 订单编号 = t.订单编号)
    GROUP BY 顺序编号
  ) lp
  ORDER BY t.最大计划完成 DESC
END
GO

ALTER PROCEDURE [dbo].[看板_中间主体订单详情_表格_查询]
  @合同号 NVARCHAR(50) = NULL
AS
BEGIN
  SET NOCOUNT ON;

  SELECT
    物料描述 AS 名称,
    物料编号 AS 规格型号,
    自制件属性 AS 零件属性,
    指派数量 AS 额定数量,
    ISNULL(指派数量, 0) AS 总需求,
    0 AS 库存数量,
    0 AS 待出数量,
    0 AS 可用库存,
    0 AS 满足情况,
    0 AS 占用数量,
    0 AS 在购数量,
    0 AS 满足二,
    0 AS 满足三,
    物料编号 AS 产品编码,
    合同号
  FROM View_生产订单_MES
  WHERE 合同号 = @合同号
  ORDER BY 顺序编号
END
GO

ALTER PROCEDURE [dbo].[看板_右上合同看板_表格_查询]
AS
BEGIN
  SET NOCOUNT ON;
  
  -- 合同看板数据暂无，返回空结果集保持前端一致
  SELECT TOP 0
    '' AS 交货期,
    '' AS 合同号,
    '' AS 产品名称,
    '' AS 工艺,
    '' AS 采购,
    '' AS 加工,
    '' AS 备料,
    '' AS 装配,
    '' AS 成品入库,
    '' AS 合同发货
END
GO

ALTER PROCEDURE [dbo].[看板_右下质量看板_表格_查询]
AS
BEGIN
  SET NOCOUNT ON;
  
  -- 质量看板数据暂无，返回空结果集保持前端一致
  SELECT TOP 0
    '' AS 出库单号,
    '' AS 加工总时,
    '' AS 有无异常,
    '' AS 缺陷类型,
    '' AS 检验人,
    '' AS 检验时间
END
GO

ALTER PROCEDURE [dbo].[看板_右下质量看板_饼图_查询]
AS
BEGIN
  SET NOCOUNT ON;
  
  -- 质量饼图数据暂无，返回默认值保持前端一致
  SELECT '无' AS 缺陷类型, 0 AS 数量
END
GO

ALTER PROCEDURE [dbo].[看板_中间实时OEE数据展示_查询]
AS
BEGIN
  SET NOCOUNT ON;
  
  CREATE TABLE #t (
    产品名称 NVARCHAR(50),
    OEE NVARCHAR(50),
    时间利用率 NVARCHAR(50),
    性能利用率 NVARCHAR(50),
    合格率 NVARCHAR(50),
    设备状态 NVARCHAR(50),
    进度 NVARCHAR(50)
  )
  INSERT INTO #t 
  VALUES ('加工中心1', '85.6', '88.2', '78.9', '98.3', '正常', '45'),
         ('加工中心2', '82.6', '82.2', '83.9', '93.3', '正常', '63')
  SELECT * FROM #t
  DROP TABLE #t
END
GO