using System.Threading.Tasks;
using Datory;
using GxPress.Entity.Topic;
using GxPress.Repository.Interface;
using GxPress.Repository.Interface.Note;
using GxPress.Repository.Interface.Visit;
using GxPress.Service.Interface.Visit;
namespace GxPress.Service.Implement.Visit
{
    /// <summary>
    /// 访问服务
    /// </summary>
    public class VisitService : IVisitService
    {
        private readonly IVisitRepository _visitRepository;
        private readonly ITopicRepository _topicRepository;
        private readonly INoteRepository _noteRepository;
        private readonly ITopicAddresseeRepository _topicAddresseeRepository;
        private readonly IArticleRepository _articleRepository;
        public VisitService(IVisitRepository visitRepository, ITopicRepository topicRepository, INoteRepository noteRepository, ITopicAddresseeRepository topicAddresseeRepository, IArticleRepository articleRepository)
        {
            _visitRepository = visitRepository;
            _topicRepository = topicRepository;
            _noteRepository = noteRepository;
            _topicAddresseeRepository = topicAddresseeRepository;
            _articleRepository = articleRepository;
        }
        /// <summary>
        /// /// 添加/数据条数
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="typeId">类型ID 类型1文章 2话题 3 收藏 4笔记 5通知 6站内信 7小组 8 会议详情</param>
        /// <param name="sourceId"></param>
        /// <returns></returns>
        public async Task<bool> AddVisit(int userId, int typeId, int sourceId)
        {
            var visitBool = await _visitRepository.ExistsAsync(Q.Where(nameof(Entity.Visit.Visit.TypeId), typeId).Where(nameof(Entity.Visit.Visit.SourceId), sourceId).Where(nameof(Entity.Visit.Visit.UserId), userId));
            if (visitBool == false)
            {
                var visit = new Entity.Visit.Visit();
                visit.TypeId = typeId;
                visit.UserId = userId;
                visit.SourceId = sourceId;
                await _visitRepository.InsertAsync(visit);
                //修改访问量数量
                if (typeId == 1)
                {
                    var article = await _articleRepository.GetAsync(sourceId);
                    article.ReadCount++;
                    await _articleRepository.UpdateAsync(article);
                }
                //修改访问量数量
                if (typeId == 2)
                {
                    var topic = await _topicRepository.GetAsync(sourceId);
                    topic.ReadCount++;
                    await _topicRepository.UpdateAsync(topic);
                    //修改
                    await _topicAddresseeRepository.UpdateAsync(Q.Set(nameof(TopicAddressee.IsRead), true)
                  .Where(nameof(TopicAddressee.TopicId), sourceId)
                  .Where(nameof(TopicAddressee.UserId), userId));
                }
                if (typeId == 4)
                {
                    var note = await _noteRepository.GetAsync(sourceId);
                    note.ReadCount++;
                    await _noteRepository.UpdateAsync(note);
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取访问数量
        /// </summary>
        /// <param name="typeId"></param>
        /// <param name="sourceId"></param>
        /// <returns></returns>
        public async Task<int> GetCountAsync(int typeId, int sourceId)
        {
            return await _visitRepository.CountAsync(Q.Where(nameof(Entity.Visit.Visit.TypeId), typeId).Where(nameof(Entity.Visit.Visit.SourceId), sourceId));
        }
    }
}