IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_cstquery]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_cstquery]
GO
create procedure [dbo].[mps_gen_cstquery](
@cmpid int,
@ctid int
)
as
begin
    set nocount on
    declare @s1 nvarchar(255) 
    declare @tbname varchar(50)
    declare @dbname varchar(50)
    declare @fkey   varchar(50) 
    declare @mctid int 
    set @mctid = @ctid * (-1) 
    exec sys_sql_getTableNamePk @cmpid,'cst',@mctid,0,@tbname output,@fkey output ,@dbname output 
    -- 招聘状态=ord_zpzt,申请人=ord_zpgw,责任部门=ord_zrbm,责任公司=ord_zrgs
    declare @searchfields varchar(max),@sql varchar(max), @s varchar(255), 
            @site int, @fieldtype varchar(50), @sqlpart varchar(max),
            @fldisize int, @fldidecimal int, @sqlpart2 varchar(max),
            @sqlnextpage varchar(max);

    declare @orders varchar(max);
    declare @o2 varchar(100);
    
    set @orders = '';
    -- 初始化sql语句
    set @sqlnextpage = '    set nocount on 
    '+char(10)+'    declare @nonedate datetime '+char(10)+'    set @nonedate = CONVERT(datetime,''1910-1-1'') '+char(10)+'    declare @s1 varchar(8000) '+char(10)+'    declare @s2 varchar(100) '+char(10)+'    declare @i int '+char(10)+'    declare @amount int '+char(10)+'    select @amount = @pagecount*@pagesize '+char(10)+'    declare @pobjs table(nid int identity(1, 1), ordid bigint) '+char(10)+'    set @s1 = @pageobjids  '+char(10)+'    while (@s1>'''') '+char(10)+'    begin '+char(10)+char(9) +'    set @i = charindex('','', @s1) '+char(10)+char(9) +'    if (@i>0) '+char(10)+char(9) +'    begin '+char(10)+char(9) +char(9) +'    set @s2 = substring(@s1,1,@i-1) '+char(10)+char(9) +char(9) +'    set @s1 = substring(@s1,@i+1,len(@s1)-@i) '+char(10)+char(9) +'    end else  '+char(10)+char(9) +'    begin '+char(10)+char(9) +char(9) +'    set @s2 = @s1 '+char(10)+char(9) +char(9) +'    set @s1 = '''' '+char(10)+char(9) +'    end  '+char(10)+char(9) +'    insert into @pobjs(ordid) values(convert(bigint,@s2)) '+char(10)+'    end '+char(10)+'    if (exists (select top 1 * from @pobjs)) '+char(10)+'    begin '+char(10)+char(9) +'    select a.* from '+@dbname+@tbname+' a,'+char(9) +'    @pobjs'+char(10)+char(9) +'    where cc_id=ordid'+char(10)+char(9) +'    order by nid  '+char(10)+char(9) +'    select a.* from '+@dbname+@tbname+'_users a , @pobjs where ccp_ccid=ordid and ccp_cuid=@useruid'+char(10)+char(9) +' order by ccp_type     return '+char(10)+'    end ';
    set @sql = ''
    set @sqlpart =  CHAR(10) +CHAR(9)+ '@cmpid int,'+CHAR(13) +CHAR(9)+ 
                    '@branchid int,'+CHAR(13) + CHAR(9)+ 
                    '@userdpid int,'+CHAR(13) + CHAR(9)+ 
                    '@userotid int ,'+CHAR(13) + CHAR(9)+ 
                    '@useruid int ,'+CHAR(13) +CHAR(9)+ 
'@onlyme int ,'+CHAR(13) +CHAR(9)+ 
                    '@pagesize int , '+CHAR(13) + CHAR(9)+ 
                    '@pagecount int , '+CHAR(13) + CHAR(9)+ 
                    '@pageobjids varchar(8000),'+CHAR(13) + CHAR(9)+ 
                    '@ctid int , '+CHAR(13) + CHAR(9)  
    set @sqlpart2 = '';
    declare @cctag int 
    declare @ctselect varchar(max)
    declare @ctfrom varchar(max)
    declare @ctwhere varchar(max)
    declare @ctorderby varchar(max)
    declare @ctmain1 varchar(max)
    declare @ctmainusers varchar(max)

    set @ctmain1 = ''
    set @ctmainusers = ''
    -- 查询出searchfield
    select @searchfields = ct_searchfields , @cctag=ct_tag , @ctselect = ct_select,@ctfrom =ct_from,@ctwhere = ct_where, @ctorderby=ct_orderby from mps_csttype where ct_id = @ctid
    set @searchfields = isnull(@searchfields,'') 

if(@ctselect is null or @ctselect = '')
begin
set @ctselect = 'a.*'
end

if(@ctwhere is null or @ctwhere = '')
begin
set @ctwhere = ''
end
else
begin
set @ctwhere = '  ('+ @ctwhere +') ' +char(10)+ CHAR(9)+ CHAR(9)+ CHAR(9)
end

if(@ctfrom is null or @ctfrom = '')
begin
set @ctmain1 = ''
end
else
begin
set @ctfrom  = REPLACE(REPLACE(REPLACE(REPLACE(@ctfrom,CHAR(9),''),CHAR(13),''),CHAR(10),''),' ','')
declare @ctf1 varchar(max)
declare @ctsite1 int
while @ctfrom > ''
begin
-- 获取逗号位置
set @ctsite1 = charindex(',',@ctfrom);
if @ctsite1 = 0
begin
set @ctf1 = @ctfrom;
set @ctfrom = ''
end
else
begin
set @ctf1 = LEFT(@ctfrom, @ctsite1-1);
set @ctfrom = SUBSTRING(@ctfrom,@ctsite1+1,LEN(@ctfrom))
end
-- 判断是否是两个主表@dbname+@tbname
if(@ctf1 != @dbname+@tbname and @ctf1 != @dbname+@tbname+'_users')
begin
set @ctmain1 = @ctmain1 + @ctf1 + ','
end
end
end

    if (@searchfields='') set @searchfields='cc_updated=cc_updated,cc_code=cc_code,cc_name=cc_name,cc_mobile=cc_mobile,cc_company=cc_company,cc_contact=cc_contact,cc_identno=cc_identno' 

declare @oldsflds nvarchar(1000) 
set @oldsflds = '' 

    while @searchfields > ''
    begin
        -- 获取逗号位置
        set @site = charindex(',',@searchfields);
        if @site = 0
        begin
            set @s = @searchfields;
            set @s1 = right(@s,len(@s)-charindex('=',@s));
            set @searchfields = ''
        end
        else
        begin
            set @s = LEFT(@searchfields, @site-1);
            set @s1 = right(@s,len(@s)-charindex('=',@s));
            set @searchfields = SUBSTRING(@searchfields,@site+1,LEN(@searchfields))
        end
set @o2 = '';
if (@s1!='' and charindex(','+@s1+',',@oldsflds)<=0)  
begin
set @oldsflds = ','+@oldsflds+','+@s1+','
if @s1 != 'cc_updated'
begin
set @o2 = ',' + @s1 ;
end
        -- 查询字段类型
        select top 1 @fieldtype = fldi_datatype,
        @fldisize = fldi_size, 
        @fldidecimal = fldi_decimal
        from mps_fieldinfo 
        where fldi_fieldname = @s1 and fldi_cmpid=@cmpid and fldi_tbtype='cst' and fldi_tbid=@cctag and fldi_ord2tag=0 
        if @fieldtype is null
        begin
            set @fieldtype = 'varchar';
            set @fldisize = '255'
        end
        if (@fieldtype = 'int' or @fieldtype = 'tinyint' or @fieldtype = 'integer' or @fieldtype = 'bigint' or @fieldtype = 'decimal' or @fieldtype = 'float' or @fieldtype = 'long') --整数型
        begin
            if @fldisize = 0
            begin
                set @fldisize = 8;
            end
            
            set @fieldtype = 'float';
            
            set @sqlnextpage = @sqlnextpage  +char(10)+ CHAR(9)+ 'if (@'+@s1+'_min'+ ' is null or @'+@s1+'_min'+ ' = -999999999) set @'+@s1+'_min'+ ' = -999999999 ' +char(10)+ CHAR(9)+ 'if (@'+@s1+'_max'+ ' is null or @'+@s1+'_max'+ ' = -999999999) set @'+@s1+'_max'+ ' = 999999999'+CHAR(10);
            
            set @sqlpart = @sqlpart + CHAR(13) + CHAR(9) +'@'+@s1+'_min ' + @fieldtype +','+CHAR(13) + CHAR(9) +'@'+@s1+'_max ' +@fieldtype + ','
            
            set @sql = @sql + @s1 + '>=@'+@s1+'_min '+char(10)+ CHAR(9)+ CHAR(9)+ CHAR(9)+ 'and '+ @s1 + '<=@'+@s1+'_max '+char(10)+ CHAR(9)+ CHAR(9)+ CHAR(9)+ 'and ';
        end else 
        if (@fieldtype = 'datetime' or @fieldtype = 'date') --日期型
        begin
            set @sqlnextpage = @sqlnextpage  +char(10)+ CHAR(9)+  '
                if (@'+@s1+'_min'+ ' is null or @'+@s1+'_min'+ ' <= @nonedate) set @'+@s1+'_min'+ ' = @nonedate 
                ' +char(10)+ CHAR(9)+ 'if (@'+@s1+'_max'+ ' is null or @'+@s1+'_max'+ ' <= @nonedate) set @'+@s1+'_max'+ ' =  ''2299-12-30 23:59:59'''+char(10)+ CHAR(9)+'else set @'+@s1+'_max'+ ' = @'+@s1+'_max+ ''23:59:59''
                '+CHAR(10);
            set @sqlpart = @sqlpart + CHAR(13) + CHAR(9) +'@'+@s1+'_min ' + @fieldtype +',' + CHAR(13) + CHAR(9) +'@'+@s1+'_max ' + @fieldtype + ','
            set @sql = @sql + @s1 + '>=@' +@s1+'_min '+char(10)+ CHAR(9)+ CHAR(9)+ CHAR(9)+ 'and ' 
            + @s1 + '<=@'+@s1+'_max '+char(10)+ CHAR(9)+ CHAR(9)+ CHAR(9)+ 'and ';
            if @s1 != 'cc_updated'
begin
                set @o2 = ',' + @s1 + ' desc';
end
        end else
        if @fieldtype = 'text' --文本型 text
        begin
            set @sqlnextpage = @sqlnextpage  +char(10)+ CHAR(9)+ 'if (@'+@s1+'_min'+ ' is null or @'+@s1+'_min'+ ' = ''_none_'') set @'+@s1+'_min'+ ' = '''' ' +char(10)+ CHAR(9)+ +'if (@'+@s1+'_max'+ ' is null or @'+@s1+'_max'+ ' = ''_none_'') set @'+@s1+'_max'+ ' = '''' '+CHAR(10);
            
            set @sqlpart2 = @sqlpart2 +char(10)+ CHAR(9)+'if @'+@s1+'_min'+ '>'''' '+CHAR(10) + CHAR(9)+'begin '+CHAR(10) +char(9)+ CHAR(9) +'if charindex(''%'',@'+@s1+'_min) = 0'+CHAR(10)+char(9)+ CHAR(9)  +' begin'+CHAR(10) +char(9) +char(9)+char(9) +'set @'+@s1+'_min = ''%''+@'+@s1+'_min+''%'';'+CHAR(10)+char(9) + CHAR(9) +' end'+CHAR(10) + CHAR(9)+'end else'+CHAR(10) + CHAR(9)+'begin '+CHAR(10) + CHAR(9)+ CHAR(9)+'set @'+@s1+'_min = ''%'' '+CHAR(10) + CHAR(9)+'end'+CHAR(10)+ CHAR(9) +'if @'+@s1+'_max'+ '>'''' '+CHAR(10)+ CHAR(9) +'begin '+CHAR(10) +char(9) + CHAR(9)+'if charindex(''%'', @'+@s1+'_max) = 0'+CHAR(10)+char(9)+ CHAR(9)  +'begin'+CHAR(10)+char(9) +char(9) + CHAR(9) +'set@'+@s1+'_max = ''%''+@'+@s1+'_max+''%'';'+CHAR(10)+char(9) + CHAR(9) +'end'+CHAR(10) + CHAR(9)+'end else'+CHAR(10) + CHAR(9)+'begin '+CHAR(10) + CHAR(9)+ CHAR(9)+'set @'+@s1+'_max = ''%'' '+CHAR(10) + CHAR(9)+'end'+CHAR(10)
            
            set @sqlpart = @sqlpart + CHAR(13) + CHAR(9) + '@'+@s1+'_min ' + @fieldtype +','+ CHAR(13) + CHAR(9) + ' @'+@s1+'_max ' + @fieldtype + ','
            set @sql = @sql + @s1 + ' like @'  +@s1+'_min '+char(10)+ CHAR(9)+ CHAR(9)+ CHAR(9)+ 'and ' 
            + @s1 + ' like @'  +@s1+'_max '+char(10)+ CHAR(9)+ CHAR(9)+ CHAR(9)+ 'and ';
        endelse
        if @fieldtype = 'varchar' or @fieldtype = 'nvarchar' or @fieldtype = 'string' or @fieldtype = 'char' --字符串型
        begin
            if @fldisize = 0
            begin
                set @fldisize = 255;
            end
            if @fieldtype = 'string'
            begin
                set @fieldtype = 'nvarchar';
            end
            set @sqlpart = @sqlpart + CHAR(13) + CHAR(9) +'@'+@s1+'_min ' + @fieldtype 
            +'('+convert(varchar,@fldisize,50)+'), '+ CHAR(13) + CHAR(9) + +'@'+@s1+'_max ' + @fieldtype 
            + '('+convert(varchar,@fldisize,50)+'),';
            
            set @sqlnextpage = @sqlnextpage +char(10)+ CHAR(9)+ 
            'if (@'+@s1+'_min'+ ' is null or @'+@s1+'_min'+ ' = ''_none_'') set @'+@s1+'_min'+ ' = '''' '+CHAR(10)+ CHAR(9) +
            'if (@'+@s1+'_max'+ ' is null or @'+@s1+'_max'+ ' = ''_none_'') set @'+@s1+'_max'+ ' = '''' '+CHAR(10);
            
            set @sqlpart2 = @sqlpart2 +char(10)+ CHAR(9)+
            'if @'+@s1+'_min'+ '>'''' '+CHAR(10) + CHAR(9)+
            'begin '+CHAR(10) +char(9)+ CHAR(9) +
            'if (charindex(''%'',@'+@s1+'_min) = 0 and @'+@s1+'_min!=''_empty_'') '+CHAR(10)+char(9)+ CHAR(9)  +
            ' begin'+CHAR(10) +char(9) +char(9)+char(9) +
            'set @'+@s1+'_min = ''%''+@'+@s1+'_min+''%'';'+CHAR(10)+char(9) + CHAR(9) +
            ' end else if (@'+@s1+'_min=''_empty_'') set @'+@s1+'_min='''' '+CHAR(10) + CHAR(9)+'end else'+CHAR(10) + CHAR(9)+'begin '+CHAR(10) + CHAR(9)+ CHAR(9)+'set @'+@s1+'_min = ''%'' '+CHAR(10) + CHAR(9)+'end'+CHAR(10)+ CHAR(9) +
            'if @'+@s1+'_max'+ '>'''' '+CHAR(10)+ CHAR(9) +'begin '+CHAR(10) +char(9) + CHAR(9)+
            'if (charindex(''%'', @'+@s1+'_max) = 0 and @'+@s1+'_max!=''_empty_'') '+CHAR(10)+char(9)+ CHAR(9)  
            +'begin'+CHAR(10)+char(9) +char(9) + CHAR(9) +'set@'+@s1+'_max = ''%''+@'+@s1+'_max+''%'';'+CHAR(10)+char(9) + CHAR(9) +
            ' end else if (@'+@s1+'_max=''_empty_'') set @'+@s1+'_max='''' '+CHAR(10) + CHAR(9)+'end else'+CHAR(10) + CHAR(9)+'begin '+CHAR(10) + CHAR(9)+ CHAR(9)+'set @'+@s1+'_max = ''%'' '+CHAR(10) + CHAR(9)+'end'+CHAR(10)
            
            
            set @sql = @sql + @s1 + ' like @' +@s1+'_min '+ char(10)+ CHAR(9)+ CHAR(9)+ CHAR(9)+ 'and ' + @s1 + ' like @' +@s1+'_max ' +char(10)+ CHAR(9)+ CHAR(9)+ CHAR(9)+ 'and ';
        end

        set @orders = @orders + @o2;
        end 
    end
    set @sql = SUBSTRING(@sql,1,LEN(@sql)-3);
    set @sqlpart = SUBSTRING(@sqlpart,1,LEN(@sqlpart)-1);
    -- 如果排序
if(@ctorderby is null or @ctorderby = '')
begin
set @ctorderby = 'order by cc_updated desc'+@orders+char(13)
end else 
begin
set @ctorderby = 'order by '+@ctorderby+'  '+char(13)
end

set @sql = @sql + @ctwhere
    declare @sqlnextend varchar(max);
    set @sqlnextend = char(10)+
'    
if (@onlyme = 0) 
begin
if (@pagesize=10000)  -- 下载
begin
select '+@ctselect+' from '+@ctmain1+@dbname+@tbname+' a,'+@dbname+@tbname+'_users 
where a.cc_id=ccp_ccid 
and ccp_type=''cstview'' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid 
and (a.cc_cuid=@useruid or a.cc_createcuid=@useruid ) 
and '+ @sql+@ctorderby+'
end else 
if (@pagesize>=100000000)  --  转存到中间表
begin
set @pagesize = @pagesize - 100000000 
if (@pagesize<=0) set @pagesize = 100000000 
delete datalinker where useruid = @useruid 
insert into datalinker(useruid,objid) 
select top(@pagesize) @useruid,a.cc_id  from '+@ctmain1+@dbname+@tbname+' a,'+@dbname+@tbname+'_users 
where a.cc_id=ccp_ccid 
and ccp_type=''cstview'' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid 
and (a.cc_cuid=@useruid or a.cc_createcuid=@useruid ) 
and '+ @sql+@ctorderby+'
select count(1) resultcount  from datalinker where useruid=@useruid 
end else 
begin
select count(1) cnt from '+@ctmain1+@dbname+@tbname+' a,'+@dbname+@tbname+'_users 
where a.cc_id=ccp_ccid 
and ccp_type=''cstview'' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid 
and (a.cc_cuid= @useruid or a.cc_createcuid= @useruid) 
and '+ @sql+'
-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) cc_id from '+@ctmain1+@dbname+@tbname+' a ,'+@dbname+@tbname+'_users 
where a.cc_id=ccp_ccid 
and ccp_type=''cstview'' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid
and (a.cc_cuid= @useruid or a.cc_createcuid= @useruid) 
and '+ @sql+@ctorderby+'
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) '+@ctselect+' from '+@ctmain1+@dbname+@tbname+' a,@pobjs 
where a.cc_id=ordid '+ @ctwhere+'
order by nid 
-- 返回权限 
select a.* from '+@dbname+@tbname+'_users a , @pobjs 
where ccp_ccid=ordid 
and nid<=@pagesize 
and ccp_cuid=@useruid
order by ccp_type  
end 
end 
else
begin
if (@pagesize=10000)  -- 下载
begin
select '+@ctselect+' from '+@ctmain1+@dbname+@tbname+' a,'+@dbname+@tbname+'_users 
where a.cc_id=ccp_ccid 
and ccp_type=''cstview'' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid 
and '+ @sql+@ctorderby+'
end else 
if (@pagesize>=100000000)  --  转存到中间表
begin
set @pagesize = @pagesize - 100000000 
if (@pagesize<=0) set @pagesize = 100000000 
delete datalinker where useruid = @useruid 
insert into datalinker(useruid,objid) 
select top(@pagesize) @useruid,a.cc_id  from '+@ctmain1+@dbname+@tbname+' a,'+@dbname+@tbname+'_users 
where a.cc_id=ccp_ccid 
and ccp_type=''cstview'' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid 
and '+ @sql+@ctorderby+'
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from '+@ctmain1+@dbname+@tbname+' a,'+@dbname+@tbname+'_users 
where a.cc_id=ccp_ccid 
and ccp_type=''cstview'' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid 
and '+ @sql+'
-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) cc_id from '+@ctmain1+@dbname+@tbname+' a,'+@dbname+@tbname+'_users 
where a.cc_id=ccp_ccid 
and ccp_type=''cstview'' 
and a.cc_ctid=@ctid 
and ccp_cuid=@useruid
and '+ @sql+@ctorderby+'
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) '+@ctselect+' from '+@ctmain1+@dbname+@tbname+' a,@pobjs 
where a.cc_id=ordid '+ @ctwhere+'
order by nid 
-- 返回权限 
select a.* from '+@dbname+@tbname+'_users a , @pobjs 
where ccp_ccid=ordid 
and nid<=@pagesize 
and ccp_cuid=@useruid 
order by ccp_type 
end 
end
set nocount off '+char(13)

    --生成存储过程
    --判断某存储过程是否存在
    declare @psql nvarchar(max) 
    if (dbo.checkprocedure(@tbname+'_query')=0) set @psql='alter ' else set @psql='create '
    set @psql = @psql+' procedure '+@tbname+N'_query('+@sqlpart+') '+char(13)+'as '+char(9) +char(13)+
    'begin '+char(9) +char(13)+@sqlnextpage+@sqlpart2 +' '+char(9)+@sqlnextend +char(13)+
    'end' 
    exec myprint @psql 
    execute(@psql)
    -- 生成mps_sql 
    set @psql = ' exec mps_sys_gen_mpssql '+CONVERT(varchar(10),@cmpid)+','''+@tbname+N'_query'',
        ''' +@sqlpart+''''
    execute(@psql)

set nocount off 
end