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

-- 生成时间:2018-06-01 09:49:17 by ebs-core  
CREATE  procedure order_89_check(
     @cmpid int,
     @branchid int,
     @userdpid int,
     @useruid int,
     @userotid int,
     @jbid bigint,
     @ordid bigint,
     @operation varchar(50) output,
     @errorinfo nvarchar(4000) output
  ) 
as  
begin  
set nocount on  
declare @ord2group2 nvarchar(50) 
set @ord2group2 = '' 
declare @sys_recid bigint 
set @sys_recid = 0 
 declare @nonedate datetime 
set @nonedate = CONVERT(datetime,'1910-1-1')
 set @operation = 'continue'
 set @errorinfo = '' 
 --- 以下是预处理事件中的临时表
 
exec drop_temp_table '#master' 
select * into #master from order_89 where ord_id=@ordid  

exec drop_temp_table '#detail2' 
select * into #detail2 from order_89_2 where ord2_ordid=@ordid  

 ---- 以下是开发人员写的 check order 事件 --------------
 /*判断具体的行程结束时间是否超出主表的结束时间*/
if exists(
select * from #master n with(nolock),#detail2 d
where (d.ord2_jssj<n.ord_ksrq 
or d.ord2_jssj > n.ord_jsrq
or d.ord2_kssj< n.ord_ksrq
or d.ord2_kssj > n.ord_jsrq) 
and n.ord_id =d.ord2_ordid
) 
begin
set @operation ='abort'
    set @errorinfo ='具体的行程日期与主行程日期不符'
    
end
/*判断具体的行程结束时间是小于开始时间*/
if exists(select * from  #master n with(nolock),#detail2 d where d.ord2_jssj<d.ord2_kssj and n.ord_id = d.ord2_ordid) 
begin
set @operation ='abort'
    set @errorinfo ='开始时间与结束时间范围错误'
    
end


/*开始时间大于结束时间*/
if exists(select 1 from #master n where n.ord_ksrq>n.ord_jsrq)
begin
    set @operation ='abort'
    set @errorinfo ='开始时间不能大于结束时间,请重新录入'
end


/*没有具体行程是提示错误*/
if not exists(select 1 from #master n,#detail2 d where n.ord_id = d.ord2_ordid)
begin
    set @operation ='abort'
    set @errorinfo ='没有具体行程,请录入后重试'
end




/*同一地点是否有时间交叉(店铺行程)*/
declare @temptable1 table(tb_kssj datetime,
tb_jssj datetime,
tb_xcmdd nvarchar(100),
tb_dpbh nvarchar(100),
tb_id bigint,
tb_ordid bigint,
tb_flag bigint
)
/*将所有重复的插入@temptable1表中*/
insert into @temptable1
select ord2_kssj,ord2_jssj,ord2_xcmdd,ord2_dpbh,ord2_id,ord2_ordid,0 
from #detail2 where ord2_dpbh in (select ord2_dpbh from #detail2 group by ord2_dpbh having COUNT(*)>1)
and isnull(ord2_dpbh,'') != ''

declare @rept table(
re_dpbh nvarchar(100),
re_count bigint,
re_flag bigint
)
/*将重复的类型记录到@rept表中*/
insert into @rept(re_dpbh,re_count)
select tb_dpbh,count(*) as tb_count 
from @temptable1 
group by tb_dpbh having count(*)>1
/*更新@rept的flag*/
update @rept set re_flag = 0

declare @dpbh nvarchar(100),
@ord2id bigint,
@isrept bigint=0,
@kssj datetime,
@jssj datetime
while(exists (select re_dpbh from @rept where re_flag = 0))
begin
select top 1 @dpbh = re_dpbh from @rept where re_flag = 0
while(exists (select tb_dpbh from @temptable1 where tb_dpbh = @dpbh and tb_flag = 0))
begin
select top 1 @kssj = tb_kssj,@jssj = tb_jssj,@ord2id = tb_id from @temptable1 where tb_dpbh = @dpbh and tb_flag = 0
/*查询到存在相同地点有交叉的时间的情况*/
if exists (select tb_dpbh from @temptable1 
   where tb_dpbh = @dpbh 
   and (
tb_kssj between @kssj and @jssj or
tb_jssj between @kssj and @jssj
)
and tb_id <> @ord2id
   )
begin
set @isrept = 1
break
end
/*未查询到存在相同地点有交叉的时间的情况*/
else
begin
set @isrept = 0
update @temptable1 set tb_flag = 1 where tb_id = @ord2id
end
end
/*存在时间有交叉的情况*/
if (@isrept = 1)
begin
        set @operation ='abort'
        set @errorinfo ='同一地点时间不能够交叉'
break
end
/*不存在时间有交叉的情况，更新此类型的flag为1(表示已处理过)*/
else
begin
update @rept set re_flag = 1 where re_dpbh = @dpbh
end
end


/*验证打卡是否打满的逻辑开始*/
declare
@ord2kssj datetime,
@ord2jssj datetime,
@ordkssj datetime,
@ordjssj datetime,
@ord2id1 bigint

declare @planA table(planmain datetime)
declare @planB table(pord2id bigint ,planmaind datetime)

select @ordkssj = ord_ksrq,@ordjssj = ord_jsrq from #master

insert into @planA(planmain)
select convert(varchar(10),dateadd(dd,number,@ordkssj),120)
from master..spt_values
where type='p'  and number <= datediff(dd,@ordkssj,@ordjssj)

declare cs cursor  read_only for 
select ord2_id,ord2_kssj,ord2_jssj from #detail2

open cs 

fetch next from cs into @ord2id1 ,@ord2kssj,@ord2jssj 

while(@@FETCH_STATUS=0)
begin

insert into @planB(pord2id,planmaind)
select @ord2id1,convert(varchar(10),dateadd(dd,number,@ord2kssj),120)
from master..spt_values
where type='p'  and number <= datediff(dd,@ord2kssj,@ord2jssj)

set @ord2id1 = 0 
set @ord2kssj ='1900-01-01'
set @ord2jssj ='1900-01-01'
fetch next from cs into @ord2id1 ,@ord2kssj,@ord2jssj 

end

close cs

deallocate cs

/*判断打卡是否打满*/
if exists(select 1 from @planA left join @planB on  planmaind =  planmain where planmaind is null )
begin
set @operation ='abort'
    set @errorinfo ='在行程计划的期间存在某天未有行程计划！'
end
/*验证打卡是否打满的逻辑结束*/

 ---- 以上是开发人员写的 check order 事件 --------------
 
 if (@operation='continue') 
 begin
 set @sys_recid=@sys_recid  -- here useless 
 
end 
--- 清除临时表 
exec drop_temp_table '#detail2'

exec drop_temp_table '#master' 

set nocount off 
end