using Models.ModelItems; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Models.View { public class ViewInit { #region 用户基本信息视图 userinfo static readonly string userinfo = @" create view View_UserInfo as select a.*,d.OrganizationName,c.OrgId,Authoncount = (select count(*) from UserAuthoes where UserAuthoes.UserId = a.Id) from UserInfo a left join OrgUsers c on c.UserId = a.Id left join Organization d on c.OrgId = d.Id --WHERE a.Id IS NOT NULL and d.Id is not null"; #endregion #region 用户-用户组权限信息 view_uoa static readonly string view_uoa = @" create view View_UOA as select distinct u.Id,u.Uid ,HotelId, OrgId = o.id, o.OrganizationName, Orgdesc = o.[Desc] , AppId = app.id, app.AppName, AppDesc = app.[Desc], AuthorityId =a.Id, a.AuthorityName, AuthDesc = a.[Desc] , oa.AuthotypeId, AstName = ast.Name, AstDesc = ast.[Desc], Sfjc = (select count(*) from UserAuthoes where UserId = u.Id and AuthorityId = a.Id and AuthotypeId = oa.AuthotypeId and HotelId = oa.HotelId) from UserInfo u left join OrgUsers ou on u.id = ou.UserId left join Organization o on ou.OrgId = o.Id left join OrgAuthority oa on o.Id = oa.OrgId left join Authority a on a.Id = oa.AuthorityId left join AppAutho aa on a.Id = aa.AuthorityId left join ApplicationDomain app on aa.AppId = app.Id left join AuthoStatusType ast on oa.AuthotypeId = ast.Id where ou.OrgId is not null and oa.AuthorityId is not null "; #endregion # region 用户权限基本信息 view_ua static readonly string view_ua = @" create view View_UA as select Id = ui.id ,HotelId, Uid , AppId = app.id, app.AppName, a.AuthorityName, AppDesc = app.[Desc], AuthorityId = a.Id, AuthDesc = a.[Desc] , ua.AuthotypeId, AstName = ast.Name, AstDesc = ast.[Desc] from UserInfo ui left join UserAuthoes ua on ui.id = ua.UserId left join AppAutho aa on ua.AuthorityId = aa.AuthorityId left join ApplicationDomain app on aa.AppId = app.Id left join Authority a on a.Id = ua.AuthorityId left join AuthoStatusType ast on ua.AuthotypeId = ast.Id where ua.UserId is not null"; #endregion #region 应用权限 view_AppAutho static readonly string view_AppAutho = @" create view View_AppAutho as select AppId = APP.Id, AppName = app.AppName, AppDesc = app.[Desc], AuthorityId, AuthorityName, IsValid, AuthorityDesc = auth.[Desc] from ApplicationDomain app left join AppAutho aa on app.Id = aa.AppId left join Authority auth on aa.AuthorityId = auth.id where aa.AuthorityId is not null "; #endregion #region 触发器 static readonly string org = @"if exists(select * from sys.triggers where name='trigger_OrgAuthority_update') --删除DML触发器 begin drop trigger trigger_OrgAuthority_update; end go --用户组更改权限 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 select @AuthotypeIdold = AuthotypeId from deleted; select @AuthotypeIdnew = AuthotypeId,@AuthoId = AuthorityId,@OrgId = OrgId from inserted; if update(AuthotypeId) begin update UserAuthoes set AuthotypeId = @AuthotypeIdnew where AuthorityId = @AuthoId and AuthotypeId = @AuthotypeIdold and UserId in (select UserId from OrgUsers where OrgId = @OrgId) print @@ROWCOUNT end; go if exists(select * from sys.triggers where name='trigger_OrgAuthority_del') begin drop trigger trigger_OrgAuthority_del; end go --用户组权限删除触发器 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 select @AuthotypeIdold = AuthotypeId, @AuthoId = AuthorityId,@OrgId = OrgId from deleted; delete UserAuthoes where AuthorityId = @AuthoId and AuthotypeId = @AuthotypeIdold and UserId in (select UserId from OrgUsers where OrgId = @OrgId) print @@ROWCOUNT go --用户组新增权限 if exists(select * from sys.triggers where name='trigger_OrgAuthority_Insert') begin drop trigger trigger_OrgAuthority_Insert; end go 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 select @AuthotypeIdnew = AuthotypeId,@AuthoId = AuthorityId,@OrgId = OrgId from inserted; insert into UserAuthoes(AuthorityId,AuthotypeId,UserId) select @AuthoId,@AuthotypeIdnew,UserId from OrgUsers where OrgId = @OrgId print @@ROWCOUNT go "; #endregion public static void Init(AuthorityDB dB) { try { if (dB.OrgUsers.Count() > 0) return; var context = dB; string drop = @"Drop Table View_UserInfo; Drop Table View_UOA; Drop Table View_AppAutho; Drop Table View_UA;"; dB.Database.ExecuteSqlCommand(drop); dB.Database.ExecuteSqlCommand(userinfo); dB.Database.ExecuteSqlCommand(view_ua); dB.Database.ExecuteSqlCommand(view_uoa); dB.Database.ExecuteSqlCommand(view_AppAutho); //dB.Database.ExecuteSqlCommand(org); } catch (Exception ex) { File.AppendAllText(AppContext.BaseDirectory+"\\view.txt", ex.ToString()); } } } }