USE [ERPTOOL_JY_20250826Back]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- 工序工价导出，复用页面查询筛选口径并导出全部匹配数据；修改人:Ld 修改时间:2026-08-03 14:24:48;
-- 修改人:Ld 修改时间:2026-08-03 14:40:10; 总时间按机动时间加辅助时间计算，不再直接使用历史存储值。
IF OBJECT_ID(N'[dbo].[车间生产管理工艺_工序工价_导出表格_查询]', N'P') IS NOT NULL
  DROP PROCEDURE [dbo].[车间生产管理工艺_工序工价_导出表格_查询];
GO

-- 工序工价导出存储过程，仅部署测试库 ERPTOOL_JY_20250826Back；修改人:Ld 修改时间:2026-08-03 14:24:48;
CREATE PROCEDURE [dbo].[车间生产管理工艺_工序工价_导出表格_查询]
  @订单流水号_check INT = 0,
  @订单流水号 INT = NULL,
  @组件流水号_check INT = 0,
  @组件流水号 INT = NULL,
  @工艺计划流水号_check INT = 0,
  @工艺计划流水号 INT = NULL,
  @工艺名称_check INT = 0,
  @工艺名称 NVARCHAR(100) = N'',
  @工序名称_check INT = 0,
  @工序名称 NVARCHAR(100) = N'',
  @零件名称_check INT = 0,
  @零件名称 NVARCHAR(100) = N'',
  @图号_check INT = 0,
  @图号 NVARCHAR(100) = N'',
  @是否启用_check INT = 0,
  @是否启用 NVARCHAR(20) = N''
AS
BEGIN
  SET NOCOUNT ON;

  SELECT N'工序工价_' + REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR(19), GETDATE(), 21), ':', ''), '-', ''), ' ', '-') AS tablename, '15' AS tabletype;

  SELECT
    256 * 20 AS 订单号,
    256 * 20 AS 产品名称,
    256 * 20 AS 工艺名称,
    256 * 20 AS 工序名称,
    256 * 24 AS 零件名称,
    256 * 20 AS 图号,
    256 * 12 AS 工序顺序,
    256 * 12 AS 定额工时,
    256 * 12 AS 准结工时,
    256 * 12 AS [机动时间s],
    256 * 12 AS [辅助时间s],
    256 * 12 AS [总时间s],
    256 * 16 AS [单装数量(件)],
    256 * 12 AS 操作台数,
    256 * 14 AS [日产能(件)],
    256 * 18 AS [最终单价(元/件)],
    256 * 14 AS 生效日期,
    256 * 12 AS 变更次数,
    256 * 12 AS 启用状态,
    256 * 24 AS 备注,
    256 * 14 AS 创建人,
    256 * 20 AS 创建时间,
    256 * 14 AS 修改人,
    256 * 20 AS 修改时间;

  SELECT
    0 AS 订单号,
    0 AS 产品名称,
    0 AS 工艺名称,
    0 AS 工序名称,
    0 AS 零件名称,
    0 AS 图号,
    1 AS 工序顺序,
    1 AS 定额工时,
    1 AS 准结工时,
    1 AS [机动时间s],
    1 AS [辅助时间s],
    1 AS [总时间s],
    1 AS [单装数量(件)],
    1 AS 操作台数,
    1 AS [日产能(件)],
    1 AS [最终单价(元/件)],
    0 AS 生效日期,
    1 AS 变更次数,
    0 AS 启用状态,
    0 AS 备注,
    0 AS 创建人,
    0 AS 创建时间,
    0 AS 修改人,
    0 AS 修改时间;

  ;WITH PlanData AS (
    -- 页面查询同源工艺计划数据源，确保导出与列表一致；修改人:Ld 修改时间:2026-08-03 14:24:48;
    SELECT gp.*
    FROM [dbo].[车间生产管理_零件工艺计划_视图] gp
    WHERE gp.考核状态 >= 1
      AND (@订单流水号_check = 0 OR gp.订单流水号 = @订单流水号)
      AND (@组件流水号_check = 0 OR gp.组件流水号 = @组件流水号)
      AND (@工艺计划流水号_check = 0 OR gp.工艺计划流水号 = @工艺计划流水号)
      AND (@零件名称_check = 0 OR gp.零件名称 + gp.零件图号 LIKE N'%' + @零件名称 + N'%')
      AND (@图号_check = 0 OR gp.零件图号 LIKE N'%' + @图号 + N'%')
  )
  SELECT
    gp.订单号,
    gp.产品名称,
    base.工艺名称 AS 工艺名称,
    base.工艺名称 AS 工序名称,
    gp.零件名称,
    gp.零件图号 AS 图号,
    gx.工序顺序,
    gx.定额工时,
    gx.准结工时,
    ISNULL(price.[机动时间s], 0) AS [机动时间s],
    ISNULL(price.[辅助时间s], 0) AS [辅助时间s],
    ISNULL(price.[机动时间s], 0) + ISNULL(price.[辅助时间s], 0) AS [总时间s],
    ISNULL(price.[单装数量(件)], 0) AS [单装数量(件)],
    ISNULL(price.[操作台数], 0) AS 操作台数,
    ISNULL(price.[日产能(件)], 0) AS [日产能(件)],
    ISNULL(price.[最终单价(元/件)], 0) AS [最终单价(元/件)],
    CONVERT(VARCHAR(10), price.生效日期, 23) AS 生效日期,
    ISNULL(price.变更次数, 0) AS 变更次数,
    CASE WHEN ISNULL(price.是否启用, 1) = 1 THEN N'启用' ELSE N'停用' END AS 启用状态,
    price.备注,
    price.创建人,
    CONVERT(VARCHAR(19), price.创建时间, 120) AS 创建时间,
    price.修改人,
    CONVERT(VARCHAR(19), price.修改时间, 120) AS 修改时间
  FROM PlanData gp
    INNER JOIN [dbo].[车间生产管理工艺_零件工序] gx ON gp.工艺计划流水号 = gx.工艺计划流水号
    LEFT JOIN [dbo].[车间生产管理工艺_基础表_工艺名称] base ON gx.工艺编号 = base.工艺编号
    LEFT JOIN [dbo].[车间生产管理工艺_工序工价] price ON gx.工序流水号 = price.工序流水号
  WHERE NULLIF(LTRIM(RTRIM(base.工艺名称)), N'') IS NOT NULL
    AND (@工艺名称_check = 0 OR base.工艺名称 LIKE N'%' + @工艺名称 + N'%')
    AND (@工序名称_check = 0 OR base.工艺名称 LIKE N'%' + @工序名称 + N'%')
    AND (@是否启用_check = 0 OR CAST(ISNULL(price.是否启用, 1) AS NVARCHAR(20)) = @是否启用)
  ORDER BY gp.工艺更新时间 DESC, gp.工艺计划流水号 DESC, gx.工序顺序 ASC, gx.工序流水号 ASC;
END
GO
