

namespace CloudGaming.Code.DataAccess
{
    /// <summary>
    /// 数据库访问，开启事务只能一个库的开启
    /// </summary>
    public class DAO : DaoBase
    {
        private ITenantInfo? _tenantInfo;

        /// <summary>
        /// 
        /// </summary>
        public ITenantInfo TenantInfo
        {
            get
            {
                if (_tenantInfo == null)
                {
                    var app = _serviceProvider.GetRequiredService<AppConfig>();
                    this._tenantInfo = app.ToITenantInfo();
                }
                return this._tenantInfo;
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="serviceProvider"></param>
        public DAO(IServiceProvider serviceProvider) : base(serviceProvider)
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="serviceProvider"></param>
        public DAO(IServiceProvider serviceProvider, AppConfig appConfig) : base(serviceProvider)
        {
            this._tenantInfo = appConfig.ToITenantInfo();
        }

        #region 扩展
        private EfCoreDaoBase<CloudGamingCBTContext>? _daoExt;
        /// <summary>
        /// 数据库[CloudGamingCBT]，
        /// 更新删除，尽量只操作本bll实例dao获取到的对象，取和存要同一个dao
        /// </summary>
        public EfCoreDaoBase<CloudGamingCBTContext> DaoExt
        {
            get
            {
                if (_daoExt == null)
                {
                    var dbContext = _serviceProvider.GetRequiredService<CloudGamingCBTContext>();
                    _daoExt = new EfCoreDaoBase<CloudGamingCBTContext>(dbContext);
                    _daoExt.Context.TenantInfo = TenantInfo;
                }
                return _daoExt;
            }
        }
        #endregion

        #region 游戏
        private EfCoreDaoBase<CloudGamingGameContext>? _daoGame;
        /// <summary>
        /// 数据库[CloudGamingGameContext]，
        /// 更新删除，尽量只操作本bll实例dao获取到的对象，取和存要同一个dao
        /// </summary>
        public EfCoreDaoBase<CloudGamingGameContext> DaoGame
        {
            get
            {
                if (_daoGame == null)
                {
                    var dbContext = _serviceProvider.GetRequiredService<CloudGamingGameContext>();
                    _daoGame = new EfCoreDaoBase<CloudGamingGameContext>(dbContext);
                    _daoGame.Context.TenantInfo = TenantInfo;
                }
                return _daoGame;
            }
        }
        #endregion

        #region app配置
        private EfCoreDaoBase<CloudGamingPhoneContext>? _daoPhone;
        /// <summary>
        /// 数据库[CloudGamingGameContext]，
        /// 更新删除，尽量只操作本bll实例dao获取到的对象，取和存要同一个dao
        /// </summary>
        public EfCoreDaoBase<CloudGamingPhoneContext> DaoPhone
        {
            get
            {
                if (_daoPhone == null)
                {
                    var dbContext = _serviceProvider.GetRequiredService<CloudGamingPhoneContext>();
                    _daoPhone = new EfCoreDaoBase<CloudGamingPhoneContext>(dbContext);
                    _daoPhone.Context.TenantInfo = TenantInfo;
                }
                return _daoPhone;
            }
        }
        #endregion

        #region 用户
        private EfCoreDaoBase<CloudGamingUserContext>? _daoUser;
        /// <summary>
        /// 数据库[CloudGamingGameContext]，
        /// 更新删除，尽量只操作本bll实例dao获取到的对象，取和存要同一个dao
        /// </summary>
        public EfCoreDaoBase<CloudGamingUserContext> DaoUser
        {
            get
            {
                if (_daoUser == null)
                {
                    var dbContext = _serviceProvider.GetRequiredService<CloudGamingUserContext>();
                    _daoUser = new EfCoreDaoBase<CloudGamingUserContext>(dbContext);
                    _daoUser.Context.TenantInfo = TenantInfo;
                }
                return _daoUser;
            }
        }
        #endregion

    }
}
