IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[order_196_newbiz_before]') AND type = 'P')
DROP PROCEDURE [dbo].[order_196_newbiz_before]
GO

-- 生成时间:2018-06-25 11:20:32 by ebs-core  
CREATE  procedure order_196_newbiz_before(
     @cmpid int,
     @branchid int,
     @userdpid int,
     @useruid int,
     @userotid int,
     @jbid bigint,
     @ordid bigint,
     @stype varchar(20),
     @sbizid int,
     @sord2tag int,
     @sord2id bigint,
     @sordidstr varchar(max),
     @extparam1 nvarchar(1000),
     @extparam2 nvarchar(1000),
     @extparam3 nvarchar(1000),
     @operation varchar(50) output,
     @errorinfo nvarchar(4000) output
  ) 
as  
begin  
set nocount on  
if ((@extparam1 is null) or (@extparam1='_none_'))  set @extparam1 = '' 
  if ((@extparam2 is null) or (@extparam2='_none_'))  set @extparam2 = '' 
  if ((@extparam3 is null) or (@extparam3='_none_'))  set @extparam3 = '' 
declare @ord2group2 nvarchar(50) 
set @ord2group2 = '' 
declare @sys_recid bigint 
set @sys_recid = 0 
declare @newordid bigint  
declare @sordids table(ordid bigint)  
 set @newordid = @ordid  
 declare @i int , @curInd int , @strlen int , @ordid_s varchar ( 20 )  
 declare @sordid bigint  
 set @sordid = 0  
 declare @nonedate datetime 
set @nonedate = CONVERT(datetime,'1910-1-1')
 if ( charindex ( ',' ,@sordidstr ) = 0 and @sordidstr > '' )  
 begin  
 set @sordid = convert ( bigint ,@sordidstr )  
 insert into @sordids ( ordid ) values ( @sordid )  
 end   
 else 
 begin  
 select @curInd = 1 , @strlen = LEN ( @sordidstr )  
 while ( @curInd <= @strlen )  
 begin  
 select @i = charindex ( ',' ,@sordidstr , @curInd )  
 if ( @i > 0 )  
 begin  
 select @ordid_s = SUBSTRING ( @sordidstr , @curInd , @i  - @curInd )  
 insert into @sordids ( ordid ) values ( convert ( bigint , @ordid_s ) )  
 set @curInd = @i  + 1  
 end  
 else if ( @curInd <= @strlen )  
 begin  
 select @ordid_s = SUBSTRING ( @sordidstr , @curInd , @strlen  - @curInd  + 1 )  
 insert into @sordids ( ordid ) values ( convert ( bigint , @ordid_s ) )  
 break  
 end  
 end  
 end 
 set @operation = 'continue'
 set @errorinfo = '' 
 --- 以下是预处理事件中的临时表
 
exec drop_temp_table '#detail1' 
select *,null recid into #detail1 from order_196_1 where ord2_ordid=@newordid  

 alter table #detail1 add default 0 for ord2_fbizid   alter table #detail1 add default 0 for ord2_ford2tag   alter table #detail1 add default 0 for ord2_order   alter table #detail1 add default 0 for ord2_rbizid   alter table #detail1 add default 0 for ord2_rord2tag   alter table #detail1 add default 0 for ord2_tag   alter table #detail1 add default '1900-1-1' for ord2_date   alter table #detail1 add default '1900-1-1' for ord2_updated   alter table #detail1 add default 0 for ord2_fatherord2id   alter table #detail1 add default 0 for ord2_ford2id   alter table #detail1 add default 0 for ord2_fordid   alter table #detail1 add default 0 for ord2_ordid   alter table #detail1 add default 0 for ord2_rord2id   alter table #detail1 add default 0 for ord2_rordid   alter table #detail1 add default '' for ord2_code   alter table #detail1 add default '' for ord2_ftype   alter table #detail1 add default '' for ord2_key   alter table #detail1 add default '' for ord2_rtype   alter table #detail1 add default '' for ord2_tags   alter table #detail1 add default '' for ord2_caption   alter table #detail1 add default '' for ord2_group   alter table #detail1 add default '' for ord2_jcjg   alter table #detail1 add default '' for ord2_jcnr   alter table #detail1 add default '' for ord2_status   alter table #detail1 add default '' for ord2_bz 
update #detail1 set recid=ord2_id 

