IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_dptmodify]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_dptmodify]
GO
create procedure [dbo].[mps_gen_dptmodify](
@cmpid int
)
as
begin
set nocount on
declare @s1 nvarchar(100)  
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 

exec sys_sql_getTableNamePk @cmpid,'dpt',@cmpid,0,@tbname output,@fkey output ,@dbname output 
declare @ss nvarchar(MAX) 
declare @sql nvarchar(MAX) 
declare @procparam nvarchar(MAX) 
set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure(@tbname+'_modify')=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure '+@tbname+N'_modify(
@dpid bigint, 
@cmpid int,
@branchid int,
@userdpid int,
@useruid int,
@userotid int,@dp_loginscope varchar(150), '+char(13)+char(10)
    set @s1 = @dbname+@tbname  
    exec mps_gen_proc_params @s1,'dp_id,dp_cmpid,dp_bcid,dp_loginscope',@procparam output 
--- 生成参数清单
if (len(@procparam)<=6) set @procparam = '@sys_nouse_v1 int' 
set @sql = @sql+@procparam+N' ) 
as  
begin  
set nocount on  
declare @nonedate datetime 
set @nonedate = CONVERT(datetime,''1910-1-1'')
 declare @operation varchar(50)
declare @errorinfo nvarchar(4000) 
 set @operation = ''continue'' 
set @errorinfo = '''' 
if (dbo.mps_sys_checkkeywords(''dpt.check'',@cmpid,0,'''','''')=0)
begin
exec mps_depart_check @cmpid,@branchid,@userdpid,@useruid,@userotid,@dpid,@operation output,@errorinfo output
if @operation = ''abort''
begin
select @operation operation, @errorinfo errorinfo
return 
end
end

declare @dpstdid bigint
set @dpstdid = 0
select @dpstdid = dp_dpstdid from mps_depart where dp_id = @dpid
if @dpstdid > 0 
begin
update mps_depart_base set 
dp_order=case when @dp_order is null or @dp_order = -999999999  then dp_order else @dp_order end,
dp_code=case when @dp_code is null or @dp_code = ''_none_''  then dp_code else @dp_code end,
dp_level=case when @dp_level is null or @dp_level = -999999999  then dp_level else @dp_level end,
dp_depart=case when @dp_depart is null or @dp_depart = ''_none_''  then dp_depart else @dp_depart end
where dp_id=@dpstdid and dp_isgroup = 1
end

 --- 以下是预处理事件中的临时表
 '

 if (dbo.mps_sys_checkkeywords('dpt.beforemodify,dpt.aftermodify',@cmpid,0,'','')<>0)   -- 没有事件
 begin
 exec mps_sys_gen_updatestatement @s1,'','@','dp_id,dp_cmpid,dp_bcid',@ss output 
 set @sql = @sql+N'update '+@dbname+@tbname+' set '+char(13)+char(10)+@ss+char(13)+char(10) + 
 'where dp_id=@dpid ' 
 end else 
 begin
 exec mps_sys_gen_updatestatement @s1,'','@','dp_id,dp_cmpid,dp_bcid',@ss output 
 if (len(@ss)>10) 
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#old'' '+char(13)+char(10)+
'select * into #old from '+@dbname+@tbname+N' where dp_id=@dpid '+char(13)+char(10)+
'exec drop_temp_table ''#new'' '+char(13)+char(10)+
'select *  into #new from #old '+char(13)+char(10)+
'update #new set '+char(13)+char(10) +  @ss + char(13)+char(10) 
 end 
 
 if (dbo.mps_sys_checkkeywords('dpt.beforemodify',@cmpid,0,'','')=0) 
 begin
 set @sql = @sql+'exec '+@tbname+'_modify_before @cmpid,@branchid,@userdpid,@useruid,@userotid,@dpid,@operation output,@errorinfo output   ' 
 end
 
 set @sql = @sql +N'
 -- 处理@dp_loginscope 
if (@dp_loginscope<>''_none_'') 
begin
declare @dps table(dpid int , ord int) 
insert into @dps(dpid , ord) values(@dpid,0) 
insert into @dps(dpid,ord) select dp_id , 1 from mps_depart with(nolock),@dps where ord=0 and dp_fdpid=dpid 
insert into @dps(dpid,ord) select dp_id , 2 from mps_depart with(nolock),@dps where ord=1 and dp_fdpid=dpid 
insert into @dps(dpid,ord) select dp_id , 3 from mps_depart with(nolock),@dps where ord=2 and dp_fdpid=dpid 
insert into @dps(dpid,ord) select dp_id , 4 from mps_depart with(nolock),@dps where ord=3 and dp_fdpid=dpid 
insert into @dps(dpid,ord) select dp_id , 5 from mps_depart with(nolock),@dps where ord=4 and dp_fdpid=dpid 
update mps_depart set dp_loginscope=@dp_loginscope from @dps where dp_id=dpid and ord>0 
update mps_employee set cu_loginscope=@dp_loginscope from @dps where cu_dpid=dpid 
update mps_login set cu_loginscope=@dp_loginscope from @dps where cu_dpid=dpid 
update mps_login_role set lgu_loginscope=@dp_loginscope from @dps where lgu_dpid=dpid 
end 
 if (@operation=''continue'') 
 begin
 set @nonedate = @nonedate 
 '
 if (dbo.mps_sys_checkkeywords('dpt.beforemodify,dpt.aftermodify',@cmpid,0,'','')=0)
 begin
 set @s1 = @dbname+@tbname  
 exec mps_sys_gen_updatestatement @s1,'old','new','dp_id,dp_cmpid,dp_bcid',@ss output 
 if (len(@ss)>10) 
 set @sql=@sql+char(13)+char(10)+
'update  old set '+char(13)+char(10)+@ss+''+char(13)+char(10)+
 'from '+@dbname+@tbname+' old,#new new where old.dp_id=new.dp_id ' 
 end
 if (dbo.mps_sys_checkkeywords('dpt.aftermodify',@cmpid,0,'','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec '+@tbname+'_modify_after @cmpid,@branchid,@userdpid,@useruid,@userotid,@dpid '+char(13)+char(10)
 end
 if (dbo.mps_sys_checkkeywords('dpt.aftermodify',@cmpid,0,'#new','update')=0) 
 begin
 set @s1 = @dbname+@tbname  
 exec mps_sys_gen_updatestatement @s1,'old','new','dp_id,dp_cmpid,dp_bcid',@ss output 
 if (len(@ss)>10) 
 set @sql=@sql+char(13)+char(10)+
'update  old set '+char(13)+char(10)+@ss+' '+char(13)+char(10)+
 'from '+@dbname+@tbname+' old,#new new where old.dp_id=new.dp_id ' 
 end
 set @sql=@sql+N'
 select ''ok'' operation, '''' errorinfo 
end  else 
begin
select @operation operation, @errorinfo errorinfo  
end
' 
if (dbo.mps_sys_checkkeywords('dpt.beforemodify,dpt.aftermodify',@cmpid,0,'','')=0)
 begin
 set @sql=@sql+char(13)+char(10)+
'exec drop_temp_table ''#new'' '+char(13)+char(10)+ 
'exec drop_temp_table ''#account'' '+char(13)+char(10)+ 
'exec drop_temp_table ''#old'' '+char(13)+char(10)
 end
set @sql=@sql+N'
 
set nocount off 
end '
exec myprint @sql 
execute(@sql)  
-- 生成mps_sql 
set @sql = ' exec mps_sys_gen_mpssql '+CONVERT(varchar(10),@cmpid)+','''+@tbname+N'_modify'',
''@dpid bigint,@cmpid int,@branchid int,@userdpid int,@useruid int,@userotid int,@dp_loginscope varchar(150),'+@procparam+'''' 
exec myprint @sql 
execute(@sql)
set nocount off 
end