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

create procedure order_243_newbizbysrc (
    @cmpid int , 
    @bcid int ,
    @branch nvarchar(50),
    @dpid int ,
    @depart nvarchar(50),
    @createcuid int ,
    @cuname nvarchar(50),
    @jbid bigint ,
    @bizstyle int,
    @biztitle nvarchar(100),  
    @bizcode nvarchar(100),
    @stype varchar(10) ,
    @sbizid int ,
    @sordid bigint ,
    @sord2tag int ,
    @sord2id bigint, 
    @newordids varchar(2000) output 
   ) 
  as 
  begin 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
set @jbid = isnull(@jbid,0) 
if (@jbid=-999999999) set @jbid=0
declare @ordno varchar(50)
  declare @maxordid bigint 
  declare @ord table(ordid bigint , ordno varchar(50)) 
  declare @newcount int 
  set @newcount = 0 
  declare @newordid bigint
  
select @maxordid = max(ord_id) from order_243 with(nolock)  
set @maxordid = isnull(@maxordid , 0 ) 

if (  @stype='biz' and @sbizid=247 and ((@sord2tag>0 and @sord2tag=2) or (@sord2tag=0)))  
begin 
 insert into order_243(ord_createcuid,ord_creator,ord_syrcuid,ord_whry,ord_xqlx,ord_xqms,ord_xwwcsj,ord_zdzx,ord_cmpid,ord_finished,ord_fjbid,ord_type,ord_style,ord_date,ord_updated,ord_bizid,ord_bcid,ord_branch,ord_createdpid,ord_dpid,ord_cuid,ord_createdepart,ord_depart,ord_empname,ord_ftype,ord_fbizid,ord_fordid,ord_ford2tag,ord_ford2id,ord_rtype,ord_rbizid,ord_rordid,ord_rord2tag,ord_rord2id) 
select new.ord_createcuid,new.ord_creator,new2.ord2_zrrcuid,new2.ord2_zrr,'面谈任务','面谈表任务内容：'+new2.ord2_rwnr,new2.ord2_xwwcsj,1,@cmpid ,1 ,@jbid ,@biztitle ,@bizstyle ,getdate(),getdate(),243,@bcid ,@branch ,@dpid ,@dpid ,@createcuid ,@depart ,@depart ,@cuname ,'biz',new.ord_bizid ,new.ord_id ,2,new2.ord2_tag ,case when new2.ord2_fordid=0 then 'biz' when new2.ord2_rordid>0 then new2.ord2_rtype else new2.ord2_ftype end ,case when new2.ord2_fordid=0 then new.ord_bizid when new2.ord2_rordid>0 then new2.ord2_rbizid  else new2.ord2_fbizid end ,case when new2.ord2_fordid=0 then new.ord_id when new2.ord2_rordid>0 then new2.ord2_rordid else new2.ord2_fordid end ,case when new2.ord2_fordid=0 then 2 when new2.ord2_rordid>0 then new2.ord2_rord2tag else new2.ord2_ford2tag end ,case when new2.ord2_fordid=0 then new2.ord2_id when new2.ord2_rordid>0 then new2.ord2_rord2id else new2.ord2_ford2id end 
  from order_247 new with(nolock), order_247_2 new2 with(nolock) where new.ord_id=@sordid and new.ord_id=new2.ord2_ordid and new2.ord2_tag=2 and ((@sord2id>0 and new2.ord2_id=@sord2id) or (@sord2id=0))  and (
new2.ord2_rwnr <> '' and new2.ord2_zrr <> '' and new2.ord2_xwwcsj <> ''
) order by ord2_tag ,ord2_order , ord2_id 
 
end

declare @myt int 
set @myt = 1

if (  @stype='biz' and @sbizid=249 and ((@sord2tag>0 and @sord2tag=2) or (@sord2tag=0)))  
begin 
 insert into order_243(ord_createcuid,ord_creator,ord_syrcuid,ord_whry,ord_xqlx,ord_xqms,ord_xwwcsj,ord_zdzx,ord_cmpid,ord_finished,ord_fjbid,ord_type,ord_style,ord_date,ord_updated,ord_bizid,ord_bcid,ord_branch,ord_createdpid,ord_dpid,ord_cuid,ord_createdepart,ord_depart,ord_empname,ord_ftype,ord_fbizid,ord_fordid,ord_ford2tag,ord_ford2id,ord_rtype,ord_rbizid,ord_rordid,ord_rord2tag,ord_rord2id) 
