IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_powerusers_data]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_powerusers_data]
GO
create procedure [dbo].[mps_gen_powerusers_data](
@cmpid int ,
@type varchar(10) , -- 'biz cst prd equ  如果是cst prd equ则@bizid为ctid 
@bizid int,  -- 
@mpctgid int,
@mptypes varchar(2000) ,
@condition nvarchar(4000) 
)
as
begin
-- 一些常用变量
set @condition = isnull(@condition , '') 
declare @prevf varchar(10) 
declare @typef varchar(10) 
declare @from nvarchar(100) 
set @from = '' 
set @prevf = '' 
set @typef = '' 

if (@type='biz')  
begin
set @prevf = 'ord' 
set @typef = 'biz' 
end 
if (@type='prd') 
begin
set @prevf = 'prd' 
set @typef = 'pc' 
end
if (@type='cst') 
begin
set @prevf = 'cc' 
set @typef = 'ct' 
end
if (@type='equ') 
begin
set @prevf = 'equ' 
set @typef = 'ec' 
end

if (@condition<='') 
begin
if (@type='biz')  
begin
set @condition = ' ('+@prevf+'_id>0) '
end 
if (@type='prd') 
begin
set @condition = ' ('+@prevf+'_id>0 and '+@prevf+'_pcid='+ltrim(rtrim(str(@bizid)))+') '
end
if (@type='cst') 
begin
set @condition = ' ('+@prevf+'_id>0 and '+@prevf+'_ctid='+ltrim(rtrim(str(@bizid)))+') '
end
if (@type='equ') 
begin
set @condition = ' ('+@prevf+'_id>0 and '+@prevf+'_ecid='+ltrim(rtrim(str(@bizid)))+') '
end
end  else 
begin
if (charindex('objid',@condition)>0) 
begin
set @from = ',#srcobjs ' 
end
end 

declare @p100 int 
declare @p10  int 
declare @p1  int 
declare @myi int 
 
declare @bclevel int 
declare @bcorder int 
declare @newbcid int 
declare @fjbid bigint 
declare @tablename varchar(30)  
declare @s varchar(100) 
declare @databasename varchar(30) 
declare @ftype varchar(10) 
declare @fbizid int
declare @fordid bigint 
declare @ftagid int 
declare @ford2id bigint 
declare @newcuid int 
declare @fdpid int 
declare @tfield varchar(50) 
declare @sql nvarchar(2000) 

declare @exttbname varchar(100) 
declare @exttbname2 varchar(100) 
set @exttbname = '' 
set @exttbname2 = '' 

-- mps_permission  可以同时计算多组权限配置
declare @t table(mptype varchar(50)) 
declare @i int,@curInd int,@strlen int,@tmps varchar(100) ,@srcs varchar(2000)
set @srcs = @mptypes 
select @curInd=1,@strlen=LEN(@srcs) 
while (@curInd<=@strlen) 
begin 
select @i=charindex(',',@srcs,@curInd) 
if (@i>0) 
begin 
select @tmps=SUBSTRING(@srcs,@curInd,@i-@curInd) 
set @curInd=@i+1 
end else if (@curInd<=@strlen) 
begin 
select @tmps=SUBSTRING(@srcs,@curInd,@strlen-@curInd+1) 
set @curInd=@strlen+1 
end 
if (@tmps>'') insert into @t(mptype) values(@tmps) 
end

declare @psql nvarchar(MAX) 

 -- 开始计算@PSQL
 -- create table #user(pptype varchar(20),cuid int , ordid bigint) 
 -- (pptype varchar(30),cuid int, ordid bigint)
 create table #user2(pptype varchar(30),cuid1 int, ordid int , dpt int, me int, level int,cuid2 int,cuid3 int,cuid4 int,cuid5 int,cuid6 int , cuid7 int , cuid8 int,cuid9 int ,cuid10 int)
 declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
declare @mctid int
if (@type='biz')
begin
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 
end 
if (@type='cst')
begin
set @mctid = @bizid * (-1) 
exec sys_sql_getTableNamePk @cmpid,'cst',@mctid,0,@tbname output,@fkey output ,@dbname output 
end 
if (@type='prd')
begin
set @mctid = @bizid * (-1) 
exec sys_sql_getTableNamePk @cmpid,'prd',@mctid,0,@tbname output,@fkey output ,@dbname output 
end 
if (@type='equ')
begin
set @mctid = @bizid * (-1) 
exec sys_sql_getTableNamePk @cmpid,'equ',@mctid,0,@tbname output,@fkey output ,@dbname output 
end 
 
declare @p table(ptype varchar(30),ctgid int, grp int, pid int , pobjid1 int, pobjid2 int,porder int ,psql nvarchar(2000), cap1 nvarchar(200)) 
insert into @p(ptype,ctgid,grp,pid,pobjid1,pobjid2,porder,psql,cap1) 
select distinct mp_type,mp_ctgid,mp_group,mp_ppid,mp_objid1,mp_objid2,isnull(mp_order,0),isnull(mp_sql,''),mp_caption  from mps_permission,@t  
  where mp_cmpid=@cmpid and mp_type=mptype and mp_ctgid=@mpctgid 
  
--看看有没有自定义 
if (exists (select pid from @p where pid=990)) 
begin
insert into @p(ptype,ctgid,grp,pid,pobjid1,pobjid2,psql,cap1) 
select ptype,ctgid,grp,pgd_flag,pgd_objid,pgd_objid2,isnull(pgd_sql,''),pgd_caption  from mps_privategroup2,@p 
where pid=990 and pobjid1=pgd_pgid 
end
-- 有没有决策链
if (exists (select pid from @p where pid=991)) 
begin
insert into @p(ptype,ctgid,grp,pid,pobjid1,pobjid2,psql,cap1) 
select ptype,ctgid,grp,mp_ppid,mp_objid1,mp_objid2,isnull(mp_sql,'') ,mp_caption  from mps_permission,@p 
where pid=991 and mp_cmpid=@cmpid and mp_type='decision' and mp_ctgid=@mpctgid and mp_group=pobjid1   
end

delete @p where pid=990 or pid=991 

declare @pobjmc varchar(30)
declare @cap nvarchar(200) 
declare @pctgid int 
declare @pgrp int 
declare @ptype int 
declare @pobjid1 int
declare @pobjid2 int 
declare @porder int 

declare status1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select ptype,ctgid,grp,pid,pobjid1,pobjid2,porder,psql,cap1 from @p 
open status1

