using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using GxPress.Common.Address;
using GxPress.Common.AppOptions;
using GxPress.Common.Tools;
using GxPress.Entity;
using GxPress.Repository.Interface;
using GxPress.Result.App.FileLibrary;
using Microsoft.Extensions.Options;
using Datory;

namespace GxPress.Repository.Implement
{
    public class FileLibraryRepository : IFileLibraryRepository
    {

        private readonly Repository<FileLibrary> _repository;
        private readonly IMapper _mapper;

        public FileLibraryRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
        {
            var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
            var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
            _repository = new Repository<FileLibrary>(database);
            _mapper = mapper;
        }

        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        /// <summary>
        /// 添加文件
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<int> InsertAsync(FileLibrary request)
        {
            return await _repository.InsertAsync(request);
        }

        /// <summary>
        /// 根据ID查询文件数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<FileLibraryResult> GetFileLibraryByIdAsync(int id)
        {
            var fileLibrary = await _repository.GetAsync(id);
            fileLibrary.FileUrl = StringUtils.AddDomain(fileLibrary.FileUrl);
            return _mapper.Map<FileLibraryResult>(fileLibrary);
        }

        /// <summary>
        /// 根据ID获取文件数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<IEnumerable<FileLibraryResult>> GetFileLibraryByIdsAsync(string ids)
        {
            var query = Q.NewQuery();
            var list = StringUtils.StringCollectionToIntList(ids);
            foreach (var item in list)
            {
                query.OrWhere(nameof(FileLibrary.Id), item);
            }

            var fileLibraryList = await _repository.GetAllAsync(query);
            foreach (var fileLibrary in fileLibraryList)
            {
                fileLibrary.FileUrl = StringUtils.AddDomain(fileLibrary.FileUrl);
            }

            return fileLibraryList.Select(n => _mapper.Map<FileLibraryResult>(n));
        }

        /// <summary>
        /// 根据用户名获取电脑上传的数据
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<FileLibraryResult>> GetFileLibraryByUserIdAsync(int userId)
        {
            var fileLibraryList = await _repository.GetAllAsync(Q.Where(nameof(Entity.FileLibrary.UserId), userId).OrderByDesc(nameof(Entity.FileLibrary.CreatedDate)));
            return fileLibraryList.Select(n => _mapper.Map<FileLibraryResult>(n));
        }
    }
}
