using System.Threading.Tasks;
using GxPress.Common.Tools;
using GxPress.Repository.Interface;
using GxPress.Repository.Interface.Missive;
using GxPress.Service.Interface.ElasticSearch;
using GxPress.Repository.Interface.Job;
using Datory;
using System.Linq;
using System;
namespace GxPress.Service.Implement.ElasticSearch
{
    public class ElasticSearchService : IElasticSearchService
    {
        private readonly IUserRepository _userRepository;
        private readonly INoticeRepository _noticeRepository;
        private readonly IChatRecordRepository _chatRecordRepository;
        private readonly IArticleRepository _articleRepository;
        private readonly IGroupChatRepository _groupChatRepository;
        private readonly IMissiveRepository _missiveRepository;
        private readonly ITopicRepository _topicRepository;
        private readonly IJobRepository _jobRepository;
        private readonly IMiddleRepository middleRepository;
        private readonly ITopicAddresseeRepository topicAddresseeRepository;
        private readonly IGroupRepository groupRepository;

        private readonly IOftenContactRepository oftenContactRepository;
        public ElasticSearchService(IUserRepository userRepository, INoticeRepository noticeRepository,
            IChatRecordRepository chatRecordRepository, IArticleRepository articleRepository, IGroupChatRepository groupChatRepository, IMissiveRepository missiveRepository, ITopicRepository topicRepository, IJobRepository jobRepository, ITopicAddresseeRepository topicAddresseeRepository, IMiddleRepository middleRepository, IGroupRepository groupRepository, IOftenContactRepository oftenContactRepository)
        {
            _userRepository = userRepository;
            _noticeRepository = noticeRepository;
            _chatRecordRepository = chatRecordRepository;
            _articleRepository = articleRepository;
            _groupChatRepository = groupChatRepository;
            _missiveRepository = missiveRepository;
            _topicRepository = topicRepository;
            _jobRepository = jobRepository;
            this.topicAddresseeRepository = topicAddresseeRepository;
            this.middleRepository = middleRepository;
            this.groupRepository = groupRepository;
            this.oftenContactRepository = oftenContactRepository;
        }

