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

-- 生成时间:2017-11-22 17:45:28 by ebs-core  
CREATE  procedure order_186_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 ,  
     @newordid bigint output  
  ) 
  as  
  begin  
  set nocount on  
  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='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 = '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 = '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 '没有找到合适的工作步骤' errorinfo  
  return   
  end  
  -- 首先要判断一下，当前用户是否可以新建  
  declare @createcuid int   
  set @createcuid=@useruid   
  declare @branch nvarchar(50)  
  declare @depart nvarchar(50)  
  declare @cuname nvarchar(50)   
  select @branchid=cu_bcid , @branch=cu_branch,@userdpid=cu_dpid,@depart=cu_depart,@cuname=cu_callname  
      from mps_login where cu_id=@createcuid  and cu_type=0  
  if (@branchid=0)  
     select @branch=cmp_grouptitle from ebs_kernel.dbo.pub_company where cmp_id=@cmpid    
  /*   
  if (not exists (select * from mps_biztype_users where (bp_type='new' or bp_type='audit') and bp_bizid=@bizid and bp_cuid=@createcuid))    
  begin    
  --  没有权限，那么就需要自动去寻找一个人    
  set @createcuid=0     
  select top 1 @createcuid=lgu_cuid from mps_login_role where lgu_bcid=@branchid and lgu_dpid=@userdpid   
  if (@createcuid=0) select top 1 @createcuid=lgu_cuid from mps_login_role where lgu_bcid=@branchid  
  if (@createcuid=0) select top 1 @createcuid=cu_id from mps_login where cu_cmpid=@cmpid  
  if (@createcuid=0) set @createcuid=@useruid  
  end     
  */
  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 order_186 
      where ord_cmpid=@cmpid and ord_bizid=@bizid and ord_createcuid=@createcuid and ord_finished=0  
  set @newordid = isnull(@newordid ,0)  
  declare @newordids table(newordid bigint)  
  if (@newordid>0) -- 不需要再新建，这时候，要清理上次创建好的数据  
  begin  
    if (@bsid>0) 
    begin 
      exec mps_delete_order @cmpid , @bizid , @newordid 
    set @newordid = 0 
    end else 
    begin 
    -- 将订单日期调整一下  
     update order_186 set ord_date=getdate() where ord_id=@newordid  
      if (@returndatasets = 0) 
        exec order_186_brief @cmpid, @newordid 
      if (@returndatasets = 2) 
        select @newordid newordid 
      return 
    end  
  end  
  begin  -- 开始新建一新记录  
  if (@bsid>0) -- 有数据源  
  begin  
     -- 这里要根据源来生成数据  这里面，要调整r,ford 信息及其它数据  
     -- 有可能生成多个订单，因此，每生成一个，就将它写入 @newordids 中  
     -- lxp   这里不用传@bsid  因为有可能有多个@bsid  
     exec order_186_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 order_186(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
  set @newordid=@@Identity   
  -- 生成订单编号（临时） 
  exec mps_bm_getorset @cmpid,'biz',@bizid,@newordid,@createcuid,@bizcode,2,@ordno output,1   
  update order_186 set ord_no = @ordno where ord_id = @newordid   
  insert into @newordids(newordid) values(@newordid)   
  end  
  end   
  declare @errorinfo nvarchar(4000)  
  declare @operation varchar(50) 
  set @operation = 'continue'  
  set @errorinfo = '' 
  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 order_186 , @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 = ''  
  
  --处理before事件 
  
  
  if (@operation='abort')   
  begin  
  delete order_186 where ord_id=@ordid  
  if (@errorinfo='') set @errorinfo='error'   
  select @errorinfo errorinfo   
  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 
  end  
  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 (@returndatasets=0) exec order_186_brief @cmpid , @ordid  
  if (@returndatasets=2) select @newordid newordid   
  set nocount off  
end