-- 库存查询需求量性能优化：直连基础表判断订单完成状态，减少重视图展开
-- 修改人:Ld 修改时间:2026-07-14 16:35:00;

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = N'IX_Ld_零件基本件_物料组件_20260714' AND object_id = OBJECT_ID(N'dbo.PDM_零件明细表_基本件'))
BEGIN
    CREATE NONCLUSTERED INDEX IX_Ld_零件基本件_物料组件_20260714
    ON dbo.PDM_零件明细表_基本件 (物料流水号, 组件流水号)
    INCLUDE (零件数量, 已领数量);
END;
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = N'IX_Ld_零件标准外购_物料组件_20260714' AND object_id = OBJECT_ID(N'dbo.PDM_零件明细表_标准件和外购件'))
BEGIN
    CREATE NONCLUSTERED INDEX IX_Ld_零件标准外购_物料组件_20260714
    ON dbo.PDM_零件明细表_标准件和外购件 (物料流水号, 组件流水号)
    INCLUDE (零件数量, 已领数量);
END;
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = N'IX_Ld_临时领料_物料组件_20260714' AND object_id = OBJECT_ID(N'dbo.车间装配管理_装配执行_临时领料明细'))
BEGIN
    CREATE NONCLUSTERED INDEX IX_Ld_临时领料_物料组件_20260714
    ON dbo.车间装配管理_装配执行_临时领料明细 (物料流水号, 组件流水号)
    INCLUDE (实领数);
END;
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = N'IX_Ld_领料明细_基本件_20260714' AND object_id = OBJECT_ID(N'dbo.车间装配管理_领料单明细'))
BEGIN
    CREATE NONCLUSTERED INDEX IX_Ld_领料明细_基本件_20260714
    ON dbo.车间装配管理_领料单明细 (基本件流水号)
    INCLUDE (已领数量);
END;
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = N'IX_Ld_领料明细_标准外购_20260714' AND object_id = OBJECT_ID(N'dbo.车间装配管理_领料单明细'))
BEGIN
    CREATE NONCLUSTERED INDEX IX_Ld_领料明细_标准外购_20260714
    ON dbo.车间装配管理_领料单明细 (标准件和外购件流水号)
    INCLUDE (已领数量);
END;
GO

IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = N'IX_Ld_领料明细_物料组件_20260714' AND object_id = OBJECT_ID(N'dbo.车间装配管理_领料单明细'))
BEGIN
    CREATE NONCLUSTERED INDEX IX_Ld_领料明细_物料组件_20260714
    ON dbo.车间装配管理_领料单明细 (物料流水号, 组件流水号)
    INCLUDE (已领数量);
END;
GO

