IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[proj_153_SignIn]') AND type = 'P')
DROP PROCEDURE [dbo].[proj_153_SignIn]
GO
CREATE proc [dbo].[proj_153_SignIn](
    @ordid int,
    @str nvarchar(40)
)
as
begin

set nocount on
    declare @ord2id int,
@sfbm nvarchar(20),
            @lastSignInTime datetime

    /*判断是否有这个人身份证号。判断条件：是否找到与之匹配的身份证号码*/
    if  not exists (select 1 from order_153_102 where ord2_sfzh = @str and ord2_ordid = @ordid )
    begin
        select 'notFind'result1, -99 qdcs
    end
    /*找到这个人的身份证号*/
    else
    begin
        /*赋值@ord2id,@ord2ordid,@lastSignInTime*/
        select @ord2id = ord2_id,@sfbm=ord2_sfbm,@lastSignInTime=ord2_dksj 
        from order_153_102 
        where ord2_ordid = @ordid and ord2_sfzh = @str
        /*正常打卡成功情况*/
        if exists(select 1 from order_153_102 where ord2_id = @ord2id and ord2_ordid = @ordid and ord2_sfbm ='是' and ord2_sfzh = @str and datediff(day,@lastSignInTime,getdate()) > 0)
        begin
            /*更新打卡时间为当前时间，打卡次数加一，是否参会：是*/
            update order_153_102 set ord2_sfch = '是',ord2_dksj = getdate(),ord2_qdcs = ord2_qdcs + 1 where ord2_id = @ord2id and ord2_ordid = @ordid
            update order_153 set ord_yqd = ord_yqd+CHAR(13)+CHAR(10)+@str where ord_id = @ordid
            /*返回OK表示打卡成功，若签到次数为1则前端打印，否则不打印*/
            select 'OK' result1,[ord2_caption],[ord2_code],[ord2_date],[ord2_fatherord2id],[ord2_fbizid],[ord2_ford2id],[ord2_ford2tag],[ord2_fordid],[ord2_ftype],[ord2_gh],[ord2_group],[ord2_id],[ord2_key],[ord2_order],[ord2_ordid],[ord2_qdcs],[ord2_rbizid],[ord2_rord2id],[ord2_rord2tag],[ord2_rordid],[ord2_rtype],[ord2_sfbm],[ord2_sfch],[ord2_sfzh],[ord2_sfzs],[ord2_sjh],[ord2_status],[ord2_szdp],[ord2_tag],[ord2_tags],[ord2_updated] ,substring([ord2_xm],1,3) ord2_xm,[ord2_tsrcuid],[ord2_dksj] 
            from order_153_102 where ord2_ordid = @ordid and ord2_id =@ord2id
        end
        /*打卡失败情况：此人未报名;打卡时间错误(上一次打卡时间大于当前时间/同一天打卡第二次)*/
        else
        begin
            /*判断是否报名。判断条件：是否报名：为''或者'否'都认为是未报名;*/
            if exists(select 1 from order_153_102 where ord2_id = @ord2id and ord2_ordid = @ordid and (ord2_sfbm = '否' or ord2_sfbm = ''))
            begin
select 'noSignUp' result1,* 
from order_153_102 
where ord2_ordid = @ordid and ord2_id =@ord2id
            end
            /*同一天签到或上次签到时间大于当前时间*/
            else
            begin
                select 'signTimeError' result1 , * from order_153_102 where ord2_ordid = @ordid and ord2_id =@ord2id
            end
        end
    end
set nocount off
end