DROP view
IF
	EXISTS View_UOA;
	DROP view
IF
	EXISTS View_UA;
	DROP view
IF
	EXISTS View_AppAutho;
	DROP view
IF
	EXISTS View_UserInfo;
	
		DROP view
IF
	EXISTS View_MAClog;
	
	
	
create view View_UOA
                        as 
                        select distinct u.Id,u.Uid ,oa.HotelId,
                        o.id OrgId,
                        o.OrganizationName,
                        o.`Desc` Orgdesc,
                        app.id AppId ,
                        app.AppName,
												app.`Desc` AppDesc ,
												a.Id AuthorityId,
                        a.AuthorityName,
                        a.`Desc` AuthDesc,
                        oa.AuthotypeId,
                        ast.Name  AstName,
                        ast.`Desc`  AstDesc,
                        (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;

create view View_UA
                                as 
                                select ui.id Id  ,ui.IsValid IsValid  ,ua.HotelId,
                                Uid , 
                                app.id AppId , 
                                app.AppName,
                                a.AuthorityName,
																app.`Desc`  AppDesc ,
                                a.Id AuthorityId,
                                a.`Desc` AuthDesc,
                                ua.AuthotypeId,
                               ast.Name AstName,
                               ast.`Desc` AstDesc
                                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;

create view View_AppAutho
                        as 
                        select APP.Id AppId,
                        app.AppName AppName ,
												app.`Desc` AppDesc,
                        AuthorityId,
                        AuthorityName,
                        app.IsValid IsValid,
                       auth.`Desc` AuthorityDesc 
                        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;

create view View_UserInfo
        as 
        select a.*,d.OrganizationName,c.OrgId,(select count(*) from UserAuthoes where UserAuthoes.UserId = a.Id and UserAuthoes.AuthotypeId!=1) Authoncount from UserInfo a 
        left join
        OrgUsers c
        on c.UserId = a.Id
        left join 
        Organization d
        on 
        c.OrgId = d.Id;
				
				
				
				
				
create view View_MAClog 
as 
SELECT   m.Id, m.HotelID, m.MAC, m.userid, m.location, m.Ip, m.ActionId, m.type, m.CreateTime, m.roomNumber, m.`Status`, u.Uid, 
                ht.`Name`, ht.Id AS Hotelsid ,m.AppType
FROM      MACLogs AS m LEFT OUTER JOIN
                Hotels AS ht ON ht.Id = m.HotelID LEFT OUTER JOIN
                UserInfo AS u ON u.Id = m.userid;