exec drop_temp_table '#detail2' 
select *,null recid into #detail2 from order_196_2 where ord2_ordid=@newordid  

 alter table #detail2 add default 0 for ord2_fbizid   alter table #detail2 add default 0 for ord2_ford2tag   alter table #detail2 add default 0 for ord2_order   alter table #detail2 add default 0 for ord2_rbizid   alter table #detail2 add default 0 for ord2_rord2tag   alter table #detail2 add default 0 for ord2_tag   alter table #detail2 add default '1900-1-1' for ord2_date   alter table #detail2 add default '1900-1-1' for ord2_updated   alter table #detail2 add default 0 for ord2_fatherord2id   alter table #detail2 add default 0 for ord2_ford2id   alter table #detail2 add default 0 for ord2_fordid   alter table #detail2 add default 0 for ord2_ordid   alter table #detail2 add default 0 for ord2_rord2id   alter table #detail2 add default 0 for ord2_rordid   alter table #detail2 add default '' for ord2_code   alter table #detail2 add default '' for ord2_ftype   alter table #detail2 add default '' for ord2_key   alter table #detail2 add default '' for ord2_rtype   alter table #detail2 add default '' for ord2_tags   alter table #detail2 add default '' for ord2_bz   alter table #detail2 add default '' for ord2_caption   alter table #detail2 add default '' for ord2_group   alter table #detail2 add default '' for ord2_jcjg   alter table #detail2 add default '' for ord2_jcnr   alter table #detail2 add default '' for ord2_status 
update #detail2 set recid=ord2_id 

exec drop_temp_table '#detail3' 
select *,null recid into #detail3 from order_196_3 where ord2_ordid=@newordid  

 alter table #detail3 add default 0 for ord2_fbizid   alter table #detail3 add default 0 for ord2_ford2tag   alter table #detail3 add default 0 for ord2_order   alter table #detail3 add default 0 for ord2_rbizid   alter table #detail3 add default 0 for ord2_rord2tag   alter table #detail3 add default 0 for ord2_tag   alter table #detail3 add default '1900-1-1' for ord2_date   alter table #detail3 add default '1900-1-1' for ord2_updated   alter table #detail3 add default 0 for ord2_fatherord2id   alter table #detail3 add default 0 for ord2_ford2id   alter table #detail3 add default 0 for ord2_fordid   alter table #detail3 add default 0 for ord2_ordid   alter table #detail3 add default 0 for ord2_rord2id   alter table #detail3 add default 0 for ord2_rordid   alter table #detail3 add default '' for ord2_code   alter table #detail3 add default '' for ord2_ftype   alter table #detail3 add default '' for ord2_key   alter table #detail3 add default '' for ord2_rtype   alter table #detail3 add default '' for ord2_tags   alter table #detail3 add default '' for ord2_bz   alter table #detail3 add default '' for ord2_caption   alter table #detail3 add default '' for ord2_group   alter table #detail3 add default '' for ord2_jcjg   alter table #detail3 add default '' for ord2_jcnr   alter table #detail3 add default '' for ord2_status 
update #detail3 set recid=ord2_id 

 ---- 以下是开发人员写的 before new order 事件 --------------
 insert into #detail1 (ord2_jcnr) 
values 
('备用金：备用金无差异'),
('试用装：试用装齐全'),
('商品销售：无规则打折和违规办卡情况'),
('临期或过期产品：无有效12个月以内的彩护产品'),
('货品管理：货品交接表填写准确、有做数据分析表')

insert into #detail2 (ord2_jcnr) 
values 
('整体形象：地面、门头、天花板、展柜干净卫生'),
('工具卫生：美容床、毛巾、美容化妆工具干净卫生'),
('物品摆放：化妆包摆放干净整齐'),
('电视播放：营业时间电视正常播放'),
('产品陈列：按标准陈列，陈列整齐美观，无损坏性产品、价签完整'),
('个人形象：工作服、妆面、发型合格'),
('早晚会：认真执行店铺早、晚会制度'),
('宿舍：员工宿舍是否干净整洁、无外宿人员'),
('顾客档案：及时登记顾客档案、VIP信息、定期回访'),
('人员培养：认真执行新人整月培训表、执行快速成为店长学习计划')

