IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[genallowtimesql]') AND type = 'FN')
DROP FUNCTION [dbo].[genallowtimesql]
GO
CREATE function [dbo].[genallowtimesql](
@cmpid int,
@objtype varchar(10),
@objid int)
returns nvarchar(max)
as
begin
declare @wrks nvarchar(4000) = ''
declare @wks nvarchar(4000) = ''
declare @dys nvarchar(4000) = ''
declare @tms nvarchar(4000) = ''
declare @hlds nvarchar(4000) = ''  -- 假日
declare @sql nvarchar(max) =''
declare @sql1 nvarchar(max) =''

declare @res nvarchar(max)='
declare @myallow__time int = 1 
declare @myallow__times varchar(10)=''''
declare @myallow__date1 varchar(19)=''''
declare @myallow__date2 varchar(19)=''''
declare @myallow__week int = 0 
declare @myallow__day int = 0 
set @myallow__times = CONVERT(varchar(5),getdate(),114)
set @myallow__date1 = CONVERT(varchar(10),getdate(),120)
set @myallow__date2 = @myallow__date1 +'' 23:59:59''
set @myallow__date1 = @myallow__date1 +'' 00:00:00''
set @myallow__week = datepart(dw,getdate()) - 1 
set @myallow__day = day(getdate())
if (@myallow__week=0) set @myallow__week=7  
' 
-- 如果 上班时间和下班时间都选择了，那说明是所有时间
if ((exists (select 1 from mps_allow_time with(nolock) where wt_cmpid=@cmpid and wt_type=@objtype and wt_objid=@objid and wt_timetype='work')) AND 
(exists (select 1 from mps_allow_time with(nolock) where wt_cmpid=@cmpid and wt_type=@objtype and wt_objid=@objid and wt_timetype='offwork'))
   )
begin
set @wrks = '1=1' 
end  else 
if (exists (select 1 from mps_allow_time with(nolock) where wt_cmpid=@cmpid and wt_type=@objtype and wt_objid=@objid and wt_timetype='work'))
begin
select @wrks = @wrks + case when @wrks='' then '' else ' or ' end +
'('''+wt_time1+'''<=@myallow__times and '''+wt_time2+'''>=@myallow__times)' 
from mps_worktime with(nolock) where wt_cmpid=@cmpid and wt_iswork=0 
end  else 
if (exists (select 1 from mps_allow_time with(nolock) where wt_cmpid=@cmpid and wt_type=@objtype and wt_objid=@objid and wt_timetype='offwork'))
begin
select @wrks = @wrks + case when @wrks='' then '' else ' or ' end +
'('''+wt_time1+'''<=@myallow__times and '''+wt_time2+'''>=@myallow__times)' 
from mps_worktime with(nolock) where wt_cmpid=@cmpid and wt_iswork=1 
end 

set @hlds = '(exists (select 1 from mps_holidays with(nolock) where hd_cmpid=@cmpid and hd_holiday=1 and hd_date>=@myallow__date1 and hd_date<=@myallow__date2))'

if (@wrks>'') 
begin
if (@wrks='1=1') set @wrks = @hlds else set @wrks='('+@wrks+' and '+@hlds+')'
end 

-- 计算星期
select @wks = @wks + case when @wks='' then '' else ' or ' end +
' ('+convert(varchar(10),wt_time)+'=@myallow__week) ' 
from mps_allow_time with(nolock) where wt_cmpid=@cmpid and wt_type=@objtype and wt_objid=@objid and wt_timetype='week'
-- 计算天
select @dys = @dys + case when @dys='' then '' else ' or ' end +
' ('+convert(varchar(10),wt_time)+'=@myallow__day) ' 
from mps_allow_time with(nolock) where wt_cmpid=@cmpid and wt_type=@objtype and wt_objid=@objid and wt_timetype='day'
-- 计算时间
select @tms = @tms + case when @tms='' then '' else ' or ' end +
'('''+case when wt_time>=1 and wt_time<10 then '0'+convert(varchar(10),wt_time) 
when wt_time>=10 and wt_time<=23 then convert(varchar(10),wt_time) 
else '00' end+':'+ 
case when wt_time2=0 then '00' 
 when wt_time2=15 then '15' 
 when wt_time2=30 then '30' 
 when wt_time2=45 then '45' else '00' end+'''<=@myallow__times and '''+
case 
 when wt_time>=1 and wt_time<10 then '0'+convert(varchar(10),wt_time) 
 when wt_time>=10 and wt_time<=23 then convert(varchar(10),wt_time) 
else '00' end+':'+ 
case when wt_time2=0 then '15' 
 when wt_time2=15 then '30' 
 when wt_time2=30 then '45' 
 when wt_time2=45 then '59' else '59' end +'''>=@myallow__times)' 
from mps_allow_time with(nolock) where wt_cmpid=@cmpid and wt_type=@objtype and wt_objid=@objid and wt_timetype='time'

select top 1 @sql = cd_text from mps_extsqljs with(nolock) where cd_objid=@objid and cd_name=@objtype+'.time' 
set @sql = ISNULL(@sql,'') 

set @sql1 = '' 
if (@wrks='')  -- 没有选择上班时间下班时间
begin
if (@wks='' and @dys='') 
begin
if (@tms>'') set @sql1 = '('+@tms +')'
end else 
begin
if (@wks>'' and @dys>'') 
begin
set @sql1 = @wks + ' or '+@dys 
end else 
begin
set @sql1 = @wks +@dys 
end
if (@tms>'') set @sql1 = '(('+@sql1+') and (' + @tms +'))'
end 
end else
begin
if (@wks='' and @dys='') 
begin
if (@tms>'') set @sql1 = '('+@tms +')'
end else 
begin
if (@wks>'' and @dys>'') 
begin
set @sql1 = @wks + ' or '+@dys 
end else 
begin
set @sql1 = @wks +@dys 
end
if (@tms>'') set @sql1 = '(('+@sql1+') and (' + @tms +'))'
end 
if (@sql1>'')
begin
set @sql1 = '(('+@wrks +') or ('+@sql1+'))'
end else
begin
set @sql1 = '('+@wrks +')'
end 
end  

if (@sql>'') 
begin
set @sql ='set @operation=''continue'' 
set @errorinfo='''' 
'+@sql+'
'
end 
if (@sql1='') 
begin
if (@sql>'') set @sql1='(@operation=''continue'')' 
end else 
begin
if (@sql>'') set @sql1 = '(('+@sql1+') and (@operation=''continue''))' 
end 
if (@sql1='')
begin
set @sql1='set @myallow__time=0 '
end else 
begin
set @sql1=@sql+'
if ('+@sql1+') 
begin
set @myallow__time=0
end'
end 
return @res +'
'+@sql1 
end