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

-- 生成时间:2018-04-25 00:28:24 by ebs-core  
CREATE  procedure order_4_newbiz(
     @cmpid int, 
     @branchid int, 
     @userdpid int, 
     @userotid int, 
     @useruid int, 
     @jbid bigint, 
     @bizid int,  
     @stype varchar(10) , 
     @sbizid int,  
     @sordids varchar(max) ,  
     @sord2tag int,  
     @sord2id bigint, 
     @returndatasets int ,  
     @extparam1 nvarchar(1000),
     @extparam2 nvarchar(1000),
     @extparam3 nvarchar(1000),
     @newordid bigint output  
  ) 
  as  
  begin  
  set nocount on   
  declare @otlevel int
declare @managelevel int
declare @professionlevel int 
declare @bcid int 
declare @dpid int 
declare @otid int 
declare @roleid int
declare @branch nvarchar(50)  
  declare @depart nvarchar(50)  
  declare @cuname nvarchar(50)   
  
  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 = '' 
  
  
  set @jbid = ISNULL(@jbid, 0)  
  if (@jbid = -999999999) set @jbid = 0  
  --- 判断一下，可不可以建  
  declare @srcs varchar(8000) 
  declare @sys_ord2group nvarchar(50)
  set @newordid = 0  
  declare @wrkid int,@sys_recid bigint  
  select top 1 @wrkid = wrk_id from mps_flowwork,mps_bizflow  
  where flw_bizid=@bizid and flw_title='缺省流程' and wrk_bizid=@bizid and wrk_flwid=flw_id and wrk_shape like 'begin%'  
  set @wrkid=isnull(@wrkid,0)   
  if (@wrkid=0)   
  select top 1 @wrkid = wrk_id from mps_flowwork,mps_bizflow   
  where flw_bizid=@bizid and flw_title='缺省流程' and wrk_bizid=@bizid and wrk_flwid=flw_id order by wrk_id   
  if(@wrkid = 0) 
  begin 
    select top 1 @wrkid = wrk_id from mps_flowwork, mps_bizflow 
    where flw_bizid = @bizid and flw_title = '' and wrk_bizid = @bizid and wrk_flwid = flw_id and wrk_shape like 'begin%' 
    set @wrkid = isnull(@wrkid, 0) 
  end 
  if (@wrkid = 0) 
    select top 1 @wrkid = wrk_id from mps_flowwork, mps_bizflow 
    where flw_bizid = @bizid and flw_title = '' and wrk_bizid = @bizid and wrk_flwid = flw_id order by wrk_id 
  if (@wrkid = 0) 
  begin 
    select top 1 @wrkid = wrk_id from mps_flowwork, mps_bizflow 
    where flw_bizid = @bizid and   wrk_bizid = @bizid and wrk_flwid = flw_id and wrk_shape like 'begin%' 
    set @wrkid = isnull(@wrkid, 0) 
  end 
  if (@wrkid = 0) 
    select top 1 @wrkid = wrk_id from mps_flowwork, mps_bizflow 
    where flw_bizid = @bizid   and wrk_bizid = @bizid and wrk_flwid = flw_id order by wrk_id 
  if (@wrkid=0)   
  begin  
  select 0 newordid , '没有找到合适的工作步骤' errorinfo  
  return   
  end  
  -- 计算新建人有哪些
  
declare @users table(pptype varchar(30), ppgroup int , useruid int) 
insert into @users(pptype,ppgroup,useruid)
select bp_type , 0 , bp_cuid 
from mps_biztype_users where bp_type='biznew' and bp_bizid=@bizid and bp_cuid=@useruid 

if (not exists (select * from @users)) 
begin
insert into @users(pptype,ppgroup,useruid)
select top 1 bp_type , 0 , bp_cuid 
from mps_biztype_users, mps_login 
where bp_type='biznew' and bp_bizid=@bizid and bp_cuid=cu_id and cu_bcid=@branchid 
end
if (not exists (select * from @users)) 
begin
insert into @users(pptype,ppgroup,useruid)
select top 1 bp_type , 0 , bp_cuid 
from mps_biztype_users 
where bp_type='biznew' and bp_bizid=@bizid  
end
  
  -- 首先要判断一下，当前用户是否可以新建  
  
  declare @createcuid int   
  set @createcuid=@useruid   
  if (@sordids<>'') 
  begin
  select top 1 @createcuid = useruid from @users 
  set @createcuid=isnull(@createcuid,0) 
  if (@createcuid=0) set @createcuid=@useruid 
  end else 
  begin
  if (not exists (select * from @users where useruid=@createcuid)) 
  begin
set @newordid = 0 
select 0 newordid , '找不到新单人员，创建失败'  errorinfo 
select 'fail','找不到新单人员，创建失败' 
return 
  end 
  end
  
  set @dpid=0 
  select top 1 @bcid=cu_bcid,@dpid=cu_dpid,@otid=cu_otid,@roleid=cu_function ,
  @branch=cu_branch , @depart=cu_depart , @cuname=cu_callname,  @otlevel=cu_otlevel ,@managelevel=cu_managelevel , @professionlevel=cu_professionlevel 
