IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_sync_architechture_branch_wechat]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_sync_architechture_branch_wechat]
GO
-- =============================================            
-- Author:  flobit            
-- Create date: 2018年2月6日            
-- Description: 通用的同步员工的存储过程,默认情况是下是根据手机号来进行同步,如果不能满足需求则可以重新创建名为mps_syn_architechture_+cmpid的实现            
-- =============================================            
CREATE PROCEDURE [dbo].[mps_sync_architechture_branch_wechat]
 @cmpid int            
AS            
BEGIN            
SET NOCOUNT ON;            
declare @cmpbcid int        
declare @cmpbcname nvarchar(100)      
set @cmpbcid=999999999        
set @cmpbcname='集团总部'      
--比对分公司信息   
--1.删除            
select arch_id
from mps_archsyn
where arch_id not in (select bc_id from (select bc_id,bc_cmpid,bc_company from mps_branch union select @cmpbcid as bc_id ,@cmpid as bc_cmpid,@cmpbcname as bc_company)  AS A where bc_cmpid=@cmpid)  and arch_parentId=1   
--2.新增            
select bc_id,bc_company
from (select bc_id,bc_cmpid,bc_company from mps_branch union select @cmpbcid as bc_id ,@cmpid as bc_cmpid,@cmpbcname as bc_company)  AS A        
where bc_id not in (select arch_id from mps_archsyn where arch_parentId = 1) and bc_cmpid=@cmpid            
--3.修改            
select bc_id,bc_company
from (select bc_id,bc_cmpid,bc_company from mps_branch union select @cmpbcid as bc_id ,@cmpid as bc_cmpid,@cmpbcname as bc_company)  AS A,mps_archsyn            
where bc_id=arch_id and arch_parentId = 1 and bc_cmpid = @cmpid and (arch_name != bc_company)            
END