IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_sync_architechture_depart_wechat]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_sync_architechture_depart_wechat]
GO
-- =============================================            
-- Author:  flobit            
-- Create date: 2018年2月6日            
-- Description: 通用的同步员工的存储过程,默认情况是下是根据手机号来进行同步,如果不能满足需求则可以重新创建名为mps_syn_architechture_+cmpid的实现            
-- =============================================            
CREATE PROCEDURE [dbo].[mps_sync_architechture_depart_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 dp_id+100000000 as dp_id from mps_depart where dp_cmpid=@cmpid) and arch_parentId > 1         
--2.新增            
select dp_id+100000000 as dp_id ,dp_depart,case when dp_bcid=0 then @cmpbcid else dp_bcid end as dp_bcid            
from mps_depart            
where dp_id+100000000 not in (select arch_id from mps_archsyn where arch_parentId > 1) and dp_cmpid=@cmpid            
--3.修改            
select dp_id+100000000 as dp_id,dp_depart,case when dp_bcid=0 then @cmpbcid else dp_bcid end as dp_bcid                  
from mps_depart,mps_archsyn            
where dp_id+100000000 =arch_id and arch_parentId > 1 and dp_cmpid = 1 and  (( dp_bcid!=0 and (arch_name != dp_depart or arch_parentId !=dp_bcid)) or (dp_bcid=0 and arch_name != dp_depart))    
END