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 GxPress.Request.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()
        {
            var result= await _repository.GetAllAsync(Q.OrderByDesc(nameof(Entity.tede2.AppStartPage.AppStartPage.CreatedDate)));
            foreach (var item in result)
            {
                item.ImageUrl=StringUtils.AddDomain(item.ImageUrl);
            }
            return result;
        }
        /// <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(AppStartPageUpdateRequest request)
        {
            var model = await _repository.GetAsync(request.Id);
            if (!string.IsNullOrEmpty(request.ImageUrl))
                model.ImageUrl = StringUtils.RemoveDomain(request.ImageUrl);
            if (request.IsDisable > 0)
                model.IsDisable = request.IsDisable == 1;
            if (request.Second > 0)
                model.Second = request.Second;
            if (!string.IsNullOrEmpty(request.Title))
                model.Title = request.Title;
            if (request.TypeId > 0)
                model.TypeId = request.TypeId;
            if (!string.IsNullOrEmpty(request.Url))
                model.Url = request.Url;
            if (request.AdminId > 0)
                model.AdminId = request.AdminId;
            if (request.AppTypeId > 0)
                model.AppTypeId = request.AppTypeId;
            if (!string.IsNullOrEmpty(request.Creator))
                model.Creator = request.Creator;
            if (request.DataId > 0)
                model.DataId = request.DataId;
            if (request.DataTypeId > 0)
                model.DataTypeId = request.DataTypeId;
            return await _repository.UpdateAsync(model);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> InsertAsync(Entity.tede2.AppStartPage.AppStartPage model)
        {
            return await _repository.InsertAsync(model) > 0;
        }
    }
}