using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using Datory;
using GxPress.Common.AppOptions;
using GxPress.Common.Tools;
using GxPress.Repository.Interface.AppStartPage;
using Microsoft.Extensions.Options;

namespace GxPress.Repository.Implement.AppStartPage
{
 
    public class AppStartPageRepository : IAppStartPageRepository
    {
        private readonly Repository<Entity.tede2.AppStartPage.AppStartPage> _repository;
        private readonly IMapper _mapper;
        private readonly string _connectionString;
        private readonly string _databaseTypestr;
        public AppStartPageRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
        {
            _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
            _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
            var databaseType =
                StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
            var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
            _repository = new Repository<Entity.tede2.AppStartPage.AppStartPage>(database);
            _mapper = mapper;
        }

        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;

        /// <summary>
        /// 获取 APP启动 1IOS 2Android
        /// </summary>
        /// <param name="appTypeId"></param>
        /// <returns></returns>
        public async Task<Entity.tede2.AppStartPage.AppStartPage> GetAsync(int appTypeId)
        {

            return await _repository.GetAsync(Q.Where(nameof(Entity.tede2.AppStartPage.AppStartPage.AppTypeId), appTypeId).Where(nameof(Entity.tede2.AppStartPage.AppStartPage.IsDisable), false).OrderByDesc(nameof(Entity.tede2.AppStartPage.AppStartPage.CreatedDate)));
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<Entity.tede2.AppStartPage.AppStartPage>> GetAllAsync()
        {
            return await _repository.GetAllAsync(Q.OrderByDesc(nameof(Entity.tede2.AppStartPage.AppStartPage.CreatedDate)));
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            return await _repository.DeleteAsync(id);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(Entity.tede2.AppStartPage.AppStartPage model)
        {
            return await _repository.UpdateAsync(model);
        }
    }
}