IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_customer_bind]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_customer_bind]
GO
-- =============================================
-- Author:flobit
-- Create date: 2018年9月21日
-- Description:客户第一次登录的逻辑,如果有ctid则自动创建用户
-- =============================================
CREATE PROCEDURE [dbo].[mps_customer_bind]
@cmpid int,
@mobile varchar(64),     --手机号码
@nick_name varchar(128), --微信昵称
@gender int,             --性别
@wxcode varchar(128),    --微信开发者账号
@openid varchar(128),    --用户openid
@cctag varchar(64),      --cctag
@ctid varchar(64)        --ctid
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
SET NOCOUNT ON;
declare @result varchar(10)='0'
declare @message varchar(128)='成功'

-- 应该去cutomer{cctag中去找},组装表名称
declare @target_tablename varchar(128) = 'mps_customer'+case when @cctag='0' then '' else '_'+@cctag end
declare @target_permission_tablename varchar(128)=@target_tablename+'_users'
declare @sql_select_and_create_customer nvarchar(max)='
declare @cu_id int
declare @cu_id2 int
declare @cu_bcid int
declare @cu_branch varchar(128)
declare @cu_dpid int
declare @cu_depart varchar(128)
declare @cu_function int 
declare @cu_ccid int
declare @cu_createcuid int
declare @cu_createdpid int
declare @cu_createdepart varchar(128)
declare @cu_loginscope varchar(1024)
--如果指定了CTID,并且用户不存在则创建用户
if(@ctid !='''' and @ctid !=''0'' and not exists(select 1 from '+@target_tablename+' where cc_mobile = @mobile))
begin
select @cu_dpid = dp_id,@cu_depart=dp_depart from mps_depart where dp_fromcc=@ctid
select top 1 @cu_createcuid =ccp_cuid from '+@target_permission_tablename+' where ccp_type=''cstedit''
select @cu_createdpid = cu_dpid,@cu_createdepart=cu_depart,@cu_bcid = cu_bcid,@cu_branch=cu_branch from mps_login where cu_id = @cu_createcuid
select @cu_loginscope = ct_loginscope from mps_csttype where ct_id=@ctid
if(@cu_dpid is null or @cu_createcuid is null or @cu_createdpid is null or @cu_bcid is null)
begin
set @result = ''1''
set @message = ''此类型用户不允许通过此途经注册''
return
end
insert into '+@target_tablename+'(cc_mobile,cc_name,cc_tag,cc_ctid,cc_bcid,cc_branch,cc_dpid,cc_depart,cc_gender,cc_code,cc_cuid,cc_createcuid,cc_createdpid,cc_createdepart) 
values(@mobile,@nick_name,@cctag,@ctid,@cu_bcid,@cu_branch,@cu_dpid,@cu_depart,@gender,'''',0,@cu_createcuid,@cu_createdpid,@cu_createdepart)
end
--查询客户信息
select 
@cu_id=cc_cuid,
@ctid =cc_ctid,
@cu_ccid=cc_id,  
@cu_bcid=cc_bcid,
@cu_branch=cc_branch,
@cu_dpid=cc_dpid,
@cu_depart=cc_depart,
@cu_function=cc_ctid+1000000
from '+@target_tablename+' where cc_mobile = @mobile
--没有找到这个用户,又没有指定手机号码,说明此类型用户不允许用户通过这种方式注册
if (@cu_id is null)
begin
set @result = ''1''
set @message = ''没有找到客户的相关信息,并且此类客户不允许通过此途径注册''
return 
end


--检查此类客户是否允许登录
select @cu_loginscope = ct_loginscope from mps_csttype where ct_id = @ctid
if(@cu_loginscope is null or not charindex(@wxcode,@cu_loginscope)>0)
begin
set @result = ''1''
set @message = ''此类客户不允许登录此平台''
return
end

--先判断这个手机号码是否存在,如果存在则取判断是否有当前这个角色
--如果存在了,则不用插入到mps_login表和mps_employee表了,
--只需判断mps_loginrole中的缺省角色是否和当前这个角色相同,如果不同则需要插入一条新的记录到mps_login表中
select @cu_id2 = cu_id from mps_employee where cu_mobile=@mobile
if(@cu_id2 is not null)
begin
print ''已经存在当前用户''
--已经存在用户,判断当前login_role中的角色是否和当前正在新建的角色相同,如果不同则新建一个登录角色
if(not exists(select 1 from mps_login_role where lgu_cuid=@cu_id2 and lgu_ccid = @cu_ccid and lgu_type = @cctag))
begin
insert into mps_login_role(lgu_cuid,lgu_bcid,lgu_branch,lgu_dpid,lgu_depart,lgu_function,lgu_defaultrole,lgu_ccid,lgu_type)
values (@cu_id2,@cu_bcid,@cu_branch,@cu_dpid,@cu_depart,@cu_function,1,@cu_ccid,@cctag)
end
end else
begin
--print ''需要新建用户''

--将信息插入mps_employee表
insert into mps_employee(cu_cmpid,cu_type,cu_mobile,cu_account,cu_name,cu_gender,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_function)
values(@cmpid,@cctag,@mobile,@mobile,@nick_name,0,@cu_bcid,@cu_branch,@cu_dpid,@cu_depart,@cu_function)
select @cu_id2 = cu_id from mps_employee where cu_mobile=@mobile
--将信息插入mps_login表
insert into mps_login(
   [cu_id]
  ,[cu_cmpid]
  ,[cu_type]
  ,[cu_objid]
  ,[cu_account]
  ,[cu_callname]
  ,[cu_gender]
  ,[cu_pass]
  ,[cu_bcid]
  ,[cu_branch]
  ,[cu_dpid]
  ,[cu_depart]
  ,[cu_otid]
  ,[cu_title]
  ,[cu_function]
  ,[cu_mobile])
SELECT 
   [cu_id]
  ,[cu_cmpid]
  ,@cctag
  ,[cu_id]
  ,[cu_account]
  ,[cu_name]
  ,[cu_gender]
  ,''''
  ,[cu_bcid]
  ,[cu_branch]
  ,[cu_dpid]
  ,[cu_depart]
  ,[cu_otid]
  ,[cu_title]
  ,[cu_function]
  ,[cu_mobile]
FROM [dbo].[mps_employee]
where cu_id =@cu_id2
--将信息插入mps_login_role表
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,
lgu_dingdpid,
lgu_ccid,
lgu_type)
select 
cu_id,
cu_name,
cu_bcid,
cu_branch,
cu_dpid,
cu_depart,
cu_otid,
cu_title,
cu_function,
cu_managelevel,
cu_professionlevel,
cu_otlevel,
0,
0,
cu_ccid,
cu_type
from mps_employee
where cu_id=@cu_id2
end
--绑定微信
if(exists(select 1 from mps_login where cu_id =@cu_id2 and (
cu_gzh1=@wxcode+''|''+@openid 
orcu_gzh2=@wxcode+''|''+@openid 
orcu_gzh3=@wxcode+''|''+@openid 
orcu_gzh4=@wxcode+''|''+@openid 
orcu_gzh5=@wxcode+''|''+@openid 
)))
begin
--已经正确的绑定了
return
end
--print @cu_id2
--检查是否绑定了当前的公众号
if(exists(select 1 from mps_login where cu_id = @cu_id2 and(
cu_gzh1 like ''''''+@wxcode+''''|%''
or  cu_gzh2 like ''''''+@wxcode+''''|%''
or  cu_gzh3 like ''''''+@wxcode+''''|%''
or  cu_gzh4 like ''''''+@wxcode+''''|%''
or  cu_gzh5 like ''''''+@wxcode+''''|%''
)))
begin
set @result = ''1''
set @message = ''此手机号码已经被其他用户绑定''
return
end

