IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_prdquery_simple]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_prdquery_simple]
GO
create procedure [dbo].[mps_gen_prdquery_simple](
@cmpid int,
@pcid 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 = @pcid * (-1) 
    exec sys_sql_getTableNamePk @cmpid,'prd',@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 prd_id=ordid'+char(10)+char(9) +'    order by nid  '+char(10)+char(9) +'    select a.* from '+@dbname+@tbname+'_users a , @pobjs where prdp_prdid=ordid and prdp_cuid=@useruid'+char(10)+char(9) +'  order by prdp_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)+ 
                    '@pcid int , '+CHAR(13) + CHAR(9) +
                    '@includesub int , '+CHAR(13) + CHAR(9)  +
'@date1 datetime ,'+CHAR(13) + CHAR(9) +
'@date2 datetime ,'+CHAR(13) + CHAR(9) +
'@key1 nvarchar(50),'+CHAR(13) + CHAR(9) +
'@key2 nvarchar(50),'+CHAR(13) + CHAR(9) +
'@key3 nvarchar(50)'+CHAR(13) + CHAR(9)
    set @sqlpart2 = '';
    declare @pctag int 
    declare @pcselect varchar(max)
    declare @pcfrom varchar(max)
    declare @pcwhere varchar(max)
    declare @pcorderby varchar(max)
    declare @ctmain1 varchar(max)
    declare @ctmainusers varchar(max)

    set @ctmain1 = ''
    set @ctmainusers = ''
    -- 查询出searchfield
    select @searchfields = pc_searchfields , @pctag=pc_tag , @pcselect = pc_select,@pcfrom =pc_from,@pcwhere = pc_where, @pcorderby=pc_orderby from mps_prdctg where pc_id = @pcid
    set @searchfields = isnull(@searchfields,'') 

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

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

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

  if (@searchfields='') set @searchfields='prd_updated=prd_updated,prd_code=prd_code,prd_title=prd_title,prd_model=prd_model,prd_spec=prd_spec' 

declare @oldsflds nvarchar(1000) 
set @oldsflds = '' 
declare @datecount int 
set @datecount = 0
declare @datestr nvarchar(1000) 
set @datecount = 0

    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 != 'prd_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='prd' and fldi_tbid=@pctag and fldi_ord2tag=0 
        if @fieldtype is null
        begin
            set @fieldtype = 'varchar';
            set @fldisize = '255'
        end
        if (@fieldtype = 'datetime' or @fieldtype = 'date') --日期型
        begin
set @datecount = @datecount + 1
if (@datecount=1) --第一次出现日期字段
begin
set @datestr = +char(10)+ CHAR(9)+ CHAR(9)+ CHAR(9)+ '(' + @s1 + ' >= @date1 and '+@s1+' <= @date2 ) and '
end
set @sql = @sql +char(10)+ CHAR(9)+ CHAR(9)+ CHAR(9)+ ' (' + @s1 + ' like @key1 and '+@s1+' like @key2 and '+ @s1 + ' like @key3 ) or '
if @s1 != 'prd_updated'
begin
set @o2 = ',' + @s1 + ' desc';
end
        end else
        begin
set @sql = @sql +char(10)+ CHAR(9)+  CHAR(9)+ CHAR(9)+' (' + @s1 + ' like @key1 and '+@s1+' like @key2 and '+ @s1 + ' like @key3 ) or '
end

        set @orders = @orders + @o2;
        end 
    end

if(@datecount=0)
begin
set @datestr = char(10)+ CHAR(9)+  CHAR(9)+ CHAR(9)+' (prd_updated >= @date1 and prd_updated <= @date2 ) and '
end
set @sql = '('+SUBSTRING(@sql,1,LEN(@sql)-2)+')';
    set @sqlpart = SUBSTRING(@sqlpart,1,LEN(@sqlpart)-1);
    -- 如果排序
