using System.Threading.Tasks;
using GxPress.Request.BlacklistArticle;
using GxPress.Service.Interface.BlacklistArticle;
using GxPress.Repository.Interface;
using GxPress.Common.Exceptions;
using Datory;
using GxPress.Service.Interface.ArticleLabel;

namespace GxPress.Service.Implement.BlacklistArticle
{
    public class BlacklistArticleService : IBlacklistArticleService
    {
        private readonly IUserRepository _userRepository;
        private readonly IArticleRepository _articleRepository;
        private readonly IBlacklistArticleRepository _blacklistArticleRepository;
        private readonly IArticleLabelService _articleLabelService;
        public BlacklistArticleService(IUserRepository userRepository, IArticleRepository articleRepository, IBlacklistArticleRepository blacklistArticleRepository, IArticleLabelService articleLabelService)
        {
            _userRepository = userRepository;
            _articleRepository = articleRepository;
            _blacklistArticleRepository = blacklistArticleRepository;
            _articleLabelService = articleLabelService;
        }
        /// <summary>
        /// 添加屏蔽文章
        /// </summary>
        /// <param name="request"></param>
        ///  <param name="userId"></param>
        /// <returns></returns>
        public async Task<bool> InsertAsync(int userId, BlacklistArticleInRequest request)
        {
            var user = await _userRepository.GetAsync(userId);
            if (user == null)
                throw new BusinessException("用户不存在");
            var article = await _articleRepository.GetAsync(request.ArticleId);
            var blacklistValue = "";
            //验证类目
            if (request.BlacklistType == 1)
                blacklistValue = article.Author;
            if (request.BlacklistType == 2)
            {
                var articleIds = await _articleLabelService.GetShieldArticleIdsAsync(article.Id);
                //删除原来屏蔽
                await _blacklistArticleRepository.DeleteAsync(Q.Where(nameof(Entity.BlacklistArticle.UserId), userId).Where(nameof(Entity.BlacklistArticle.BlacklistType), 2));
                //blacklistValue = article.GroupId.ToString();
                foreach (var item in articleIds)
                {
                    var blacklistArticleDto = new Entity.BlacklistArticle
                    {
                        UserId = userId,
                        BlacklistType = request.BlacklistType,
                        BlacklistValue = item.ToString()
                    };
                    await _blacklistArticleRepository.InsertAsync(blacklistArticleDto);
                }
                return true;
            }
            //查询是否添加过了
            var blacklistArticleEntity = await _blacklistArticleRepository.GetAsync(Q
                .Where(nameof(Entity.BlacklistArticle.UserId), userId)
                .Where(nameof(Entity.BlacklistArticle.BlacklistType), request.BlacklistType)
                .Where(nameof(Entity.BlacklistArticle.BlacklistValue), blacklistValue));
            if (blacklistArticleEntity != null)
            {
                throw new BusinessException("已添加");
            }
            var blacklistArticle = new Entity.BlacklistArticle
            {
                UserId = userId,
                BlacklistType = request.BlacklistType,
                BlacklistValue = blacklistValue
            };
            return await _blacklistArticleRepository.InsertAsync(blacklistArticle);
        }
    }
}