IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_picksource_bykey]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_picksource_bykey]
GO
CREATE procedure [dbo].[mps_g_picksource_bykey](
@cmpid int ,
@branchid int ,
@userdpid  int,
@useruid int,
@objtype varchar(20) ,
@objid int,
@objid2 int,
@sourcename nvarchar(50) , 
@ordid bigint,
@ord2id bigint,
@key nvarchar(50),
@extparam1 nvarchar(100), 
@extparam2 nvarchar(100), 
@extparam3 nvarchar(100), 
@extparam4 nvarchar(100), 
@extparam5 nvarchar(100), 
@extparam6 nvarchar(100), 
@extparam7 nvarchar(100), 
@extparam8 nvarchar(100), 
@extparam9 nvarchar(100), 
@objidname varchar(50)=default 
)
as
begin
 set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
 declare @tbid int 
 declare @tbname varchar(50) 
 declare @tbname2 varchar(50) 
 declare @dbname varchar(30) 
 
 declare @flag int 
set @flag = 0 
declare @ctid int 
set @ctid =0 

declare @stype varchar(20) 
declare @sctgid int 
declare @sctgid2 int 
declare @cond  nvarchar(4000) 
declare @inssql  nvarchar(4000) 
declare @disp  nvarchar(4000) 
declare @extcond nvarchar(4000)

declare @keyfld varchar(30)
declare @keyfld2 varchar(30)

declare @bsid int
declare @objcount int
declare @sobjid bigint
declare @sfld varchar(50)  
select @bsid=bizs_id,@stype=bizs_sourcetype , @sctgid=bizs_ctgid , @sctgid2=bizs_ctg2id,
@cond=bizs_condition , @inssql=bizs_insertsql , @disp=bizs_displayfield , @sfld=bizs_sourcefield 
from mps_biztype_source where bizs_type=@objtype and bizs_bizid=@objid and bizs_ord2tag=@objid2 and bizs_source=@sourcename 

if ((@objidname is null) or (@objidname='_none_')) set @objidname=@sfld 

set @stype = isnull(@stype,'') 
set @stype = lower(@stype) 
if (@stype='') 
begin
select 0 objcount
set nocount off
return 
end


if (@sctgid>100000000) 
begin
set @flag = @sctgid / 100000000 
set @sctgid = @sctgid - @flag * 100000000 
end 

if (@flag>=5) 
begin
--set @sctgid = @sctgid - @flag * 100000000 
set @ctid = @sctgid 
if (@stype='cst') 
begin
select @sctgid = ct_tag from mps_csttype where ct_id=@sctgid 
end 
if (@stype='prd') 
begin
select @sctgid = pc_tag from mps_prdctg where pc_id=@sctgid 
end 
if (@stype='equ') 
begin
select @sctgid = ec_tag from mps_equctg where ec_id=@sctgid 
end 
if (@stype='etc') 
begin
select @sctgid = etc_tag from mps_ext_resource where etc_id=@sctgid 
end 
end 

-- cu_branch|分公司||left;cu_code|员工编号||center;cu_depart|部门名称||left;cu_name|员工姓名||left;cu_title|岗位||left
declare @sql nvarchar(4000) 
declare @i int 
declare @s1 nvarchar(1000) 

declare @disp1 nvarchar(4000)
set @disp1 = '' 
while (@disp>'') 
begin
set @i = charindex(';',@disp) 
if (@i>0) 
begin
set @s1 = substring(@disp,1,@i-1) 
set @disp = substring(@disp,@i+1,len(@disp)-@i) 
end else 
begin
set @s1 = @disp 
set @disp ='' 
end
if (@s1>'') 
begin
set @i = charindex('|',@s1) 
if (@i>0) 
begin
set @s1 = substring(@s1,1,@i-1) 
if (@disp1>'') set @disp1=@disp1 + ' or ' 
set @disp1 = @disp1 + @s1 +' like ''%''+@key+''%''' 
end
end
end 
if (@disp1>'') set @disp1 = ' ('+@disp1+') ' 
 