from mps_login where cu_id=@createcuid   

if (@dpid=0) 
select top 1 @bcid=lgu_bcid,@dpid=lgu_dpid,@otid=lgu_otid,@roleid=lgu_function ,
  @branch=lgu_branch , @depart=lgu_depart , @cuname=lgu_name,  @otlevel=lgu_otlevel ,@managelevel=lgu_managelevel , @professionlevel=lgu_professionlevel 
from mps_login_role where lgu_cuid=@createcuid  and   lgu_bcid=@branchid 

if (@dpid=0) 
select top 1 @bcid=lgu_bcid,@dpid=lgu_dpid,@otid=lgu_otid,@roleid=lgu_function ,
  @branch=lgu_branch , @depart=lgu_depart , @cuname=lgu_name,  @otlevel=lgu_otlevel ,@managelevel=lgu_managelevel , @professionlevel=lgu_professionlevel 
from mps_login_role where lgu_cuid=@createcuid   

  set @userdpid = @dpid 
  set @branchid = @bcid
  
  
  declare @biztitle nvarchar(100)     
  declare @bizstyle int     
  declare @bizcode nvarchar(100)    
  select @biztitle=biz_title , @bizstyle=biz_formstyle , @bizcode=biz_code  from mps_biztype where  biz_id=@bizid    
  declare @ordid bigint     
  declare @ordbcid int 
  declare @rtype varchar(10)    
  declare @rordid bigint     
  declare @ordno varchar(50)     
  declare @rbizid int     
  declare @rord2tag int     
  declare @rord2id bigint 
  declare @ftype varchar(10) 
  declare @fordid bigint  
  declare @fbizid int 
  declare @ford2tag int  
  declare @ford2id bigint  
  declare @ordtype nvarchar(50)  
  declare @ordtitle nvarchar(100) 
  declare @orddate datetime  
  declare @ordcreator nvarchar(50)  
  -- 根据数据源来生成数据  
  declare @bsid  int    
  declare @newordids_str varchar(max)  
  set @bsid=0  
  declare @sordid bigint  
  set @sordid=0  
  if (charindex(',',@sordids)=0 and @sordids>'') 
  begin  
  set @sordid = convert(bigint , @sordids)  
  end  
  if (@sbizid>0 and @sordid>0) 
  begin  
  select top 1 @bsid=bs_id  
          from mps_biztype_comefrom  
          where bs_bizid=@bizid and bs_sbizid=@sbizid and bs_stype=@stype and  
          (exists (select * from mps_biztype_comefrom_fieldmap  where bsf_bsid=bs_id and bsf_use=0))    
  set @bsid=isnull(@bsid,0) 
  end  
  -- 首先判断，是不是需要创建  这里要注意性能问题，建议ord_finished要加入索引  
  select top 1 @newordid=ord_id  
      from ebs20_order.dbo.order_4 
      where ord_cmpid=@cmpid and ord_bizid=@bizid and ord_createdpid=@dpid and ord_createcuid=@createcuid and (ord_finished=0 or ord_no like '%临%')  
  set @newordid = isnull(@newordid ,0)  
  declare @newordids table(newordid bigint)  
  if (@newordid>0) -- 不需要再新建，这时候，要清理上次创建好的数据  
  begin  
    if (@bsid>0 or @extparam1>'' or @extparam2>'' or @extparam3>'') 
    begin 
      exec mps_delete_order @cmpid ,@useruid, @bizid , @newordid ,'由系统自动清理未提交的草稿业务单'
    set @newordid = 0 
    end else 
    begin 
    -- 这里加一个逻辑，如果没有工作步骤，则无效
    if (not exists (select 1 from mps_job where jb_cmpid=@cmpid and jb_crmid=@bizid and jb_ordid=@newordid))  
    begin
    exec mps_delete_order @cmpid ,@useruid, @bizid , @newordid ,'由系统自动清理未提交的草稿业务单'
    set @newordid = 0 
    end else
    begin
    -- 将订单日期调整一下  
     update ebs20_order.dbo.order_4 set ord_date=getdate() where ord_id=@newordid  
    end 
    end  
  end  
  if (@newordid=0)
  begin  -- 开始新建一新记录  
  if (@bsid>0) -- 有数据源  
  begin  
     -- 这里要根据源来生成数据  这里面，要调整r,ford 信息及其它数据  
     -- 有可能生成多个订单，因此，每生成一个，就将它写入 @newordids 中  
     -- lxp   这里不用传@bsid  因为有可能有多个@bsid  
     exec order_4_newbizbysrc @cmpid,@branchid,@branch,@userdpid,@depart,@createcuid,@cuname,@jbid,@bizstyle,@biztitle,@bizcode,@stype,@sbizid,@sordid,@sord2tag,@sord2id,@srcs output   
     insert into @newordids select * from dbo.fn_split_long(@srcs)  
  end else  
  begin  
  insert into ebs20_order.dbo.order_4(ord_cmpid,ord_style,ord_bcid,ord_branch,ord_bizid,ord_type,ord_date,ord_updated,    
  ord_createdpid,ord_createcuid,ord_createdepart,ord_creator,ord_finished,ord_fjbid,ord_dpid,ord_cuid,
  ord_depart,ord_empname) 
  select @cmpid,@bizstyle,@branchid,@branch,@bizid,@biztitle,getdate(),getdate(),   
     @userdpid,@createcuid,@depart,@cuname ,0  , @jbid ,@userdpid,@createcuid,@depart,@cuname
  select top 1 @newordid = ord_id from ebs20_order.dbo.order_4 where ord_cmpid=@cmpid and ord_bcid=@branchid and ord_createcuid=@createcuid 
  and ord_fjbid=@jbid order by ord_id desc  
  --set @newordid=@@Identity   
  set @newordid = isnull(@newordid,0) 
  -- 生成订单编号（临时） 
  exec mps_bm_getorset @cmpid,'biz',@bizid,@newordid,@createcuid,@bizcode,2,@ordno output,1   
  update ebs20_order.dbo.order_4 set ord_no = @ordno where ord_id = @newordid   
  insert into @newordids(newordid) values(@newordid)   
  end 
  -- 写同步  取消了这段程序，增加了insert 觖发
  -- insert into mps_sync(sk_type,sk_bizid,sk_tag2,sk_operation,sk_objid) 
   -- select 1,ord_bizid,0,1,ord_id from ebs20_order.dbo.order_4, @newordids where ord_id=newordid 
  --- 这创建成功，将建单人写入权限表
  
  -- why here otid=-1 , it's a secret , I don't know yet 
  insert into ebs20_order.dbo.order_4_users(ordp_type,ordp_ordid,ordp_cuid,ordp_otid) 
  select distinct 'bizview',newordid,ord_createcuid,-1 from ebs20_order.dbo.order_4,@newordids where ord_id=newordid  
  end   else
  begin
  insert into @newordids(newordid) values(@newordid)   
  end 
  declare @errorinfo nvarchar(4000)  
  declare @operation varchar(50) 
  set @operation = 'continue'  
  set @errorinfo = '' 
  declare @hasrelation int 
  set @hasrelation =  1 
  set @hasrelation=dbo.checkprocedure('order_4_calc_relation')  
  declare newords cursor LOCAL  FORWARD_ONLY READ_ONLY  
  for select  ord_id,ord_rtype,ord_rordid,ord_rbizid,ord_rord2tag,ord_rord2id,ord_ftype,ord_fordid,ord_fbizid,ord_ford2tag,ord_ford2id, 
  ord_type,ord_no,ord_title,ord_date,ord_creator , ord_bcid 
  from ebs20_order.dbo.order_4 , @newordids  
  where ord_id=newordid  
  open newords  
  FETCH NEXT FROM newords INTO   @ordid , @rtype,@rordid,@rbizid,@rord2tag,@rord2id,@ftype,@fordid,@fbizid,@ford2tag,@ford2id,@ordtype,@ordno,@ordtitle,@orddate,@ordcreator ,@ordbcid  
  WHILE (@@FETCH_STATUS = 0)  
  BEGIN  
  set @operation = 'continue'  
  set @errorinfo = ''  
  set @newordid=@ordid 
  --处理before事件 
  
  
  if (@operation='abort')   
  begin  
  delete ebs20_order.dbo.order_4 where ord_id=@ordid  
  if (@errorinfo='') set @errorinfo='error'   
  select  0 newordid,  @errorinfo errorinfo   
  set @ordid=0
  set @newordid = 0   
  end else   
  begin  
  -- 执行业务单创建建后SQL  
  
   exec mps_new_work @cmpid ,@createcuid,@bizid  ,@ordbcid, @ordid ,@ordtype ,@ordno,@ordtitle,@orddate,@ordcreator, '', 
               @ftype, @fbizid, @fordid, @ford2tag, @ford2id, @rtype, @rbizid, @rordid, @rord2tag, @rord2id, @wrkid, 0 , 0 
  end  
  if (@fordid>0 and @hasrelation=0) exec order_4_calc_relation @cmpid,@ordid 
  FETCH NEXT FROM newords INTO   @ordid , @rtype,@rordid,@rbizid,@rord2tag,@rord2id,@ftype,@fordid,@fbizid,@ford2tag,@ford2id,@ordtype,@ordno,@ordtitle,@orddate,@ordcreator ,@ordbcid  
  END  
  CLOSE newords  
  DEALLOCATE newords   
   -- 返回业务数据  
  --set @newordid=@ordid  
  if (@newordid>0) 
  begin
if (dbo.checkprocedure('order_4_set_subform')=0)  
begin
declare @sql nvarchar(4000) 
  set @sql='exec order_4_set_subform @newordid '
  exec sp_executesql @sql, N'@newordid bigint', @newordid 
  end
  end 
  if (@returndatasets=0) exec order_4_brief @cmpid ,@useruid, @newordid  
  if (@returndatasets=2) select @newordid newordid   
  set nocount off  
end