select new.ord_createcuid,new.ord_creator,ordp_cuid,cu_callname,'关键动作执行',new2.ord2_xqms,new2.ord2_xwwcsj,1,@cmpid ,1 ,@jbid ,@biztitle ,@bizstyle ,getdate(),getdate(),243,@bcid ,@branch ,@dpid ,@dpid ,@createcuid ,@depart ,@depart ,@cuname ,'biz',new.ord_bizid ,new.ord_id ,2,new2.ord2_tag ,case when new2.ord2_fordid=0 then 'biz' when new2.ord2_rordid>0 then new2.ord2_rtype else new2.ord2_ftype end ,case when new2.ord2_fordid=0 then new.ord_bizid when new2.ord2_rordid>0 then new2.ord2_rbizid  else new2.ord2_fbizid end ,case when new2.ord2_fordid=0 then new.ord_id when new2.ord2_rordid>0 then new2.ord2_rordid else new2.ord2_fordid end ,case when new2.ord2_fordid=0 then 2 when new2.ord2_rordid>0 then new2.ord2_rord2tag else new2.ord2_ford2tag end ,case when new2.ord2_fordid=0 then new2.ord2_id when new2.ord2_rordid>0 then new2.ord2_rord2id else new2.ord2_ford2id end 
  from order_249 new with(nolock), order_249_2 new2 with(nolock) , mps_login with(nolock)  , order_249_users with(nolock)  where new.ord_id=@sordid and new.ord_id=new2.ord2_ordid and new2.ord2_tag=2 and ((@sord2id>0 and new2.ord2_id=@sord2id) or (@sord2id=0))  and (
ordp_cuid=cu_id and ordp_ordid=ord_id and ordp_type = '_'+convert(varchar(10),ord2_id)
) order by ord2_tag ,ord2_order , ord2_id 
 
end

if (  @stype='biz' and @sbizid=255 and ((@sord2tag>0 and @sord2tag=2) or (@sord2tag=0)))  
begin 
 insert into order_243(ord_createcuid,ord_creator,ord_syrcuid,ord_whry,ord_xqlx,ord_xqms,ord_xwwcsj,ord_zdzx,ord_cmpid,ord_finished,ord_fjbid,ord_type,ord_style,ord_date,ord_updated,ord_bizid,ord_bcid,ord_branch,ord_createdpid,ord_dpid,ord_cuid,ord_createdepart,ord_depart,ord_empname,ord_ftype,ord_fbizid,ord_fordid,ord_ford2tag,ord_ford2id,ord_rtype,ord_rbizid,ord_rordid,ord_rord2tag,ord_rord2id) 
select new.ord_xzmddzid,new.ord_mddz,new.ord_createcuid,new.ord_creator,'派工维护','维护专员巡检中发现如下问题'+new2.ord2_xjwt+'详情请点击蓝色字体任务描述,进行查看',new2.ord2_xwwcsj,1,@cmpid ,1 ,@jbid ,@biztitle ,@bizstyle ,getdate(),getdate(),243,@bcid ,@branch ,@dpid ,@dpid ,@createcuid ,@depart ,@depart ,@cuname ,'biz',new.ord_bizid ,new.ord_id ,2,new2.ord2_tag ,case when new2.ord2_fordid=0 then 'biz' when new2.ord2_rordid>0 then new2.ord2_rtype else new2.ord2_ftype end ,case when new2.ord2_fordid=0 then new.ord_bizid when new2.ord2_rordid>0 then new2.ord2_rbizid  else new2.ord2_fbizid end ,case when new2.ord2_fordid=0 then new.ord_id when new2.ord2_rordid>0 then new2.ord2_rordid else new2.ord2_fordid end ,case when new2.ord2_fordid=0 then 2 when new2.ord2_rordid>0 then new2.ord2_rord2tag else new2.ord2_ford2tag end ,case when new2.ord2_fordid=0 then new2.ord2_id when new2.ord2_rordid>0 then new2.ord2_rord2id else new2.ord2_ford2id end 
  from order_255 new with(nolock), order_255_2 new2 with(nolock) where new.ord_id=@sordid and new.ord_id=new2.ord2_ordid and new2.ord2_tag=2 and ((@sord2id>0 and new2.ord2_id=@sord2id) or (@sord2id=0))  and (
new2.ord2_jcqk <> '合格'
) order by ord2_tag ,ord2_order , ord2_id 
 