if (@stype='emp') 
begin
-- @sctgid=-1/0/所有人  0/0/集团总部所有人  -2/0/当前分公司所有人 -3/0/当前部门所有人  
set @sql = ' cu_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and cu_use=0 ' 
set @sql  = @sql+N' and (cu_fastkey like ''%''+@key+''%'' 
or cu_account like ''%''+@key+''%'' 
or cu_name like ''%''+@key+''%'' 
or cu_mobile like ''%''+@key+''%'' 
or cu_identno like ''%''+@key+''%'' 
or cu_code like ''%''+@key+''%'' ) ' 

if (@sctgid=0)  --集团总部所有人
begin
set @sql = @sql +' and  cu_bcid=0 '
end else 
if (@sctgid=-2)  -- -2/0/当前分公司所有人
begin
set @sql = @sql +' and  cu_bcid='+ltrim(rtrim(str(@branchid)))+' ' 
   end else 
   if (@sctgid=-3)  -- -3/0/当前部门所有人  
   begin
   set @sql = @sql +' and cu_bcid='+ltrim(rtrim(str(@branchid)))+' and cu_dpid=' + ltrim(rtrim(str(@userdpid))) 
   end else 
   begin
   set @sql = @sql +' and  cu_bcid='+ltrim(rtrim(str(@branchid)))+' and cu_dpid=' + ltrim(rtrim(str(@userdpid))) 
   end
   
   set @sql = 'select @objcount = count(cu_id) , @sobjid=min(cu_id) from mps_employee where '+@sql 
   exec sp_executesql @sql, N'@extparam1 nvarchar(100),@extparam2 nvarchar(100),@extparam3 nvarchar(100),@extparam4 nvarchar(100),@extparam5 nvarchar(100),@extparam6 nvarchar(100),@extparam7 nvarchar(100),@extparam8 nvarchar(100),@extparam9 nvarchar(100),@key nvarchar(50),@objcount int out,@sobjid int out', 
   @extparam1,@extparam2,@extparam3,@extparam4,@extparam5,@extparam6,@extparam7,@extparam8,@extparam9,@key  , @objcount out , @sobjid out 
end  

if (@stype='web') 
begin
-- @sctgid=webservice svc_id 
set @sql = ' svc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and svc_bcid='+LTRIM(rtrim(str(@branchid)))+' and svc_dpid='+ LTRIM(rtrim(str(@userdpid)))

declare @sq varchar(1000) 
set @sq=''
select @sq=@sq + case when @sq='' then '' else ' or ' end +fldi_fieldname+' like ''%''+@key+''%'' ' from mps_fieldinfo 
where fldi_cmpid=@cmpid and fldi_tbid=@sctgid and fldi_tbtype='web' and fldi_system=1 and 
(fldi_datatype like '%char%' or fldi_datatype like '%string%' or fldi_datatype like '%text%') 

if (@sq>'') set @sql  = @sql+N' and ('+@sq+') ' 

   set @sql = 'select @objcount = count(svc_id) , @sobjid=min(svc_id) from webservice_'+LTRIM(rtrim(str(@sctgid)))+' where '+@sql 
   exec sp_executesql @sql, N'@extparam1 nvarchar(100),@extparam2 nvarchar(100),@extparam3 nvarchar(100),@extparam4 nvarchar(100),@extparam5 nvarchar(100),@extparam6 nvarchar(100),@extparam7 nvarchar(100),@extparam8 nvarchar(100),@extparam9 nvarchar(100),@key nvarchar(50),@objcount int out,@sobjid int out', 
   @extparam1,@extparam2,@extparam3,@extparam4,@extparam5,@extparam6,@extparam7,@extparam8,@extparam9,@key  , @objcount out , @sobjid out 
end  

