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

namespace GxPress.Repository.Implement.RecordFolder
{
    public class RecordFolderRepository : IRecordFolderRepository
    {
        private readonly Repository<Entity.RecordFolder.RecordFolder> _repository;
        private readonly Repository<Entity.Middle.Middle> _middleRepository;

        private readonly IMapper _mapper;
        public RecordFolderRepository(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<Entity.RecordFolder.RecordFolder>(database);
            _middleRepository = new Repository<Entity.Middle.Middle>(database);
            _mapper = mapper;
        }
        public IDatabase Database => _repository.Database;
        public string TableName => _repository.TableName;
        public List<TableColumn> TableColumns => _repository.TableColumns;
        /// <summary>
        /// 获取最新的
        /// </summary>
        /// <param name="folderType"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<Entity.RecordFolder.RecordFolder> GetAsync(int folderType, int userId)
        {
            var recordFolder = await _repository.GetAsync(Q.Where(nameof(Entity.RecordFolder.RecordFolder.UserId), userId).Where(nameof(Entity.RecordFolder.RecordFolder.FolderType), folderType));
            if (recordFolder == null)
            {
                recordFolder = new Entity.RecordFolder.RecordFolder();
                recordFolder.FolderId = 0;
            }
            //获取文件夹
            var middle = await _middleRepository.GetAsync(recordFolder.FolderId);
            recordFolder.FolderName = middle != null ? middle.FolderName : "";
            recordFolder.FolderId = middle != null ? middle.Id : 0;
            return recordFolder;
        }
        /// <summary>
        /// 添加/修改
        /// </summary>
        /// <param name="folderType"></param>
        /// <param name="userId"></param>
        /// <param name="folderId"></param>
        /// <returns></returns>
        public async Task<bool> AddAsync(int folderType, int userId, int folderId)
        {
            var recordFolder = await _repository.GetAsync(Q.Where(nameof(Entity.RecordFolder.RecordFolder.UserId), userId).Where(nameof(Entity.RecordFolder.RecordFolder.FolderType), folderType));
            var middle = await _middleRepository.GetAsync(folderId);
            if (middle == null)
                return true;
            if (folderType == 21)
            {
                if (middle.RoleId == 2)
                    throw new Common.Exceptions.BusinessException("文件夹私有");
            }
            if (recordFolder == null)
            {
                //添加
                recordFolder = new Entity.RecordFolder.RecordFolder();
                recordFolder.UserId = userId;
                recordFolder.FolderId = folderId;
                recordFolder.FolderType = folderType;
                return await _repository.InsertAsync(recordFolder) > 0;
            }
            //修改
            recordFolder.FolderId = folderId;
            recordFolder.FolderName = middle.FolderName;
            return await _repository.UpdateAsync(recordFolder);
        }

        public async Task<bool> DeleteAsync(int folderType, int userId, List<int> folderIds)
        {
            return await _repository.DeleteAsync(Q.Where(nameof(Entity.RecordFolder.RecordFolder.UserId), userId).Where(nameof(Entity.RecordFolder.RecordFolder.FolderType), folderType).WhereIn(nameof(Entity.RecordFolder.RecordFolder.Id), folderIds)) > 0;
        }
    }
}