IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_import_shop_employee]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_import_shop_employee]
GO
create procedure [dbo].[mps_import_shop_employee](
--new 11111
@cmpid int, 
@branchid int, 
@userdpid int, 
@userotid int, 
@useruid int, 
@keyflds varchar(500),
@dataflds varchar(1000),
@datas nvarchar(MAX) 
) as
begin
declare @sql1 nvarchar(MAX) 
declare @sql2 nvarchar(MAX)
declare @sql3 nvarchar(MAX)
declare @sql4 nvarchar(MAX)
set @sql1 = '' 
set @sql2 = '' 
set @sql3 = '' 
set @sql4 = '' 
declare @ts1 varchar(1000) 
declare @v1 nvarchar(500)
declare @gsv1 nvarchar(500) 
declare @bmbasev1 nvarchar(500)
declare @bmv1 nvarchar(500)
declare @ygv1 nvarchar(500)   
declare @j int 
declare @s1 nvarchar(500)
declare @s2 nvarchar(500)
declare @s3 nvarchar(500)
declare @s4 nvarchar(500)
declare @s5 nvarchar(500)
declare @s6 nvarchar(500)
declare @s7 nvarchar(500)
declare @s8 nvarchar(500)
declare @f1 nvarchar(500)
declare @gsf1 nvarchar(500)
declare @bmbasef1 nvarchar(500)
declare @bmf1 nvarchar(500)
declare @ygf1 nvarchar(500)    
declare @keycond nvarchar(4000)
declare @gsvallist nvarchar(4000)
declare @bmbasevallist nvarchar(4000)
declare @bmvallist nvarchar(4000)
declare @ygvallist nvarchar(4000)
declare @gsfldlist nvarchar(4000)
declare @bmfldlist nvarchar(4000)
declare @bmbasefldlist nvarchar(4000)
declare @ygfldlist nvarchar(4000)
    declare @gsziduan nvarchar(500)
declare @bmbaseziduan nvarchar(500)
declare @bmziduan nvarchar(500)
declare @ygziduan nvarchar(500)
declare @gsdata nvarchar(500)
declare @bmbasedata nvarchar(500)
declare @bmdata nvarchar(500)
declare @ygdata nvarchar(500)
declare @gsdt nvarchar(500)
declare @bmbasedt nvarchar(500)
declare @bmdt nvarchar(500)
declare @ygdt nvarchar(500)
declare @gsk int
declare @bmbasek int
declare @bmk int
declare @ygk int
declare @gsm int
declare @bmbasem int
declare @bmm int
declare @ygm int
declare @ff int
declare @gscode nvarchar(500)
set @gscode=''
set @s8=''
declare @i int,@curInd int,@strlen int,@tmps nvarchar(4000)  
select @curInd=1,@strlen=LEN(@datas) 
while (@curInd<=@strlen) 
begin 
set @sql1 = '' 
select @i=charindex('///',@datas,@curInd) 
if (@i>0) 
begin 
select @tmps=SUBSTRING(@datas,@curInd,@i-@curInd) 
set @curInd=@i+3 
end else if (@curInd<=@strlen) 
begin 
select @tmps=SUBSTRING(@datas,@curInd,@strlen-@curInd+1) 
set @curInd=@strlen+1 
end 
set @keycond = '' 
set @gsvallist = ''
set @bmbasevallist = '' 
set @bmvallist = '' 
set @ygvallist = '' 
set @gsfldlist = '' 
set @bmbasefldlist = '' 
set @bmfldlist = '' 
set @ygfldlist = '' 
set @f1 = ''
set @ts1 = @dataflds 
set @gsdt=''
set @bmdt=''
set @bmbasedt=''
set @ygdt=''
while (@tmps>'') 
begin
set @j = charindex('||',@tmps) 
if (@j>0) 
begin
set @v1 = substring(@tmps,1,@j-1)
set @tmps = substring(@tmps,@j+2, len(@tmps) - @j - 1) 