if (@stype='dpt') 
begin
-- @sctgid=0/0/公司部门  1/0/部门子部门
set @sql = ' dp_isdepart=0 and dp_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and (dp_code like ''%''+@key+''%'' or dp_depart like ''%''+@key+''%'') ' 

if (@sctgid=0)  --公司部门
begin
set @sql = @sql +' and  dp_bcid='+ltrim(rtrim(str(@branchid)))+' ' 
   end else 
   if (@sctgid=1)  -- -3/0/子部门 
   begin
   set @sql = @sql +' and (dp_fdpid='+ltrim(rtrim(str(@userdpid)))+' and dp_ffdpid=' + ltrim(rtrim(str(@userdpid))) +') '
   end  
   
   set @sql = 'select @objcount = count(dp_id) , @sobjid=min(dp_id) from mps_depart where '+@sql 
   exec sp_executesql @sql, N'@extparam1 nvarchar(100),@extparam2 nvarchar(100),@extparam3 nvarchar(100),@extparam4 nvarchar(100),@extparam5 nvarchar(100),@extparam6 nvarchar(100),@extparam7 nvarchar(100),@extparam8 nvarchar(100),@extparam9 nvarchar(100),@key nvarchar(50),@objcount int out,@sobjid int out', 
   @extparam1,@extparam2,@extparam3,@extparam4,@extparam5,@extparam6,@extparam7,@extparam8,@extparam9,@key  , @objcount out , @sobjid out 
   end
   if (@stype='shp') 
begin
-- @sctgid=0/0/公司部门  1/0/部门子部门
set @sql = ' dp_isdepart=1 and dp_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and (dp_code like ''%''+@key+''%'' or dp_depart like ''%''+@key+''%'') ' 

if (@sctgid=0)  --公司部门
begin
set @sql = @sql +' and  dp_bcid='+ltrim(rtrim(str(@branchid)))+' ' 
   end else 
   if (@sctgid=1)  -- -3/0/子部门 
   begin
   set @sql = @sql +' and (dp_fdpid='+ltrim(rtrim(str(@userdpid)))+' and dp_ffdpid=' + ltrim(rtrim(str(@userdpid))) +') '
   end  
   
   set @sql = 'select @objcount = count(dp_id) , @sobjid=min(dp_id) from mps_depart where '+@sql 
   exec sp_executesql @sql, N'@extparam1 nvarchar(100),@extparam2 nvarchar(100),@extparam3 nvarchar(100),@extparam4 nvarchar(100),@extparam5 nvarchar(100),@extparam6 nvarchar(100),@extparam7 nvarchar(100),@extparam8 nvarchar(100),@extparam9 nvarchar(100),@key nvarchar(50),@objcount int out,@sobjid int out', 
 @extparam1,@extparam2,@extparam3,@extparam4,@extparam5,@extparam6,@extparam7,@extparam8,@extparam9,@key  , @objcount out , @sobjid out 
   end
  if (@stype='brch') 
begin
-- @sctgid=0/0/公司部门  1/0/部门子部门
set @sql = ' bc_cmpid='+ltrim(rtrim(str(@cmpid)))  +' and (bc_code like ''%''+@key+''%'' or bc_company like ''%''+@key+''%'') ' 
   set @sql = 'select @objcount = count(dp_id) , @sobjid=min(dp_id) from mps_groupbranch where '+@sql 
   exec sp_executesql @sql, N'@extparam1 nvarchar(100),@extparam2 nvarchar(100),@extparam3 nvarchar(100),@extparam4 nvarchar(100),@extparam5 nvarchar(100),@extparam6 nvarchar(100),@extparam7 nvarchar(100),@extparam8 nvarchar(100),@extparam9 nvarchar(100),@key nvarchar(50),@objcount int out,@sobjid int out', 
   @extparam1,@extparam2,@extparam3,@extparam4,@extparam5,@extparam6,@extparam7,@extparam8,@extparam9,@key  , @objcount out , @sobjid out 
   end 
  if (@stype='private') 
