IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_sync_architechture_cleardepart_dingtalk]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_sync_architechture_cleardepart_dingtalk]
GO
-- =============================================
-- Author:flobit
-- Create date: 2018年8月27日20点31分
-- Description:清理应该删除的部门
-- =============================================
CREATE PROCEDURE [dbo].[mps_sync_architechture_cleardepart_dingtalk]
@cmpid int
AS
BEGIN
SET NOCOUNT ON;
--返回需要删除的部门
select * from mps_archsyn
where arch_id not in(

select _arch_id from (
select dp_dingid _arch_id from mps_depart 
where dp_depart is not null and dp_depart!=''
and dp_cmpid=@cmpid
union 
select bc_dingid _arch_id from mps_groupbranch
where bc_cmpid=@cmpid
) _archids

)
and arch_id !=1
--尝试自动绑定钉钉用户
declare @corpid varchar(128) = null
select @corpid = wx_code from ebs_kernel.dbo.pub_weixin where wx_title='通讯录' and wx_type='DING'
if(@corpid is not null)
begin
update mps_login
set cu_ding=@corpid+'|'+user_id
from mps_usersyn
where  user_mobile=cu_mobile
and cu_callname=user_name
end
END