IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_csttype_update]') AND type = 'TR')
DROP TRIGGER [dbo].[mps_csttype_update]
GO
CREATE trigger [dbo].[mps_csttype_update] on [dbo].[mps_csttype] for update as
begin
    declare
       @maxcard  int,
       @numrows  int,
       @numnull  int,
       @errno    int,
       @errmsg   varchar(255)

    select  @numrows = @@rowcount
    if @numrows = 0
       return

declare @cmpid int 
select top 1 @cmpid = ct_cmpid from  inserted  
set @cmpid = isnull(@cmpid,0)

update mps_tableupdated set tu_updated=0 where  tu_table='mps_csttype' and tu_cmpid=@cmpid 

-- 以前是不能登录，现在是要登录

-- 首先调整角色

insert into mps_role(rl_cmpid,rl_value,rl_system,rl_title) 
select n.ct_cmpid , 1000000+n.ct_id,1,n.ct_type from inserted n, deleted o 
where  n.ct_id=o.ct_id and 
n.ct_allowlogin<>o.ct_allowlogin and n.ct_allowlogin=0 and 
(not exists (select 1 from mps_role where rl_value=1000000+n.ct_id))


-- 增加部门_base 
insert into mps_depart_base(dp_cmpid,dp_code,dp_depart,dp_order,dp_level,dp_fromcc) 
select n.ct_cmpid , CONVERT(varchar(10),n.ct_id),n.ct_type,100000+n.ct_id,0,n.ct_id from inserted n, deleted o 
where  n.ct_id=o.ct_id and 
n.ct_allowlogin<>o.ct_allowlogin and n.ct_allowlogin=0 and 
(not exists (select 1 from mps_depart_base where dp_cmpid=n.ct_cmpid and  dp_fromcc=n.ct_id))

-- 增加部门
insert into mps_depart(dp_cmpid,dp_bcid,dp_code,dp_innercode,dp_depart,dp_fullname,dp_order,dp_level,dp_dpstdid,dp_display,dp_isdepart,dp_use,dp_fromcc,dp_loginscope) 
select distinct a.dp_cmpid ,g.bc_id , a.dp_code , a.dp_code , a.dp_depart,a.dp_depart,a.dp_order,a.dp_level,a.dp_id,a.dp_depart,0,0,a.dp_fromcc ,b.ct_loginscope
from mps_depart_base a with(nolock)   , inserted  b , mps_groupbranch g
where a.dp_cmpid=b.ct_cmpid and a.dp_fromcc=b.ct_id and g.bc_cmpid=a.dp_cmpid and 
(not exists (select 1 from mps_depart d with(nolock) where d.dp_cmpid=a.dp_cmpid and d.dp_bcid=g.bc_id and d.dp_dpstdid=a.dp_id))

-- 如果是改了名称，则修改 mps_role mps_depart_base mps_depart 
update mps_role set rl_title=n.ct_type 
from inserted n, deleted o
where rl_cmpid=@cmpid and rl_value=1000000+n.ct_id and 
n.ct_id=o.ct_id and 
n.ct_type<>o.ct_type 

update mps_depart_base set dp_depart=n.ct_type 
from inserted n, deleted o
where dp_cmpid=@cmpid and  
n.ct_id=o.ct_id and 
n.ct_type<>o.ct_type and 
dp_fromcc = n.ct_id  

update mps_depart  set dp_depart=n.ct_type ,dp_loginscope=n.ct_loginscope
from inserted n, deleted o
where dp_cmpid=@cmpid and  
n.ct_id=o.ct_id and 
n.ct_type<>o.ct_type and 
dp_fromcc = n.ct_id  

--  如果以前是允许登录，现在是不允许登录，则要删除
delete mps_role from inserted n, deleted o 
where rl_cmpid=@cmpid and rl_value=1000000+n.ct_id  and 
n.ct_id=o.ct_id and 
n.ct_allowlogin<>o.ct_allowlogin and n.ct_allowlogin=1 


declare @dp table(dpid int) 
insert into @dp(dpid) 
select dp_id from mps_depart , inserted n, deleted o 
where dp_cmpid=n.ct_cmpid and dp_fromcc=n.ct_id   and 
n.ct_id=o.ct_id and 
n.ct_allowlogin<>o.ct_allowlogin and n.ct_allowlogin=1  

-- 删除mps_depart_base表 
delete mps_depart_base 
from inserted n, deleted o  
where dp_cmpid=n.ct_cmpid and dp_fromcc=n.ct_id and 
n.ct_id=o.ct_id and 
n.ct_allowlogin<>o.ct_allowlogin and n.ct_allowlogin=1  

-- 删除mps_depart表 
delete mps_depart from @dp  where dp_id=dpid 

-- 删除mps_employee 
delete mps_employee from @dp where cu_dpid=dpid 

-- 清除 mps_login表 
delete mps_login from @dp 
where cu_dpid=dpid 

-- 清除 mps_login_role 
delete mps_login_role from @dp where lgu_dpid=dpid 

    return

/*  Errors handling  */
error:
    -- raiserror @errno @errmsg
    rollback  transaction
end