using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using GxPress.Common.AppOptions;
using GxPress.Common.Exceptions;
using GxPress.Common.Tools;
using GxPress.Entity;
using GxPress.Repository.Interface;
using GxPress.Request.ArticleGroupUser;
using GxPress.Result.ArticleGroupUser;
using Microsoft.Extensions.Options;
using Datory;

namespace GxPress.Repository.Implement
{
    public class ArticleGroupUserRepository : IArticleGroupUserRepository
    {
        private readonly Repository<ArticleGroupUser> _repository;
        private readonly Repository<ArticleGroup> _articleGroupRepository;
        private readonly Repository<User> _userRepository;
        private readonly IMapper _mapper;

        public ArticleGroupUserRepository(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<ArticleGroupUser>(database);
            _articleGroupRepository = new Repository<ArticleGroup>(database);
            _userRepository = new Repository<User>(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(ArticleGroupUserInRequest request)
        {
            var user = await _userRepository.GetAsync(request.UserId);
            if (user == null)
                throw new BusinessException("用户不存在");
            var articleGroup = await _articleGroupRepository.GetAsync(request.ArticleGroupId);
            if (articleGroup == null)
                throw new BusinessException("频道不存在");
            //查询是否存在
            var articleGroupUserCount = await _repository.CountAsync(Q.Where(nameof(ArticleGroupUser.UserId), request.UserId)
                .Where(nameof(ArticleGroupUser.ArticleGroupId), request.ArticleGroupId));
            if (articleGroupUserCount > 0)
                throw new BusinessException("频道已存在");
            var articleGroupUserEntity = new ArticleGroupUser
            {
                ArticleGroupId = request.ArticleGroupId,
                UserId = request.UserId
            };
            return await _repository.InsertAsync(articleGroupUserEntity);
        }

        /// <summary>
        /// 删除用户频道
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            var articleGroup = await _repository.GetAsync(id);
            if (articleGroup == null)
                throw new BusinessException("频道不存在");
            return await _repository.DeleteAsync(id);
        }

        /// <summary>
        /// 根据用户ID查询用户的频道
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<IEnumerable<ArticleGroupUserListResult>> GetListAsync(ArticleGroupUserSearchRequest request)
        {
            var list = await _repository.GetAllAsync(Q.Where(nameof(ArticleGroupUser.UserId), request.UserId));
            var listArray = list.Select(n => _mapper.Map<ArticleGroupUserListResult>(n)).ToList();
            foreach (var item in listArray)
            {
                var articleGroup = await _articleGroupRepository.GetAsync(item.ArticleGroupId);
                item.ArticleGroupName = articleGroup.Name;
            }

            return listArray;
        }
    }
}
