using System;
using System.Collections.Generic;
using System.Linq;
using Infrastructure;
using OpenAuth.App.Base;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.Response;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using SqlSugar;

namespace OpenAuth.App
{
    /// <summary>
    /// 领域服务
    /// <para>超级管理员权限</para>
    /// <para>超级管理员使用-1，可以根据需要修改</para>
    /// </summary>
    public class SystemAuthStrategy : SqlSugarBaseApp<SysUser, SugarDbContext>, IAuthStrategy
    {
        protected SysUser _user;

        public List<ModuleView> Modules
        {
            get
            {
                return Repository.ChangeRepository<SugarRepositiry<SysModule>>().AsQueryable()
                        .Includes(a => a.Elements)
                        .Select(a => new ModuleView
                        {
                            Elements = a.Elements
                        }, true)
                        .ToList();
            }
        }

        public List<SysRole> Roles
        {
            get { return Repository.ChangeRepository<SugarRepositiry<SysRole>>().GetList(); }
        }

        public List<SysModuleElement> ModuleElements
        {
            get { return Repository.ChangeRepository<SugarRepositiry<SysModuleElement>>().GetList(); }
        }

        public List<SysResource> Resources
        {
            get { return Repository.ChangeRepository<SugarRepositiry<SysResource>>().GetList(); }
        }

        public List<SysOrg> Orgs
        {
            get { return Repository.ChangeRepository<SugarRepositiry<SysOrg>>().GetList(); }
        }

        public List<SysPosition> Positions
        {
            get { return Repository.ChangeRepository<SugarRepositiry<SysPosition>>().GetList(); }
        }

        public SysUser User
        {
            get { return _user; }
            set   //禁止外部设置
            {
                throw new Exception("超级管理员，禁止设置用户");
            }
        }



        public SystemAuthStrategy(ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<SysUser> repository) : base(unitWork, repository, null)
        {
            _user = new SysUser
            {
                Account = Define.SYSTEM_USERNAME,
                Name = "超级管理员",
                Id = -1
            };
        }
    }
}