FETCH NEXT FROM status1 INTO @pobjmc,@pctgid,@pgrp,@ptype , @pobjid1, @pobjid2 , @porder,@psql,@cap
WHILE (@@FETCH_STATUS = 0)
BEGIN
 -- 开始计算权限   这段程序代码量很大 **************************************************************************
 set @psql = ''   -- 对于订单权限的计算 ， 不需要扩展条件
 set @p100 = @ptype / 100  
 set @p10 = (@ptype - @p100 * 100)/10 
 set @p1 = @ptype - @p100 * 100 - @p10 * 10 
 print '--- '+@pobjmc+'('+convert(varchar(10),@ptype)+','+convert(varchar(10),@pobjid1)+','+convert(varchar(10),@pobjid2)+') '+@cap 
 if (@p100=0) 
 begin
 if (@p10=0) 
 begin
 if (@p1=1) --1、所有人100
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_login   where cu_cmpid=@cmpid   
 end else 
 if (@p1=2) --2、分公司所有人200
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_login where cu_cmpid=@cmpid  and cu_bcid>0 
 end else
 if (@p1=3) --3、指定分公司所有人300
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and cu_bcid=@pobjid1 
 end else
 if (@p1=4) --4、指定角色(按分公司)4bcid   角色id  bcid=0 总部，非0表示指定分公司ID
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and cu_bcid=@pobjid1 and cu_function=@pobjid2 
 end else
 if (@p1=5) --5 指定某一个人1cuid0   cuid表示指定人  
 begin
 insert into #user(pptype,cuid,ordid) values(@pobjmc,@pobjid1,0) 
 end else
 if (@p1=6) --6指定角色角色id    0     所有的角色，不管是不是总部还是分公司  
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and cu_function=@pobjid1 and cu_bcid=@pobjid2  
 end else
 if (@p1=7) --7指定角色角色id    0     所有分公司的某角色ID  
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and cu_bcid>0 and cu_function=@pobjid1 
 end  else
 if (@p1=8) --8指定岗位(所有分公司岗位 8otid       0 所有分公司的岗位 
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and cu_bcid>0 and cu_otid=@pobjid1 
 end else 
 if (@p1=9) --9指定岗位(按分公司或总部)  9otidbcid   bcid=0 所有总部，非0表示指定分公司ID
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_otid=@pobjid1 and cu_bcid=@pobjid2 
 end
 end else 
 if (@p10=1)
 begin
 if (@p1=0) --指定部门及岗位  10dpidotid   (不管是集团版还是非集团版，都可以使用此角色) 
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_dpid=@pobjid1 and cu_otid=@pobjid2 
 end else
 if (@p1=1) --指定部门及岗位(按分司)   11dpstidotid   (所有分公司) 
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_dpstdid=@pobjid1 and cu_otid=@pobjid2 
 end else
 if (@p1=2) --指定部门 12dpid         (如果是集团，那么总部也是用这个）
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_dpid=@pobjid1  
 end else
 if (@p1=3) --指定部门(按分公司)13dpstidbcid   bcid=0 所有分公司，非0表示指定分公司ID
 begin
 if (@pobjid2=0) 
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_dpstdid=@pobjid1  and cu_bcid>0 
 else insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_dpstdid=@pobjid1  and cu_bcid=@pobjid2 
 end
 end else
 if (@p10=2)
 begin
 if (@p1=0) --指定管理级别20  level  0   全集团，包括总部，如果不是集团，则全公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_managelevel=@pobjid1  
 end else
 if (@p1=1) --指定管理级别及更高21  level  0   全集团，包括总部，如果不是集团，则全公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_managelevel>=@pobjid1  
 end else
 if (@p1=2) --指定管理级别及更低22  level  0   全集团，包括总部，如果不是集团，则全公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_managelevel<=@pobjid1  
 end else
 if (@p1=3) --指定管理级别(所有分公司)23  level  0   所有分公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_managelevel=@pobjid1 and cu_bcid>0  
 end else
 if (@p1=4) --指定管理级别及更高(所有分公司)24  level  0   所有分公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_managelevel>=@pobjid1  and cu_bcid>0  
 end else
 if (@p1=5) --指定管理级别及更低(所有分公司)25  level  0   所有分公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_managelevel<=@pobjid1  and cu_bcid>0   
 end  else
 if (@p1=6) --指定管理级别(指定公司)26  bcid  level   指定公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_managelevel=@pobjid2  and cu_bcid=@pobjid1    
 end  else
 if (@p1=7) --指定管理级别及更高(指定公司)27  bcid  level   指定公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_managelevel>=@pobjid2  and cu_bcid=@pobjid1    
 end  else
 if (@p1=8) --指定管理级别及更低(指定公司)28  bcid  level   指定公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_managelevel<=@pobjid2  and cu_bcid=@pobjid1    
 end else
 if (@p1=9) --指定管理级别(分公司部门)29  stddpid  level   分公司部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_managelevel=@pobjid2  and cu_dpstdid=@pobjid1    
 end
end else
if (@p10=3)
begin
if (@p1=0) --指定管理级别及更高(分公司部门)30  stddpid  level   分公司部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_managelevel>=@pobjid2  and cu_dpstdid=@pobjid1    
 end else 
 if (@p1=1) --指定管理级别及更低(分公司部门)31  stddpid  level   分公司部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_managelevel<=@pobjid2  and cu_dpstdid=@pobjid1    
 end else 
 if (@p1=2) --指定管理级别(当前公司)32  dpid  level   指定部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_managelevel=@pobjid2  and cu_dpid=@pobjid1    
 end else 
 if (@p1=3) --指定管理级别及更高(当前公司)33  dpid  level   指定部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_managelevel>=@pobjid2  and cu_dpid=@pobjid1    
 end else 
 if (@p1=4) --指定管理级别及更低(当前公司)34  dpid  level   指定部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_managelevel<=@pobjid2  and cu_dpid=@pobjid1    
 end else 
if (@p1=5) --指定业务级别35  level  0   全集团，包括总部，如果不是集团，则全公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_professionlevel=@pobjid1  
 end else
 if (@p1=6) --指定业务级别及更高36  level  0   全集团，包括总部，如果不是集团，则全公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_professionlevel>=@pobjid1  
 end else
 if (@p1=7) --指定业务级别及更低37  level  0   全集团，包括总部，如果不是集团，则全公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_professionlevel<=@pobjid1  
 end else
 if (@p1=8) --指定业务级别(所有分公司)38  level  0   所有分公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_professionlevel=@pobjid1 and cu_bcid>0  
 end else
 if (@p1=9) --指定业务级别及更高(所有分公司)39  level  0   所有分公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_professionlevel>=@pobjid1  and cu_bcid>0  
 end 
end else
if (@p10=4)
begin
if (@p1=0) --指定业务级别及更低(所有分公司)40  level  0   所有分公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_professionlevel<=@pobjid1  and cu_bcid>0   
 end else 
 if (@p1=1) --指定业务级别(指定公司)41  bcid  level   指定公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_professionlevel=@pobjid2  and cu_bcid=@pobjid1   
 end else 
 if (@p1=2) --指定业务级别及更高(指定公司)42  bcid  level   指定公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_professionlevel>=@pobjid2  and cu_bcid=@pobjid1   
 end else 
 if (@p1=3) --指定业务级别及更低(指定公司)43  bcid  level   指定公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_professionlevel<=@pobjid2  and cu_bcid=@pobjid1   
 end else 
 if (@p1=4) --指定业务级别(分公司部门)44  stddpid  level   分公司部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_professionlevel=@pobjid2  and cu_dpstdid=@pobjid1   
 end else 
 if (@p1=5) --指定业务级别及更高(分公司部门)45  stddpid  level   分公司部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_professionlevel>=@pobjid2  and cu_dpstdid=@pobjid1   
 end else 
 if (@p1=6) --指定业务级别及更低(分公司部门)46  stddpid  level   分公司部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_professionlevel<=@pobjid2  and cu_dpstdid=@pobjid1   
 end else 
 if (@p1=7) --指定业务级别(当前部门 )47  dpid  level   指定部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_professionlevel=@pobjid2  and cu_dpid=@pobjid1   
 end else 
 if (@p1=8) --指定业务级别及更高(当前部门)48  dpid  level   指定部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_professionlevel>=@pobjid2  and cu_dpid=@pobjid1   
 end else 
 if (@p1=9) --指定业务级别及更低(当前部门)49  dpid  level   指定部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_professionlevel<=@pobjid2  and cu_dpid=@pobjid1   
 end
end else
if (@p10=5)
begin
if (@p1=0) --指定岗位级别50  level  0   全集团，包括总部，如果不是集团，则全公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_otlevel=@pobjid1  
 end else
 if (@p1=1) --指定岗位级别及更高51  level  0   全集团，包括总部，如果不是集团，则全公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_otlevel>=@pobjid1  
 end else
 if (@p1=2) --指定岗位级别及更低52  level  0   全集团，包括总部，如果不是集团，则全公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_otlevel<=@pobjid1  
 end else
 if (@p1=3) --指定岗位级别(所有分公司)53  level  0   所有分公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_otlevel=@pobjid1 and cu_bcid>0  
 end else
 if (@p1=4) --指定岗位级别及更高(所有分公司)54  level  0   所有分公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_otlevel>=@pobjid1  and cu_bcid>0  
 end else
 if (@p1=5) --指定岗位级别及更低(所有分公司)55  level  0   所有分公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_otlevel<=@pobjid1  and cu_bcid>0   
 end else
 if (@p1=6) --指定岗位级别(指定公司)56  bcid  level   指定公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_otlevel=@pobjid2  and cu_bcid=@pobjid1   
 end else
 if (@p1=7) --指定岗位级别及更高(指定公司)57  bcid  level   指定公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_otlevel>=@pobjid2  and cu_bcid=@pobjid1   
 end else
 if (@p1=8) --指定岗位级别及更低(指定公司)58  bcid  level   指定公司范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_otlevel<=@pobjid2  and cu_bcid=@pobjid1   
 end else
 if (@p1=9) --指定岗位级别(分公司部门)59  stddpid  level   分公司部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_otlevel=@pobjid2  and cu_dpstdid=@pobjid1   
 end
end else 
if (@p10=6)
begin
if (@p1=0) --指定岗位级别及更高(分公司部门)60  stddpid  level   分公司部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_otlevel>=@pobjid2  and cu_dpstdid=@pobjid1   
 end else
 if (@p1=1) --指定岗位级别及更低(分公司部门)61  stddpid  level   分公司部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_otlevel<=@pobjid2  and cu_dpstdid=@pobjid1   
 end else
 if (@p1=2) --指定岗位级别(当前公司 )62  dpid  level   指定部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_otlevel=@pobjid2  and cu_dpid=@pobjid1   
 end else
 if (@p1=3) --指定岗位级别及更高(当前公司)63  dpid  level   指定部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_otlevel>=@pobjid2  and cu_dpid=@pobjid1   
 end else
 if (@p1=4) --指定岗位级别及更低(当前公司)64  dpid  level   指定部门范围内
 begin
 insert into #user(pptype,cuid,ordid) select @pobjmc, cu_id,0  from mps_user where cu_cmpid=@cmpid  and  cu_otlevel<=@pobjid2  and cu_dpid=@pobjid1   
 end
end
 end else 
 if (@p100=1)
 begin
 if (@p10=0)
 begin
 if (@p1=1)--101    otid          flag 当前业务所有步骤的执行人。 otid=0所有，>0指定岗位的
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, '+@prevf+'p_cuid,'+@prevf+'_id from '+@dbname+@tbname+','+@dbname+@tbname+'_users '+@from 
 if (@pobjid1>0 or @pobjid2>0) set @psql = @psql+',mps_user '
 set @psql=@psql+' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and  '+@prevf+'p_type=''wrkexecutor'' and '+
 ' '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@condition+' ' 
 if (@pobjid1>0 or @pobjid2>0) set @psql = @psql+' and '+@prevf+'p_cuid=cu_id ' 
 if (@pobjid1>0 and @pobjid2=0) set @psql = @psql+' and cu_otid=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=1) set @psql = @psql+' and cu_otlevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=2) set @psql = @psql+' and cu_otlevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=3) set @psql = @psql+' and cu_otlevel<=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=4) set @psql = @psql+' and cu_managelevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=5) set @psql = @psql+' and cu_managelevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=6) set @psql = @psql+' and cu_managelevel<=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=7) set @psql = @psql+' and cu_professionlevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=8) set @psql = @psql+' and cu_professionlevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=9) set @psql = @psql+' and cu_professionlevel<=@pobjid1 ' 
 end else 
 if (@p1=2)--业务所有知会人102    otid          0  当前业务所有知会人， 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, '+@prevf+'p_cuid,'+@prevf+'_id from '+@dbname+@tbname+','+@dbname+@tbname+'_users '+@from 
 if (@pobjid1>0 or @pobjid2>0) set @psql = @psql+',mps_user '
 set @psql=@psql+' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and  '+@prevf+'p_type=''wrkview'' and '+
 ' '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@condition+' ' 
 if (@pobjid1>0 or @pobjid2>0) set @psql = @psql+' and '+@prevf+'p_cuid=cu_id ' 
 if (@pobjid1>0 and @pobjid2=0) set @psql = @psql+' and cu_otid=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=1) set @psql = @psql+' and cu_otlevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=2) set @psql = @psql+' and cu_otlevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=3) set @psql = @psql+' and cu_otlevel<=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=4) set @psql = @psql+' and cu_managelevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=5) set @psql = @psql+' and cu_managelevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=6) set @psql = @psql+' and cu_managelevel<=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=7) set @psql = @psql+' and cu_professionlevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=8) set @psql = @psql+' and cu_professionlevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=9) set @psql = @psql+' and cu_professionlevel<=@pobjid1 ' 
 end else 
 if (@p1=3)--业务所有监督人103    otid          0    otid=0所有，>0指定岗位的
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, '+@prevf+'p_cuid,'+@prevf+'_id from '+@dbname+@tbname+','+@dbname+@tbname+'_users '+@from 
 if (@pobjid1>0 or @pobjid2>0) set @psql = @psql+',mps_user '
 set @psql=@psql+' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and  '+@prevf+'p_type=''wrkaudit'' and '+
 ' '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@condition+' ' 
 if (@pobjid1>0 or @pobjid2>0) set @psql = @psql+' and '+@prevf+'p_cuid=cu_id ' 
 if (@pobjid1>0 and @pobjid2=0) set @psql = @psql+' and cu_otid=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=1) set @psql = @psql+' and cu_otlevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=2) set @psql = @psql+' and cu_otlevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=3) set @psql = @psql+' and cu_otlevel<=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=4) set @psql = @psql+' and cu_managelevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=5) set @psql = @psql+' and cu_managelevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=6) set @psql = @psql+' and cu_managelevel<=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=7) set @psql = @psql+' and cu_professionlevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=8) set @psql = @psql+' and cu_professionlevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=9) set @psql = @psql+' and cu_professionlevel<=@pobjid1 ' 
 end else 
 if (@p1=4)--业务所有协作人104    otid          0    otid=0所有，>0指定岗位的
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, '+@prevf+'p_cuid,'+@prevf+'_id from '+@dbname+@tbname+','+@dbname+@tbname+'_users '+@from 
 if (@pobjid1>0 or @pobjid2>0) set @psql = @psql+',mps_user '
 set @psql=@psql+' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and  '+@prevf+'p_type=''wrkcoops'' and '+
 ' '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@condition+' ' 
 if (@pobjid1>0 or @pobjid2>0) set @psql = @psql+' and '+@prevf+'p_cuid=cu_id ' 
 if (@pobjid1>0 and @pobjid2=0) set @psql = @psql+' and cu_otid=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=1) set @psql = @psql+' and cu_otlevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=2) set @psql = @psql+' and cu_otlevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=3) set @psql = @psql+' and cu_otlevel<=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=4) set @psql = @psql+' and cu_managelevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=5) set @psql = @psql+' and cu_managelevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=6) set @psql = @psql+' and cu_managelevel<=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=7) set @psql = @psql+' and cu_professionlevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=8) set @psql = @psql+' and cu_professionlevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=9) set @psql = @psql+' and cu_professionlevel<=@pobjid1 ' 
 end else 
 if (@p1=5)--业务所有日志参与人105    otid          0    otid=0所有，>0指定岗位的
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, '+@prevf+'p_cuid,'+@prevf+'_id from '+@dbname+@tbname+','+@dbname+@tbname+'_users '+@from 
 if (@pobjid1>0 or @pobjid2>0) set @psql = @psql+',mps_user '
 set @psql=@psql+' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and  '+@prevf+'p_type=''wrklog'' and '+
 ' '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@condition+' ' 
 if (@pobjid1>0 or @pobjid2>0) set @psql = @psql+' and '+@prevf+'p_cuid=cu_id ' 
 if (@pobjid1>0 and @pobjid2=0) set @psql = @psql+' and cu_otid=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=1) set @psql = @psql+' and cu_otlevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=2) set @psql = @psql+' and cu_otlevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=3) set @psql = @psql+' and cu_otlevel<=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=4) set @psql = @psql+' and cu_managelevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=5) set @psql = @psql+' and cu_managelevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=6) set @psql = @psql+' and cu_managelevel<=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=7) set @psql = @psql+' and cu_professionlevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=8) set @psql = @psql+' and cu_professionlevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=9) set @psql = @psql+' and cu_professionlevel<=@pobjid1 ' 
 end else 
 if (@p1=6)--业务步骤执行人直接领导106    otid          0 当前业务所有步骤的执行人。otid=0所有，>0指定岗位的
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname+','+@dbname+@tbname+'_users,mps_employee_subusers '+@from 
 if (@pobjid1>0 or @pobjid2>0) set @psql = @psql+',mps_user '
 set @psql=@psql+' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and  '+@prevf+'p_type=''wrkexecutor'' and '+
 ' '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and cs_subcuid='+@prevf+'p_cuid and '+@condition+' ' 
 if (@pobjid1>0 or @pobjid2>0) set @psql = @psql+' and cs_cuid=cu_id ' 
 if (@pobjid1>0 and @pobjid2=0) set @psql = @psql+' and cu_otid=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=1) set @psql = @psql+' and cu_otlevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=2) set @psql = @psql+' and cu_otlevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=3) set @psql = @psql+' and cu_otlevel<=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=4) set @psql = @psql+' and cu_managelevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=5) set @psql = @psql+' and cu_managelevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=6) set @psql = @psql+' and cu_managelevel<=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=7) set @psql = @psql+' and cu_professionlevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=8) set @psql = @psql+' and cu_professionlevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=9) set @psql = @psql+' and cu_professionlevel<=@pobjid1 ' 
 end else
 if (@p1=7)--业务步骤执行人所有领导107    otid          0 当前业务所有步骤的所有领导 otid=0所有，>0指定岗位的
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname+','+@dbname+@tbname+'_users,mps_employee_subusersreal '+@from 
 if (@pobjid1>0 or @pobjid2>0) set @psql = @psql+',mps_user '
 set @psql=@psql+' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and  '+@prevf+'p_type=''wrkexecutor'' and '+
 ' '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and cus_subcuid='+@prevf+'p_cuid and '+@condition+' ' 
 if (@pobjid1>0 or @pobjid2>0) set @psql = @psql+' and cus_cuid=cu_id ' 
 if (@pobjid1>0 and @pobjid2=0) set @psql = @psql+' and cu_otid=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=1) set @psql = @psql+' and cu_otlevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=2) set @psql = @psql+' and cu_otlevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=3) set @psql = @psql+' and cu_otlevel<=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=4) set @psql = @psql+' and cu_managelevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=5) set @psql = @psql+' and cu_managelevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=6) set @psql = @psql+' and cu_managelevel<=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=7) set @psql = @psql+' and cu_professionlevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=8) set @psql = @psql+' and cu_professionlevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=9) set @psql = @psql+' and cu_professionlevel<=@pobjid1 ' 
 end  
 end else
 if (@p10=2)
 begin
 if (@p1=1)  -- 父级业务建单人121   00   // 以前还需要指定OTID，现取消，注意SQL
 begin
 set @p1=@p1
 end
 end
 end else
 if (@p100=2) --200 
 begin
 if (@p10=0) 
 begin
 if (@p1=0) --200 本公司所有人20000     // 本公司所有人，即当前业务对应公司
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=cu_bcid ' 
 if (@pobjid1>0 and @pobjid2=1) set @psql=@psql+' and cu_otlevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=2) set @psql=@psql+' and cu_otlevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=3) set @psql=@psql+' and cu_otlevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=4) set @psql=@psql+' and cu_managelevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=5) set @psql=@psql+' and cu_managelevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=6) set @psql=@psql+' and cu_managelevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=7) set @psql=@psql+' and cu_professionlevel=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=8) set @psql=@psql+' and cu_professionlevel>=@pobjid1 ' 
 if (@pobjid1>0 and @pobjid2=9) set @psql=@psql+' and cu_professionlevel=@pobjid1 ' 
 end else
 if (@p1=1) --本公司某部门201dpstd 0
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=cu_bcid and cu_dpstdid=@pobjid1 ' 
 end else
 if (@p1=2) --本公司某部门202 dpstd level//岗位等级 = 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=cu_bcid and cu_dpstdid=@pobjid1 and cu_otlevel=@pobjid2 ' 
 end else
 if (@p1=3) --本公司某部门203 dpstd level//岗位等级 >= 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=cu_bcid and cu_dpstdid=@pobjid1 and cu_otlevel>=@pobjid2 ' 
 end else
 if (@p1=4) --本公司某部门204 dpstd level//岗位等级 <= 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=cu_bcid and cu_dpstdid=@pobjid1 and cu_otlevel<=@pobjid2 ' 
 end else
 if (@p1=5) --本公司某部门205 dpstd level//管理等级 = 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=cu_bcid and cu_dpstdid=@pobjid1 and cu_managelevel=@pobjid2 ' 
 end else
 if (@p1=6) --本公司某部门206 dpstd level//管理等级 >= 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=cu_bcid and cu_dpstdid=@pobjid1 and cu_managelevel>=@pobjid2 ' 
 end else
 if (@p1=7) --本公司某部门207 dpstd level//管理等级 <= 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=cu_bcid and cu_dpstdid=@pobjid1 and cu_managelevel<=@pobjid2 ' 
 end else
 if (@p1=8) --本公司某部门208 dpstd level//专业等级 = 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=cu_bcid and cu_dpstdid=@pobjid1 and cu_professionlevel=@pobjid2 ' 
 end else
 if (@p1=9) --本公司某部门209 dpstd level//专业等级 >= 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=cu_bcid and cu_dpstdid=@pobjid1 and cu_professionlevel>=@pobjid2 ' 
 end 
 end else 
 if (@p10=1)
 begin
 if (@p1=0) --本公司某部门210 dpstd level//专业等级 <= 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=cu_bcid and cu_dpstdid=@pobjid1 and cu_professionlevel<=@pobjid2 ' 
 end 
 end else 
 if (@p10=2)
 begin
 if (@p1=0) --本公司某岗位220otid 0//
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=cu_bcid and cu_otid=@pobjid1  ' 
 end
 end else 
 if (@p10=3)
 begin
 if (@p1=0) --本公司某部门+岗位230dpstidotid //
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=cu_bcid and cu_dpstdid=@pobjid1 and cu_otid=@pobjid2  ' 
 end
 end else 
 if (@p10=4)
 begin
 if (@p1=0) --本公司按角色240角色ID0 //
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=cu_bcid and cu_function=@pobjid1  ' 
 end
 end else 
 if (@p10=5)
 begin
 if (@p1=0) --本公司上级公司所有人2500 0 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user,mps_branch '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=bc_id and bc_fbcid=cu_bcid  ' 
 end else 
 if (@p1=1) --本公司上上级公司所有人2510 0 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user,mps_branch '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=bc_id and bc_ffbcid=cu_bcid  ' 
 end else 
 if (@p1=2) --本公司上上上级公司所有人2520 0 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user,mps_branch '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=bc_id and bc_fffbcid=cu_bcid  ' 
 end
 end else 
 if (@p10=6)
 begin
 if (@p1=0) --本公司上级公司某部门260dpstd 0
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user,mps_branch '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=bc_id and bc_fbcid=cu_bcid and cu_dpstdid=@pobjid1 ' 
 end else 
 if (@p1=1) --本公司上上级公司某部门261dpstd 0
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user,mps_branch '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=bc_id and bc_ffbcid=cu_bcid  and cu_dpstdid=@pobjid1 ' 
 end else 
 if (@p1=2) --本公司上上上级公司某部门262dpstd 0
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user,mps_branch '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=bc_id and bc_fffbcid=cu_bcid  and cu_dpstdid=@pobjid1 ' 
 end
 end  else 
 if (@p10=7)
 begin
 if (@p1=0) --本公司上级公司某岗位270otid 0
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user,mps_branch '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=bc_id and bc_fbcid=cu_bcid and cu_otid=@pobjid1 ' 
 end else 
 if (@p1=1) --本公司上上级公司某岗位271otid 0
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user,mps_branch '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=bc_id and bc_ffbcid=cu_bcid  and cu_otid=@pobjid1 ' 
 end else 
 if (@p1=2) --本公司上上上级公司某岗位272otid 0
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user,mps_branch '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=bc_id and bc_fffbcid=cu_bcid  and cu_otid=@pobjid1 ' 
 end
 end else 
 if (@p10=8)
 begin
 if (@p1=0) --本公司上级公司某部门+岗位280dpstidotid 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user,mps_branch '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=bc_id and bc_fbcid=cu_bcid and cu_dpstdid=@pobjid1 and cu_otid=@pobjid2 ' 
 end else 
 if (@p1=1) --本公司上上级公司某部门+岗位281dpstidotid 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user,mps_branch '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=bc_id and bc_ffbcid=cu_bcid  and cu_dpstdid=@pobjid1 and cu_otid=@pobjid2 ' 
 end else 
 if (@p1=2) --本公司上上上级公司某部门+岗位282dpstidotid 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user,mps_branch '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=bc_id and bc_fffbcid=cu_bcid  and cu_dpstdid=@pobjid1 and cu_otid=@pobjid2 ' 
 end
 end else 
 if (@p10=9)
 begin
 if (@p1=0) --本公司上级公司某角色290角色ID0
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user,mps_branch '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=bc_id and bc_fbcid=cu_bcid and cu_function=@pobjid1 ' 
 end else 
 if (@p1=1) --本公司上上级公司某角色291角色ID0 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user,mps_branch '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=bc_id and bc_ffbcid=cu_bcid  and cu_function=@pobjid1 ' 
 end else 
 if (@p1=2) --本公司上上上级公司某角色292角色ID0 
 begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname+',mps_user,mps_branch '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition+' and '+@prevf+'_cmpid=cu_cmpid and '+@prevf+'_bcid=bc_id and bc_fffbcid=cu_bcid  and cu_function=@pobjid1 ' 
 end
 end
