Files
Web_AuthorityManagement_Mvc…/Models/SQLQuery1.sql
2025-11-20 09:51:24 +08:00

131 lines
5.0 KiB
Transact-SQL
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
--用户组触发器
exec ('
if exists(select * from sys.triggers where name=''trigger_OrgAuthority_update'')
--删除DML触发器
begin
drop trigger trigger_OrgAuthority_update;
end
')
exec ('
--用户组更改权限 ------------------------改
create trigger trigger_OrgAuthority_update
  on OrgAuthority
  for update
  as
declare @AuthotypeIdnew int; --从临时表Inserted记录新的ID
declare @AuthotypeIdold int; --从临时表Delete记录更新以前旧的ID
declare @AuthoId int; --从临时表Inserted记录权限ID
declare @OrgId int; --从临时表Inserted记录用户组ID
declare @HotelId int;
select @AuthotypeIdold = AuthotypeId
from deleted;
select @AuthotypeIdnew = AuthotypeId,@AuthoId = AuthorityId,@OrgId = OrgId,@HotelId = HotelId
from inserted;
if update(AuthotypeId)
begin
update UserAuthoes set AuthotypeId = @AuthotypeIdnew where HotelId = @HotelId and AuthorityId = @AuthoId and AuthotypeId = @AuthotypeIdold and UserId in (select UserId from OrgUsers where OrgId = @OrgId)
end;
')
exec('
if exists(select * from sys.triggers where name=''trigger_OrgAuthority_del'')
begin
drop trigger trigger_OrgAuthority_del;
end
')
exec('
--用户组权限删除触发器 ------------------------改
create trigger trigger_OrgAuthority_del
  on OrgAuthority
  for delete
  as
declare @AuthotypeIdold int; --从临时表Delete记录更新以前旧的ID
declare @AuthoId int; --从临时表Inserted记录权限ID
declare @OrgId int; --从临时表Inserted记录用户组ID
declare @HotelId int;
select @AuthotypeIdold = AuthotypeId, @AuthoId = AuthorityId,@OrgId = OrgId,@HotelId = HotelId
from deleted;
delete UserAuthoes where HotelId = @HotelId and AuthorityId = @AuthoId and AuthotypeId = @AuthotypeIdold and UserId in (select UserId from OrgUsers where OrgId = @OrgId)
')
exec('
--用户组新增权限 ------------------------改
if exists(select * from sys.triggers where name=''trigger_OrgAuthority_Insert'')
begin
drop trigger trigger_OrgAuthority_Insert;
end
')
exec('
create trigger trigger_OrgAuthority_Insert --- 组新增 ---------------------------- 用户未有用户会新增 已改
  on OrgAuthority
  for insert
  as
declare @AuthotypeIdnew int; --从临时表Inserted记录新的ID
declare @AuthoId int; --从临时表Inserted记录权限ID
declare @OrgId int; --从临时表Inserted记录用户组ID
declare @HotelId int;
select @AuthotypeIdnew = AuthotypeId,@AuthoId = AuthorityId,@OrgId = OrgId,@HotelId = HotelId
from inserted;
insert into UserAuthoes(AuthorityId,AuthotypeId,UserId, HotelId) select @AuthoId,@AuthotypeIdnew,UserId,@HotelId from OrgUsers where OrgId = @OrgId and (select count(*) from UserAuthoes where UserId = OrgUsers.UserId and AuthorityId = @AuthoId and HotelId = @HotelId)<0
')
exec('
--用户加入用户组 用户加入用户组时期选择权限 更新权限赋值
if exists(select * from sys.triggers where name=''trigger_Orguserinfo_Insert'')
begin
drop trigger trigger_Orguserinfo_Insert
end
')
exec('
create trigger trigger_Orguserinfo_Insert ------------------------改
  on OrgUsers
  for insert
  as
declare @userid int; --从临时表Inserted 记录用户id
declare @OrgId int; --从临时表Inserted记录用户组ID
select @userid = UserId,@OrgId = OrgId
from inserted;
--添加没有的权限
insert into UserAuthoes(AuthorityId,AuthotypeId,UserId,HotelId)
select AuthorityId,AuthotypeId,@userid,HotelId from OrgAuthority
where OrgId = @OrgId and
(select count(*) from UserAuthoes where UserId = @userid and AuthorityId = OrgAuthority.AuthorityId and HotelId = OrgAuthority.HotelId) = 0
')
--select * from View_UOA
--用户变更用户组 新增用户时期选择权限 更新权限赋值
exec('
if exists(select * from sys.triggers where name=''trigger_Orguserinfo_Updata'')
begin
drop trigger trigger_Orguserinfo_Updata
end
')
exec('
create trigger trigger_Orguserinfo_Updata
  on OrgUsers
  for update
  as
declare @userid int; --从临时表Inserted 记录用户id
declare @OrgId int; --从临时表Inserted记录变更用户组ID
declare @oldOrgId int; --从临时表Inserted记录变更用户组ID
select @userid = UserId,@OrgId = OrgId
from inserted;
select @oldOrgId = OrgId
from deleted;
--删除旧权限
delete UserAuthoes where (
select count(*) from OrgAuthority where UserAuthoes.UserId = @userid
and UserAuthoes.HotelId = HotelId
and UserAuthoes.AuthotypeId = AuthotypeId
and UserAuthoes.AuthorityId = AuthorityId
)>0
--新增权限
insert into UserAuthoes(AuthorityId,AuthotypeId,UserId,HotelId)
select AuthorityId,AuthotypeId,@userid,HotelId from OrgAuthority
where OrgId = @OrgId and
(select count(*) from UserAuthoes where UserId = @userid and AuthorityId = OrgAuthority.AuthorityId and HotelId = OrgAuthority.HotelId) = 0
')
insert into OrgUsers(OrgId,UserId,CreateTime) select 1,Id,GETDATE() from UserInfo where id not in (select UserId from OrgUsers)