end else 
begin
set @v1 = @tmps 
set @tmps = '' 
end
set @j = charindex(',',@ts1) 
if (@j>0) 
begin
set @f1 = substring(@ts1,1,@j-1) 
set @ts1 = substring(@ts1,@j+1, len(@ts1) - @j ) 

end else                                                                  
begin                                                                     --@f1 单个字段值
set @f1 = @ts1                                                        --@v1 单个数据值
set @ts1 = '' 
end
if (@f1>'') 
begin
 if exists (select * from syscolumns  where id=object_id('mps_branch') and name=@f1)
begin
    set @gsv1=@v1
set @gsf1=@f1
set @ff=charindex(@gsf1,@gsfldlist,1)
if(@ff=0)
begin
 if (@gsfldlist>'') set @gsfldlist=@gsfldlist+','
  set @gsfldlist=@gsfldlist +@gsf1
  if (@gsvallist>'') set @gsvallist=@gsvallist+',' 
      set @gsvallist=@gsvallist+''''+@gsv1+'''' 
end 
if (charindex(','+@f1+',',','+@keyflds+',')>0) 
begin
if (@keycond>'') set @keycond=@keycond+' and ' 
set @keycond = @keycond+' '+@f1+'='''+@v1+''' ' 
end
end
     if exists (select * from syscolumns  where id=object_id('mps_depart_base') and name=@f1)
begin
    set @bmbasev1=@v1
set @bmbasef1=@f1
set @ff=charindex(@bmbasef1,@bmbasefldlist,1)
if(@ff=0)
begin
if (@bmbasefldlist>'') set @bmbasefldlist=@bmbasefldlist+','
set @bmbasefldlist=@bmbasefldlist +@bmbasef1 
if (@bmbasevallist>'') set @bmbasevallist=@bmbasevallist+',' 
        set @bmbasevallist=@bmbasevallist+''''+@bmbasev1+''''  
end
if (charindex(','+@f1+',',','+@keyflds+',')>0) 
begin
if (@keycond>'') set @keycond=@keycond+' and ' 
set @keycond = @keycond+' '+@f1+'='''+@v1+''' ' 
end

end  
 if exists (select * from syscolumns  where id=object_id('mps_depart') and name=@f1)
begin
    set @bmv1=@v1
set @bmf1=@f1
set @ff=charindex(@bmf1,@bmfldlist,1)
if(@ff=0)
begin
if (@bmfldlist>'') set @bmfldlist=@bmfldlist+','
set @bmfldlist=@bmfldlist +@bmf1 
if (@bmvallist>'') set @bmvallist=@bmvallist+',' 
           set @bmvallist=@bmvallist+''''+@bmv1+'''' 
    end
--if (charindex(','+@f1+',',','+@keyflds+',')>0) 
--begin
--if (@keycond>'') set @keycond=@keycond+' and ' 
--set @keycond = @keycond+' '+@f1+'='''+@v1+''' ' 
--end
end  
if exists (select * from syscolumns  where id =object_id('mps_employee') and name =@f1)
begin
    set @ygv1=@v1
set @ygf1=@f1
set @ff=charindex(@ygf1,@ygfldlist,1)
if(@ff=0)
begin
if (@ygfldlist>'') set @ygfldlist=@ygfldlist+','
set @ygfldlist=@ygfldlist +@ygf1 
    if (@ygvallist>'') set @ygvallist=@ygvallist+',' 
        set @ygvallist=@ygvallist+''''+@ygv1+'''' 
end
if (charindex(','+@f1+',',','+@keyflds+',')>0) 
begin
if (@keycond>'') set @keycond=@keycond+' and ' 
set @keycond = @keycond+' '+@f1+'='''+@v1+''' ' 
end
end
if(@f1='bc_company')
begin
if(@ff=0)
   set @s1=@gsv1
end 
if(@f1='bc_code')
begin
if(@ff=0)
   set @gscode=@gsv1
end 
if(@f1='dp_depart')
begin
 if(@ff=0)
   set @s2=@bmbasev1
end 
if(@f1='dp_code')
begin
 if(@ff=0)
   set @s5=@bmbasev1
end 
if(@f1='dp_depart')
begin
if(@ff=0)
   set @s3=@bmv1
end 
if(@f1='dp_code')
begin
 if(@ff=0)
   set @s6=@bmv1
end 
if(@f1='cu_name')
begin
 if(@ff=0)
   set @s4=@ygv1
end 
if(@f1='cu_code')
begin
 if(@ff=0)
   set @s7=@ygv1
end 
if(@f1='cu_account')
begin
 if(@ff=0)
   set @s8=@ygv1
end 
end 
end
    declare @dpstdid int
declare @dpbcid int
declare @cudpid int
declare @cubcid int
declare @m     int 
declare @n    int
declare @gscfpd nvarchar(500)  
declare @gspdtj nvarchar(500)     
if (@keyflds='') 
begin
--mps_branch插入更新数据
if(@gsfldlist>''and @s1>''and @s2>''and @s5>'')
begin    
if(exists (select * from mps_branch where (bc_code=@gscode or bc_company=@s1)  and bc_cmpid=@cmpid))
begin
while(@gsfldlist>'')
begin
       set @m=charindex('bc_display',@gsfldlist,1)
   set @n=charindex('bc_fullname',@gsfldlist,1)
set @gsk = charindex(',',@gsfldlist) 
if (@gsk>0) 
begin
set @gsziduan = substring(@gsfldlist,1,@gsk-1)
set @gsfldlist = substring(@gsfldlist,@gsk+1, len(@gsfldlist) - @gsk ) 
end else                                                                  
begin                                                                    
set @gsziduan = @gsfldlist                                                      
set @gsfldlist = '' 
end
set @gsm = charindex(',',@gsvallist) 
if (@gsm>0) 
begin
set @gsdata = substring(@gsvallist,1,@gsm-1)
set @gsdt=@gsdt+@gsziduan+'='+@gsdata+','
set @gsvallist = substring(@gsvallist,@gsm+1, len(@gsvallist) - @gsm ) 
end else                                                                  
begin                                                                    
set @gsdata = @gsvallist                                                      
set @gsvallist = '' 
end
set @sql1='update mps_branch set '+@gsdt+@gsziduan+'='+@gsdata+' where bc_company='''+@s1+'''and bc_code='''+@gscode+'''and bc_cmpid='+convert(varchar(20),@cmpid)
end    
 end 
else if(@m>0 and @n>0 and @s1 != '集团总部')
begin
set @sql1 = 
'insert into mps_branch(bc_cmpid,'+@gsfldlist+') '+
'values('+convert(varchar(20),@cmpid)+
','+@gsvallist+') '
end
else if(@n=0 and @s1 != '集团总部')
begin
    set @sql1 = 
'insert into mps_branch(bc_cmpid,bc_fullname,'+@gsfldlist+') '+
'values('+convert(varchar(20),@cmpid)+
','''+@s1+''','+@gsvallist+') '
end
else if(@m=0 and @s1 != '集团总部')
begin
   set @sql1 = 
'insert into mps_branch(bc_cmpid,bc_display,'+@gsfldlist+') '+
'values('+convert(varchar(20),@cmpid)+
','''+@s1+''','+@gsvallist+') '
end
else 
begin
    set @sql1 = 
'insert into mps_branch(bc_cmpid,bc_display,bc_fullname,'+@gsfldlist+') '+
'values('+convert(varchar(20),@cmpid)+
','''+@s1+''','''+@s1+''','+@gsvallist+') '
end
exec(@sql1)
print @sql1
end
--dp_depart_base插入更新数据
/*if(@bmbasefldlist>''and @s2>''and @s5>'')
begin
if(exists (select * from mps_depart_base where dp_code=@s5 and dp_cmpid=@cmpid))
begin
while(@bmbasefldlist>'')
begin
set @bmbasek = charindex(',',@bmbasefldlist) 
if (@bmbasek>0) 
begin
set @bmbaseziduan = substring(@bmbasefldlist,1,@bmbasek-1)
set @bmbasefldlist = substring(@bmbasefldlist,@bmbasek+1, len(@bmbasefldlist) - @bmbasek ) 
end else                                                                  
begin                                                                    
set @bmbaseziduan = @bmbasefldlist                                                      
set @bmbasefldlist = '' 
end
set @bmbasem = charindex(',',@bmbasevallist) 
if (@bmbasem>0) 
begin
set @bmbasedata = substring(@bmbasevallist,1,@bmbasem-1)
set @bmbasedt=@bmbasedt+@bmbaseziduan+'='+@bmbasedata+','
set @bmbasevallist = substring(@bmbasevallist,@bmbasem+1, len(@bmbasevallist) - @bmbasem ) 
end else                                                                  
begin                                                                    
set @bmbasedata = @bmbasevallist                                                      
set @bmbasevallist = '' 
end
set @sql2='update mps_depart_base set '+@bmbasedt+@bmbaseziduan+'='+@bmbasedata+' where  dp_code='''+@s5+''' and dp_cmpid='+convert(varchar(20),@cmpid)
end    
end 
else
begin
set @sql2 = 
'insert into mps_depart_base(dp_cmpid,'+@bmbasefldlist+') '+
'values('+convert(varchar(20),@cmpid)+
','+@bmbasevallist+')    '
end
exec(@sql2)
print @sql2
end*/
--dp_depart插入更新数据   
              set @dpstdid= (select dp_id from mps_depart_base where  dp_code=@s5 and dp_cmpid=@cmpid  and dp_depart=@s2)
   if( @s1>'')
   begin
set @dpbcid= (select bc_id from mps_branch where  bc_cmpid=@cmpid and bc_company=@s1)
   end
   else
   begin
       set @dpbcid= @branchid
   end 
   if @s1 = '集团总部'
   begin
 set @dpbcid= 0 
   end    
if(@bmfldlist>''and @s3>''and @s6>'')
begin
if(exists (select * from mps_depart where /*dp_bcid=@dpbcid and */ dp_code=@s6 and dp_cmpid=@cmpid ))
begin
while(@bmfldlist>'')
begin
        set @m=charindex('dp_display',@bmfldlist,1)
set @n=charindex('dp_fullname',@bmfldlist,1)
set @bmk = charindex(',',@bmfldlist) 
if (@bmk>0) 
begin
set @bmziduan = substring(@bmfldlist,1,@bmk-1)
set @bmfldlist = substring(@bmfldlist,@bmk+1, len(@bmfldlist) - @bmk ) 
end else                                                                  
begin                                                                    
set @bmziduan = @bmfldlist                                                      
set @bmfldlist = '' 
end
set @bmm = charindex(',',@bmvallist) 
if (@bmm>0) 
begin
set @bmdata = substring(@bmvallist,1,@bmm-1)
set @bmdt=@bmdt+@bmziduan+'='+@bmdata+','
set @bmvallist = substring(@bmvallist,@bmm+1, len(@bmvallist) - @bmm ) 
end else                                                                  
begin                                                                    
set @bmdata = @bmvallist                                                      
set @bmvallist = '' 
end
select @dpbcid = dp_bcid from mps_depart where dp_code = @s6 and dp_cmpid = @cmpid
set @sql3='update mps_depart set dp_bcid='+convert(varchar(20),@dpbcid)+','+@bmdt+@bmziduan+'='+@bmdata+' where dp_code='''+@s6+'''and dp_cmpid='+convert(varchar(20),@cmpid)
end    
end 
        else if(@m>0 and @n>0)
begin
set @sql3 = 'insert into mps_depart(dp_isdepart,dp_cmpid,dp_dpstdid,dp_bcid,'+@bmfldlist+') '+
'values(1,'+convert(varchar(20),@cmpid)+
',0,'+convert(varchar(20),@dpbcid)+','+@bmvallist+')'
end
else if(@n=0)
begin
     set @sql3 = 'insert into mps_depart(dp_isdepart,dp_cmpid,dp_dpstdid,dp_bcid,dp_fullname,'+@bmfldlist+') '+
'values(1,'+convert(varchar(20),@cmpid)+
',0,'+convert(varchar(20),@dpbcid)+','''+@s3+''','+@bmvallist+')'
end
else if(@m=0)
begin
    set @sql3 = 'insert into mps_depart(dp_isdepart,dp_cmpid,dp_dpstdid,dp_bcid,dp_display'+@bmfldlist+') '+
'values(1,'+convert(varchar(20),@cmpid)+
',0,'+convert(varchar(20),@dpbcid)+','''+@s3+''','+@bmvallist+')'
end
else
begin
   set @sql3 = 'insert into mps_depart(dp_isdepart,dp_cmpid,dp_dpstdid,dp_bcid,dp_display,dp_fullname,'+@bmfldlist+') '+
'values(1,'+convert(varchar(20),@cmpid)+
',0,'+convert(varchar(20),@dpbcid)+','''+@s3+''','''+@s3+''','+@bmvallist+')'
end
exec(@sql3)
print @sql3
end
--mps_employee插入更新数据
            select @cudpid = dp_id,@cubcid = dp_bcid from mps_depart where  dp_bcid=@dpbcid and  dp_code=@s6 and dp_cmpid=@cmpid
if(@ygfldlist>''and @s4>''and @s7>'' )
begin 
if @s1 > ''
begin
set @s1 = @s1
end else
begin
if (@dpbcid=0) set @s1 = '集团总部'
else
begin
select @s1 = bc_company from mps_branch where bc_id = @dpbcid
end
end
            if(@s8='')
begin
    set @s8=@s7
end    
  set @m=charindex('cu_account',@ygfldlist,1)
if(exists (select * from mps_employee where cu_cmpid=@cmpid and cu_code=@s7 and cu_account=@s8 ))
begin
while(@ygfldlist>'')
begin
set @ygk = charindex(',',@ygfldlist) 
if (@ygk>0) 
begin
set @ygziduan = substring(@ygfldlist,1,@ygk-1)
set @ygfldlist = substring(@ygfldlist,@ygk+1, len(@ygfldlist) - @ygk ) 
end else                                                                  
begin                                                                    
set @ygziduan = @ygfldlist                                                      
set @ygfldlist = '' 
end
set @ygm = charindex(',',@ygvallist) 
if (@ygm>0) 
begin
set @ygdata = substring(@ygvallist,1,@ygm-1)
set @ygdt=@ygdt+@ygziduan+'='+@ygdata+','
set @ygvallist = substring(@ygvallist,@ygm+1, len(@ygvallist) - @ygm ) 
end else                                                                  
begin                                                                    
set @ygdata = @ygvallist                                                      
set @ygvallist = '' 
end
set @sql4 = char(10)+'declare @upcuid int ' + char(10)+
 ' select @upcuid = cu_id from mps_employee where cu_code = '''+@s7+''' '
set @sql4+= char(10) + ' update mps_employee set '+@ygdt+@ygziduan+'='+@ygdata+' where cu_cmpid='+convert(varchar(20),@cmpid)+' and cu_code='''+@s7+''' '
set @sql4 += char(10) + 'update mps_login 
set cu_account = b.cu_account,
cu_callname = b.cu_name,
cu_gender = b.cu_gender, 
cu_bcid = b.cu_bcid,
cu_branch = b.cu_branch,
cu_dpid = b.cu_dpid,
cu_depart = b.cu_depart,
cu_otid = b.cu_otid,
cu_title = b.cu_title,
cu_function = b.cu_function,
cu_mobile = b.cu_mobile,
cu_managelevel = b.cu_managelevel,
cu_professionlevel = b.cu_professionlevel,
cu_otlevel = b.cu_otlevel,
cu_departtitles = b.cu_depart+''.''+b.cu_title
from mps_employee b 
where cu_objid = @upcuid  and b.cu_id  =@upcuid ' + char(10)+
' update mps_login_role  
 set lgu_bcid = b.cu_bcid,  
 lgu_branch = b.cu_branch,  
 lgu_dpid = b.cu_dpid,  
 lgu_depart = b.cu_depart,  
 lgu_otid = b.cu_otid,  
 lgu_title = b.cu_title,  
 lgu_function = b.cu_function,  
 lgu_name = b.cu_name,  
 lgu_otlevel = b.cu_otlevel,  
 lgu_managelevel = b.cu_managelevel,  
 lgu_professionlevel = b.cu_professionlevel  
 from mps_employee b 
 where lgu_cuid = @upcuid and lgu_defaultrole=0  and b.cu_id  = @upcuid 
 exec mps_update_logindeparttitles @upcuid
 '


end    
end 
       else 
   begin
   if(@m>0)
begin
set @sql4 = 'insert into mps_employee(cu_cmpid,cu_dpid,cu_bcid,cu_depart,cu_branch,'+@ygfldlist+') '+
'values('+convert(varchar(20),@cmpid)+
','+convert(varchar(20),@cudpid)+','+convert(varchar(20),@cubcid)+','''+@s3+''','''+@s1+''','+@ygvallist+')';
end
else
begin
set @sql4 = 'insert into mps_employee(cu_cmpid,cu_dpid,cu_bcid,cu_depart,cu_branch,cu_account,'+@ygfldlist+') '+
'values('+convert(varchar(20),@cmpid)+
','+convert(varchar(20),@cudpid)+','+convert(varchar(20),@cubcid)+','''+@s3+''','''+@s1+''','''+@s7+''','+@ygvallist+')';
end
set @sql4 += char(10)+'declare @upcuid int ' + char(10)+
' select @upcuid = cu_id from mps_employee where cu_code = '''+@s7+''' ' +char(10) +
'if(not exists (select * from mps_login where cu_objid=@upcuid )) 
 begin
  insert into mps_login(cu_id,cu_objid,cu_cmpid,cu_type,cu_account,cu_callname,cu_gender,cu_pass,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_otlevel,cu_managelevel,cu_professionlevel)
  select @upcuid,@upcuid,cu_cmpid,0,cu_account,cu_name,cu_gender,''d41d8cd98f00b204e9800998ecf8427e'',cu_bcid,cu_branch,cu_dpid,cu_depart,cu_otlevel,cu_managelevel,cu_professionlevel
  from mps_employee    with(nolock)
  where cu_id=@upcuid

  --插入登录角色表
  insert into [mps_login_role](
   [lgu_cuid]
  ,[lgu_name]
  ,[lgu_bcid]
  ,[lgu_branch]
  ,[lgu_dpid]
  ,[lgu_depart]
  ,[lgu_otid]
  ,[lgu_title]
  ,[lgu_function]
  ,[lgu_managelevel]
  ,[lgu_professionlevel]
  ,[lgu_otlevel]
  ,lgu_defaultrole)
 select
 @upcuid,
 cu_name,
 cu_bcid,
 case when cu_branch = '''' then ''集团总部'' else cu_branch end,
 cu_dpid,
 cu_depart,
 cu_otid,
 cu_title,
 cu_function,
 cu_otlevel,
 cu_managelevel,
 cu_professionlevel,
 0
 from mps_employee  with(nolock)
 where cu_id=@upcuid
 
 exec mps_update_logindeparttitles @upcuid  
 end
'

end
   print @sql4
       exec(@sql4)
end
set @s1=''
set @s2=''
set @s3=''
set @s4=''
set @s5=''
set @s6=''
set @s7=''
set @s8=''
end
end
 end