IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_sync_architechture_user_wechat]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_sync_architechture_user_wechat]
GO
 -- =============================================                  
-- Author:  flobit                  
-- Create date: 2018年3月11日      
-- Description: 通用的同步员工的存储过程,默认情况是下是根据手机号来进行同步,如果不能满足需求则可以重新创建名为mps_syn_architechture_+cmpid的实现                  
-- =============================================    
CREATE PROC [dbo].[mps_sync_architechture_user_wechat]
@cmpid int
as
begin
 declare @cmpbcid int
 declare @cmpbcname varchar(128)
 set @cmpbcname='集团总部'
 select @cmpbcid=999999999
 --比对员工信息
 --假设是以cu_account作为编号
 --1.删除
 select user_id
 from mps_usersyn
 where user_mobile not in (select cu_mobile from mps_login where cu_cmpid=@cmpid and cu_mobile!='') /*or user_id not in (select cu_account from mps_login where cu_cmpid=@cmpid )      */
 --2.新增
 select cu_account as user_id,cu_callname,cu_gender,cu_mobile,cu_dpids,cu_title
 from mps_login,(select cu_id as dps_cuid,STUFF(REPLACE(REPLACE(stuff((select distinct lgu_dpid as i from mps_login_role where lgu_cuid=cu_id order by lgu_dpid for xml path('')),1,0,''),'<i>',','),'</i>',''),1,1,'') as cu_dpids from mps_login) as A       
 where cu_cmpid=@cmpid  and cu_mobile!=''  and dps_cuid=cu_id and ( cu_mobile not in (select user_mobile from mps_usersyn) /*or cu_account not in (select user_id from mps_usersyn)*/)      
 --3.修改                
 select distinct user_id ,cu_callname,cu_gender,cu_mobile,cu_dpids,cu_title
 from mps_login,mps_usersyn,(select cu_id as dps_cuid,STUFF(REPLACE(REPLACE(stuff((select distinct lgu_dpid as i from mps_login_role where lgu_cuid=cu_id order by lgu_dpid for xml path('')),1,0,''),'<i>',','),'</i>',''),1,1,'') as cu_dpids from mps_login) as A                
 where cu_cmpid=@cmpid and dps_cuid=cu_id and cu_mobile!='' and cu_mobile=user_mobile and (user_name!=cu_callname or user_gender!=cu_gender or cu_title!=user_position or cu_dpids!=user_dpids)                
end