IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_cst_aftergzh]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_cst_aftergzh]
GO
create procedure [dbo].[mps_gen_cst_aftergzh](
@cmpid int,
@ctid int 
) as
begin
set nocount on
declare @s1 nvarchar(100) 
declare @detail nvarchar(100) 
declare @ds1 varchar(100)
declare @dss nvarchar(MAX)  
declare @detailold nvarchar(100) 
declare @order2 nvarchar(100) 
declare @i int 
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 

declare @mctid int 
set @mctid = @ctid * (-1) 
exec sys_sql_getTableNamePk @cmpid,'cst',@mctid,0,@tbname output,@fkey output ,@dbname output 
declare @ord2tag2 int 
declare @sql nvarchar(MAX) 
declare @ss nvarchar(MAX) 
declare @prcname nvarchar(100) 

set @prcname = 'mps_customer_cst_aftergzh_'+convert(varchar(10),@ctid) 

if (dbo.mps_sys_checkkeywords('cst.gzh',@ctid,0,'','')<>0) 
begin
set @sql = 'drop procedure '+@prcname  
if (dbo.checkprocedure(@prcname)=0) execute(@sql) 
return 
end

set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure(@prcname)=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure '+@prcname+'(
     @cmpid int,
     @useruid int,
     @ctid int , 
     @gzh varchar(50),
     @openid varchar(50),
     @nickname nvarchar(50) , 
     @gender int 
  ) 
as  
begin  
set nocount on  

declare @nonedate datetime 
set @nonedate = CONVERT(datetime,''1910-1-1'')

