IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_login_main]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_login_main]
GO
CREATE proc [dbo].[mps_login_main]
  @cmpid int,
  @username varchar(100),
  @pass varchar(100),
  @branch varchar(30),
  @type int,
  @ip varchar(30),
  @device varchar(30),
  @site varchar(200)
  as begin
      set nocount on
  if (@type = -1)
  begin
     declare @nowcuid int
 select @nowcuid = cu_id from mps_login  with(nolock)
 where cu_cmpid=@cmpid and (cu_account=@username or cu_mobile=@username) 
 if(@nowcuid>0)
 begin
update mps_login set cu_pass = @pass where cu_id = @nowcuid and cu_cmpid = @cmpid
select -1 as flag
 end else
 begin
select 0 as flag 
 end
  end
      if (@type = 1)
  begin
      declare @t table(cuid bigint)
  insert into @t(cuid) 
  select cu_id from mps_login  with(nolock)
  where cu_cmpid=@cmpid 
  and (cu_account=@username or cu_mobile=@username) 
  and cu_pass=@pass 
  
  declare @count int 
  select @count = count(*) from @t 
  if (@count>1)
  begin   
      select 2 as flag  
  end else
  if (@count=1) 
  begin
/*添加初始密码（此处为空）不允许登录的逻辑*/
 if( upper(@pass) = upper('d41d8cd98f00b204e9800998ecf8427e') and (exists (select 1 from ebs_kernel.dbo.pub_company where cmp_id=@cmpid and cmp_allowemptypass=1)))
 begin
select 9999 as flag  
 end
 else
 begin
declare @cuid bigint
select @cuid=cuid from @t 
exec mps_login_userinfo @cuid --查询用户信息
exec mps_login_log @ip,@device,@cuid,@cmpid,@site   --插入登陆日志表与在线表
 end        
  end 
  else 
  begin
  select 0 as flag  
  end  
  end else
  if(@type = 2)   --表示手机验证码登录
  begin
      
  declare @x table(cuid bigint)
  insert into @x(cuid) 
  select cu_id from mps_login  with(nolock)
  where cu_cmpid=@cmpid 
  and cu_mobile=@username

  declare @count1 int 
  select @count1 = count(*) from @x
  if (@count1>1)
  begin
      select 2 as flag  
  end else
  if (@count1=1) 
  begin
      declare @cuid1 int 
  select @cuid1 = cuid from @x
  exec mps_login_userinfo @cuid1
  exec mps_login_log @ip,@device,@cuid1,@cmpid,@site    --插入登陆日志表与在线表
  end 
  else 
  begin
  select 0 as flag  
  end  
  end else
  if(@type = 8)   --表示微信确认登录
  begin
  declare @y table(cuid bigint)
  insert into @y(cuid) 
  select cu_id from mps_login  with(nolock)
  where cu_cmpid=@cmpid 
  and (cu_mobile=@username or cu_account=@username)

  declare @count2 int 
  select @count2 = count(*) from @y 
  if (@count2>1)
  begin
      select 2 as flag  
  end else
  if (@count2=1) 
  begin
      declare @cuid2 int 
  select @cuid2 = cuid from @y
  exec mps_login_userinfo @cuid2
  exec mps_login_log @ip,@device,@cuid2,@cmpid,@site    --插入登陆日志表与在线表
  end 
  else 
  begin
  select 0 as flag  
  end  
  end else
  if (@type = 9)  --此模式验证手机号用户名是否重复
  begin
      declare @k table(cuid bigint)
  insert into @k(cuid) 
  select cu_id from mps_login  with(nolock)
  where cu_cmpid=@cmpid 
  and (cu_account=@username or cu_mobile=@username) 

  declare @count4 int 
  select @count4 = count(*) from @k 
  if (@count4>1)
  begin
      select 2 as flag  
  end else
  if (@count4=1) 
  begin
      select 1 as flag  
  end 
  else 
  begin
  select 0 as flag  
  end  
  end
  else
    begin
     select 0 as flag 
end
      set nocount off
  end