        /// <summary>
        /// 插入数据
        /// </summary>
        /// <returns></returns>
        public async Task<bool> InsetElasticSearchData(TedeData tedeData)
        {
            var helper = new ElasticSearchHelper();
            await helper.InsetData(tedeData);
            return true;
        }
        /// <summary>
        /// 插入数据
        /// </summary>
        /// <returns></returns>
        public async Task<bool> ElasticSearchData()
        {
            var topicResult = await _topicRepository.ExecuteTopic();
            //话题
            foreach (var topic in topicResult)
            {
                TedeData tedeData = new TedeData
                {
                    Name = topic.Name,
                    AvatarUrl = topic.AvatarUrl,
                    Content = topic.Content,
                    CreatedDate = topic.CreatedDate,
                    Id = topic.Id.ToString(),
                    GuId = topic.GuId,
                    SearchType = 2,
                    Title = topic.Title,
                    UserId = topic.UserId,
                    ChatType = 0,
                    ImId = "",
                    ToUserId = 0
                };
                var helper = new ElasticSearchHelper();
                await helper.InsetData(tedeData);
            }
            //修改话题
            await topicAddresseeRepository.UpdateAsync(Q.WhereIn(nameof(Entity.Topic.TopicAddressee.Id), topicResult.Select(n => n.TopicAddresseeId)).Set(nameof(Entity.Topic.TopicAddressee.IsUpload), true));
            //通知
            var noticeResult = await _noticeRepository.ElasticSearchNotice();
            foreach (var notice in noticeResult)
            {
                TedeData tedeData = new TedeData
                {
                    Name = notice.Name,
                    AvatarUrl = notice.AvatarUrl,
                    Content = notice.Content,
                    CreatedDate = notice.CreatedDate,
                    Id = notice.Id.ToString(),
                    GuId = notice.GuId,
                    SearchType = 1,
                    Title = notice.Title,
                    UserId = notice.UserId,
                    ChatType = 0,
                    ImId = "",
                    ToUserId = 0
                };
                var helper = new ElasticSearchHelper();
                await helper.InsetData(tedeData);
            }
            //修改话题
            await middleRepository.UpdateAsync(Q.WhereIn(nameof(Entity.Middle.Middle.Id), noticeResult.Select(n => n.MiddleId)).Set(nameof(Entity.Middle.Middle.IsUpload), true));
            //站内信
            var missiveResult = await _missiveRepository.ElasticSearchMissive();
            foreach (var missive in missiveResult)
            {
                TedeData tedeData = new TedeData
                {
                    Name = missive.Name,
                    AvatarUrl = missive.AvatarUrl,
                    Content = missive.Content,
                    CreatedDate = missive.CreatedDate,
                    Id = missive.Id.ToString(),
                    GuId = missive.GuId,
                    SearchType = 5,
                    Title = missive.Title,
                    UserId = missive.UserId,
                    ChatType = 0,
                    ImId = "",
                    ToUserId = 0
                };
                var helper = new ElasticSearchHelper();
                await helper.InsetData(tedeData);
            }
            //修改话题
            await middleRepository.UpdateAsync(Q.WhereIn(nameof(Entity.Middle.Middle.Id), missiveResult.Select(n => n.MiddleId)).Set(nameof(Entity.Middle.Middle.IsUpload), true));
            //小组
            var groupResult = await groupRepository.ElasticSearchGroup();
            foreach (var group in groupResult)
            {
                TedeData tedeData = new TedeData
                {
                    Name = group.Name,
                    AvatarUrl = group.AvatarUrl,
                    Content = group.Content,
                    CreatedDate = group.CreatedDate,
                    Id = group.Id.ToString(),
                    GuId = group.GuId,
                    SearchType = 7,
                    Title = group.Title,
                    UserId = group.UserId,
                    ChatType = 0,
                    ImId = "",
                    ToUserId = 0
                };
                var helper = new ElasticSearchHelper();
                await helper.InsetData(tedeData);
            }
            //修改小组
            await middleRepository.UpdateAsync(Q.WhereIn(nameof(Entity.Middle.Middle.Id), groupResult.Select(n => n.MiddleId)).Set(nameof(Entity.Middle.Middle.IsUpload), true));
            //通讯录
            var userResult = await _userRepository.ElasticSearchUser();
            foreach (var user in userResult)
            {
                TedeData tedeData = new TedeData
                {
                    Name = user.Name,
                    AvatarUrl = user.AvatarUrl,
                    Content = "",
                    Id = user.Id.ToString(),
                    GuId = user.GuId,
                    SearchType = 100,
                    CreatedDate = DateTime.Now,
                    ChatType = 0,
                    ImId = "",
                    ToUserId = 0
                };
                var helper = new ElasticSearchHelper();
                await helper.InsetData(tedeData);
            }
            //修改通讯录
            await _userRepository.UpdateAsync(Q.WhereIn(nameof(Entity.User.Id), userResult.Select(n => n.Id)).Set(nameof(Entity.User.IsUpload), true));
            //聊天群
            var groupChatResult = await oftenContactRepository.ElasticSearchGroupChat();
            foreach (var groupChat in groupChatResult)
            {
                TedeData tedeData = new TedeData
                {
                    Name = groupChat.Name,
                    AvatarUrl = groupChat.AvatarUrl,
                    Content = "",
                    Id = groupChat.Id.ToString(),
                    GuId = groupChat.GuId,
                    ImId = groupChat.ImId,
                    CreatedDate = groupChat.CreatedDate,
                    SearchType = 10,
                    ChatType = 2
                };
                var helper = new ElasticSearchHelper();
                await helper.InsetData(tedeData);
            }
            //聊天群
            await middleRepository.UpdateAsync(Q.WhereIn(nameof(Entity.Middle.Middle.Id), groupChatResult.Select(n => n.Id)).Set(nameof(Entity.Middle.Middle.IsUpload), true));
            //聊天
            var chatResult = await oftenContactRepository.ElasticSearchChat();
            foreach (var chat in chatResult)
            {
                TedeData tedeData = new TedeData
                {
                    Name = chat.Name,
                    AvatarUrl = chat.AvatarUrl,
                    Content = "",
                    Id = chat.Id.ToString(),
                    GuId = chat.GuId,
                    ImId = chat.ImId,
                    CreatedDate = chat.CreatedDate,
                    SearchType = 10,
                    ChatType = 1
                };
                var helper = new ElasticSearchHelper();
                await helper.InsetData(tedeData);
            }
            //聊天群
            await middleRepository.UpdateAsync(Q.WhereIn(nameof(Entity.Middle.Middle.Id), chatResult.Select(n => n.Id)).Set(nameof(Entity.Middle.Middle.IsUpload), true));
            //聊天页面
            return true;
        }
    }
}