ALTER VIEW dbo.物料管理_需求量_视图
AS
WITH 组件订单 AS
(
    SELECT
        c.组件流水号,
        c.是否分配,
        o.订单类型,
        o.是否提交,
        p.是否启用
    FROM dbo.PDM_组件名称 c
    INNER JOIN dbo.PDM_机床名称 m ON c.机床流水号 = m.机床流水号
    INNER JOIN dbo.PDM_项目名称 p ON m.项目流水号 = p.项目流水号
    INNER JOIN dbo.合同管理_订单信息_基础表 o ON p.项目流水号 = o.项目流水号
),
需求 AS
(
    SELECT b.物料流水号, SUM(ISNULL(b.零件数量, 0)) AS 需求量
    FROM dbo.PDM_零件明细表_基本件 b
    INNER JOIN 组件订单 co ON b.组件流水号 = co.组件流水号
    WHERE co.是否分配 = 1
      AND co.订单类型 <> 3
      AND NOT (ISNULL(co.是否提交, 0) = 6 AND ISNULL(co.是否启用, 0) = 1) -- 修改人:Ld 修改时间:2026-07-14 16:35:00; 直接用基础字段排除已完成订单
    GROUP BY b.物料流水号

    UNION ALL

    SELECT s.物料流水号, SUM(ISNULL(s.零件数量, 0)) AS 需求量
    FROM dbo.PDM_零件明细表_标准件和外购件 s
    INNER JOIN 组件订单 co ON s.组件流水号 = co.组件流水号
    WHERE co.是否分配 = 1
      AND co.订单类型 <> 3
      AND NOT (ISNULL(co.是否提交, 0) = 6 AND ISNULL(co.是否启用, 0) = 1) -- 修改人:Ld 修改时间:2026-07-14 16:35:00; 直接用基础字段排除已完成订单
    GROUP BY s.物料流水号

    UNION ALL

    SELECT t.物料流水号, SUM(ISNULL(t.实领数, 0)) AS 需求量
    FROM dbo.车间装配管理_装配执行_临时领料明细 t
    LEFT JOIN 组件订单 co ON t.组件流水号 = co.组件流水号
    WHERE t.物料流水号 IS NOT NULL
      AND NOT (ISNULL(co.是否提交, 0) = 6 AND ISNULL(co.是否启用, 0) = 1) -- 修改人:Ld 修改时间:2026-07-14 16:35:00; 临时领料同口径排除已完成订单
    GROUP BY t.物料流水号
),
需求汇总 AS
(
    SELECT 物料流水号, SUM(需求量) AS 需求量
    FROM 需求
    GROUP BY 物料流水号
),
已领 AS
(
    SELECT b.物料流水号, SUM(ISNULL(l.已领数量, 0)) AS 已领数
    FROM dbo.车间装配管理_领料单明细 l
    INNER JOIN dbo.PDM_零件明细表_基本件 b ON l.基本件流水号 = b.基本件流水号
    INNER JOIN 组件订单 co ON b.组件流水号 = co.组件流水号
    WHERE NOT (ISNULL(co.是否提交, 0) = 6 AND ISNULL(co.是否启用, 0) = 1) -- 修改人:Ld 修改时间:2026-07-14 16:35:00; 扣减已领数时同步排除已完成订单
    GROUP BY b.物料流水号

    UNION ALL

    SELECT s.物料流水号, SUM(ISNULL(l.已领数量, 0)) AS 已领数
    FROM dbo.车间装配管理_领料单明细 l
    INNER JOIN dbo.PDM_零件明细表_标准件和外购件 s ON l.标准件和外购件流水号 = s.标准件和外购件流水号
    INNER JOIN 组件订单 co ON s.组件流水号 = co.组件流水号
    WHERE NOT (ISNULL(co.是否提交, 0) = 6 AND ISNULL(co.是否启用, 0) = 1) -- 修改人:Ld 修改时间:2026-07-14 16:35:00; 扣减已领数时同步排除已完成订单
    GROUP BY s.物料流水号

    UNION ALL

    SELECT l.物料流水号, SUM(ISNULL(l.已领数量, 0)) AS 已领数
    FROM dbo.车间装配管理_领料单明细 l
    LEFT JOIN 组件订单 co ON l.组件流水号 = co.组件流水号
    WHERE l.物料流水号 IS NOT NULL
      AND ISNULL(l.物料流水号, 0) <> 0
      AND ISNULL(l.基本件流水号, 0) = 0
      AND ISNULL(l.标准件和外购件流水号, 0) = 0
      AND NOT (ISNULL(co.是否提交, 0) = 6 AND ISNULL(co.是否启用, 0) = 1) -- 修改人:Ld 修改时间:2026-07-14 16:35:00; 临时件扣减已领数时同步排除已完成订单
    GROUP BY l.物料流水号
),
已领汇总 AS
(
    SELECT 物料流水号, SUM(已领数) AS 已领数
    FROM 已领
    GROUP BY 物料流水号
)
SELECT
    d.物料流水号,
    CASE WHEN d.需求量 > ISNULL(l.已领数, 0) THEN d.需求量 - ISNULL(l.已领数, 0) ELSE 0 END AS 需求量
FROM 需求汇总 d
LEFT JOIN 已领汇总 l ON d.物料流水号 = l.物料流水号;
GO

ALTER PROCEDURE [dbo].[物料管理_需求量_更新]
AS
BEGIN
    SET NOCOUNT ON;

    DELETE FROM dbo.物料管理_需求量;

    INSERT INTO dbo.物料管理_需求量
    SELECT 物料流水号, 需求量
    FROM dbo.物料管理_需求量_视图
    WHERE 需求量 > 0;
