IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_biz_power_sources]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_biz_power_sources]
GO
CREATE procedure [dbo].[mps_g_biz_power_sources](
@cmpid int ,
@mybcid int,
@bizid int 
)
as
begin
set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
-- 首先返回工作步骤清单
declare @t table(wrkid int , code varchar(50)) 
insert into @t(wrkid,code) 
select MIN(wrk_id) , case when wrk_code='' then LTRIM(rtrim(str(wrk_id))) else wrk_code end  
from mps_flowwork with(nolock) 
where wrk_bizid=@bizid and charindex('auto',wrk_keyparams)=0 and charindex('nomanual',wrk_keyparams)=0 and 
charindex('back',wrk_shape)<>1 and charindex('abolish',wrk_shape)<>1 and charindex('backany',wrk_shape)<>1
 and charindex('end',wrk_shape)<>1 and charindex('waitting',wrk_shape)<>1 and charindex('waitting',wrk_shape)<>1
group by case when wrk_code='' then LTRIM(rtrim(str(wrk_id))) else wrk_code end

select wrk_id ,wrk_title,wrk_code 
from mps_flowwork with(nolock) , @t 
where wrk_id=wrkid 
order by wrk_order 

-- 返回部门名称
select dp_id , dp_depart,dp_fullname
from mps_depart  with(nolock) 
where dp_cmpid=@cmpid and dp_bcid=@mybcid and dp_fullname>'' and dp_depart>''  and dp_isdepart=0 
order by dp_level , dp_order 

-- 返回岗位
declare @tt table(otid int) 
insert into @tt(otid) 
select distinct dt_otid 
from mps_depart_title  with(nolock) , mps_depart  with(nolock) 
where dp_cmpid=@cmpid and dp_bcid=@mybcid and dt_dpid=dp_id and dp_isdepart=0 

select  ot_id , ot_title,ot_display 
from mps_title  with(nolock) , @tt 
where ot_id=otid and ot_title>'' and ot_display>'' 
order by ot_order 

-- 

select dp_id , ot_id , dp_fullname+'/'+ot_title  dp_fullname 
from mps_depart  with(nolock) ,  mps_depart_title  with(nolock) , mps_title  with(nolock) 
where dp_cmpid=@cmpid and dp_bcid=@mybcid and dt_dpid=dp_id and dt_otid=ot_id and dp_fullname>'' and ot_title>'' and dp_isdepart=0  
order by dp_level , dp_order , ot_level,ot_order 


if (@mybcid=0)
begin
--分公司所有部门
select distinct dp_dpstdid , dp_depart,dp_fullname ,dp_level , dp_order 
from mps_depart  with(nolock) 
where dp_cmpid=@cmpid and dp_bcid>0 and dp_fullname>'' and dp_depart>''  and dp_isdepart=0 
order by dp_level , dp_order 

-- 返回岗位
declare @tt1 table(otid int) 
insert into @tt1(otid) 
select distinct dt_otid 
from mps_depart_title with(nolock)  , mps_depart  with(nolock) 
where dp_cmpid=@cmpid and dp_bcid>0 and dt_dpid=dp_id and dp_isdepart=0 

select  ot_id , ot_title,ot_display 
from mps_title  with(nolock) , @tt1 
where ot_id=otid and ot_title>'' and ot_display>'' 
order by ot_order 

select distinct dp_dpstdid , ot_id , dp_fullname+'/'+ot_title  dp_fullname ,dp_level , dp_order , ot_level,ot_order
from mps_depart  with(nolock) ,  mps_depart_title  with(nolock) , mps_title  with(nolock) 
where dp_cmpid=@cmpid and dp_bcid>0 and dt_dpid=dp_id and dt_otid=ot_id and dp_fullname>'' and ot_title>'' and dp_isdepart=0  
order by dp_level , dp_order , ot_level,ot_order 
end
set nocount off
end