IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_sys_gen_powerfactor]') AND type = 'FN')
DROP FUNCTION [dbo].[mps_sys_gen_powerfactor]
GO
 
CREATE FUNCTION [dbo].[mps_sys_gen_powerfactor]
(
    @cmpid int,
    @biztype varchar(10) , 
    @bizid int,
@expression nvarchar(4000)
)
RETURNS varchar(4000)
AS
BEGIN
-- Declare the return variable here
if (CHARINDEX('ord_id',@expression)>0) return 'ord_id'  
    DECLARE @md5result varchar(32)
declare @datatype varchar(20)
declare @s nvarchar(4000),@s1 nvarchar(4000),@s2 nvarchar(4000),@s3 nvarchar(4000),@countvalue varchar(30)
declare @sqlstr nvarchar(2000),@sqlstr1 nvarchar(2000)
declare @i int ,@j int, @k int
declare @existgroup int 
set @i = 0
set @countvalue = '1'
set @sqlstr = ''
set @sqlstr1 = ''
set @existgroup = 1 --存在分组

    -- Add the T-SQL statements to compute the return value here
if len(@expression) > 4 
begin
set @s = @expression
while @s > ''
begin
set @i = charindex('|', @s) 
if (@i > 0) 
begin
set @s1 = substring(@s, 1, @i-1) 
set @s = substring(@s, @i+1 , len(@s) - @i) 
end else 
begin
set @s1 = @s 
set @s = '' 
end 
--处理当前值
if (@s1 > '') 
begin
--判断是否存在分组情况
set @i = charindex('=', @s1) 
if (@i>0)
begin
--存在分组情况，s1为字段名称，s2为字段值
set @s2 = substring(@s1, @i+1 , len(@s1) - @i)
set @s1 = substring(@s1, 1, @i-1)
select @datatype=fldi_datatype from mps_fieldinfo with(nolock) 
where fldi_cmpid = @cmpid and fldi_tbtype = @biztype 
and fldi_tbid = @bizid and fldi_fieldname =@s1 and fldi_ord2tag = 0
--获取具体分组
if (@s2 > '')
begin
while @s2 > ''
begin
set @j = charindex(',', @s2)
if (@j > 0)
begin
set @s3 = substring(@s2, 1, @j-1)
set @s2 = substring(@s2, @j+1 , len(@s2) - @j)
--获取分组
set @k = charindex(':', @s3)
if (@k > 0)
begin
--根据字段类型
if (@datatype = 'decimal' or @datatype = 'int' or @datatype = 'bigint' or @datatype = 'integer' or @datatype = 'float')
begin
set @sqlstr1 = @sqlstr1 + ' when '+ @s1 +' >= '+ substring(@s3, 1, @k-1) + ' and '+@s1+' <= ' +substring(@s3, @k+1 , len(@s3) - @k) + ' then ''' + @countvalue+''''
end else
begin
set @sqlstr1 = @sqlstr1 + ' when '+ @s1 +' >= '''+ substring(@s3, 1, @k-1) + ''' and '+@s1+' <= ''' +substring(@s3, @k+1 , len(@s3) - @k) + ''' then ''' + @countvalue+''''
end
end else
begin
--根据字段类型
if (@datatype = 'decimal' or @datatype = 'int' or @datatype = 'bigint' or @datatype = 'integer' or @datatype = 'float')
begin
set @sqlstr1 = @sqlstr1 + ' when '+ @s1 +' = ''' + @s3 + ''' then ''' + @countvalue+''''
end else
begin
set @sqlstr1 = @sqlstr1 + ' when '+ @s1 +' = ''' + @s3 + ''' then ''' + @countvalue+''''
end

end
end else
begin
--最后一次分组详情
set @sqlstr1 = ' case ' + @sqlstr1 + ' else ''' + @countvalue + ''' end '
set @s2 = ''
set @countvalue = ''
end
set @countvalue = '1' + @countvalue 
end
set @sqlstr = @sqlstr + @sqlstr1 + '+''-''+'
set @sqlstr1  = ''
end else
begin
set @existgroup  = 0 --不存在分组
end
end else
begin
--不存在分组的情况
set @existgroup  = 0
end

--处理不存在分组
if (@existgroup=0)
begin
select @datatype=fldi_datatype from mps_fieldinfo  with(nolock) 
where fldi_cmpid = @cmpid and fldi_tbtype = @biztype 
and fldi_tbid = @bizid and fldi_fieldname =@s1 and fldi_ord2tag = 0

--根据字段类型
if (@datatype = 'decimal' or @datatype = 'int' or @datatype = 'bigint' or @datatype = 'integer' or @datatype = 'float' or @datatype = 'datetime' or @datatype = 'date' or @datatype is null)
begin
set @sqlstr = @sqlstr + ' convert(varchar(100),'+@s1+') ' + '+''-''+'
end else
begin
set @sqlstr = @sqlstr + @s1+ '+''-''+'
end
end
end
set @existgroup  = 1
end 
set @sqlstr =  substring(@sqlstr, 1, len(@sqlstr)-5)
end else
begin
set @sqlstr = ' convert(varchar(40),ord_createcuid) '
end
return @sqlstr

    -- Return the result of the function
END