IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_save_biztype_auto]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_save_biztype_auto]
GO
CREATE procedure [dbo].[mps_save_biztype_auto](
@cmpid int ,
@style int , 
@bizid int , 
@interval int ,
@unit varchar(10),
@webservice varchar(100),
@starttime datetime ,
@sql nvarchar(max) ,
@condsql nvarchar(max)  
) as
begin

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
declare @name varchar(100)
declare @tskid int 
if (@unit<>'分钟' and @unit<>'小时' and @unit<>'天') 
begin
set @interval = 0 
set @unit = ''
set @starttime = '2199-01-01' 
end 

if (@style=0) 
begin
update mps_biztype set biz_auto_interval = @interval  ,
biz_auto_unit = @unit ,
biz_auto_webservice = @webservice ,
biz_auto_starttime = @starttime  
where biz_id=@bizid 
if (not exists (select 1 from mps_extsqljs with(nolock) where cd_objid=@bizid and cd_name='crm.import' )) 
begin
insert into mps_extsqljs(cd_name,cd_objid,cd_objid2,cd_text)
values('crm.import',@bizid,0,@sql )
end else 
begin
update mps_extsqljs set cd_text=@sql where cd_objid=@bizid and cd_name='crm.import' 
end 
if (LEN(@sql)<10) 
begin
delete mps_extsqljs where cd_objid=@bizid and cd_name='crm.import'  
end 

if (not exists (select 1 from mps_extsqljs with(nolock) where cd_objid=@bizid and cd_name='crm.impcond' )) 
begin
insert into mps_extsqljs(cd_name,cd_objid,cd_objid2,cd_text)
values('crm.impcond',@bizid,0,@condsql )
end else 
begin
update mps_extsqljs set cd_text=@condsql where cd_objid=@bizid and cd_name='crm.impcond' 
end 
if (LEN(@condsql)<10) 
begin
delete mps_extsqljs where cd_objid=@bizid and cd_name='crm.impcond'  
end 

if (@webservice<>'') 
begin
set @name = @webservice+'&bizid='+CONVERT(varchar(10),@bizid) 
end else
begin
set @name = 'order_'+CONVERT(varchar(10),@bizid)+'_import' 
end 

select top 1 @tskid=tsk_id from mps_task_java with(nolock) 
where tsk_cmpid=@cmpid and tsk_name=@name and tsk_objtype='biz' and tsk_bizid=@bizid 
set @tskid = ISNULL(@tskid,0) 

if (@tskid<=0) 
begin
if (@interval>0) 
begin
insert into mps_task_java(tsk_cmpid,tsk_name,tsk_interval,tsk_nextruntime,tsk_times,tsk_type,tsk_objtype,tsk_bizid) 
select @cmpid , @name,
case when @unit='分钟' then @interval 
 when @unit='小时' then @interval*60 
 when @unit='天' then @interval*60*24 
 else  0  
end ,
@starttime,0,case when @webservice='' then 'sql' else 'webservice' end , 'biz' , @bizid 
end 

end else
begin
update mps_task_java set 
tsk_nextruntime = case when tsk_nextruntime<@starttime then @starttime else tsk_nextruntime end , 
tsk_type = case when @webservice='' then 'sql' else 'webservice' end ,
tsk_interval=case when @unit='分钟' then @interval 
   when @unit='小时' then @interval*60 
  when @unit='天' then @interval*60*24 
  else  0   
 end
where tsk_id=@tskid 
end 
if (@interval<=0) 
begin
delete mps_task_java where tsk_id=@tskid
end  
end else 
begin
update mps_biztype set biz_auto2_interval = @interval  ,
biz_auto2_unit = @unit ,
biz_auto2_webservice = @webservice ,
biz_auto2_starttime = @starttime  
where biz_id=@bizid 
if (not exists (select 1 from mps_extsqljs with(nolock) where cd_objid=@bizid and cd_name='crm.bizauto' )) 
begin
insert into mps_extsqljs(cd_name,cd_objid,cd_objid2,cd_text)
values('crm.bizauto',@bizid,0,@sql )
end else 
begin
update mps_extsqljs set cd_text=@sql where cd_objid=@bizid and cd_name='crm.bizauto' 
end 
if (LEN(@sql)<10) 
begin
delete mps_extsqljs where cd_objid=@bizid and cd_name='crm.bizauto'  
end 

if (not exists (select 1 from mps_extsqljs with(nolock) where cd_objid=@bizid and cd_name='crm.bizcond' )) 
begin
insert into mps_extsqljs(cd_name,cd_objid,cd_objid2,cd_text)
values('crm.bizcond',@bizid,0,@condsql )
end else 
begin
update mps_extsqljs set cd_text=@condsql where cd_objid=@bizid and cd_name='crm.bizcond' 
end 
if (LEN(@condsql)<10) 
begin
delete mps_extsqljs where cd_objid=@bizid and cd_name='crm.bizcond'  
end 

-- 业务单的自动化，是在业务单生效后才能有效，因此这里是不需要加任务的。 
end
end