ALTER PROCEDURE [dbo].[CallWebApi_FactoryMes_JobFinished]
    @订单号 nvarchar(50) = null,
    @工序号 nvarchar(50) = null,
    @工件编号 nvarchar(100) = null
AS
BEGIN
    SET NOCOUNT ON

    declare @开工时间 datetime
    declare @结束时间 datetime
    declare @操作时间 datetime
    declare @节拍 int
    declare @是否合格 int=0
    declare @是否断路器聚合 bit = 0

    if(@工序号 like 'AC06%' and charindex('-', @工序号) = 0)
    begin
        set @是否断路器聚合 = 1
    end

    -- 1、获取当前触发报工工件的开完工时间
    select top(1)
        @开工时间=[到达时间],
        @结束时间=[离开时间],
        @节拍=[在线时间],
        @操作时间=操作时间,
        @是否合格=是否合格
    from [测量数据发动机在线状态]
    where 发动机号=@工件编号 and 工位号=@工序号
    order by ID desc

    -- 如果开完工数据没有，手动报工使用当前时间兜底
    if(@开工时间 is null or @开工时间 = '')
    begin
        SET @开工时间 = GETDATE()
        SET @结束时间 = GETDATE()
        SET @节拍 = 0
        SET @操作时间 = GETDATE()
        SET @是否合格 = 1
    end

    -- 表1：订单任务信息
    SELECT TOP(1) a.[订单ID],[任务ID],[工序ID],[工序编号],[工序名称],产品编号,1 as 完工数量,
        FORMAT(@开工时间, 'yyyy-MM-dd HH:mm:ss') as 开工时间,
        FORMAT(@结束时间, 'yyyy-MM-dd HH:mm:ss') as 结束时间,
        @节拍 as 节拍,
        FORMAT(@操作时间, 'yyyy-MM-dd HH:mm:ss') as 最后修改时间,
        b.产品型号
    FROM [生产计划_派工订单_工序任务] a
    inner join [生产计划_派工订单_执行计划] b on a.workOrderNo=b.订单号
    where a.workOrderNo=@订单号 and a.工序编号=@工序号
    order by [任务号]

    -- 2、获取检验信息
    declare @Id_tagCF int=0
    select top(1) @Id_tagCF=Id_tagCF
    from [测量数据合格索引]
    where EngineID=@工件编号 and 工位号=@工序号
    order by Id_tagCF desc

    -- 获取当前订单的物料号（产品编号）
    declare @物料号 nvarchar(100) = ''
    select top(1) @物料号 = 产品编号
    from [生产计划_派工订单_工序任务]
    where workOrderNo=@订单号 and 工序编号=@工序号
    order by [任务号]

    -- 获取该物料号+工序号下最新下发的模板ID
    declare @最新模板ID nvarchar(100) = ''
    select top(1) @最新模板ID = Id
    from [MOM_检验模板]
    where MaterialCode = @物料号 and ProcedureCode = @工序号
    order by [操作时间] desc

    if(@是否断路器聚合 = 1)
    begin
        declare @聚合是否合格 int = 1

        ;with 最新索引 as
        (
            select
                i.Id_tagCF,
                i.EngineID,
                i.工位号,
                i.合格标志,
                row_number() over(partition by i.EngineID, i.工位号 order by i.Id_tagCF desc) as rn
            from [测量数据合格索引] i
            inner join [生产计划_派工订单_执行计划] p on p.工件编号 = i.EngineID
            where p.订单号 = @订单号
              and i.工位号 = @工序号
              and isnull(p.小机型, '') in ('1','2','3')
        )
        select @聚合是否合格 = case when count(1) = 0 then @是否合格 else min(isnull(合格标志, 1)) end
        from 最新索引
        where rn = 1

        ;with 最新索引 as
        (
            select
                i.Id_tagCF,
                i.EngineID,
                i.工位号,
                row_number() over(partition by i.EngineID, i.工位号 order by i.Id_tagCF desc) as rn
            from [测量数据合格索引] i
            inner join [生产计划_派工订单_执行计划] p on p.工件编号 = i.EngineID
            where p.订单号 = @订单号
              and i.工位号 = @工序号
              and isnull(p.小机型, '') in ('1','2','3')
        )
        SELECT a.[工位号],a.[Id_tagCF],a.[EngineID],
            @聚合是否合格 as 是否合格,
            a.[测量项目],a.测量位置 as 质检项目名称,
            CASE
                WHEN TRY_CAST(a.[TagValue] AS FLOAT) IS NOT NULL
                THEN CAST(ROUND(CAST(a.[TagValue] AS FLOAT), 2) AS NVARCHAR(100))
                ELSE a.[TagValue]
            END AS TagValue,
            [IsGoodBad] as 检测结果,上限值,下限值,理论值,a.测量单位,
            c.TemplateId as 检验项目ID, t.Code as 模板编号, c.LineNum as 检验项目编号
        FROM 最新索引 x
        inner join [测量数据合格] a on a.Id_tagCF = x.Id_tagCF
        LEFT JOIN MOM_检验模板_关联表 b ON a.TagID = b.TagID
        LEFT JOIN MOM_检验模板_检验项目 c ON c.TemplateId = @最新模板ID AND c.InspectionItemName = b.模板项名称
        LEFT JOIN MOM_检验模板 t ON t.Id = @最新模板ID
        where x.rn = 1
          and a.测量项目 != '备用'
          and a.TagValue != '0'
        order by a.EngineID, a.id desc
    end
    else if(@Id_tagCF = 0 or @Id_tagCF is null) -- 前端手动报工用
    begin
        SELECT [工位号],[Id_tagCF],[EngineID],@是否合格 as 是否合格,[测量项目],测量位置 as 质检项目名称,
            CASE
                WHEN TRY_CAST([TagValue] AS FLOAT) IS NOT NULL
                THEN CAST(ROUND(CAST([TagValue] AS FLOAT), 2) AS NVARCHAR(100))
                ELSE [TagValue]
            END AS TagValue,
            [IsGoodBad] as 检测结果,上限值,下限值,理论值,测量单位,
            '' as 检验项目ID, '' as 模板编号, '' as 检验项目编号
        FROM [测量数据合格]
        where Id_tagCF=2
        order by id desc
    end
    else
    begin
        SELECT a.[工位号],[Id_tagCF],[EngineID],@是否合格 as 是否合格,a.[测量项目],a.测量位置 as 质检项目名称,
            CASE
                WHEN TRY_CAST(a.[TagValue] AS FLOAT) IS NOT NULL
                THEN CAST(ROUND(CAST(a.[TagValue] AS FLOAT), 2) AS NVARCHAR(100))
                ELSE a.[TagValue]
            END AS TagValue,
            [IsGoodBad] as 检测结果,上限值,下限值,理论值,a.测量单位,
            c.TemplateId as 检验项目ID, t.Code as 模板编号, c.LineNum as 检验项目编号
        FROM [测量数据合格] a
        LEFT JOIN MOM_检验模板_关联表 b ON a.TagID = b.TagID
        LEFT JOIN MOM_检验模板_检验项目 c ON c.TemplateId = @最新模板ID AND c.InspectionItemName = b.模板项名称
        LEFT JOIN MOM_检验模板 t ON t.Id = @最新模板ID
        where EngineID=@工件编号 and a.工位号=@工序号 and Id_tagCF=@Id_tagCF and a.测量项目 != '备用' and TagValue != '0'
        order by a.id desc
    end

    -- 3、获取物料信息（从PLC扫码物料记录读取）
    SELECT [工位号]
          ,[产品编号] as [工件编号]
          ,[工单号] as [订单号]
          ,'' as [任务号]
          ,[物料代码] as 物料编号
          ,[物料批次号]
          ,[物料流水号]
          ,[物料名称]
          ,1 as [数量]
    FROM [dbo].[XD_物料记录]
    where [工位号]=@工序号 and [产品编号]=@工件编号 and isnull([物料代码], '') != ''
    order by [操作时间]

    -- 4、获取工时信息
END
