IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_sync_architechture_branch_dingtalk]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_sync_architechture_branch_dingtalk]
GO
 -- =============================================                  
-- Author:  flobit                  
-- Create date: 2018年3月11日      
-- Description: 通用的同步员工的存储过程,
-- 默认情况是下是根据手机号来进行同步
-- 分公司信息来自 mps_groupbranch这个视图,
-- 所以一定要保证这个视图的信息的正确性
-- =============================================    
CREATE PROC [dbo].[mps_sync_architechture_branch_dingtalk]          
@cmpid int    
AS
BEGIN
 set nocount on

     --1.获取需要删除的分公司,对应钉钉上的部门
 select arch_id,
 case when charindex(arch_name,'__待删除') > 0
 then arch_name
 else arch_name+'__待删除' end arch_name
 from mps_archsyn                  
 where arch_id not in (select bc_dingid from mps_groupbranch where bc_cmpid=@cmpid)  and arch_parentId=1         
 --2.新增                  
 select bc_id,bc_company                  
 from mps_groupbranch 
 where bc_dingid not in (select arch_id from mps_archsyn where arch_parentId = 1) and bc_cmpid=@cmpid                  
 --3.修改                  
 select bc_id,bc_company                  
 from mps_groupbranch,mps_archsyn                  
 where bc_dingid=arch_id and arch_parentId = 1 and bc_cmpid = @cmpid and (arch_name != bc_company)                  
 END