ALTER PROCEDURE [dbo].[接口_IOT接口_报工记录_是否报工判断]
@工位号  nvarchar(50) = 'OP10',
@订单号 nvarchar(50) = 'SA10025',
@报工类型 nvarchar(50) = '开工',
@工件编号 nvarchar(100) = null
AS
SET NOCOUNT ON

-- 已成功报工的工序不重复报工
declare @成功次数 int
select @成功次数 = count(1)
from 接口_IOT接口_报工记录
where 工位号 = @工位号
  and 订单号 = @订单号
  and 报工类型 = @报工类型
  and 报工结果 = 1

if(@成功次数 > 0)
begin
    select 2 as result,@工位号 + '订单号：'+@订单号+'产品工序已'+@报工类型+'，无需二次报工' as msg
    return
end

-- 断路器主工位：静端进站开工，拐臂箱出站完工；副工位如 AC0603-1 不参与此判断
if(@工位号 like 'AC06%' and charindex('-', @工位号) = 0)
begin
    declare @当前工件编号 nvarchar(100) = null
    declare @小机型 int = null

    set @当前工件编号 = nullif(@工件编号, '')

    if(@当前工件编号 is null)
    begin
        select top 1 @当前工件编号 = 变速器序列号
        from 北汽福田工位状态监控MOBY
        where 工位号 = @工位号
        order by ID desc
    end

    select top 1 @小机型 = try_convert(int, 小机型)
    from 生产计划_派工订单_执行计划
    where 工件编号 = @当前工件编号

    if(@小机型 is null)
    begin
        select 2 as result,@工位号 + '订单号：'+@订单号+'产品工序'+@报工类型+'，未查询到工件编号['+isnull(@当前工件编号,'')+']对应的小机型' as msg
        return
    end

    if(@报工类型 = '开工' and @小机型 <> 1)
    begin
        select 2 as result,@工位号 + '订单号：'+@订单号+'产品工序开工，非静端不进行报工' as msg
        return
    end

    if(@报工类型 = '完工' and @小机型 <> 3)
    begin
        select 2 as result,@工位号 + '订单号：'+@订单号+'产品工序完工，非拐臂箱不进行报工' as msg
        return
    end
end

select 1 as result,'' as msg