begin
-- @sctgid mps_privatesql ps_id
select @extcond = ps_extcond from mps_privatesql where ps_id=@sctgid 
set @extcond=isnull(@extcond,'') 
set @tbname = 'privateview_'+ltrim(rtrim(str(@sctgid))) 

  set @sql = 'select @objcount = count(objid) , @sobjid=min(objid) from '+@tbname 
  if (len(@extcond)>10) set @sql = @sql + ' where '+@extcond 
  exec sp_executesql @sql, N'@extparam1 nvarchar(100),@extparam2 nvarchar(100),@extparam3 nvarchar(100),@extparam4 nvarchar(100),@extparam5 nvarchar(100),@extparam6 nvarchar(100),@extparam7 nvarchar(100),@extparam8 nvarchar(100),@extparam9 nvarchar(100),@key nvarchar(50),@objcount int out,@sobjid int out', 
  @extparam1,@extparam2,@extparam3,@extparam4,@extparam5,@extparam6,@extparam7,@extparam8,@extparam9,@key  , @objcount out , @sobjid out 
  end 
  if (@stype='biz') 
begin

select top 1 @tbid = biz_table , @dbname=biz_database from mps_biztype where biz_id=@sctgid 
if (@tbid>0) select top 1 @tbid = biz_id , @dbname=biz_database from mps_biztype where biz_id=@tbid 
if (@tbid=0) set @tbid = @sctgid 
if (@dbname>'') set @tbname=@dbname+'.dbo.' 
set @tbname = @tbname+'order_'+ltrim(rtrim(str(@tbid))) 

set @sql = ' ord_cmpid='+ltrim(rtrim(str(@cmpid)))  +'   ' 
if (@disp1>'') set @sql  = @sql+' and '+@disp1 
if (@cond>'') set @sql  = @sql+' and ('+@cond+') ' 
if (@sctgid2>0) -- 是子表
begin
set @sql = @sql +' and  ord2_ordid=ord_id   '
end 
  
  if (@sctgid2>0) 
  begin 
  set @sql = 'select @objcount = count(ord2_id) , @sobjid=min(ord2_id) from '+@tbname+','+@tbname+
  '_'+ltrim(rtrim(str(@sctgid2)))+' where '+@sql 
  end else
  begin
  set @sql = 'select @objcount = count(ord_id) , @sobjid=min(ord_id) from '+@tbname+' where '+@sql 
  end
  exec sp_executesql @sql, N'@extparam1 nvarchar(100),@extparam2 nvarchar(100),@extparam3 nvarchar(100),@extparam4 nvarchar(100),@extparam5 nvarchar(100),@extparam6 nvarchar(100),@extparam7 nvarchar(100),@extparam8 nvarchar(100),@extparam9 nvarchar(100),@key nvarchar(50),@objcount int out,@sobjid int out', 
  @extparam1,@extparam2,@extparam3,@extparam4,@extparam5,@extparam6,@extparam7,@extparam8,@extparam9,@key  , @objcount out , @sobjid out 
end  
if (@stype='cst') 
begin

exec sys_sql_getTableNamePk @cmpid,'cst',@sctgid,0,@tbname output , @keyfld output , @dbname output 
set @keyfld='cc_id' 
set @keyfld2='cc2_id' 
set @tbname = @dbname+@tbname 
set @tbname2 = '' 
if (@sctgid2>0) 
begin
set @tbname2 = @tbname+'_'+ltrim(rtrim(str(@sctgid2))) 
end

