IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[proj_qsy_TrainSignin]') AND type = 'P')
DROP PROCEDURE [dbo].[proj_qsy_TrainSignin]
GO

CREATE proc [dbo].[proj_qsy_TrainSignin](
    @ordid int,
    @str nvarchar(20),
    @ord2id int
)
as
begin

set nocount on
    declare @ord2id2 int,
@minid int,
@days int,
@sfbm nvarchar(20)

    /*找到身份证号对应的ord2_id*/
    select @ord2id2=ord2_id from order_48_105 where ord2_sfz = @str and ord2_ordid = @ordid 
    
    /*判断培训课程的天数，若天数小于1天，便不再打印胸牌*/
    
    select @days  = DATEDIFF(DAY,ord_ksrq,ord_jkrq) from order_48 where ord_id = @ordid
    
    /*判断当前签到信息录入行所在id 是否最小*/
    select @minid=MIN(ord2_id) from order_48_3 where ord2_ordid = @ordid

/*判断打卡的行数所对应的备份信息*/
    declare @bf nvarchar(max)
select @bf  = ord2_qdxxbf from order_48_3 where  ord2_ordid = @ordid and ord2_id  = @ord2id

    select @sfbm = ord2_sfbm from order_48_105 where ord2_sfz = @str and @ord2id = @minid and ord2_ordid = @ordid
    /*判断是否 报名*/
    if exists(select 1 from order_48_105 where ord2_ordid = @ordid and ord2_id = @ord2id2 and ord2_sfbm ='是')
        begin
/*判断是否重复打卡*/
   if CHARINDEX(@str,@bf)>0
   begin
select 'notok' result1,* from order_48_105 where ord2_ordid = @ordid and ord2_id = @ord2id2
   end
   else
   begin
update order_48_105 set ord2_sqdcs = ord2_sqdcs + 1 where ord2_ordid = @ordid and ord2_id = @ord2id2

select  'ok' result1,* from order_48_105 where ord2_ordid = @ordid and ord2_id = @ord2id2

/*将已经打卡的数据存入备份中*/
update order_48_3  set ord2_qdxxbf =ord2_qdxxbf +@str+',' where ord2_ordid = @ordid and ord2_id  = @ord2id
update order_48_3 set ord2_qdrs = ord2_qdrs+1  where ord2_ordid = @ordid and ord2_id  = @ord2id
   end
end
     else 
     /*判断未报名的*/
     if exists(select 1 from order_48_105 
     where ord2_ordid = @ordid and ord2_id = @ord2id2 and (ord2_sfbm ='否' or ord2_sfbm =''))
        begin
            select  'error' result1,* from order_48_105 where ord2_ordid = @ordid and ord2_id = @ord2id2

        end
     else
     /*判断此人是否在推送人员中*/
     if not exists(select 1 from order_48_105 
     where ord2_ordid = @ordid and ord2_id = @ord2id2 )
begin
select 'notfound' result1

end

declare @print nvarchar(50)
if exists(select 1 from order_48_3  where ord2_ordid = @ordid and ord2_qdxxbf like '%'+@str+'%' and ord2_id = @ord2id)
begin
set @print = 'flase'
end
else
begin
set @print = 'true'
end
/*判断是否执行打印*/
     if @ord2id =@minid and @sfbm = '是' and @days>1 and @print = 'true'
begin
select 'print1'  dayin,@ord2id a,@minid b,@ordid c
end
else 
begin
select 'notprint1'  dayin,@ord2id a,@minid b,@ordid c
end
set nocount off

end