insert into #detail3 (ord2_jcnr) 
values 
('产品检查：无仟丝缘渠道以外产品存放及销售，无仟丝缘体系的服务。'),
('人事系统：人事系统人员档案准确及时完整'),
('重点工作执行：顾客评分表按要求执行检查')
 ---- 以上是开发人员写的 before new order 事件 --------------
 
 if (@operation='continue') 
 begin
 set @nonedate = @nonedate 
 
 set @ord2group2='' 
 select top 1 @ord2group2=ord2_group from #detail1 where ord2_group>'' 
 set @ord2group2 = isnull(@ord2group2 , '') 

 insert into order_196_1(ord2_date,ord2_updated,ord2_tag,ord2_group,ord2_ordid,ord2_fbizid,ord2_ford2tag,ord2_order,ord2_rbizid,ord2_rord2tag,ord2_fatherord2id,ord2_ford2id,ord2_fordid,ord2_rord2id,ord2_rordid,ord2_code,ord2_ftype,ord2_key,ord2_rtype,ord2_tags,ord2_caption,ord2_jcjg,ord2_jcnr,ord2_status,ord2_bz) 
 select case when new.ord2_date>getdate()-40000 then new.ord2_date else getdate() end ,getdate(),1,
 case when new.ord2_group>'' then new.ord2_group else @ord2group2 end , @ordid,
new.ord2_fbizid,new.ord2_ford2tag,new.ord2_order,new.ord2_rbizid,new.ord2_rord2tag,new.ord2_fatherord2id,new.ord2_ford2id,new.ord2_fordid,new.ord2_rord2id,new.ord2_rordid,new.ord2_code,new.ord2_ftype,new.ord2_key,new.ord2_rtype,new.ord2_tags,new.ord2_caption,new.ord2_jcjg,new.ord2_jcnr,new.ord2_status,new.ord2_bz
 from #detail1 new where recid is null 

 set @ord2group2='' 
 select top 1 @ord2group2=ord2_group from #detail2 where ord2_group>'' 
 set @ord2group2 = isnull(@ord2group2 , '') 

 insert into order_196_2(ord2_date,ord2_updated,ord2_tag,ord2_group,ord2_ordid,ord2_fbizid,ord2_ford2tag,ord2_order,ord2_rbizid,ord2_rord2tag,ord2_fatherord2id,ord2_ford2id,ord2_fordid,ord2_rord2id,ord2_rordid,ord2_code,ord2_ftype,ord2_key,ord2_rtype,ord2_tags,ord2_bz,ord2_caption,ord2_jcjg,ord2_jcnr,ord2_status) 
 select case when new.ord2_date>getdate()-40000 then new.ord2_date else getdate() end ,getdate(),2,
 case when new.ord2_group>'' then new.ord2_group else @ord2group2 end , @ordid,
new.ord2_fbizid,new.ord2_ford2tag,new.ord2_order,new.ord2_rbizid,new.ord2_rord2tag,new.ord2_fatherord2id,new.ord2_ford2id,new.ord2_fordid,new.ord2_rord2id,new.ord2_rordid,new.ord2_code,new.ord2_ftype,new.ord2_key,new.ord2_rtype,new.ord2_tags,new.ord2_bz,new.ord2_caption,new.ord2_jcjg,new.ord2_jcnr,new.ord2_status
 from #detail2 new where recid is null 

 set @ord2group2='' 
 select top 1 @ord2group2=ord2_group from #detail3 where ord2_group>'' 
 set @ord2group2 = isnull(@ord2group2 , '') 

 insert into order_196_3(ord2_date,ord2_updated,ord2_tag,ord2_group,ord2_ordid,ord2_fbizid,ord2_ford2tag,ord2_order,ord2_rbizid,ord2_rord2tag,ord2_fatherord2id,ord2_ford2id,ord2_fordid,ord2_rord2id,ord2_rordid,ord2_code,ord2_ftype,ord2_key,ord2_rtype,ord2_tags,ord2_bz,ord2_caption,ord2_jcjg,ord2_jcnr,ord2_status) 
 select case when new.ord2_date>getdate()-40000 then new.ord2_date else getdate() end ,getdate(),3,
 case when new.ord2_group>'' then new.ord2_group else @ord2group2 end , @ordid,
new.ord2_fbizid,new.ord2_ford2tag,new.ord2_order,new.ord2_rbizid,new.ord2_rord2tag,new.ord2_fatherord2id,new.ord2_ford2id,new.ord2_fordid,new.ord2_rord2id,new.ord2_rordid,new.ord2_code,new.ord2_ftype,new.ord2_key,new.ord2_rtype,new.ord2_tags,new.ord2_bz,new.ord2_caption,new.ord2_jcjg,new.ord2_jcnr,new.ord2_status
 from #detail3 new where recid is null 

end 
--- 清除临时表 
exec drop_temp_table '#detail1'
exec drop_temp_table '#detail2'
exec drop_temp_table '#detail3'

set nocount off 
end