alter procedure dbo.spCreateDetectsBySetting(@nodes nvarchar(max), @isManual bit=0, @personId int=null) as
begin
	set nocount on
	create table #plan (
		id int identity(1,1) primary key clustered,
		NodeId int,
		MethodId int,
		SharePath nvarchar(255),
		TcpIpPort int,
		DetectKindId int,
		IsSwFull bit,
		SwInclude nvarchar(255),
		SwExclude nvarchar(255),
		[Update] bit,
		DetectPeriod int
	)

	create table #ids (id int primary key)
	if( isnull(@nodes,'')!='')
		insert #ids select id from dbo.ftCommaListToTableIds(@nodes)

		if( @nodes is null )
	begin
		insert #plan (NodeId,MethodId,SharePath,TcpIpPort,DetectKindId,IsSwFull,SwInclude,SwExclude,[Update],DetectPeriod)
		select
			ds.NodeId,
			ds.MethodId,
			ds.SharePath,
			ds.TcpIpPort,
			ds.KindId,
			ds.IsSwFull,
			ds.SwInclude,
			ds.SwExclude,
			ds.[Update],
			ds.DetectPeriod
		from vDetectSettings ds
			left join vNodeDisposed nd on nd.NodeId=ds.NodeId
			left join tblDetect d on d.lintComputerNodeId=ds.NodeId and d.lintDetectKindId=ds.KindId 
				and ((d.dteImported is not null and dateadd(day,ds.DetectPeriod,d.dteImported)>getutcdate())
					or d.lintDetectStatusId=1)
		where nd.NodeId is null
			and ds.DetectPeriod>0
			and ds.MethodId is not null
			and d.intDetectId is null
	end else
	begin
		insert #plan (NodeId,MethodId,SharePath,TcpIpPort,DetectKindId,IsSwFull,SwInclude,SwExclude,[Update],DetectPeriod)
		select
			ds.NodeId,
			ds.MethodId,
			ds.SharePath,
			ds.TcpIpPort,
			ds.KindId,
			ds.IsSwFull,
			ds.SwInclude,
			ds.SwExclude,
			ds.[Update],
			ds.DetectPeriod
		from vDetectSettings ds
			join #ids ids on ids.id=ds.NodeId
			left join vNodeDisposed nd on nd.NodeId=ds.NodeId
		where nd.NodeId is null
			and ds.MethodId is not null
	end

	if( exists(select top 1 1 from #ids))
	begin
		delete do
		from #plan pl
			join tblDetect d on d.lintComputerNodeId=pl.NodeId and d.lintDetectStatusId=1 and d.lintDetectKindId=pl.DetectKindId
			join tblDetectOpts do on do.lintDetectId=d.intDetectId
		set nocount off	
		
		delete l
		from #plan pl
			join tblDetect d on d.lintComputerNodeId=pl.NodeId and d.lintDetectStatusId=1 and d.lintDetectKindId=pl.DetectKindId
			join tblLog l on l.lintDetectId=d.intDetectId
		
		delete d
		from #plan pl
			join tblDetect d on d.lintComputerNodeId=pl.NodeId and d.lintDetectStatusId=1 and d.lintDetectKindId=pl.DetectKindId
	end
	set nocount on
	create table #ids2 (detId int primary key,planId int)
	
	if( isnull(@personId,0)=0 )
		select @personId=iPersonId from tPerson where bPersonSystem=1

	set nocount off
	merge into tblDetect d
	using #plan pl on 1=0
	when not matched by target then
	insert (lintComputerNodeId,lintDetectKindId,lintDetectStatusId,dteRqCreated,liRqCreatedPersonId,ManualRq)
	values (pl.NodeId,pl.DetectKindId,1,getutcdate(),@personId,@isManual)
	output inserted.intDetectId, pl.id into #ids2 (detId,planId);
	
	set nocount on

	insert tblDetectOpts (lintDetectId,lintComputerNodeId,lintDetectMethodId,txtSharePath,intTcpipPort,lintDetectKindId,bolSwFull,txtSwInclude,txtSwExclude,bolUpdate,intDetectPeriod)
	select
		ids.detId,
		pl.NodeId,
		pl.MethodId,
		pl.SharePath,
		pl.TcpIpPort,
		pl.DetectKindId,
		pl.IsSwFull,
		pl.SwInclude,
		pl.SwExclude,
		pl.[Update],
		pl.DetectPeriod
	from #plan pl
		join #ids2 ids on ids.planId=pl.id

	drop table #plan
	drop table #ids
	drop table #ids2
end