set @sql = ' cc_cmpid='+ltrim(rtrim(str(@cmpid)))  +'   ' 
if (@disp1>'') set @sql  = @sql+' and '+@disp1 
if (@cond>'') set @sql  = @sql+' and ('+@cond+') ' 
if (@sctgid2>0) -- 是子表
begin
set @sql = @sql +' and  cc2_ccid=cc_id   '
end 
  
  if (@sctgid2>0) 
  begin 
  set @sql = 'select @objcount = count(cc2_id) , @sobjid=min(cc2_id) from '+@tbname2+','+@tbname+
  ' where '+@sql 
  end else
  begin
  set @sql = 'select @objcount = count(cc_id) , @sobjid=min(cc_id) from '+@tbname+' where '+@sql 
  end
  exec sp_executesql @sql, N'@extparam1 nvarchar(100),@extparam2 nvarchar(100),@extparam3 nvarchar(100),@extparam4 nvarchar(100),@extparam5 nvarchar(100),@extparam6 nvarchar(100),@extparam7 nvarchar(100),@extparam8 nvarchar(100),@extparam9 nvarchar(100),@key nvarchar(50),@objcount int out,@sobjid int out', 
  @extparam1,@extparam2,@extparam3,@extparam4,@extparam5,@extparam6,@extparam7,@extparam8,@extparam9,@key  , @objcount out , @sobjid out 
end 
if (@stype='prd') 
begin

exec sys_sql_getTableNamePk @cmpid,'prd',@sctgid,0,@tbname output , @keyfld output , @dbname output 
set @keyfld='prd_id' 
set @keyfld2='prd2_id' 
set @tbname = @dbname+@tbname 
set @tbname2 = '' 
if (@sctgid2>0) 
begin
set @tbname2 = @tbname+'_'+ltrim(rtrim(str(@sctgid2))) 
end

set @sql = ' prd_cmpid='+ltrim(rtrim(str(@cmpid)))  +'   ' 
if (@disp1>'') set @sql  = @sql+' and '+@disp1 
if (@cond>'') set @sql  = @sql+' and ('+@cond+') ' 
if (@sctgid2>0) -- 是子表
begin
set @sql = @sql +' and  prd2_prdid=prd_id   '
end 
  
  if (@sctgid2>0) 
  begin 
  set @sql = 'select @objcount = count(prd2_id) , @sobjid=min(prd2_id) from '+@tbname2+','+@tbname+
  ' where '+@sql 
  end else
  begin
  set @sql = 'select @objcount = count(prd_id) , @sobjid=min(prd_id) from '+@tbname+' where '+@sql 
  end
  exec sp_executesql @sql, N'@extparam1 nvarchar(100),@extparam2 nvarchar(100),@extparam3 nvarchar(100),@extparam4 nvarchar(100),@extparam5 nvarchar(100),@extparam6 nvarchar(100),@extparam7 nvarchar(100),@extparam8 nvarchar(100),@extparam9 nvarchar(100),@key nvarchar(50),@objcount int out,@sobjid int out', 
  @extparam1,@extparam2,@extparam3,@extparam4,@extparam5,@extparam6,@extparam7,@extparam8,@extparam9,@key  , @objcount out , @sobjid out 
end 
    
   if (@objcount=1)
   begin
   select @objcount as objcount
   if (@ordid>0 and @ord2id=0) 
   begin
   exec mps_g_picksource_value @cmpid,@branchid,@userdpid,@useruid,0,@ordid , 0,@objid2,'',@bsid,@sobjid ,@objidname
   end else 
   begin
   exec mps_g_picksource_value @cmpid,@branchid,@userdpid,@useruid,0,@ord2id , @ordid ,@objid2,'',@bsid,@sobjid ,@objidname
   end
  end else 
  begin
  if (@ordid>0 and @ord2id=0) 
   begin
   exec mps_g_picksource_value @cmpid,@branchid,@userdpid,@useruid,0,@ordid , 0,@objid2,'',@bsid,'-1' ,@objidname
   end else 
   begin
   exec mps_g_picksource_value @cmpid,@branchid,@userdpid,@useruid,0,@ord2id , @ordid ,@objid2,'',@bsid,'-1'  ,@objidname
   end
   select @objcount as objcount 
   select @inssql upfldmap , 0 nonefld1 
  end
  set nocount off
end