end

if (  @stype='biz' and @sbizid=275 and ((@sord2tag>0 and @sord2tag=2) or (@sord2tag=0)))  
begin 
 insert into order_243(ord_createcuid,ord_creator,ord_syrcuid,ord_whry,ord_xqlx,ord_xqms,ord_xwwcsj,ord_zdzx,ord_cmpid,ord_finished,ord_fjbid,ord_type,ord_style,ord_date,ord_updated,ord_bizid,ord_bcid,ord_branch,ord_createdpid,ord_dpid,ord_cuid,ord_createdepart,ord_depart,ord_empname,ord_ftype,ord_fbizid,ord_fordid,ord_ford2tag,ord_ford2id,ord_rtype,ord_rbizid,ord_rordid,ord_rord2tag,ord_rord2id) 
select new.ord_createcuid,new.ord_creator,new2.ord2_syrcuid,new2.ord2_dzr,'关键动作执行','工作事项:'+new2.ord2_gzsx+char(10)+'结题变化:'+new2.ord2_jtbh,new2.ord2_xwwcsj,1,@cmpid ,1 ,@jbid ,@biztitle ,@bizstyle ,getdate(),getdate(),243,@bcid ,@branch ,@dpid ,@dpid ,@createcuid ,@depart ,@depart ,@cuname ,'biz',new.ord_bizid ,new.ord_id ,2,new2.ord2_tag ,case when new2.ord2_fordid=0 then 'biz' when new2.ord2_rordid>0 then new2.ord2_rtype else new2.ord2_ftype end ,case when new2.ord2_fordid=0 then new.ord_bizid when new2.ord2_rordid>0 then new2.ord2_rbizid  else new2.ord2_fbizid end ,case when new2.ord2_fordid=0 then new.ord_id when new2.ord2_rordid>0 then new2.ord2_rordid else new2.ord2_fordid end ,case when new2.ord2_fordid=0 then 2 when new2.ord2_rordid>0 then new2.ord2_rord2tag else new2.ord2_ford2tag end ,case when new2.ord2_fordid=0 then new2.ord2_id when new2.ord2_rordid>0 then new2.ord2_rord2id else new2.ord2_ford2id end 
  from order_275 new with(nolock), order_275_2 new2 with(nolock) where new.ord_id=@sordid and new.ord_id=new2.ord2_ordid and new2.ord2_tag=2 and ((@sord2id>0 and new2.ord2_id=@sord2id) or (@sord2id=0))  order by ord2_tag ,ord2_order , ord2_id 
 
end

insert into @ord(ordid,ordno) select ord_id,'' from order_243 with(nolock) 
where ord_cmpid=@cmpid and ord_id>@maxordid and ord_bizid=243 and ord_no='' and ord_finished=1    and ord_fordid=@sordid 
declare status11 cursor LOCAL FORWARD_ONLY READ_ONLY for
select ordid from   @ord  order by ordid 
open status11
FETCH NEXT FROM status11 INTO @newordid 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @newcount = @newcount + 1
exec mps_bm_getorset @cmpid,'biz',243,@newordid,@createcuid,@bizcode,2,@ordno output,1 
update @ord set ordno=@ordno where ordid=@newordid 
FETCH NEXT FROM status11 INTO @newordid 
END
CLOSE status11
DEALLOCATE status11   
update order_243 with(rowlock) set ord_no=ordno , ord_finished=1 from @ord where ord_id=ordid 
select top 1 @newordid=ordid from @ord  
set @newordid = isnull(@newordid,0) 
set @newordids=null 
select @newordids=ISNULL(@newordids+',','')+convert(varchar(16),ordid) from @ord 
set @newordids = isnull(@newordids,'')  
end