end else
if (@p100=4) --400 
begin
if (@p10=0) 
begin
if (@p1=1)   -- 建单人401 00   
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, '+@prevf+'_createcuid,'+@prevf+'_id from '+@dbname+@tbname +@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition 
end else 
if (@p1=2)   -- 建单人直接主管4020/otid00=所有直接领导   otid指定的otid的直接领导
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusers '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cs_subcuid='+@prevf+'_createcuid and '+@condition 
end else 
if (@p1=3)   -- 建单人所有领导4030/otid00=所有直接领导   otid指定的otid的直接领导(包括上级的上级)
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusersreal '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cus_subcuid='+@prevf+'_createcuid and '+@condition 
end else 
if (@p1=4)   -- 建单人上级领导(职级)4040/level00=上一级职级的人 otid是是上一职级，并且是指定的otid 
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusers , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cs_subcuid='+@prevf+'_createcuid and cs_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'_createcuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusers , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cs_subcuid='+@prevf+'_createcuid and cs_cuid=cu_id and cu_otlevel>=@pobjid1 and '+@condition 
end else 
if (@p1=5)   -- 建单人所有领导(职级)4050/level00=所有上级职级的人 otid是是上级职级，并且是指定的otid  (包括上级的上级。。。)
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusersreal , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cus_subcuid='+@prevf+'_createcuid and cus_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'_createcuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusersreal , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cus_subcuid='+@prevf+'_createcuid and cus_cuid=cu_id and cu_otlevel>=@pobjid1 and '+@condition 
end else 
if (@p1=6)   -- 建单人上级领导(管理级)4060/level00=上一级职级的人 otid是是上一职级，并且是指定的otid 
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusers , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cs_subcuid='+@prevf+'_createcuid and cs_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'_createcuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusers , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cs_subcuid='+@prevf+'_createcuid and cs_cuid=cu_id and cu_managelevel>=@pobjid1 and '+@condition 
end else 
if (@p1=7)   -- 建单人所有领导(管理级)4070/level00=上一级职级的人 otid是是上一职级，并且是指定的otid 
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusersreal , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cus_subcuid='+@prevf+'_createcuid and cus_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'_createcuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusersreal , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cus_subcuid='+@prevf+'_createcuid and cus_cuid=cu_id and cu_managelevel>=@pobjid1 and '+@condition 
end else
if (@p1=8)   -- 建单人上级领导(业务级)4080/level00=上一级职级的人 otid是是上一职级，并且是指定的otid 
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusers , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cs_subcuid='+@prevf+'_createcuid and cs_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'_createcuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusers , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cs_subcuid='+@prevf+'_createcuid and cs_cuid=cu_id and cu_professionlevel>=@pobjid1 and '+@condition 
end else 
if (@p1=9)   -- 建单人所有领导(业务级)4090/level00=上一级职级的人 otid是是上一职级，并且是指定的otid 
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusersreal , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cus_subcuid='+@prevf+'_createcuid and cus_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'_createcuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusersreal , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cus_subcuid='+@prevf+'_createcuid and cus_cuid=cu_id and cu_professionlevel>=@pobjid1 and '+@condition 
end
end else
if (@p10=2)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --建单人部门420     0/otid0        0表示所有人，otid指定岗位的人
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_otid=@pobjid1 and '+@condition  
end else 
if (@p1=1)  --6、建单人部门(某管理级）421     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_managelevel=@pobjid1 and '+@condition  
end else 
if (@p1=2)  --6、建单人部门(某管理级及更高）422     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_managelevel>=@pobjid1 and '+@condition  
end else 
if (@p1=3)  --6、建单人部门(某管理级及更低）423     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_managelevel<=@pobjid1 and '+@condition  
end else 
if (@p1=4)  --6、建单人部门(某业务级）424     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_professionlevel=@pobjid1 and '+@condition  
end else 
if (@p1=5)  --6、建单人部门(某业务级及更高）425     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_professionlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=6)  --6、建单人部门(某业务级及更低）426     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7)  --6、建单人部门(某岗位级）427     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_otlevel=@pobjid1 and '+@condition  
end else 
if (@p1=8)  --6、建单人部门(某岗位级及更高）428     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_otlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=9)  --6、建单人部门(某岗位级及更低）429     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_otlevel<=@pobjid1 and '+@condition  
end
end else
if (@p10=4)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --建单人上级部门中的某岗位     440     0/otid    0        
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart, mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_otid=@pobjid1 and '+@condition  
end else 
if (@p1=1)  --6、建单人上级部门(某管理级）441     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_managelevel=@pobjid1 and '+@condition  
end else 
if (@p1=2)  --6、建单人上级部门(某管理级及更高）442     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_managelevel>=@pobjid1 and '+@condition  
end else 
if (@p1=3)  --6、建单人上级部门(某管理级及更低）443     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_managelevel<=@pobjid1 and '+@condition  
end else 
if (@p1=4)  --6、建单人上级部门(某业务级）444     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_professionlevel=@pobjid1 and '+@condition  
end else 
if (@p1=5)  --6、建单人上级部门(某业务级及更高）445     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_professionlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=6)  --6、建单人上级部门(某业务级及更低）446     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7)  --6、建单人上级部门(某岗位级）447     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_otlevel=@pobjid1 and '+@condition  
end else 
if (@p1=8)  --6、建单人上级部门(某岗位级及更高）448     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_otlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=9)  --6、建单人上级部门(某岗位级及更低）449     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_otlevel<=@pobjid1 and '+@condition  
end  
end  else
if (@p10=6)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --9、建单人上上级部门中的某岗位   460     0/otid    0        
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart, mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_otid=@pobjid1 and '+@condition  
end else 
if (@p1=1)  --6、建单人上上级部门(某管理级）461     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_managelevel=@pobjid1 and '+@condition  
end else 
if (@p1=2)  --6、建单人上上级部门(某管理级及更高）462     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_managelevel>=@pobjid1 and '+@condition  
end else 
if (@p1=3)  --6、建单人上上级部门(某管理级及更低）463     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_managelevel<=@pobjid1 and '+@condition  
end else 
if (@p1=4)  --6、建单人上上级部门(某业务级）464     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_professionlevel=@pobjid1 and '+@condition  
end else 
if (@p1=5)  --6、建单人上上级部门(某业务级及更高）465     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_professionlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=6)  --6、建单人上上级部门(某业务级及更低）466     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7)  --6、建单人上上级部门(某岗位级）467     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_otlevel=@pobjid1 and '+@condition  
end else 
if (@p1=8)  --6、建单人上上级部门(某岗位级及更高）468     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_otlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=9)  --6、建单人上上级部门(某岗位级及更低）469     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_otlevel<=@pobjid1 and '+@condition  
end  
end else 
if (@p10=7)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --9、建单人上上上级部门中的某岗位   470     0/otid    0        
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart, mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_otid=@pobjid1 and '+@condition  
end else 
if (@p1=1)  --6、建单人上上上级部门(某管理级）471     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_managelevel=@pobjid1 and '+@condition  
end else 
if (@p1=2)  --6、建单人上上上级部门(某管理级及更高）472     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_managelevel>=@pobjid1 and '+@condition  
end else 
if (@p1=3)  --6、建单人上上上级部门(某管理级及更低）473     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_managelevel<=@pobjid1 and '+@condition  
end else 
if (@p1=4)  --6、建单人上上上级部门(某业务级）474     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_professionlevel=@pobjid1 and '+@condition  
end else 
if (@p1=5)  --6、建单人上上上级部门(某业务级及更高）475     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_professionlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=6)  --6、建单人上上上级部门(某业务级及更低）476     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7)  --6、建单人上上上级部门(某岗位级）477     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_otlevel=@pobjid1 and '+@condition  
end else 
if (@p1=8)  --6、建单人上上上级部门(某岗位级及更高）478     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_otlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=9)  --6、建单人上上上级部门(某岗位级及更低）479     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createcuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_otlevel<=@pobjid1 and '+@condition  
end  
end 
end else
if (@p100=5)
begin
if (@p10=2)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --6、建单部门   520     0/otid0        0表示所有人，otid指定岗位的人
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=cu_dpid and cu_otid=@pobjid1 and '+@condition  
end else  
if (@p1=1) --6、建单部门(某管理级）521     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=cu_dpid and cu_managelevel=@pobjid1 and '+@condition  
end else  
if (@p1=2) --6、建单部门(某管理级及更高）522     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=cu_dpid and cu_managelevel>=@pobjid1 and '+@condition  
end else  
if (@p1=3) --6、建单部门(某管理级及更低）523     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=cu_dpid and cu_managelevel<=@pobjid1 and '+@condition  
end else  
if (@p1=4) --6、建单部门(某业务级）524     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=cu_dpid and cu_professionlevel=@pobjid1 and '+@condition  
end else  
if (@p1=5) --6、建单部门(某业务级及更高）525     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=cu_dpid and cu_professionlevel>=@pobjid1 and '+@condition  
end else  
if (@p1=6) --6、建单部门(某业务级及更低）526     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=cu_dpid and cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7) --6、建单部门(某岗位级）527     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=cu_dpid and cu_otlevel=@pobjid1 and '+@condition  
end else  
if (@p1=8) --6、建单部门(某岗位级及更高）528     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=cu_dpid and cu_otlevel>=@pobjid1 and '+@condition  
end else  
if (@p1=9) --6、建单部门(某岗位级及更低）529     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=cu_dpid and cu_otlevel<=@pobjid1 and '+@condition  
end   
end else
if (@p10=4)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --9、建单上级部门中的某岗位           540     0/otid    0        
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user a  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fdpid=cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fdpid=cu_dpid and cu_otid=@pobjid1 and '+@condition  
end else  
if (@p1=1) --6、建单上级部门(某管理级）541     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fdpid=cu_dpid and cu_managelevel=@pobjid1 and '+@condition  
end else  
if (@p1=2) --6、建单上级部门(某管理级及更高）542     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fdpid=cu_dpid and cu_managelevel>=@pobjid1 and '+@condition  
end else  
if (@p1=3) --6、建单上级部门(某管理级及更低）543     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fdpid=cu_dpid and cu_managelevel<=@pobjid1 and '+@condition  
end else  
if (@p1=4) --6、建单上级部门(某业务级）544     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fdpid=cu_dpid and cu_professionlevel=@pobjid1 and '+@condition  
end else  
if (@p1=5) --6、建单上级部门(某业务级及更高）545     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fdpid=cu_dpid and cu_professionlevel>=@pobjid1 and '+@condition  
end else  
if (@p1=6) --6、建单上级部门(某业务级及更低）546     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fdpid=cu_dpid and cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7) --6、建单上级部门(某岗位级）547     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fdpid=cu_dpid and cu_otlevel=@pobjid1 and '+@condition  
end else  
if (@p1=8) --6、建单上级部门(某岗位级及更高）548     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fdpid=cu_dpid and cu_otlevel>=@pobjid1 and '+@condition  
end else  
if (@p1=9) --6、建单上级部门(某岗位级及更低）549     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fdpid=cu_dpid and cu_otlevel<=@pobjid1 and '+@condition  
end   
end else
if (@p10=6)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --9、建单上上级部门中的某岗位         560     0/otid    0       
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user a  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_ffdpid=cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_ffdpid=cu_dpid and cu_otid=@pobjid1 and '+@condition  
end else  
if (@p1=1) --6、建单上上级部门(某管理级）561     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_ffdpid=cu_dpid and cu_managelevel=@pobjid1 and '+@condition  
end else  
if (@p1=2) --6、建单上上级部门(某管理级及更高）562     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_ffdpid=cu_dpid and cu_managelevel>=@pobjid1 and '+@condition  
end else  
if (@p1=3) --6、建单上级部门(某管理级及更低）563     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_ffdpid=cu_dpid and cu_managelevel<=@pobjid1 and '+@condition  
end else  
if (@p1=4) --6、建单上上级部门(某业务级）564     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_ffdpid=cu_dpid and cu_professionlevel=@pobjid1 and '+@condition  
end else  
if (@p1=5) --6、建单上上级部门(某业务级及更高）565     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_ffdpid=cu_dpid and cu_professionlevel>=@pobjid1 and '+@condition  
end else  
if (@p1=6) --6、建单上上级部门(某业务级及更低）566     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_ffdpid=cu_dpid and cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7) --6、建单上上级部门(某岗位级）567     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_ffdpid=cu_dpid and cu_otlevel=@pobjid1 and '+@condition  
end else  
if (@p1=8) --6、建单上上级部门(某岗位级及更高）568     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_ffdpid=cu_dpid and cu_otlevel>=@pobjid1 and '+@condition  
end else  
if (@p1=9) --6、建单上上级部门(某岗位级及更低）569     0/level0        0表示所有人，otid指定岗位的人 
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_ffdpid=cu_dpid and cu_otlevel<=@pobjid1 and '+@condition  
end   
end else
if (@p10=7)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --9、建单上上上级部门中的某岗位         570     0/otid    0       
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user a  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fffdpid=cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fffdpid=cu_dpid and cu_otid=@pobjid1 and '+@condition  
end else  
if (@p1=1) --6、建单上上上级部门(某管理级）571     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fffdpid=cu_dpid and cu_managelevel=@pobjid1 and '+@condition  
end else  
if (@p1=2) --6、建单上上上级部门(某管理级及更高）572     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fffdpid=cu_dpid and cu_managelevel>=@pobjid1 and '+@condition  
end else  
if (@p1=3) --6、建单上上上级部门(某管理级及更低）573     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fffdpid=cu_dpid and cu_managelevel<=@pobjid1 and '+@condition  
end else  
if (@p1=4) --6、建单上上上级部门(某业务级）574     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fffdpid=cu_dpid and cu_professionlevel=@pobjid1 and '+@condition  
end else  
if (@p1=5) --6、建单上上上级部门(某业务级及更高）575     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fffdpid=cu_dpid and cu_professionlevel>=@pobjid1 and '+@condition  
end else  
if (@p1=6) --6、建单上上上级部门(某业务级及更低）576     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fffdpid=cu_dpid and cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7) --6、建单上上上级部门(某岗位级）577     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fffdpid=cu_dpid and cu_otlevel=@pobjid1 and '+@condition  
end else  
if (@p1=8) --6、建单上上上级部门(某岗位级及更高）578     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fffdpid=cu_dpid and cu_otlevel>=@pobjid1 and '+@condition  
end else  
if (@p1=9) --6、建单上上上级部门(某岗位级及更低）579     0/level0        0表示所有人，otid指定岗位的人 
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_createdpid=dp_id and dp_fffdpid=cu_dpid and cu_otlevel<=@pobjid1 and '+@condition  
end   
end 
end else 
if (@p100=6) --600 
begin
if (@p10=0) 
begin
if (@p1=1)   -- 1、责任人601 00   
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, '+@prevf+'_cuid,'+@prevf+'_id from '+@dbname+@tbname +@from+  
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition 
end else 
if (@p1=2)   -- 2、责任人直接主管6020/otid00=所有直接领导   otid指定的otid的直接领导
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusers '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cs_subcuid='+@prevf+'_cuid and '+@condition 
end else 
if (@p1=3)   -- 3、责任人所有领导6030/otid00=所有直接领导   otid指定的otid的直接领导(包括上级的上级)
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusersreal '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cus_subcuid='+@prevf+'_cuid and '+@condition 
end else 
if (@p1=4)   -- 4、责任人上级领导(职级)6040/otid00=上一级职级的人 otid是是上一职级，并且是指定的otid 
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusers , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cs_subcuid='+@prevf+'_cuid and cs_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'_cuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusers , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cs_subcuid='+@prevf+'_cuid and cs_cuid=cu_id and cu_otlevel>=@pobjid1 and '+@condition 
end else 
if (@p1=5)   -- 5、责任人所有领导(职级)6050/otid00=所有上级职级的人 otid是是上级职级，并且是指定的otid  (包括上级的上级。。。)
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusersreal , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cus_subcuid='+@prevf+'_cuid and cus_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'_cuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusersreal , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cus_subcuid='+@prevf+'_cuid and cus_cuid=cu_id and cu_otlevel>=@pobjid1 and '+@condition 
end else 
if (@p1=6)   -- 4、责任人上级领导(管理级)6060/otid00=上一级职级的人 otid是是上一职级，并且是指定的otid 
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusers , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cs_subcuid='+@prevf+'_cuid and cs_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'_cuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusers , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cs_subcuid='+@prevf+'_cuid and cs_cuid=cu_id and cu_managelevel>=@pobjid1 and '+@condition 
end else 
if (@p1=7)   -- 4、责任人下级人员(管理级)6070/otid00=上一级职级的人 otid是是上一职级，并且是指定的otid 
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusersreal , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cus_subcuid='+@prevf+'_cuid and cus_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'_cuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusersreal , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cus_subcuid='+@prevf+'_cuid and cus_cuid=cu_id and cu_managelevel>=@pobjid1 and '+@condition 
end else
if (@p1=8)   -- 4、责任人上级领导(业务级)6080/otid00=上一级职级的人 otid是是上一职级，并且是指定的otid 
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusers , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cs_subcuid='+@prevf+'_cuid and cs_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'_cuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusers , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cs_subcuid='+@prevf+'_cuid and cs_cuid=cu_id and cu_professionlevel>=@pobjid1 and '+@condition 
end else 
if (@p1=9)   -- 4、责任人下级人员(业务级)6090/otid00=上一级职级的人 otid是是上一职级，并且是指定的otid 
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusersreal , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cus_subcuid='+@prevf+'_cuid and cus_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'_cuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ',mps_employee_subusersreal , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and cus_subcuid='+@prevf+'_cuid and cus_cuid=cu_id and cu_professionlevel>=@pobjid1 and '+@condition 
end
end else 
if (@p10=2)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --6、责任人部门620     0/otid0        0表示所有人，otid指定岗位的人
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_otid=@pobjid1 and '+@condition  
end else 
if (@p1=1)  --6、责任人部门(某管理级）621     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_managelevel=@pobjid1 and '+@condition  
end else 
if (@p1=2)  --6、责任人部门(某管理级及更高）622     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_managelevel>=@pobjid1 and '+@condition  
end else 
if (@p1=3)  --6、责任人部门(某管理级及更低）623     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_managelevel<=@pobjid1 and '+@condition  
end else 
if (@p1=4)  --6、责任人部门(某业务级）624     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_professionlevel=@pobjid1 and '+@condition  
end else 
if (@p1=5)  --6、责任人部门(某业务级及更高）625     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_professionlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=6)  --6、责任人部门(某业务级及更低）626     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7)  --6、责任人部门(某岗位级）627     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_otlevel=@pobjid1 and '+@condition  
end else 
if (@p1=8)  --6、责任人部门(某岗位级及更高）628     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_otlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=9)  --6、责任人部门(某岗位级及更低）629    0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_otlevel<=@pobjid1 and '+@condition  
end
end else
if (@p10=4)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --9、责任人上级部门中的某岗位           640     0/otid    0        
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart, mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_otid=@pobjid1 and '+@condition  
end else 
if (@p1=1)  --6、责任人上级部门(某管理级）641     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_managelevel=@pobjid1 and '+@condition  
end else 
if (@p1=2)  --6、责任人上级部门(某管理级及更高）642     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_managelevel>=@pobjid1 and '+@condition  
end else 
if (@p1=3)  --6、责任人上级部门(某管理级及更低）643     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_managelevel<=@pobjid1 and '+@condition  
end else 
if (@p1=4)  --6、责任人上级部门(某业务级）644     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_professionlevel=@pobjid1 and '+@condition  
end else 
if (@p1=5)  --6、责任人上级部门(某业务级及更高）645     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_professionlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=6)  --6、责任人上级部门(某业务级及更低）646     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7)  --6、责任人上级部门(某岗位级）647     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_otlevel=@pobjid1 and '+@condition  
end else 
if (@p1=8)  --6、责任人上级部门(某岗位级及更高）648     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_otlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=9)  --6、责任人上级部门(某岗位级及更低）649     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_otlevel<=@pobjid1 and '+@condition  
end  
end  else
if (@p10=6)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --9、责任人上上级部门中的某岗位        660     0/otid    0      
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart, mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_otid=@pobjid1 and '+@condition  
end else 
if (@p1=1)  --6、责任人上上级部门(某管理级）661     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_managelevel=@pobjid1 and '+@condition  
end else 
if (@p1=2)  --6、责任人上上级部门(某管理级及更高）662     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_managelevel>=@pobjid1 and '+@condition  
end else 
if (@p1=3)  --6、责任人上上级部门(某管理级及更低）663     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_managelevel<=@pobjid1 and '+@condition  
end else 
if (@p1=4)  --6、责任人上上级部门(某业务级）664     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_professionlevel=@pobjid1 and '+@condition  
end else 
if (@p1=5)  --6、责任人上上级部门(某业务级及更高）665     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_professionlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=6)  --6、责任人上上级部门(某业务级及更低）666     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7)  --6、责任人上上级部门(某岗位级）667     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_otlevel=@pobjid1 and '+@condition  
end else 
if (@p1=8)  --6、责任人上上级部门(某岗位级及更高）668     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_otlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=9)  --6、责任人上上级部门(某岗位级及更低）669     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_otlevel<=@pobjid1 and '+@condition  
end  
end else 
if (@p10=7)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --9、责任人上上上级部门中的某岗位        670     0/otid    0      
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart, mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_otid=@pobjid1 and '+@condition  
end else 
if (@p1=1)  --6、责任人上上上级部门(某管理级）671     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_managelevel=@pobjid1 and '+@condition  
end else 
if (@p1=2)  --6、责任人上上上级部门(某管理级及更高）672     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_managelevel>=@pobjid1 and '+@condition  
end else 
if (@p1=3)  --6、责任人上上上级部门(某管理级及更低）673     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_managelevel<=@pobjid1 and '+@condition  
end else 
if (@p1=4)  --6、责任人上上上级部门(某业务级）674     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_professionlevel=@pobjid1 and '+@condition  
end else 
if (@p1=5)  --6、责任人上上上级部门(某业务级及更高）675     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_professionlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=6)  --6、责任人上上上级部门(某业务级及更低）676     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7)  --6、责任人上上上级部门(某岗位级）677     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_otlevel=@pobjid1 and '+@condition  
end else 
if (@p1=8)  --6、责任人上上上级部门(某岗位级及更高）678     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_otlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=9)  --6、责任人上上上级部门(某岗位级及更低）679     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_otlevel<=@pobjid1 and '+@condition  
end  
end
end else
if (@p100=7)
begin
if (@p10=2)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --6、责任部门(业务部门)   720     0/otid0        0表示所有人，otid指定岗位的人
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user a  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=cu_dpid and cu_otid=@pobjid1 and '+@condition  
end else  
if (@p1=1) --6、责任部门(某管理级）721     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=cu_dpid and cu_managelevel=@pobjid1 and '+@condition  
end else  
if (@p1=2) --6、责任部门(某管理级及更高）722     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=cu_dpid and cu_managelevel>=@pobjid1 and '+@condition  
end else  
if (@p1=3) --6、责任部门(某管理级及更低）723     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=cu_dpid and cu_managelevel<=@pobjid1 and '+@condition  
end else  
if (@p1=4) --6、责任部门(某业务级）724     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=cu_dpid and cu_professionlevel=@pobjid1 and '+@condition  
end else  
if (@p1=5) --6、责任部门(某业务级及更高）725     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=cu_dpid and cu_professionlevel>=@pobjid1 and '+@condition  
end else  
if (@p1=6) --6、责任部门(某业务级及更低）726     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=cu_dpid and cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7) --6、责任部门(某岗位级）727     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=cu_dpid and cu_otlevel=@pobjid1 and '+@condition  
end else  
if (@p1=8) --6、责任部门(某岗位级及更高）728     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=cu_dpid and cu_otlevel>=@pobjid1 and '+@condition  
end else  
if (@p1=9) --6、责任部门(某岗位级及更低）729     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=cu_dpid and cu_otlevel<=@pobjid1 and '+@condition  
end   
end else
if (@p10=4)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --9、责任上级部门中的某岗位          740     0/otid    0        
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user a  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fdpid=cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fdpid=cu_dpid and cu_otid=@pobjid1 and '+@condition  
end else  
if (@p1=1) --6、责任上级部门(某管理级）741     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fdpid=cu_dpid and cu_managelevel=@pobjid1 and '+@condition  
end else  
if (@p1=2) --6、责任上级部门(某管理级及更高）742     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fdpid=cu_dpid and cu_managelevel>=@pobjid1 and '+@condition  
end else  
if (@p1=3) --6、责任上级部门(某管理级及更低）743     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fdpid=cu_dpid and cu_managelevel<=@pobjid1 and '+@condition  
end else  
if (@p1=4) --6、责任上级部门(某业务级）744     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fdpid=cu_dpid and cu_professionlevel=@pobjid1 and '+@condition  
end else  
if (@p1=5) --6、责任上级部门(某业务级及更高）745     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fdpid=cu_dpid and cu_professionlevel>=@pobjid1 and '+@condition  
end else  
if (@p1=6) --6、责任上级部门(某业务级及更低）746     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fdpid=cu_dpid and cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7) --6、责任上级部门(某岗位级）747     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fdpid=cu_dpid and cu_otlevel=@pobjid1 and '+@condition  
end else  
if (@p1=8) --6、责任上级部门(某岗位级及更高）748     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fdpid=cu_dpid and cu_otlevel>=@pobjid1 and '+@condition  
end else  
if (@p1=9) --6、责任上级部门(某岗位级及更低）749     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fdpid=cu_dpid and cu_otlevel<=@pobjid1 and '+@condition  
end   
end else
if (@p10=6)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --9、责任上上级部门中的某岗位        760     0/otid    0        
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user a  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_ffdpid=cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_ffdpid=cu_dpid and cu_otid=@pobjid1 and '+@condition  
end else  
if (@p1=1) --6、责任上上级部门(某管理级）761     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_ffdpid=cu_dpid and cu_managelevel=@pobjid1 and '+@condition  
end else  
if (@p1=2) --6、责任上上级部门(某管理级及更高）762     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_ffdpid=cu_dpid and cu_managelevel>=@pobjid1 and '+@condition  
end else  
if (@p1=3) --6、责任上上级部门(某管理级及更低）763     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_ffdpid=cu_dpid and cu_managelevel<=@pobjid1 and '+@condition  
end else  
if (@p1=4) --6、责任上上级部门(某业务级）764     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_ffdpid=cu_dpid and cu_professionlevel=@pobjid1 and '+@condition  
end else  
if (@p1=5) --6、责任上上级部门(某业务级及更高）765     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_ffdpid=cu_dpid and cu_professionlevel>=@pobjid1 and '+@condition  
end else  
if (@p1=6) --6、责任上上级部门(某业务级及更低）766     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_ffdpid=cu_dpid and cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7) --6、责任上上级部门(某岗位级）767     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_ffdpid=cu_dpid and cu_otlevel=@pobjid1 and '+@condition  
end else  
if (@p1=8) --6、责任上上级部门(某岗位级及更高）768     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_ffdpid=cu_dpid and cu_otlevel>=@pobjid1 and '+@condition  
end else  
if (@p1=9) --6、责任上上级部门(某岗位级及更低）769     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_ffdpid=cu_dpid and cu_otlevel<=@pobjid1 and '+@condition  
end   
end else
if (@p10=7)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --9、责任上上上级部门中的某岗位        770     0/otid    0        
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user a  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fffdpid=cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fffdpid=cu_dpid and cu_otid=@pobjid1 and '+@condition  
end else  
if (@p1=1) --6、责任上上上级部门(某管理级）771     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fffdpid=cu_dpid and cu_managelevel=@pobjid1 and '+@condition  
end else  
if (@p1=2) --6、责任上上上级部门(某管理级及更高）772     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fffdpid=cu_dpid and cu_managelevel>=@pobjid1 and '+@condition  
end else  
if (@p1=3) --6、责任上上上级部门(某管理级及更低）773     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fffdpid=cu_dpid and cu_managelevel<=@pobjid1 and '+@condition  
end else  
if (@p1=4) --6、责任上上上级部门(某业务级）774     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fffdpid=cu_dpid and cu_professionlevel=@pobjid1 and '+@condition  
end else  
if (@p1=5) --6、责任上上上级部门(某业务级及更高）775     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fffdpid=cu_dpid and cu_professionlevel>=@pobjid1 and '+@condition  
end else  
if (@p1=6) --6、责任上上上级部门(某业务级及更低）776     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fffdpid=cu_dpid and cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7) --6、责任上上上级部门(某岗位级）777     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fffdpid=cu_dpid and cu_otlevel=@pobjid1 and '+@condition  
end else  
if (@p1=8) --6、责任上上上级部门(某岗位级及更高）778     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fffdpid=cu_dpid and cu_otlevel>=@pobjid1 and '+@condition  
end else  
if (@p1=9) --6、责任上上上级部门(某岗位级及更低）779     0/level0        0表示所有人，otid指定岗位的人
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cu_id,'+@prevf+'_id from '+@dbname+@tbname + ',mps_depart,mps_user  '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_dpid=dp_id and dp_fffdpid=cu_dpid and cu_otlevel<=@pobjid1 and '+@condition  
end   
end
end else
if (@p100=8) --800 
begin
if (@p10=0) 
begin
if (@p1=1)   -- 1、执行人801 00   
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, '+@prevf+'p_cuid,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition +' and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' '
end else 
if (@p1=2)   -- 2、执行人直接主管8020/otid00=所有直接领导   otid指定的otid的直接领导
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_employee_subusers '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and cs_subcuid='+@prevf+'p_cuid and '+@condition 
end else 
if (@p1=3)   -- 3、执行人所有领导8030/otid00=所有直接领导   otid指定的otid的直接领导(包括上级的上级)
begin
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname +','+@dbname+@tbname + '_users,mps_employee_subusersreal '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and cus_subcuid='+@prevf+'p_cuid and '+@condition 
end else 
if (@p1=4)   -- 4、执行人上级领导(职级)8040/otid00=上一级职级的人 otid是是上一职级，并且是指定的otid 
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_employee_subusers , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and cs_subcuid='+@prevf+'p_cuid and cs_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'p_cuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_employee_subusers , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and cs_subcuid='+@prevf+'p_cuid and cs_cuid=cu_id and cu_otlevel>=@pobjid1 and '+@condition 
end else 
if (@p1=5)   -- 5、执行人所有领导(职级)8050/otid00=所有上级职级的人 otid是是上级职级，并且是指定的otid  (包括上级的上级。。。)
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_employee_subusersreal , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and cus_subcuid='+@prevf+'p_cuid and cus_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'p_cuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname +','+@dbname+@tbname + '_users,mps_employee_subusersreal , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and cus_subcuid='+@prevf+'p_cuid and cus_cuid=cu_id and cu_otlevel>=@pobjid1 and '+@condition 
end else 
if (@p1=6)   -- 4、执行人上级领导(管理级)8060/otid00=上一级职级的人 otid是是上一职级，并且是指定的otid 
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_employee_subusers , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and cs_subcuid='+@prevf+'p_cuid and cs_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'p_cuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_employee_subusers , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and cs_subcuid='+@prevf+'p_cuid and cs_cuid=cu_id and cu_managelevel>=@pobjid1 and '+@condition 
end else 
if (@p1=7)   -- 4、执行人下级人员(管理级)8070/otid00=上一级职级的人 otid是是上一职级，并且是指定的otid 
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_employee_subusersreal , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and cus_subcuid='+@prevf+'p_cuid and cus_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'p_cuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_employee_subusersreal , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and cus_subcuid='+@prevf+'p_cuid and cus_cuid=cu_id and cu_managelevel>=@pobjid1 and '+@condition 
end else
if (@p1=8)   -- 4、执行人上级领导(业务级)8080/otid00=上一级职级的人 otid是是上一职级，并且是指定的otid 
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_employee_subusers , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and cs_subcuid='+@prevf+'p_cuid and cs_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'p_cuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cs_cuid,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_employee_subusers , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and cs_subcuid='+@prevf+'p_cuid and cs_cuid=cu_id and cu_professionlevel>=@pobjid1 and '+@condition 
end else 
if (@p1=9)   -- 4、执行人下级人员(业务级)8090/otid00=上一级职级的人 otid是是上一职级，并且是指定的otid 
begin
if (@pobjid1=0) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_employee_subusersreal , mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and cus_subcuid='+@prevf+'p_cuid and cus_cuid=a.cu_id and '+
 ' a.cu_otlevel>b.cu_level and b.cu_id='+@prevf+'p_cuid and '+@condition 
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, cus_cuid,'+@prevf+'_id from '+@dbname+@tbname +','+@dbname+@tbname + '_users,mps_employee_subusersreal , mps_user '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and cus_subcuid='+@prevf+'p_cuid and cus_cuid=cu_id and cu_professionlevel>=@pobjid1 and '+@condition 
end
end else
if (@p10=2)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --6、执行人部门820     0/otid0        0表示所有人，otid指定岗位的人
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_otid=@pobjid1 and '+@condition  
end else 
if (@p1=1)  --6、执行人部门(某管理级）821     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_managelevel=@pobjid1 and '+@condition  
end else 
if (@p1=2)  --6、执行人部门(某管理级及更高）822     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_managelevel>=@pobjid1 and '+@condition  
end else 
if (@p1=3)  --6、执行人部门(某管理级及更低）823     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_managelevel<=@pobjid1 and '+@condition  
end else 
if (@p1=4)  --6、执行人部门(某业务级）824     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_professionlevel=@pobjid1 and '+@condition  
end else 
if (@p1=5)  --6、执行人部门(某业务级及更高）825     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_professionlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=6)  --6、执行人部门(某业务级及更低）826     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7)  --6、执行人部门(某岗位级）827     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_otlevel=@pobjid1 and '+@condition  
end else 
if (@p1=8)  --6、执行人部门(某岗位级及更高）828     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_otlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=9)  --6、执行人部门(某岗位级及更低）829     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and b.cu_cmpid=@cmpid and a.cu_dpid=b.cu_dpid and b.cu_otlevel<=@pobjid1 and '+@condition  
end
end else
if (@p10=4)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --9、执行人上级部门中的某岗位     840     0/otid    0    
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart, mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_otid=@pobjid1 and '+@condition  
end else 
if (@p1=1)  --6、执行人上级部门(某管理级）841     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_managelevel=@pobjid1 and '+@condition  
end else 
if (@p1=2)  --6、执行人上级部门(某管理级及更高842     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_managelevel>=@pobjid1 and '+@condition  
end else 
if (@p1=3)  --6、执行人上级部门(某管理级及更低）843     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_managelevel<=@pobjid1 and '+@condition  
end else 
if (@p1=4)  --6、执行人上级部门(某业务级）844     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_professionlevel=@pobjid1 and '+@condition  
end else 
if (@p1=5)  --6、执行人上级部门(某业务级及更高）845     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_professionlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=6)  --6、执行人上级部门(某业务级及更低）846     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7)  --6、执行人上级部门(某岗位级）847     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_otlevel=@pobjid1 and '+@condition  
end else 
if (@p1=8)  --6、执行人上级部门(某岗位级及更高）848     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_otlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=9)  --6、执行人上级部门(某岗位级及更低）849     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fdpid=b.cu_dpid and b.cu_otlevel<=@pobjid1 and '+@condition  
end  
end  else
if (@p10=6)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --9、执行人上上级部门中的某岗位     860     0/otid    0    
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart, mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_otid=@pobjid1 and '+@condition  
end else 
if (@p1=1)  --6、执行人上上级部门(某管理级）861     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_managelevel=@pobjid1 and '+@condition  
end else 
if (@p1=2)  --6、执行人上上级部门(某管理级及更高）862    0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_managelevel>=@pobjid1 and '+@condition  
end else 
if (@p1=3)  --6、执行人上上级部门(某管理级及更低）863     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_managelevel<=@pobjid1 and '+@condition  
end else 
if (@p1=4)  --6、执行人上上级部门(某业务级）864     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_professionlevel=@pobjid1 and '+@condition  
end else 
if (@p1=5)  --6、执行人上上级部门(某业务级及更高）865     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname +','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_professionlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=6)  --6、执行人上上级部门(某业务级及更低）866     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7)  --6、执行人上上级部门(某岗位级）867     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_otlevel=@pobjid1 and '+@condition  
end else 
if (@p1=8)  --6、执行人上上级部门(某岗位级及更高）868     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_otlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=9)  --6、执行人上上级部门(某岗位级及更低）869     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_ffdpid=b.cu_dpid and b.cu_otlevel<=@pobjid1 and '+@condition  
end  
end  else
if (@p10=7)
begin
if (@p1=0)
begin
if (@pobjid1=0)  --9、执行人上上上级部门中的某岗位     870     0/otid    0    
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart, mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and '+@condition 
 else
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_otid=@pobjid1 and '+@condition  
end else 
if (@p1=1)  --6、执行人上上上级部门(某管理级）871     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_managelevel=@pobjid1 and '+@condition  
end else 
if (@p1=2)  --6、执行人上上上级部门(某管理级及更高）872    0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_managelevel>=@pobjid1 and '+@condition  
end else 
if (@p1=3)  --6、执行人上上上级部门(某管理级及更低）873     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_managelevel<=@pobjid1 and '+@condition  
end else 
if (@p1=4)  --6、执行人上上上级部门(某业务级）874     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_professionlevel=@pobjid1 and '+@condition  
end else 
if (@p1=5)  --6、执行人上上上级部门(某业务级及更高）875     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname +','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_professionlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=6)  --6、执行人上上上级部门(某业务级及更低）876     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_professionlevel<=@pobjid1 and '+@condition  
end else 
if (@p1=7)  --6、执行人上上上级部门(某岗位级）877     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_otlevel=@pobjid1 and '+@condition  
end else 
if (@p1=8)  --6、执行人上上上级部门(某岗位级及更高）878     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_otlevel>=@pobjid1 and '+@condition  
end else 
if (@p1=9)  --6、执行人上上上级部门(某岗位级及更低）879     0/level0        0表示所有人，otid指定岗位的人
begin
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, b.cu_id,'+@prevf+'_id from '+@dbname+@tbname + ','+@dbname+@tbname + '_users,mps_user a , mps_depart,mps_user b '+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'p_'+@prevf+'id='+@prevf+'_id and '+@prevf+'p_type=''wrkexecutor'' and '+@prevf+'p_cuid=a.cu_id and a.cu_dpid=dp_id and b.cu_cmpid=@cmpid and dp_fffdpid=b.cu_dpid and b.cu_otlevel<=@pobjid1 and '+@condition  
end  
end
end else
if (@p100=9) --900 
begin
if (@p10=0) 
begin
if (@p1=1)  -- 9、店长\督导.. 901x0 寻找 mps_shop中 dp_mcuidx   如果找不到，就向上走一层
begin
if (@pobjid1<=0) set @pobjid1=1 
if (@pobjid1>=10) set @pobjid1=10  
if (@pobjid2<=1) 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, dp_mcuid'+convert(varchar(10),@pobjid1)+','+@prevf+'_id from '+@dbname+@tbname +@from+ 
 ',mps_depart b '+
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_bcid=0 and dp_cmpid='+@prevf+'_cmpid and  dp_isdepart=@pobjid2 and dp_use=0 and  '+@condition  
 else 
 set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, dp_mcuid'+convert(varchar(10),@pobjid1)+','+@prevf+'_id from '+@dbname+@tbname +@from+ 
 ',mps_depart b '+
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@prevf+'_bcid>0 and dp_cmpid='+@prevf+'_cmpid and  dp_isdepart=(@pobjid2-2) and dp_use=0 and  '+@condition  
 --' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and dp_cmpid='+@prevf+'_cmpid and    '+@condition  
end else 
if (@p1=2)  -- 本公司指定的店长\督导.. 902x0 寻找 mps_shop中 dp_mcuidx 同时 dp_bcid 要相等 如果找不到，就向上走一层
begin
if (@pobjid1<=0) set @pobjid1=1 
if (@pobjid1>=10) set @pobjid1=10  
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, dp_mcuid'+convert(varchar(10),@pobjid1)+','+@prevf+'_id from '+@dbname+@tbname +@from+  
 ',mps_depart b '+
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and dp_cmpid='+@prevf+'_cmpid and dp_bcid='+@prevf+'_bcid and dp_isdepart=@pobjid2 and dp_use=0  and '+@condition  
 --' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and dp_cmpid='+@prevf+'_cmpid and dp_bcid='+@prevf+'_bcid and   '+@condition  
end else 
if (@p1=3)  -- 本管理区域指定的店长\督导..建单部门 903x0 寻找 mps_shop中 dp_mcuidx 同时 dp_managearea 要相等 如果找不到，就向上走一层
begin
if (@pobjid1<=0) set @pobjid1=1 
if (@pobjid1>=10) set @pobjid1=10  
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, a.dp_mcuid'+convert(varchar(10),@pobjid1)+','+@prevf+'_id from '+@dbname+@tbname +@from+  
 ',mps_depart b , mps_depart a '+
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and b.dp_id='+@prevf+'_createdpid and a.dp_cmpid=b.dp_cmpid and a.dp_bcid=b.dp_bcid and a.dp_isdepart=@pobjid2 and  a.dp_managearea=b.dp_managearea and a.dp_use=0 and '+@condition  
 --' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and b.dp_id='+@prevf+'_createdpid and a.dp_cmpid=b.dp_cmpid and a.dp_bcid=b.dp_bcid and    a.dp_managearea=b.dp_managearea  and '+@condition  
end  else 
if (@p1=4)  -- 本管理区域指定的店长\督导..责任部门 904x0 寻找 mps_shop中 dp_mcuidx 同时 dp_managearea 要相等 如果找不到，就向上走一层
begin
if (@pobjid1<=0) set @pobjid1=1 
if (@pobjid1>=10) set @pobjid1=10  
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select @pobjmc, a.dp_mcuid'+convert(varchar(10),@pobjid1)+','+@prevf+'_id from '+@dbname+@tbname +@from+  
 ',mps_depart b , mps_depart a '+
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and b.dp_id='+@prevf+'_dpid and a.dp_cmpid=b.dp_cmpid and a.dp_bcid=b.dp_bcid and a.dp_isdepart=@pobjid2 and  a.dp_managearea=b.dp_managearea and a.dp_use=0 and '+@condition  
 --' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and b.dp_id='+@prevf+'_dpid and a.dp_cmpid=b.dp_cmpid and a.dp_bcid=b.dp_bcid and    a.dp_managearea=b.dp_managearea  and '+@condition  
end  else 
if (@p1=5)  -- 本店指定的店长\督导..建单部门 905x0 寻找 mps_shop中 dp_mcuidx 同时 dp_id 要相等 如果找不到，就向上走一层
begin
if (@pobjid1<=0) set @pobjid1=1 
if (@pobjid1>=10) set @pobjid1=10  
 set @psql='delete #user2 '+
 'insert into #myuser2(pptype,cuid1,grp , dpt,me) '+
 'select @pobjmc, dp_mcuid1+dp_mcuid2+dp_mcuid3+dp_mcuid4+dp_mcuid5+dp_mcuid6+dp_mcuid7+dp_mcuid8+dp_mcuid9+dp_mcuid10,'+@prevf+'_id ,'+@prevf+'_createdpid,'+@prevf+'_createcuid from '+@dbname+@tbname+ ',mps_depart '+@from+ 
 ' where dp_id='+@prevf+'_createdpid and  '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition  +char(13)+char(10)+
 'update #myuser2 set dpt=dp_id from mps_depart where dp_cmpid=@cmpid and dp_isdepart=1 and dp_use=0 and (dp_mcuid1=me or dp_mcuid2=me or dp_mcuid3=me or dp_mcuid4=me or dp_mcuid5=me or dp_mcuid6=me or dp_mcuid7=me or dp_mcuid8=me or dp_mcuid9=me or dp_mcuid10=me) '+char(13)+char(10)+ 
 'update #myuser2 set cuid1=0 , me=0 ' 
  
 set @myi = @pobjid1 
 while (@myi<=10) 
 begin
 set @psql=@psql+char(13)+char(10)+
 'update #user2 set cuid1= a.dp_mcuid'+convert(varchar(10),@myi)+
 ' from mps_depart a '+
 ' where  a.dp_id=dpt  and   cuid1=0  and a.dp_mcuid'+convert(varchar(10),@myi)+'>0 '   
 set @myi = @myi + 1 
 end 
 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select pptype, cuid1,ordid from #user2 where cuid1>0 ' 
 
end  else 
if (@p1=6)  -- 本店指定的店长\督导..责任部门 906x0 寻找 mps_shop中 dp_mcuidx 同时 dp_id 要相等 如果找不到，就向上走一层
begin
if (@pobjid1<=0) set @pobjid1=1 
if (@pobjid1>=10) set @pobjid1=10  
 set @psql='delete #user2 '+
 'insert into #myuser2(pptype,cuid1,grp , dpt,me) '+
 'select @pobjmc, dp_mcuid1+dp_mcuid2+dp_mcuid3+dp_mcuid4+dp_mcuid5+dp_mcuid6+dp_mcuid7+dp_mcuid8+dp_mcuid9+dp_mcuid10,'+@prevf+'_id ,'+@prevf+'_dpid,'+@prevf+'_cuid from '+@dbname+@tbname+ ',mps_depart '+@from+ 
 ' where dp_id='+@prevf+'_dpid and  '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition  +char(13)+char(10)+
 'update #myuser2 set dpt=dp_id from mps_depart where dp_cmpid=@cmpid and dp_isdepart=1 and dp_use=0 and (dp_mcuid1=me or dp_mcuid2=me or dp_mcuid3=me or dp_mcuid4=me or dp_mcuid5=me or dp_mcuid6=me or dp_mcuid7=me or dp_mcuid8=me or dp_mcuid9=me or dp_mcuid10=me) '+char(13)+char(10)+ 
 'update #myuser2 set cuid1=0 , me=0 ' 
  
 set @myi = @pobjid1 
 while (@myi<=10) 
 begin
 set @psql=@psql+char(13)+char(10)+
 'update #user2 set cuid1= a.dp_mcuid'+convert(varchar(10),@myi)+
 ' from mps_depart a '+
 ' where  a.dp_id=dpt  and   cuid1=0  and a.dp_mcuid'+convert(varchar(10),@myi)+'>0 '   
 set @myi = @myi + 1 
 end 
 
set @psql='insert into #user(pptype,cuid,ordid) '+
 'select pptype, cuid1,ordid from #user2 where cuid1>0 ' 
end else 
if (@p1=7)  -- 我的门店主管\ 建单部门 90700 寻找 mps_shop中 dp_mcuidx 同时 dp_id 要相等 如果找不到，就向上走一层
begin
 set @psql='delete #user2 '+
'insert into #user2(pptype,cuid1,ordid,dpt,me,level) '+
 'select @pobjmc, 0,'+@prevf+'_id,'+@prevf+'_createdpid,'+@prevf+'_createcuid,0 from '+@dbname+@tbname+@from+ 
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid  and '+@condition   
 -- 计算我在门店上的位置
 -- 首先要纠正一下这种情况 ，当前部门中没有任何管理者信息，则重新去一个部门搜索。这种情况主要用于督导去找他的上级
 set @psql=@psql+char(13)+char(10)+
 'update #user2 set cuid1=-1 from mps_depart where dpt=dp_id and (dp_mcuid1+dp_mcuid2+dp_mcuid3+dp_mcuid4+dp_mcuid5+dp_mcuid6+dp_mcuid7+dp_mcuid8+dp_mcuid9+dp_mcuid10)=0 ' +char(13)+char(10)+
 'update #user2 set dpt=dp_id, cuid1=0 from mps_depart where cuid1=-1 and dp_cmpid=@cmpid and dp_id<>dpt and dp_isdepart=1 and dp_use=0 and '+
 '(dp_mcuid1=me or dp_mcuid2=me or dp_mcuid3=me or dp_mcuid4=me or dp_mcuid5=me or dp_mcuid6=me or dp_mcuid7=me or dp_mcuid8=me or dp_mcuid9=me or dp_mcuid10=me) ' + char(13)+char(10)+
 'update #user2 set cuid1=0 where cuid1=-1 ' +char(13)+char(10) 
 
 set @psql=@psql+char(13)+char(10)+
 'update #user2 set level = case when dp_mcuid1=me then 1 '+
 ' when dp_mcuid2=me then 2 '+
 ' when dp_mcuid3=me then 3 '+
 ' when dp_mcuid4=me then 4 '+
 ' when dp_mcuid5=me then 5 '+
 ' when dp_mcuid6=me then 6 '+
 ' when dp_mcuid7=me then 7 '+
 ' when dp_mcuid8=me then 8 '+
 ' when dp_mcuid9=me then 9 '+
 ' when dp_mcuid10=me then 10 else 0 end  '+
 'from mps_depart where dp_cmpid=@cmpid and  dpt=dp_id '+char(13)+char(10) 
 set @myi = 1 
 while (@myi<=10) 
 begin
 if (@pobjid1=0) -- 没有设置级别，则只是找我的上级，并且不=我
 begin
 set @psql=@psql+char(13)+char(10) +
 'update  #user2 set cuid1=dp_mcuid'+convert(varchar(10),@myi)+
 ' from mps_depart where dp_cmpid=@cmpid and   dpt=dp_id '+
 ' and cuid1=0 and level<'+convert(varchar(10),@myi)+' and me<>dp_mcuid'+convert(varchar(10),@myi)+' and dp_mcuid'+convert(varchar(10),@myi)+'>0 '+char(13)+char(10) 
 end else 
 begin
 set @psql=@psql+char(13)+char(10) +
 'update  #user2 set cuid1=dp_mcuid'+convert(varchar(10),@myi)+
 ' from mps_depart where dp_cmpid=@cmpid and   dpt=dp_id '+
 ' and cuid1=0 and level<'+convert(varchar(10),@myi)+' and @pobjid1<='+convert(varchar(10),@myi)+' and  dp_mcuid'+convert(varchar(10),@myi)+'>0 '+char(13)+char(10) 
 end
 set @myi = @myi + 1 
 end 
 
 set @psql=@psql+char(13)+char(10)+'update #user2 set cuid1=me where cuid1<=0  '
 
 set @psql=@psql+char(13)+char(10)+'insert into #user(pptype,cuid,ordid) select distinct pptype,cuid1,ordid from #user2 where cuid1>0 '

end else
if (@p1=8)  -- 我的门店主管\督导..责任部门 90700 寻找 mps_shop中 dp_mcuidx 同时 dp_id 要相等 如果找不到，就向上走一层
begin
set @psql='delete #user2 '+
'insert into #user2(pptype,cuid1,ordid,dpt,me,level) '+
 'select @pobjmc, 0,'+@prevf+'_id,'+@prevf+'_dpid,'+@prevf+'_cuid,0 from '+@dbname+@tbname+@from+  
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid  and '+@condition   
 
 -- 首先要纠正一下这种情况 ，当前部门中没有任何管理者信息，则重新去一个部门搜索。这种情况主要用于督导去找他的上级
 set @psql=@psql+char(13)+char(10)+
 'update #user2 set cuid1=-1 from mps_depart where dpt=dp_id and (dp_mcuid1+dp_mcuid2+dp_mcuid3+dp_mcuid4+dp_mcuid5+dp_mcuid6+dp_mcuid7+dp_mcuid8+dp_mcuid9+dp_mcuid10)=0 ' +char(13)+char(10)+
 'update #user2 set dpt=dp_id, cuid1=0 from mps_depart where cuid1=-1 and dp_cmpid=@cmpid and dp_id<>dpt and dp_isdepart=1 and dp_use=0 and '+
 '(dp_mcuid1=me or dp_mcuid2=me or dp_mcuid3=me or dp_mcuid4=me or dp_mcuid5=me or dp_mcuid6=me or dp_mcuid7=me or dp_mcuid8=me or dp_mcuid9=me or dp_mcuid10=me) ' + char(13)+char(10)+
 'update #user2 set cuid1=0 where cuid1=-1 ' +char(13)+char(10) 
 
 set @psql=@psql+char(13)+char(10)+
 'update #user2 set level = case when dp_mcuid1=me then 1 '+
 ' when dp_mcuid2=me then 2 '+
 ' when dp_mcuid3=me then 3 '+
 ' when dp_mcuid4=me then 4 '+
 ' when dp_mcuid5=me then 5 '+
 ' when dp_mcuid6=me then 6 '+
 ' when dp_mcuid7=me then 7 '+
 ' when dp_mcuid8=me then 8 '+
 ' when dp_mcuid9=me then 9 '+
 ' when dp_mcuid10=me then 10 else 0 end  '+
 'from mps_depart where dp_cmpid=@cmpid and  dpt=dp_id '+char(13)+char(10) 
 set @myi = 1 
 while (@myi<=10) 
 begin
 if (@pobjid1=0) -- 没有设置级别，则只是找我的上级，并且不=我
 begin
 set @psql=@psql+char(13)+char(10) +
 'update  #user2 set cuid1=dp_mcuid'+convert(varchar(10),@myi)+
 ' from mps_depart where dp_cmpid=@cmpid and   dpt=dp_id '+
 ' and cuid1=0 and level<'+convert(varchar(10),@myi)+' and me<>dp_mcuid'+convert(varchar(10),@myi)+' and dp_mcuid'+convert(varchar(10),@myi)+'>0 '+char(13)+char(10) 
 end else 
 begin
 set @psql=@psql+char(13)+char(10) +
 'update  #user2 set cuid1=dp_mcuid'+convert(varchar(10),@myi)+
 ' from mps_depart where dp_cmpid=@cmpid and   dpt=dp_id '+
 ' and cuid1=0 and level<'+convert(varchar(10),@myi)+' and @pobjid1<='+convert(varchar(10),@myi)+' and  dp_mcuid'+convert(varchar(10),@myi)+'>0 '+char(13)+char(10) 
 end
 set @myi = @myi + 1 
 end 
 
 set @psql=@psql+char(13)+char(10)+'update #user2 set cuid1=me where cuid1<=0  '
 
 set @psql=@psql+char(13)+char(10)+'insert into #user(pptype,cuid,ordid) select distinct pptype,cuid1,ordid from #user2 where cuid1>0 '
end  else 
if (@p1=9)  -- 我的门店所有主管 . 建单部门 90700 寻找 mps_shop中 dp_mcuidx 同时 dp_id 要相等 如果找不到，就向上走一层
begin
set @psql='delete #user2 '+
'insert into #user2(pptype,cuid1,ordid,dpt,me,level,cuid2,cuid3,cuid4,cuid5,cuid6,cuid7,cuid8,cuid9,cuid10) '+
 'select @pobjmc, 0,'+@prevf+'_id,'+@prevf+'_createdpid,'+@prevf+'_createcuid,0,0,0,0,0,0,0,0,0,0 from '+@dbname+@tbname+@from+  
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid  and '+@condition  
 
 -- 首先要纠正一下这种情况 ，当前部门中没有任何管理者信息，则重新去一个部门搜索。这种情况主要用于督导去找他的上级
 set @psql=@psql+char(13)+char(10)+
 'update #user2 set cuid1=-1 from mps_depart where dpt=dp_id and (dp_mcuid1+dp_mcuid2+dp_mcuid3+dp_mcuid4+dp_mcuid5+dp_mcuid6+dp_mcuid7+dp_mcuid8+dp_mcuid9+dp_mcuid10)=0 ' +char(13)+char(10)+
 'update #user2 set dpt=dp_id from mps_depart where cuid1=-1 and dp_cmpid=@cmpid and dp_id<>dpt and dp_use=0 and dp_isdepart=1 and '+
 '(dp_mcuid1=me or dp_mcuid2=me or dp_mcuid3=me or dp_mcuid4=me or dp_mcuid5=me or dp_mcuid6=me or dp_mcuid7=me or dp_mcuid8=me or dp_mcuid9=me or dp_mcuid10=me) ' + char(13)+char(10)+
 'update #user2 set cuid1=0 where cuid1=-1 ' +char(13)+char(10) 
 
 -- 计算我在门店上的位置
 set @psql=@psql+char(13)+char(10)+
 'update #user2 set level = case when dp_mcuid1=me then 1 '+
 ' when dp_mcuid2=me then 2 '+
 ' when dp_mcuid3=me then 3 '+ 
 ' when dp_mcuid4=me then 4 '+
 ' when dp_mcuid5=me then 5 '+
 ' when dp_mcuid6=me then 6 '+
 ' when dp_mcuid7=me then 7 '+
 ' when dp_mcuid8=me then 8 '+
 ' when dp_mcuid9=me then 9 '+
 ' when dp_mcuid10=me then 10 else 0 end  '+
 'from mps_depart where dp_cmpid=@cmpid and   dpt=dp_id '+char(13)+char(10) 
 set @myi = 1 
 while (@myi<=10) 
 begin
 if (@pobjid1=0) -- 没有设置级别，则只是找我所有的上级，并且不=我
 begin
 set @psql=@psql+char(13)+char(10) +
 'update  #user2 set cuid'+convert(varchar(10),@myi)+'=dp_mcuid'+convert(varchar(10),@myi)+
 ' from mps_depart where dp_cmpid=@cmpid and   dpt=dp_id '+
 ' and cuid'+convert(varchar(10),@myi)+'=0 and level<'+convert(varchar(10),@myi)+' and me<>dp_mcuid'+convert(varchar(10),@myi)+' and dp_mcuid'+convert(varchar(10),@myi)+'>0 '+char(13)+char(10) 
 end else 
 begin
 set @psql=@psql+char(13)+char(10) +
 'update  #user2 set cuid'+convert(varchar(10),@myi)+'=dp_mcuid'+convert(varchar(10),@myi)+
 ' from mps_depart where dp_cmpid=@cmpid and   dpt=dp_id '+
 ' and cuid'+convert(varchar(10),@myi)+'=0 and level<'+convert(varchar(10),@myi)+' and @pobjid1<='+convert(varchar(10),@myi)+' and me<>dp_mcuid'+convert(varchar(10),@myi)+' and dp_mcuid'+convert(varchar(10),@myi)+'>0 '+char(13)+char(10) 
 end 
 set @myi = @myi + 1 
 end 
 
 set @psql=@psql+char(13)+char(10)+'update #user2 set cuid1=me where (cuid1+cuid2+cuid3+cuid4+cuid5+cuid6+cuid7+cuid8+cuid9+cuid10)<=0  '
 
 set @myi = 1 
 while (@myi<=10) 
 begin
 set @psql=@psql+char(13)+char(10)+
 'insert into #user(pptype,cuid,ordid) select distinct pptype,cuid'+convert(varchar(10),@myi)+
 ',ordid from #user2 where cuid'+convert(varchar(10),@myi)+'>0 '
 set @myi = @myi + 1 
 end 

end
end else 
if (@p10=1)
begin
if (@p1=0)  -- 我的门店所有主管 . 责任部门 90700 寻找 mps_shop中 dp_mcuidx 同时 dp_id 要相等 如果找不到，就向上走一层
begin
set @psql='delete #user2 '+
'insert into #user2(pptype,cuid1,ordid,dpt,me,level,cuid2,cuid3,cuid4,cuid5,cuid6,cuid7,cuid8,cuid9,cuid10) '+
 'select @pobjmc, 0,'+@prevf+'_id,'+@prevf+'_dpid,'+@prevf+'_cuid,0,0,0,0,0,0,0,0,0,0 from '+@dbname+@tbname+@from+  
 ' where '+@prevf+'_cmpid=@cmpid and '+@prevf+'_'+@typef+'id=@bizid and '+@condition  
 
 -- 首先要纠正一下这种情况 ，当前部门中没有任何管理者信息，则重新去一个部门搜索。这种情况主要用于督导去找他的上级
 set @psql=@psql+char(13)+char(10)+
 'update #user2 set cuid1=-1 from mps_depart where dpt=dp_id and (dp_mcuid1+dp_mcuid2+dp_mcuid3+dp_mcuid4+dp_mcuid5+dp_mcuid6+dp_mcuid7+dp_mcuid8+dp_mcuid9+dp_mcuid10)=0 ' +char(13)+char(10)+
 'update #user2 set dpt=dp_id from mps_depart where cuid1=-1 and dp_cmpid=@cmpid and dp_id<>dpt and dp_use=0 and dp_isdepart=1 and '+
 '(dp_mcuid1=me or dp_mcuid2=me or dp_mcuid3=me or dp_mcuid4=me or dp_mcuid5=me or dp_mcuid6=me or dp_mcuid7=me or dp_mcuid8=me or dp_mcuid9=me or dp_mcuid10=me) ' + char(13)+char(10)+
 'update #user2 set cuid1=0 where cuid1=-1 ' +char(13)+char(10) 
 
 -- 计算我在门店上的位置
 set @psql=@psql+char(13)+char(10)+
 'update #user2 set level = case when dp_mcuid1=me then 1 '+
 ' when dp_mcuid2=me then 2 '+
 ' when dp_mcuid3=me then 3 '+ 
 ' when dp_mcuid4=me then 4 '+
 ' when dp_mcuid5=me then 5 '+
 ' when dp_mcuid6=me then 6 '+
 ' when dp_mcuid7=me then 7 '+
 ' when dp_mcuid8=me then 8 '+
 ' when dp_mcuid9=me then 9 '+
 ' when dp_mcuid10=me then 10 else 0 end  '+
 'from mps_depart where dp_cmpid=@cmpid and   dpt=dp_id '+char(13)+char(10) 
 set @myi = 1 
 while (@myi<=10) 
 begin
 if (@pobjid1=0) -- 没有设置级别，则只是找我所有的上级，并且不=我
 begin
 set @psql=@psql+char(13)+char(10) +
 'update  #user2 set cuid'+convert(varchar(10),@myi)+'=dp_mcuid'+convert(varchar(10),@myi)+
 ' from mps_depart where dp_cmpid=@cmpid and   dpt=dp_id '+
 ' and cuid'+convert(varchar(10),@myi)+'=0 and level<'+convert(varchar(10),@myi)+' and me<>dp_mcuid'+convert(varchar(10),@myi)+' and dp_mcuid'+convert(varchar(10),@myi)+'>0 '+char(13)+char(10) 
 end else 
 begin
 set @psql=@psql+char(13)+char(10) +
 'update  #user2 set cuid'+convert(varchar(10),@myi)+'=dp_mcuid'+convert(varchar(10),@myi)+
 ' from mps_depart where dp_cmpid=@cmpid and   dpt=dp_id '+
 ' and cuid'+convert(varchar(10),@myi)+'=0 and level<'+convert(varchar(10),@myi)+' and @pobjid1<='+convert(varchar(10),@myi)+' and me<>dp_mcuid'+convert(varchar(10),@myi)+' and dp_mcuid'+convert(varchar(10),@myi)+'>0 '+char(13)+char(10) 
 end 
 set @myi = @myi + 1 
 end 
 
 set @psql=@psql+char(13)+char(10)+'update #user2 set cuid1=me where (cuid1+cuid2+cuid3+cuid4+cuid5+cuid6+cuid7+cuid8+cuid9+cuid10)<=0  '
 
 set @myi = 1 
 while (@myi<=10) 
 begin
 set @psql=@psql+char(13)+char(10)+
 'insert into #user(pptype,cuid,ordid) select distinct pptype,cuid'+convert(varchar(10),@myi)+
 ',ordid from #user2 where cuid'+convert(varchar(10),@myi)+'>0 '
 set @myi = @myi + 1 
 end  
end
end
end

 if (@psql>'')
 begin
 print @psql 
 exec sp_executesql @psql, 
N'@cmpid int,@bizid int,@pobjmc varchar(30),@pobjid1 int ,@pobjid2 int,@pgrp int', 
@cmpid ,@bizid,@pobjmc,@pobjid1,@pobjid2,@pgrp 
 insert into #user(pptype,cuid,ordid) select pptype,cuid,ordid from #user 
 end  
 -- 权限计算完毕  ***************************************************************************************
 FETCH NEXT FROM status1 INTO @pobjmc,@pctgid,@pgrp,@ptype , @pobjid1, @pobjid2 , @porder,@psql,@cap
END
CLOSE status1
DEALLOCATE status1 
drop table #user2 
--select * from #user 
--drop table #user 

end