IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_bizmodify]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_bizmodify]
GO
create procedure [dbo].[mps_gen_bizmodify](
@cmpid int,
@bizid int
)
as
begin
set nocount on
declare @s1 nvarchar(100) 
declare @detail varchar(100)
declare @ds1 varchar(100)
declare @dss nvarchar(MAX) 
declare @order2 varchar(100)
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 
declare @ss nvarchar(MAX) 
declare @sql nvarchar(MAX) 
declare @ord2tag2 int 
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(
@cmpid int,
@branchid int,
@userdpid int,
@useruid int,
@userotid int,
@jbid bigint,
@ordid bigint,@sourcefield varchar(50)=default, '+char(13)+char(10)
    set @s1 = @dbname+@tbname  
    exec mps_gen_proc_params_default @s1,'ord_id,ord_bizid,ord_cmpid,ord_updated,ord_bcid,ord_groupkey,ord_finished,ord_date',@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('crm.beforemodify,crm.aftermodify',@bizid,0,'','')<>0)   -- 没有事件
 begin
 exec mps_sys_gen_updatestatement @s1,'','@','ord_id,ord_bizid,ord_cmpid,ord_updated,ord_bcid,ord_groupkey,ord_finished,ord_date',@ss output 
 if (len(@ss)>10) 
 set @sql = @sql+N'update '+@dbname+@tbname+' set '+char(13)+char(10)+@ss+','+char(13)+char(10)+
 'ord_finished=case when ord_finished=0 then 1 else ord_finished end ,'+char(13)+char(10) + 
 'ord_updated=getdate() '+char(13)+char(10) + 
 'where ord_id=@ordid ' 
 end else 
 begin
 exec mps_sys_gen_updatestatement @s1,'','@','ord_id,ord_bizid,ord_cmpid,ord_updated,ord_bcid,ord_groupkey,ord_finished,ord_date',@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 ord_id=@ordid '+char(13)+char(10)+
 'exec drop_temp_table ''#account'' '+char(13)+char(10)+
'create table #account(acctype varchar(30),acccategory varchar(30),accdate varchar(30),fiidreal int,fiid int,objidreal bigint,objid bigint,origin int,value int,used int,suspended int,ready int,changed int,available int,actual int) '+char(13)+char(10) +
'exec drop_temp_table ''#new'' '+char(13)+char(10)+
'select * , case when ((@sourcefield is null) or (@sourcefield = ''_none_'')) then '''' else @sourcefield end sourcefield 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('crm.beforemodify',@bizid,0,'','')=0) 
 begin
 set @sql = @sql+'exec '+@tbname+'_modify_before @cmpid,@branchid,@userdpid,@useruid,@userotid,@jbid,@ordid,@operation output,@errorinfo output   ' 
 end
 
 set @sql = @sql +N'
 if (@operation=''continue'') 
 begin
 set @nonedate = @nonedate 
 '
 if (dbo.mps_sys_checkkeywords('crm.beforemodify,crm.aftermodify',@bizid,0,'','')=0)
 begin
 set @s1 = @dbname+@tbname  
 exec mps_sys_gen_updatestatement @s1,'old','new','ord_id,ord_bizid,ord_cmpid,ord_updated,ord_bcid,ord_groupkey,ord_finished,ord_date',@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)+
'old.ord_finished=case when old.ord_finished=0 then 1 else old.ord_finished end ,'+char(13)+char(10) + 
 'old.ord_updated=getdate() '+char(13)+char(10) + 
 'from '+@dbname+@tbname+' old,#new new where old.ord_id=new.ord_id ' 
 end
 if (dbo.mps_sys_checkkeywords('crm.aftermodify',@bizid,0,'','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec '+@tbname+'_modify_after @cmpid,@branchid,@userdpid,@useruid,@userotid,@jbid,@ordid '+char(13)+char(10)
 end
 if (dbo.mps_sys_checkkeywords('crm.aftermodify',@bizid,0,'#new','update')=0) 
 begin
 set @s1 = @dbname+@tbname  
 exec mps_sys_gen_updatestatement @s1,'old','new','ord_id,ord_bizid,ord_cmpid,ord_updated,ord_bcid,ord_groupkey,ord_finished,ord_date',@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)+
'old.ord_finished=case when old.ord_finished=0 then 1 else old.ord_finished end ,'+char(13)+char(10) + 
 'old.ord_updated=getdate() '+char(13)+char(10) + 
 'from '+@dbname+@tbname+' old,#new new where old.ord_id=new.ord_id ' 
 end
 set @sql=@sql+N'
 if (@ord_fordid>0) -- 说明有可能在修改上级关系
begin
if (dbo.checkprocedure('''+@tbname+'_calc_relation'')=0)
begin
exec '+@tbname+N'_calc_relation @cmpid , @ordid 
end
end 
 select ''ok'' operation, '''' errorinfo 
end  else 
begin
select @operation operation, @errorinfo errorinfo  
end
' 
if (dbo.mps_sys_checkkeywords('crm.beforemodify,crm.aftermodify',@bizid,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'

-- 对项目进行处理
declare @style int 
declare @finished int 
select @style=ord_style , @finished=ord_finished from '+@dbname+@tbname+' where ord_id=@ordid 
if (@style=0)
begin
if (@finished>=10 or @finished<=0) 
begin
delete mps_order where ord_bizid='+convert(varchar(10),@bizid)+' and ord_id=@ordid 
end  else 
begin
if (exists (select 1 from mps_order where ord_bizid='+convert(varchar(10),@bizid)+' and ord_id=@ordid)) 
begin
update a set 
a.ord_bcid=b.ord_bcid ,
a.ord_branch=b.ord_branch,
a.ord_createdpid=b.ord_createdpid,
a.ord_createdepart=b.ord_createdepart,
a.ord_dpid=b.ord_dpid,
a.ord_depart=b.ord_depart,
a.ord_createcuid=b.ord_createcuid,
a.ord_creator=b.ord_creator,
a.ord_cuid=b.ord_cuid,
a.ord_empname=b.ord_empname,
a.ord_date=b.ord_date,
a.ord_enddate=b.ord_enddate,
a.ord_finished=b.ord_finished,
a.ord_no=b.ord_no,
a.ord_title=b.ord_title,
a.ord_status=b.ord_status,
a.ord_type =b.ord_type
from mps_order a , '+@dbname+@tbname+' b
where a.ord_bizid='+convert(varchar(10),@bizid)+' and a.ord_id=b.ord_id and b.ord_id=@ordid 
end else
begin
insert into mps_order(
ord_bizid,
ord_id,
ord_cmpid,
ord_bcid,
ord_branch,
ord_createdpid,
ord_createdepart,
ord_dpid,
ord_depart,
ord_createcuid,
ord_creator,
ord_cuid,
ord_empname,
ord_date,
ord_enddate,
ord_finished,
ord_no,
ord_title,
ord_status,
ord_type )
select 
ord_bizid,
ord_id,
ord_cmpid,
ord_bcid,
ord_branch,
ord_createdpid,
ord_createdepart,
ord_dpid,
ord_depart,
ord_createcuid,
ord_creator,
ord_cuid,
ord_empname,
ord_date,
ord_enddate,
ord_finished,
ord_no,
ord_title,
ord_status,
ord_type
from '+@dbname+@tbname+' where ord_id=@ordid 
end 
end 
end 
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'',
''@cmpid int,@branchid int,@userdpid int,@useruid int,@userotid int,@jbid bigint,@ordid bigint,@sourcefield varchar(50),'+@procparam+'''' 
execute(@sql)
set nocount off 
end