IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[userfunc_744_importdata]') AND type = 'P')
DROP PROCEDURE [dbo].[userfunc_744_importdata]
GO
CREATE PROCEDURE userfunc_744_importdata(@cmpid int,   @branchid int,  @userdpid int,  @useruid int,   @userotid int,  @usersecurity int,@ctgid int,@ctgid2 int,@objid bigint,@bcid1 int,@dpid1 int,@cuid1 int,@ordid1 int,@type varchar(100))
as
begin


set nocount on 
declare @objtype varchar(20)='biz' 
declare @ordid bigint 
set @ordid=@objid 
declare @bizid int 
set @bizid = @ctgid 
declare @nonedate datetime 
set @nonedate = CONVERT(datetime,'1910-1-1')
declare @ord2group2 nvarchar(50) 
set @ord2group2 = '' 
declare @sys_recid bigint 
set @sys_recid = 0 


    SET NOCOUNT on ;
    
    delete from order_266_2 where ord2_ordid = @ordid1
    delete from order_266_3 where ord2_ordid = @ordid1
    declare @temp table  (dp nvarchar(100),info nvarchar(100))
    declare @t1 table (str nvarchar(max))
    declare @fdpid int = 0
    declare @mxid table (ordid bigint) 
    select @fdpid = isnull(dp_fdpid,0) from mps_depart where dp_id = @dpid1 and dp_bcid = @bcid1 
    if(@type ='维修机')
    begin
        
        update order_266 set ord_hplx = '维修机' where ord_id = @ordid1
        if(@bcid1 = 20)
        begin
            if((@fdpid = 3517 and @dpid1 = 3739) or @fdpid = 3756 )
            begin
                insert into @mxid
                select ord_id from order_260 
                where ord_bcid = 20 
                    and ord_createdpid in (select dp_id from mps_depart where (CHARINDEX('仁寿',dp_depart)>0 or CHARINDEX('仁寿',dp_location)>0) and dp_id <> 3756)
                    and ord_dqzt = '待门店返厂装箱'
                    and ord_no not like '%临%' 
                    and ord_wxlx = '返厂维修'
            end
            else
            begin
                insert into @mxid
                select ord_id from order_260 
                where ord_bcid = 20 
                    and ord_createdpid not in (select dp_id from mps_depart where (CHARINDEX('仁寿',dp_depart)>0 or CHARINDEX('仁寿',dp_location)>0) and dp_id <> 3756)
                    and ord_dqzt = '待门店返厂装箱'
                    and ord_no not like '%临%' 
                    and ord_wxlx = '返厂维修'
            end
        end
        else
        begin
            insert into @mxid
            select ord_id from order_260
            where ord_bcid = @bcid1
            and ord_dqzt = '待门店返厂装箱'
            and ord_no not like '%临%'
            and ord_wxlx = '返厂维修'
        end
        select count(ordid) as count1 from @mxid
        
        
        insert into order_266_2 (
            ord2_bxbl,
            ord2_jjmd,
            ord2_jjr,
            ord2_jjrq,
            ord2_pp,
            ord2_sjfj,
            ord2_sxlx,
            ord2_xh,
            ord2_ys,
            ord2_zdimei,
            ord2_fbizid,
            ord2_mdordid,
            ord2_ordid,
            ord2_tag,
            ord2_bz)
        select  
            ord_fwlx,
            ord_createdepart,
            ord_creator,
            ord_date,
            ord_zdpp,
            ord_sxfj,
            case ord_zdlx when '手机' then '手机' else '其他' end,
            ord_zdxh,
            ord_ys,
            ord_imei,
            ord_bizid,
            ord_id,
            @ordid1,
            2,
            ord_gzfl1j
        from order_260 where ord_id in (select ordid from @mxid)
        order by ord_zdpp

        
        insert into @temp
        select ord2_jjmd,ord2_pp+'('+cast(count(*) as nvarchar(50))+'个)' as co 
        from order_266_2 where ord2_ordid =@ordid1 group by ord2_jjmd,ord2_pp
        insert into @t1
        select '【'+a.dp+'】:'+(stuff((select ','+info from @temp where dp = a.dp for xml path('')),1,1,'')) from @temp a group by dp
        update order_266 set ord_gl = (select top 1 stuff((select ';'+str from @t1 for xml path('')),1,1,'') as str from @t1) where ord_id = @ordid1
        select ord_gl as gl from order_266 where ord_id = @ordid1
        select 'OK' result1     
    end
    else if(@type = '旧件')
    begin
        
        update order_266 set ord_hplx = '旧件' where ord_id = @ordid1
        if(@bcid1 = 20)
        begin
            if((@fdpid = 3517 and @dpid1 = 3739) or @fdpid = 3756 )
            begin
                insert into @mxid
                select ord_id from order_265 
                where ord_bcid = 20 
                    and ord_createdpid in (select dp_id from mps_depart where (CHARINDEX('仁寿',dp_depart)>0 or CHARINDEX('仁寿',dp_location)>0) and dp_id <> 3756)
                    and ord_status = '待门店发货装箱'
                    and ord_no not like '%临%' 
            end
            else
            begin
                insert into @mxid
                select ord_id from order_265 
                where ord_bcid = 20 
                    and ord_createdpid not in (select dp_id from mps_depart where (CHARINDEX('仁寿',dp_depart)>0 or CHARINDEX('仁寿',dp_location)>0) and dp_id <> 3756)
                    and ord_status = '待门店发货装箱'
                    and ord_no not like '%临%' 
            end
        end
        else
        begin
            insert into @mxid
            select ord_id from order_265
            where ord_bcid = @bcid1
            and ord_status = '待门店发货装箱'
            and ord_no not like '%临%'
        end
        select count(ordid) as count1 from @mxid
        
        
        insert into order_266_3 (
            ord2_ch,
            ord2_hh,
            ord2_khsj,
            ord2_khxm,
            ord2_pp,
            ord2_sjmd,
            ord2_sjr,
            ord2_sjrq,
            ord2_xh,
            ord2_fbizid,
            ord2_jjordid,
            ord2_ordid,
            ord2_tag,
            ord2_bz)
        select
            ord_ch,
            ord_hhbm,
            ord_sjh,
            ord_khxm,
            ord_pp,
            ord_createdepart,
            ord_creator,
            ord_date,
            ord_xh,
            ord_bizid,
            ord_id,
            @ordid1,
            3,
            ord_bz
        from order_265 where ord_id in (select ordid from @mxid)
        order by ord_createdepart
        
        
        insert into @temp
        select ord2_sjmd,ord2_pp+'('+cast(count(*) as nvarchar(50))+'个)' as co 
        from order_266_3 where ord2_ordid =@ordid1 group by ord2_sjmd,ord2_pp
        insert into @t1
        select '【'+a.dp+'】:'+(stuff((select ','+info from @temp where dp = a.dp for xml path('')),1,1,'')) from @temp a group by dp
        update order_266 set ord_gl = (select top 1 stuff((select ';'+str from @t1 for xml path('')),1,1,'') as str from @t1) where ord_id = @ordid1
        select ord_gl as gl from order_266 where ord_id = @ordid1
        select 'OK' result1
    end



end