if(@pcorderby is null or @pcorderby = '')
begin
set @pcorderby = 'order by prd_updated desc '+@orders+char(13)
end else 
begin
set @pcorderby = 'order by  '+@pcorderby+char(13)
end 

set @sql = @datestr+char(10) + CHAR(9)+  CHAR(9)+ CHAR(9) + @sql + @pcwhere+char(10)+ CHAR(9)+  CHAR(9)+ CHAR(9)
    declare @sqlnextend varchar(max);
    set @sqlnextend = char(10)+
'    
if (@key1='''') set @key1=''%'' 
if (charindex(''%'',@key1)<=0 and @key1>'''') set @key1=''%''+@key1+''%''

if (@key2='''') set @key2=''%'' 
if (charindex(''%'',@key2)<=0 and @key2>'''') set @key2=''%''+@key2+''%''

if (@key3='''') set @key3=''%'' 
if (charindex(''%'',@key3)<=0 and @key3>'''') set @key3=''%''+@key3+''%''

declare @prdctg1 table(pcid int) 
declare @prdctg table(pcid int) 
insert into @prdctg1(pcid) values(@pcid) 
if (@includesub=0) 
begin
insert into @prdctg1(pcid) 
select pc_id from mps_prdctg,@prdctg1 b where pc_father=b.pcid and (not exists (select 1 from @prdctg1 c where c.pcid=pc_id)) 
insert into @prdctg1(pcid) 
select pc_id from mps_prdctg,@prdctg1 b where pc_father=b.pcid and (not exists (select 1 from @prdctg1 c where c.pcid=pc_id)) 
insert into @prdctg1(pcid) 
select pc_id from mps_prdctg,@prdctg1 b where pc_father=b.pcid and (not exists (select 1 from @prdctg1 c where c.pcid=pc_id)) 
insert into @prdctg1(pcid) 
select pc_id from mps_prdctg,@prdctg1 b where pc_father=b.pcid and (not exists (select 1 from @prdctg1 c where c.pcid=pc_id)) 
insert into @prdctg1(pcid) 
select pc_id from mps_prdctg,@prdctg1 b where pc_father=b.pcid and (not exists (select 1 from @prdctg1 c where c.pcid=pc_id)) 
insert into @prdctg(pcid) select distinct pcid from @prdctg1 
end    else 
begin
insert into @prdctg(pcid) select distinct pcid from @prdctg1 
end

declare @prduse int = 0
if @onlyme = 2 set @prduse = 1
else set @prduse = 0

if (@onlyme = 0) 
begin
if (@pagesize=10000)  -- 下载
begin
select '+@pcselect+' from '+@ctmain1+@dbname+@tbname+' a,'+@dbname+@tbname+'_users , @prdctg
where a.prd_id=prdp_prdid 
and prdp_type=''prdview'' 
and (a.prd_code not like ''%临%'' and a.prd_code<>'''')
and a.prd_use = @prduse
and a.prd_pcid=pcid 
and prdp_cuid=@useruid 
and (a.prd_cuid=@useruid or a.prd_createcuid=@useruid ) 
and '+ @sql+@pcorderby+'
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.prd_id  from '+@ctmain1+@dbname+@tbname+' a,'+@dbname+@tbname+'_users , @prdctg
where a.prd_id=prdp_prdid 
and prdp_type=''prdview'' 
and (a.prd_code not like ''%临%'' and a.prd_code<>'''') 
and a.prd_use = @prduse
and a.prd_pcid=pcid 
and prdp_cuid=@useruid 
and (a.prd_cuid=@useruid or a.prd_createcuid=@useruid ) 
and '+ @sql+@pcorderby+'
select count(1) resultcount  from datalinker where useruid=@useruid 
end else 
begin
select count(1) cnt from '+@ctmain1+@dbname+@tbname+' a,'+@dbname+@tbname+'_users , @prdctg
where a.prd_id=prdp_prdid 
and prdp_type=''prdview'' 
and (a.prd_code not like ''%临%'' and a.prd_code<>'''')
and a.prd_use = @prduse
and a.prd_pcid=pcid 
and prdp_cuid=@useruid 
and (a.prd_cuid= @useruid or a.prd_createcuid= @useruid) 
and '+ @sql+'
-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) prd_id from '+@ctmain1+@dbname+@tbname+' a ,'+@dbname+@tbname+'_users , @prdctg
where a.prd_id=prdp_prdid 
and prdp_type=''prdview'' 
and (a.prd_code not like ''%临%'' and a.prd_code<>'''')
and a.prd_use = @prduse
and a.prd_pcid=pcid 
and prdp_cuid=@useruid
and (a.prd_cuid= @useruid or a.prd_createcuid= @useruid) 
and '+ @sql+@pcorderby+'
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) '+@pcselect+' from '+@ctmain1+@dbname+@tbname+' a,@pobjs 
where a.prd_id=ordid '+ @pcwhere+'
order by nid 
-- 返回权限 
select a.* from '+@dbname+@tbname+'_users a , @pobjs 
where prdp_prdid=ordid 
and nid<=@pagesize 
and prdp_cuid=@useruid 
order by prdp_type 
end 
end 
else
begin
if (@pagesize=10000)  -- 下载
begin
select '+@pcselect+' from '+@ctmain1+@dbname+@tbname+' a,'+@dbname+@tbname+'_users , @prdctg
where a.prd_id=prdp_prdid 
and prdp_type=''prdview'' 
and (a.prd_code not like ''%临%'' and a.prd_code<>'''') 
and a.prd_use = @prduse
and a.prd_pcid=pcid 
and prdp_cuid=@useruid 
and '+ @sql+@pcorderby+'
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.prd_id  from '+@ctmain1+@dbname+@tbname+' a,'+@dbname+@tbname+'_users , @prdctg
where a.prd_id=prdp_prdid 
and prdp_type=''prdview'' 
and (a.prd_code not like ''%临%'' and a.prd_code<>'''') 
and a.prd_use = @prduse
and a.prd_pcid=pcid 
and prdp_cuid=@useruid 
and '+ @sql+@pcorderby+'
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from '+@ctmain1+@dbname+@tbname+' a,'+@dbname+@tbname+'_users , @prdctg
where a.prd_id=prdp_prdid 
and prdp_type=''prdview'' 
and (a.prd_code not like ''%临%'' and a.prd_code<>'''')
            and a.prd_use = @prduse
and a.prd_pcid=pcid 
and prdp_cuid=@useruid 
and '+ @sql+'
-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) prd_id from '+@ctmain1+@dbname+@tbname+' a,'+@dbname+@tbname+'_users , @prdctg
where a.prd_id=prdp_prdid 
and prdp_type=''prdview'' 
and (a.prd_code not like ''%临%'' and a.prd_code<>'''') 
            and a.prd_use = @prduse
and a.prd_pcid=pcid 
and prdp_cuid=@useruid
and '+ @sql+@pcorderby+'
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) '+@pcselect+' from '+@ctmain1+@dbname+@tbname+' a,@pobjs 
where a.prd_id=ordid '+ @pcwhere+'
order by nid 
-- 返回权限 
select a.* from '+@dbname+@tbname+'_users a , @pobjs 
where prdp_prdid=ordid 
and nid<=@pagesize 
and prdp_cuid=@useruid 
order by prdp_type 
end 
end
set nocount off '+char(13)

    --生成存储过程
    --判断某存储过程是否存在
    declare @psql nvarchar(max) 
    if (dbo.checkprocedure(@tbname+'_query_simple')=0) set @psql='alter ' else set @psql='create '
    set @psql = @psql+' procedure '+@tbname+N'_query_simple('+@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_simple'',
        ''' +@sqlpart+''''
    execute(@psql)

set nocount off 
end