if ((@nickname is null) or (@nickname=''_none_''))  set @nickname = '''' 
 
declare @ccid bigint  
declare @cctag int 
declare @branchid int 
declare @userdpid int 
declare @userotid int 
declare @userccid bigint 
declare @cstlogin int 

select top 1 @cstlogin=ct_allowlogin,@cctag=ct_tag from mps_csttype with(nolock) where ct_id=@ctid 


select @branchid=cu_bcid,@userdpid=cu_dpid,@userotid=cu_otid,@userccid=cu_ccid from mps_login with(nolock) where cu_id=@useruid 
set @userccid = isnull(@userccid,0) 

declare @cc_useruid int 
set @cc_useruid = 0 
set @ccid = 0 
-- 判断是否已经关注
select @cc_useruid = cu_id , @ccid=cu_ccid  from mps_login with(nolock) where cu_cmpid=@cmpid and (cu_gzh1 = @gzh+''|''+@openid or cu_gzh2 = @gzh+''|''+@openid or cu_gzh3 = @gzh+''|''+@openid or cu_gzh4 = @gzh+''|''+@openid or cu_gzh5 = @gzh+''|''+@openid) 

if (@ccid=0)
begin
select top 1 @ccid=cc_id from '+@dbname+@tbname+' with(nolock)  
where cc_cmpid=@cmpid and  cc_msnqq=@openid 
end 

if (@ccid=0)
begin
declare @maxccid bigint 
select @maxccid=max(cc_id) from '+@dbname+@tbname+' with(nolock) 
set @maxccid = isnull(@maxccid,0)  + 1 

insert into '+@dbname+@tbname+'(cc_cmpid,cc_bcid,cc_branch,cc_dpid,cc_depart,cc_cuid,cc_cuname,cc_tag,cc_caption,cc_ctid,cc_type,cc_code,cc_createdpid,
cc_createdepart,cc_createcuid,cc_creator,cc_name,cc_gender,cc_mobile,cc_fccid,cc_allowlogin,cc_msnqq) 
select @cmpid,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_id,cu_callname,ct_tag,ct_type,ct_id,ct_type,convert(varchar(20),@maxccid),cu_dpid,
cu_depart,cu_id,cu_callname,@nickname,@gender,convert(varchar(20),@maxccid),@userccid,@cstlogin , @openid
from mps_login with(nolock) , mps_csttype with(nolock) 
where cu_id=@useruid and ct_id=@ctid 

select @ccid=cc_id from '+@dbname+@tbname+' with(nolock) where cc_cmpid=@cmpid and cc_ctid=@ctid and cc_id>=@maxccid and cc_name=@nickname and cc_msnqq=@openid 

end 
-- 建议关系表
if (@userccid>0) 
begin
if (not exists (select 1 from '+@dbname+@tbname+'_relation with(nolock) where ccr_fccid=@userccid and ccr_ccid=@ccid)) 
begin
insert into '+@dbname+@tbname+'_relation(ccr_fccid,ccr_ccid) values(@userccid , @ccid) 
end 
end  else 
begin
if (not exists (select 1 from '+@dbname+@tbname+'_relation with(nolock) where ccr_fccid=(-1)*@useruid and ccr_ccid=@ccid)) 
begin
insert into '+@dbname+@tbname+'_relation(ccr_fccid,ccr_ccid) values((-1)*@useruid , @ccid) 
end 
end 
-- 根据实际情况，生成员工表记录
if (@cstlogin=0) 
begin
set @cc_useruid = 0 
select @cc_useruid = cu_id from mps_employee with(nolock) where cu_cmpid=@cmpid and cu_ccid=@ccid 
if (@cc_useruid=0) 
begin
insert into mps_employee(cu_type,cu_cmpid,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_code,cu_account,cu_gender,cu_function,cu_name,cu_mobile,cu_login,cu_ccid) 
select distinct 1+cc_tag,cc_cmpid,cc_bcid,cc_branch,dp_id , dp_depart , cc_code , cc_code , cc_gender  , 1000000+cc_ctid , cc_name , cc_mobile , 0 , @ccid 
from '+@dbname+@tbname+' with(nolock)  , mps_depart with(nolock) where cc_id=@ccid and dp_cmpid=@cmpid and dp_fromcc=@ctid 
select @cc_useruid=cu_id from mps_employee with(nolock) where cu_cmpid=@cmpid and cu_ccid=@ccid 
-- 回写到客户表
update '+@dbname+@tbname+' with(rowlock)  set cc_useruid=@cc_useruid where cc_id=@ccid 
-- 将员工加入到login表，并且绑定微信号
insert into mps_login(cu_id,cu_cmpid,cu_type,cu_objid,cu_account,cu_callname,cu_gender,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_function,cu_mobile,cu_gzh1,cu_ccid) 
select cu_id,cu_cmpid,cu_type,cu_id,cu_account,cu_name,cu_gender,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_function,cu_mobile,@gzh+''|''+@openid,cu_ccid 
from mps_employee with(nolock) where cu_cmpid=@cmpid and cu_id=@cc_useruid 
-- 将记录加入到login_role中
insert into mps_login_role(lgu_cuid,lgu_name,lgu_bcid,lgu_branch,lgu_dpid,lgu_depart,lgu_function,lgu_defaultrole,lgu_type,lgu_ccid)
select cu_id,cu_callname,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_function,0,cu_type,cu_ccid 
from  mps_login with(nolock) where cu_id=@cc_useruid 
end 
end 
 --- 以下是预处理事件中的临时表
 
 '
 
 if (dbo.mps_sys_checkkeywords('cst.gzh',@ctid,0,'#master','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#master'' '+char(13)+char(10)+
'select *   into #master from '+@dbname+@tbname+N' where cc_id=@ccid '
 end
 if (dbo.mps_sys_checkkeywords('cst.gzh',@ctid,0,'#task','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#task'' '+char(13)+char(10)+
'create table #task(objtype varchar(20),ctgid int,objid bigint,objid2 bigint,cuid int,infotype nvarchar(50),info nvarchar(4000)) '
 end
 
 if (dbo.mps_sys_checkkeywords('cst.gzh',@ctid,0,'#account','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#account'' '+char(13)+char(10)+
'create table #account(acctype varchar(30),acccategory varchar(30),accdate varchar(30),fiidreal int,fiid int,objidreal bigint,objid bigint,origin int,value int,used int,suspended int,ready int,changed int,available int,actual int,available int,actual int) '+char(13)+char(10) 
 end
 
  
set @sql=@sql+N'
 ---- 以下是开发人员写的 before new order 事件 --------------
 '
 + dbo.mps_sys_getusercode('cst.gzh',@ctid,0) +N'
 ---- 以上是开发人员写的 before new order 事件 --------------
 
  
 begin
 set @nonedate = @nonedate 
 ' 
 set @s1 = @dbname+@tbname 
 if (dbo.mps_sys_checkkeywords('cst.gzh',@ctid,0,'#master','update')=0) 
 begin
 exec mps_sys_gen_updatestatement @s1,'old','new','cc_id,cc_cmpid,cc_bcid,cc_tag,cc_ctid,cc_createtime,cc_updated',@ss output 
 
 -- 回写主表
 if (len(@ss)>10) 
 set @sql = @sql+char(13)+char(10)+
'update old set '+char(13)+char(10)+@ss + char(13)+char(10)+
'from '+@dbname+@tbname+N' old , #master new  where old.cc_id=new.cc_id ' +char(13)+char(10) 
 end
 

set @sql = @sql +N'
end 
--- 清除临时表 
'

 if (dbo.mps_sys_checkkeywords('cst.gzh',@ctid,0,'#master','')=0) 
 set @sql = @sql+N'exec drop_temp_table ''#master'' '+char(13)+char(10) 
 
if (dbo.mps_sys_checkkeywords('cst.gzh',@ctid,0,'#task','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 ' delete #task where cuid is null or info is null or objid is null  or ctgid is null '+
 ' insert into mps_task(tsk_cmpid,tsk_bcid,tsk_branch,tsk_dpid,tsk_depart,tsk_cuid,tsk_cuname,tsk_date,tsk_type,tsk_objtype,tsk_ctgid,tsk_objid,tsk_objid2,tsk_message) '+char(13)+char(10)+
 ' select distinct cu_cmpid,cu_bcid,cu_branch,cu_dpid,cu_depart,cu_id,cu_callname,getdate(),'+char(13)+char(10)+
 'case when infotype is null then ''普通任务'' else infotype end , '+char(13)+char(10)+
 'case when objtype is null or objtype='''' then ''cst'' else objtype end , '+char(13)+char(10)+
 'case when ctgid is null or ctgid=0 then '+convert(varchar(10),@ctid)+' else ctgid end , '+char(13)+char(10)+
 'case when objid  is null or objid=0 then @newccid else objid end , '+char(13)+char(10)+
 'case when objid2  is null then 0 else objid2 end , '+char(13)+char(10)+
 'case when info  is null then '''' else info end '+char(13)+char(10)+
 'from #task , mps_login where cu_id=cuid '+char(13)+char(10)+
 'exec drop_temp_table ''#task'' '+char(13)+char(10) 
 end
 
 if (dbo.mps_sys_checkkeywords('cst.gzh',@ctid,0,'#account','')=0) 
 begin
 set @sql = @sql+char(13)+char(10)+
 'exec drop_temp_table ''#account'' '+char(13)+char(10) 
 end
 
set @sql = @sql +N'
set nocount off 
end
'
exec myprint @sql 
execute(@sql)
set nocount off 
end