using GxPress.Service.Interface.ArticleLabel;
using GxPress.Repository.Interface.ArticleLabel;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Transactions;
using Datory;
using GxPress.Result.App.ArticleLabel;
using System.Linq;
using GxPress.Common.Exceptions;
namespace GxPress.Service.Implement.ArticleLabel
{
    public class ArticleLabelService : IArticleLabelService
    {
        private readonly IArticleLabelRepository _articleLabelRepository;
        private readonly IArticleInnerLabelRepository _articleInnerLabelRepository;
        public ArticleLabelService(IArticleLabelRepository articleLabelRepository, IArticleInnerLabelRepository articleInnerLabelRepository)
        {
            _articleLabelRepository = articleLabelRepository;
            _articleInnerLabelRepository = articleInnerLabelRepository;
        }
        /// <summary>
        /// 添加标签
        /// </summary>
        /// <param name="labelNames"></param>
        /// <returns></returns>
        public async Task<int> InsertAsync(string labelName)
        {
            try
            {
                var articleLabel = await _articleLabelRepository.GetAsync(Q.Where(nameof(Entity.ArticleLabel.ArticleLabel.LabelName), labelName));
                if (articleLabel != null)
                    throw new BusinessException("标签已存在！");
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    articleLabel = new Entity.ArticleLabel.ArticleLabel();
                    articleLabel.LabelName = labelName;
                    var id = await _articleLabelRepository.InsertAsync(articleLabel);
                    transactionScope.Complete();
                    return id;
                }
            }
            catch (System.Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 获取标签列表
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<Entity.ArticleLabel.ArticleLabel>> GetArticleLabelsAsync()
        {
            return await _articleLabelRepository.GetAllAsync(Q.Where(nameof(Entity.ArticleLabel.ArticleLabel.Id), ">", 0));
        }
        /// <summary>
        /// 添加文章标签关联
        /// </summary>
        /// <param name="articleId"></param>
        /// <param name="articleLabelIds"></param>
        /// <returns></returns>
        public async Task<bool> AddArticleInnerLabel(int articleId, List<int> articleLabelIds)
        {
            try
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    //先删除
                    await _articleInnerLabelRepository.DeleteAsync(Q.Where(nameof(Entity.ArticleLabel.ArticleInnerLabel.ArticleId), articleId));
                    foreach (var articleLabelId in articleLabelIds)
                    {
                        var articleInnerLabel = new Entity.ArticleLabel.ArticleInnerLabel();
                        articleInnerLabel.ArticleId = articleId;
                        articleInnerLabel.ArticleLabelId = articleLabelId;
                        await _articleInnerLabelRepository.InsertAsync(articleInnerLabel);
                    }
                    transactionScope.Complete();
                }
            }
            catch (System.Exception)
            {

                throw;
            }
            return true;
        }
        /// <summary>
        /// 根据文章ID获取文章标签
        /// </summary>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<ArticleInnerLabelResult>> GetArticleLabelsAsync(int articleId)
        {
            return await _articleLabelRepository.GetArticleLabelsAsync(articleId);
        }
        /// <summary>
        /// 获取屏蔽文章
        /// </summary>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<int>> GetShieldArticleIdsAsync(int articleId)
        {
            var articleInnerLabels = await _articleInnerLabelRepository.GetAllAsync(Q.Where(nameof(Entity.ArticleLabel.ArticleInnerLabel.ArticleId), articleId));
            if (articleInnerLabels.Count() == 0)
                return articleInnerLabels.Select(n => n.ArticleId);
            articleInnerLabels = await _articleInnerLabelRepository.GetAllAsync(Q.WhereIn(nameof(Entity.ArticleLabel.ArticleInnerLabel.ArticleLabelId), articleInnerLabels.Select(n => n.ArticleLabelId)));
            return articleInnerLabels.Select(n => n.ArticleId);
        }
    }
}