--检查还能否绑定
if(not exists(select 1 from mps_login where cu_id =@cu_id2 and (
cu_gzh1=''''
orcu_gzh2=''''
orcu_gzh3=''''
orcu_gzh4=''''
orcu_gzh5=''''
)))
begin
set @result = ''1''
set @message = ''此账号已达到公众号绑定上限''
end
if (exists(select 1 from mps_login where cu_id =@cu_id2 and cu_gzh1=''''))
begin
update mps_login set cu_gzh1=@wxcode+''|''+@openid where cu_id=@cu_id2
end else
if (exists(select 1 from mps_login where cu_id =@cu_id2 and cu_gzh2=''''))
begin
update mps_login set cu_gzh2=@wxcode+''|''+@openid where cu_id=@cu_id2
end else
if (exists(select 1 from mps_login where cu_id =@cu_id2 and cu_gzh3=''''))
begin
update mps_login set cu_gzh3=@wxcode+''|''+@openid where cu_id=@cu_id2
end else
if (exists(select 1 from mps_login where cu_id =@cu_id2 and cu_gzh4=''''))
begin
update mps_login set cu_gzh4=@wxcode+''|''+@openid where cu_id=@cu_id2
end else
if (exists(select 1 from mps_login where cu_id =@cu_id2 and cu_gzh5=''''))
begin
update mps_login set cu_gzh5=@wxcode+''|''+@openid where cu_id=@cu_id2
end
'
--print @sql_select_and_create_customer
--执行SQL获取客户信息
exec sp_executesql @sql_select_and_create_customer,N'
@cmpid int,
@mobile varchar(64),
@nick_name varchar(128), 
@gender int,
@wxcode varchar(128), 
@openid varchar(128),
@cctag varchar(64),
@ctid varchar(64),
@result varchar(10) out,
@message varchar(128) out',
@cmpid,
@mobile,
@nick_name,
@gender,
@wxcode,
@openid,
@cctag,
@ctid,
@result out,
@message out

select @result result,@message message
END