END;
GO

ALTER PROCEDURE [dbo].[仓储管理_报缺补货_需求量明细_查询]
    @物料流水号 INT = 15448
AS
BEGIN
    SET NOCOUNT ON;

    ;WITH 组件订单 AS
    (
        SELECT
            c.组件流水号,
            o.订单编号,
            c.是否分配,
            o.订单类型,
            o.是否提交,
            p.是否启用,
            ISNULL(k.简称, '') AS 简称
        FROM dbo.PDM_组件名称 c
        INNER JOIN dbo.PDM_机床名称 m ON c.机床流水号 = m.机床流水号
        INNER JOIN dbo.PDM_项目名称 p ON m.项目流水号 = p.项目流水号
        INNER JOIN dbo.合同管理_订单信息_基础表 o ON p.项目流水号 = o.项目流水号
        LEFT JOIN dbo.客户关系管理系统_客户名称 k ON p.客户流水号 = k.客户流水号
    ),
    需求明细 AS
    (
        SELECT
            co.订单编号,
            b.组件流水号,
            b.物料流水号,
            SUM(ISNULL(b.零件数量, 0)) AS 需求量,
            N'生产订单需求' AS 来源
        FROM dbo.PDM_零件明细表_基本件 b
        INNER JOIN 组件订单 co ON b.组件流水号 = co.组件流水号
        WHERE co.是否分配 = 1
          AND co.订单类型 <> 3
          AND NOT (ISNULL(co.是否提交, 0) = 6 AND ISNULL(co.是否启用, 0) = 1) -- 修改人:Ld 修改时间:2026-07-14 16:35:00; 明细不显示已完成订单
          AND (@物料流水号 IS NULL OR b.物料流水号 = @物料流水号)
        GROUP BY co.订单编号, b.组件流水号, b.物料流水号

        UNION ALL

        SELECT
            co.订单编号,
            s.组件流水号,
            s.物料流水号,
            SUM(ISNULL(s.零件数量, 0)) AS 需求量,
            N'生产订单需求' AS 来源
        FROM dbo.PDM_零件明细表_标准件和外购件 s
        INNER JOIN 组件订单 co ON s.组件流水号 = co.组件流水号
        WHERE co.是否分配 = 1
          AND co.订单类型 <> 3
          AND NOT (ISNULL(co.是否提交, 0) = 6 AND ISNULL(co.是否启用, 0) = 1) -- 修改人:Ld 修改时间:2026-07-14 16:35:00; 明细不显示已完成订单
          AND (@物料流水号 IS NULL OR s.物料流水号 = @物料流水号)
        GROUP BY co.订单编号, s.组件流水号, s.物料流水号

        UNION ALL

        SELECT
            co.订单编号,
            t.组件流水号,
            t.物料流水号,
            SUM(ISNULL(t.实领数, 0)) AS 需求量,
            N'临时领料需求' AS 来源
        FROM dbo.车间装配管理_装配执行_临时领料明细 t
        LEFT JOIN 组件订单 co ON t.组件流水号 = co.组件流水号
        WHERE t.物料流水号 IS NOT NULL
          AND NOT (ISNULL(co.是否提交, 0) = 6 AND ISNULL(co.是否启用, 0) = 1) -- 修改人:Ld 修改时间:2026-07-14 16:35:00; 临时领料明细不显示已完成订单
          AND (@物料流水号 IS NULL OR t.物料流水号 = @物料流水号)
        GROUP BY co.订单编号, t.组件流水号, t.物料流水号
    ),
    已领明细 AS
    (
        SELECT
            co.订单编号,
            b.组件流水号,
            b.物料流水号,
            SUM(ISNULL(l.已领数量, 0)) AS 已领数
        FROM dbo.车间装配管理_领料单明细 l
        INNER JOIN dbo.PDM_零件明细表_基本件 b ON l.基本件流水号 = b.基本件流水号
        INNER JOIN 组件订单 co ON b.组件流水号 = co.组件流水号
        WHERE NOT (ISNULL(co.是否提交, 0) = 6 AND ISNULL(co.是否启用, 0) = 1) -- 修改人:Ld 修改时间:2026-07-14 16:35:00; 明细扣减已领数时同步排除已完成订单
          AND (@物料流水号 IS NULL OR b.物料流水号 = @物料流水号)
        GROUP BY co.订单编号, b.组件流水号, b.物料流水号

        UNION ALL

        SELECT
            co.订单编号,
            s.组件流水号,
            s.物料流水号,
            SUM(ISNULL(l.已领数量, 0)) AS 已领数
        FROM dbo.车间装配管理_领料单明细 l
        INNER JOIN dbo.PDM_零件明细表_标准件和外购件 s ON l.标准件和外购件流水号 = s.标准件和外购件流水号
        INNER JOIN 组件订单 co ON s.组件流水号 = co.组件流水号
        WHERE NOT (ISNULL(co.是否提交, 0) = 6 AND ISNULL(co.是否启用, 0) = 1) -- 修改人:Ld 修改时间:2026-07-14 16:35:00; 明细扣减已领数时同步排除已完成订单
          AND (@物料流水号 IS NULL OR s.物料流水号 = @物料流水号)
        GROUP BY co.订单编号, s.组件流水号, s.物料流水号

        UNION ALL

        SELECT
            co.订单编号,
            l.组件流水号,
            l.物料流水号,
            SUM(ISNULL(l.已领数量, 0)) AS 已领数
        FROM dbo.车间装配管理_领料单明细 l
        LEFT JOIN 组件订单 co ON l.组件流水号 = co.组件流水号
        WHERE l.物料流水号 IS NOT NULL
          AND ISNULL(l.物料流水号, 0) <> 0
          AND ISNULL(l.基本件流水号, 0) = 0
          AND ISNULL(l.标准件和外购件流水号, 0) = 0
          AND NOT (ISNULL(co.是否提交, 0) = 6 AND ISNULL(co.是否启用, 0) = 1) -- 修改人:Ld 修改时间:2026-07-14 16:35:00; 临时件明细扣减已领数时同步排除已完成订单
          AND (@物料流水号 IS NULL OR l.物料流水号 = @物料流水号)
        GROUP BY co.订单编号, l.组件流水号, l.物料流水号
    ),
    已领汇总 AS
    (
        SELECT 订单编号, 组件流水号, 物料流水号, SUM(已领数) AS 已领数
        FROM 已领明细
        GROUP BY 订单编号, 组件流水号, 物料流水号
    ),
    明细 AS
    (
        SELECT
            d.订单编号,
            d.组件流水号,
            d.物料流水号,
            d.需求量,
            ISNULL(l.已领数, 0) AS 已领数,
            CASE WHEN d.需求量 > ISNULL(l.已领数, 0) THEN d.需求量 - ISNULL(l.已领数, 0) ELSE 0 END AS 最终需求量,
            d.来源
        FROM 需求明细 d
        LEFT JOIN 已领汇总 l
            ON d.物料流水号 = l.物料流水号
           AND d.组件流水号 = l.组件流水号
           AND ISNULL(d.订单编号, '') = ISNULL(l.订单编号, '')
    )
    SELECT
        d.订单编号,
        d.组件流水号,
        d.物料流水号,
        d.需求量,
        d.已领数,
        d.最终需求量,
        ISNULL(co.简称, '') AS 简称,
        d.来源
    FROM 明细 d
    LEFT JOIN 组件订单 co ON d.组件流水号 = co.组件流水号
    ORDER BY d.订单编号, d.组件流水号, d.来源;

    ;WITH 汇总 AS
    (
        SELECT 物料流水号, 需求量
        FROM dbo.物料管理_需求量_视图
        WHERE (@物料流水号 IS NULL OR 物料流水号 = @物料流水号)
    )
    SELECT
        物料流水号,
        需求量 AS 总需求量,
        0 AS 总已领数,
        需求量 AS 总最终需求量
    FROM 汇总
    ORDER BY 物料流水号;
END;
GO
