using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Transactions;
using AutoMapper;
using Datory;
using GxPress.Common.Exceptions;
using GxPress.Common.Page;
using GxPress.Common.Tools;
using GxPress.Entity;
using GxPress.EnumConst;
using GxPress.Repository.Interface;
using GxPress.Request.Analyze;
using GxPress.Request.Notice;
using GxPress.Result;
using GxPress.Result.Notice;
using GxPress.Service.Interface.Analyze;
using GxPress.Service.Interface.Middle;
using GxPress.Service.Interface.Notice;
using GxPress.Service.Interface.Visit;
using Newtonsoft.Json;
using GxPress.Service.Interface.UserMiddle;
using GxPress.Service.Interface.ContentJson;

namespace GxPress.Service.Implement.Notice
{
    public partial class NoticeService : INoticeService
    {
        private readonly IUserRepository _userRepository;
        private readonly INoticeRepository _noticeRepository;
        private readonly ICcRepository _ccRepository;
        private readonly IAddresseeRepository _addresseeRepository;
        private readonly IDepartmentRepository _departmentRepository;
        private readonly IMiddleService _middleService;
        private readonly IMiddleRepository _middleRepository;
        private readonly IMapper _mapper;
        private readonly IAnalyzeService _analyzeService;
        private readonly IVisitService _visitService;
        private readonly ICommentRepository _commentRepository;
        private readonly IUserMiddleService _userMiddleService;
        private readonly IContentJsonService _contentJsonService;
        public NoticeService(IUserRepository userRepository, IMapper mapper, INoticeRepository noticeRepository,
            ICcRepository ccRepository, IAddresseeRepository addresseeRepository,
            IDepartmentRepository departmentRepository, IMiddleService middleService,
            IMiddleRepository middleRepository, IAnalyzeService analyzeService, ICommentRepository commentRepository, IVisitService visitService, IUserMiddleService userMiddleService, IContentJsonService contentJsonService)
        {
            _noticeRepository = noticeRepository;
            _userRepository = userRepository;
            _addresseeRepository = addresseeRepository;
            _ccRepository = ccRepository;
            _departmentRepository = departmentRepository;
            _mapper = mapper;
            _middleService = middleService;
            _middleRepository = middleRepository;
            _analyzeService = analyzeService;
            _commentRepository = commentRepository;
            _visitService = visitService;
            _userMiddleService = userMiddleService;
            _contentJsonService = contentJsonService;
        }
        /// <summary>
        /// 获取通知的点赞列表
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<NoticeDetailResult> GetNoticeDetailAsync(int id, int userId)
        {

            var notice = await _noticeRepository.GetAsync(id);
            if (notice == null)
                throw new BusinessException("通知不存在");
            var noticeDetailResult = new Result.Notice.NoticeDetailResult();
            var result = _mapper.Map<Result.Notice.NoticeDetailResult>(notice);
            //获取新版
            result.AddresseeUserMiddles = await _userMiddleService.FindUserMiddlesAsync(10, id);
            result.CcUserMiddles = await _userMiddleService.FindUserMiddlesAsync(20, id);
            //json
            var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(result.Content);
            foreach (var item in contentJsonData)
                item.File = StringUtils.AddDomain(item.File);
            result.Data = contentJsonData;
            //获取用户
            var user = await _userRepository.GetAsync(result.UserId);
            result.UserName = user.Name;
            result.AllCount = await _addresseeRepository.CountAsync(Q.Where(nameof(Addressee.SourceId), id));
            result.ReadCount = await _addresseeRepository.CountAsync(Q.Where(nameof(Addressee.SourceId), id).Where(nameof(Addressee.IsRead), true));
            //获取收件人
            var noticeAddresseeList =
                await _addresseeRepository.GetUserByNoticeIdAsync(id);
            result.NoticeAddresseeNames = noticeAddresseeList.Select(n => n.Name).ToList();
            var IsRead = await _addresseeRepository.ExistsAsync(Q.Where(nameof(Entity.Middle.Middle.IsRead), true)
                  .Where(nameof(Addressee.SourceId), id)
                  .Where(nameof(Addressee.UserId), userId));
            //修改阅读量
            if (IsRead == false)
            {
                if (await _addresseeRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsRead), true)
                    .Where(nameof(Addressee.SourceId), id)
                    .Where(nameof(Addressee.UserId), userId)))
                    result.ReadCount++;
            }
            //修改访问量
            await _visitService.AddVisit(userId, 5, id);
            //修改middle的阅读
            // await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsRead), true).Where(nameof(Entity.Middle.Middle.UserId), userId).Where(nameof(Entity.Middle.Middle.MiddleId), id).Where(nameof(Entity.Middle.Middle.FolderType), 1));
            var analyzeRequest = new Request.App.Analyze.AnalyzeRequest();
            analyzeRequest.TypeValue = 2;
            analyzeRequest.AnalyzeType = 1;
            analyzeRequest.SourceId = id;
            analyzeRequest.UserId = userId;
            //点赞数量
            result.PraiseCount = await _analyzeService.CountAsync(analyzeRequest);
            //获取话题的评论数量
            var commentCount =
                await _commentRepository.CountAsync(Q.Where(nameof(Entity.Comment.ArticleId), id).Where(nameof(Entity.Comment.TypeValue), analyzeRequest.TypeValue).Where(nameof(Entity.Comment.Pid), 0));
            result.CommentCount = commentCount;
            //获取话题的转发数量
            analyzeRequest.AnalyzeType = 4;
            var retransmissionCount = await _analyzeService.CountAsync(analyzeRequest);
            result.RetransmissionCount = retransmissionCount;
            result.IsRetransmission = await _analyzeService.ExistsAsync(analyzeRequest);
            //获取话题的收藏数量
            analyzeRequest.AnalyzeType = 3;
            var collectCount = await _analyzeService.CountAsync(analyzeRequest);
            result.CollectCount = collectCount;
            //是否收藏
            result.IsCollect = await _analyzeService.ExistsAsync(analyzeRequest);
            //获取话题的点赞数量
            analyzeRequest.AnalyzeType = 1;
            var praiseCount = await _analyzeService.CountAsync(analyzeRequest);
            result.PraiseCount = praiseCount;
            //是否点赞
            result.IsPraise = await _analyzeService.ExistsAsync(analyzeRequest);
            var request = new PraisePageSearchRequest { SourceId = id, TypeValue = 2, Page = 1, PerPage = 3 };
            result.NoticePraisePagedList = await _analyzeService.GetPraisePageAsync(request);
            //是否是当前用户创建
            result.IsAdmin = notice.UserId == userId;
            return result;
        }
        /// <summary>
        /// 根据通知ID获取通知详情
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<NoticeEditDetailResult> NoticeEditDetailAsync(int id)
        {
            var notice = await _noticeRepository.GetAsync(id);
            var result = _mapper.Map<NoticeEditDetailResult>(notice);
            //抄送人
            var noticeCcList = await _ccRepository.GetNoticeCcByNoticeIdAsync(id, GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode());
            //收件人
            var noticeAddressee = await _addresseeRepository.GetNoticeAddresseeByNoticeIdAsync(id);
            //获取新版
            result.AddresseeUserMiddles = await _userMiddleService.FindUserMiddlesAsync(10, id);
            result.CcUserMiddles = await _userMiddleService.FindUserMiddlesAsync(20, id);
            var noticeAddresseeUsers =
                await _userRepository.UserListInsAsync(noticeAddressee.Select(n => n.UserId).ToList());
            result.NoticeAddresseeUsers =
                noticeAddresseeUsers.Select(n => _mapper.Map<NoticeAddresseeUser>(n)).ToList();

            var noticeCcUsers = await _userRepository.UserListInsAsync(noticeCcList.Select(n => n.UserId).ToList());
            result.NoticeCcUsers = noticeCcUsers.Select(n => _mapper.Map<NoticeCcUser>(n)).ToList();
            foreach (var noticeAddresseeUser in result.NoticeAddresseeUsers)
            {
                noticeAddresseeUser.AvatarUrl = StringUtils.AddDomainMin(noticeAddresseeUser.AvatarUrl);
            }

            foreach (var noticeCcUser in result.NoticeCcUsers)
            {
                noticeCcUser.AvatarUrl = StringUtils.AddDomainMin(noticeCcUser.AvatarUrl);
            }
            var jsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(result.Content);
            foreach (var item in jsonData)
                item.File = StringUtils.AddDomain(item.File);
            result.Data = jsonData;
            return result;
        }

        /// <summary>
        /// 获取通知详情
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<Result.Notice.NoticeDetailResult> GetNoticeDetailResultAsync(int id, int userId)
        {
            var notice = await _noticeRepository.GetAsync(id);
            if (notice == null)
                throw new BusinessException("通知不存在");
            var noticeDetailResult = new Result.Notice.NoticeDetailResult();
            var result = _mapper.Map<Result.Notice.NoticeDetailResult>(notice);
            //获取新版
            result.AddresseeUserMiddles = await _userMiddleService.FindUserMiddlesAsync(10, id);
            result.CcUserMiddles = await _userMiddleService.FindUserMiddlesAsync(20, id);
            //json
            var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(result.Content);
            foreach (var item in contentJsonData)
                item.File = StringUtils.AddDomain(item.File);
            result.Data = contentJsonData;
            //获取用户
            var user = await _userRepository.GetAsync(result.UserId);
            result.UserName = user.Name;
            result.AllCount =
                await _addresseeRepository.CountAsync(Q.Where(nameof(Addressee.SourceId), id));
            result.ReadCount = await _addresseeRepository.CountAsync(Q
                .Where(nameof(Addressee.SourceId), id).Where(nameof(Addressee.IsRead), true));
            //获取收件人
            var noticeAddresseeList =
                await _addresseeRepository.GetAllAsync(Q.Where(nameof(Addressee.SourceId), id)
                    .Where(nameof(Entity.Middle.Middle.IsAdmin), false));
            result.NoticeAddresseeNames = noticeAddresseeList.Select(n => n.UserName).ToList();
            //修改阅读量
            await _addresseeRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsRead), true)
                .Where(nameof(Addressee.SourceId), id)
                .Where(nameof(Addressee.UserId), userId));
            //修改middle的阅读
            await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsRead), true).Where(nameof(Entity.Middle.Middle.UserId), userId).Where(nameof(Entity.Middle.Middle.MiddleId), id).Where(nameof(Entity.Middle.Middle.FolderType), 1));
            var analyzeRequest = new Request.App.Analyze.AnalyzeRequest();
            analyzeRequest.TypeValue = 2;
            analyzeRequest.AnalyzeType = 1;
            analyzeRequest.SourceId = id;
            analyzeRequest.UserId = userId;
            //点赞数量
            result.PraiseCount = await _analyzeService.CountAsync(analyzeRequest);
            //获取话题的评论数量
            var commentCount =
                await _commentRepository.CountAsync(Q.Where(nameof(Entity.Comment.ArticleId), id).Where(nameof(Entity.Comment.TypeValue), 1));
            result.CommentCount = commentCount;
            //获取话题的转发数量
            analyzeRequest.AnalyzeType = 4;
            var retransmissionCount = await _analyzeService.CountAsync(analyzeRequest);
            result.RetransmissionCount = retransmissionCount;
            result.IsRetransmission = await _analyzeService.ExistsAsync(analyzeRequest);
            //获取话题的收藏数量
            analyzeRequest.AnalyzeType = 3;
            var collectCount = await _analyzeService.CountAsync(analyzeRequest);
            result.CollectCount = collectCount;
            //是否收藏
            result.IsCollect = await _analyzeService.ExistsAsync(analyzeRequest);
            //获取话题的点赞数量
            analyzeRequest.AnalyzeType = 1;
            var praiseCount = await _analyzeService.CountAsync(analyzeRequest);
            result.PraiseCount = praiseCount;
            //是否点赞
            result.IsPraise = await _analyzeService.ExistsAsync(analyzeRequest);
            return result;
        }

        /// <summary>
        /// 编辑通知
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<bool> NoticeUpAsync(NoticeUpRequest request)
        {
            try
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    var notice = await _noticeRepository.GetAsync(request.Id);
                    if (notice == null)
                        throw new BusinessException("通知不存在");
                    var noticeInRequest = new NoticeInRequest();
                    noticeInRequest.Content = request.Content;
                    noticeInRequest.HtmlContent = request.HtmlContent;
                    noticeInRequest.NoticeType = request.NoticeType;
                    noticeInRequest.AddresseeUserMiddles = request.AddresseeUserMiddles;
                    noticeInRequest.CcUserMiddles = request.CcUserMiddles;
                    noticeInRequest.Title = request.Title;
                    noticeInRequest.UserId = request.UserId;
                    noticeInRequest.SoureId = 0;
                    noticeInRequest.Id = request.Id;
                    if (request.NoticeType == NoticeTypeConst.Send)
                    {
                        if (notice.NoticeType == NoticeTypeConst.Draft)
                        {
                            //删除
                            await _noticeRepository.DeleteAsync(Q.Where(nameof(Entity.Notice.Id), notice.Id));
                            //删除middle
                            await _middleRepository.DeleteAsync(notice.Id, GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode());
                            if (notice.SourceId > 0)
                            {
                                var noticeSource = await _noticeRepository.GetAsync(notice.SourceId);
                                if (noticeSource != null)
                                    noticeInRequest.Id = noticeSource.Id;
                                else
                                    noticeInRequest.Id = 0;
                            }
                            else
                                noticeInRequest.Id = 0;
                        }
                    }
                    else
                    {
                        if (notice.NoticeType == NoticeTypeConst.Draft)
                            noticeInRequest.Id = request.Id;
                        else
                        {
                            noticeInRequest.SoureId = notice.Id;
                            var noticeSource = await _noticeRepository.GetAsync(Q.Where(nameof(Entity.Notice.SourceId), notice.Id));
                            if (noticeSource != null)
                                noticeInRequest.Id = noticeSource.Id;
                            else noticeInRequest.Id = 0;
                        }
                    }
                    await InsertAsync(noticeInRequest);
                    transactionScope.Complete();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            return true;
        }
       
        /// <summary>
        /// 根据已读未读查询通知数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<IEnumerable<NoticeReadListResult>> NoticeReadListAsync(NoticeReadListRequest request)
        {
            var noticeReadListResults = await _addresseeRepository.GetNoticeAddresseeByUserIdIdAsync(request);
            return noticeReadListResults;
        }

        /// <summary>
        /// 获取通知列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PagedList<NoticeListPageResult>> GetNoticeListPageAsync(NoticePageSearchRequest request)
        {
            var result = await _noticeRepository.GetNoticeListPageAsync(request);
            foreach (var item in result.Items)
            {
                item.Title = _contentJsonService.GetTitile(item.Title, item.Content);
                if (item.IsDelete == true)
                    item.NoticeStatus = 0;
                else if (item.NoticeAddresseeType == NoticeTypeConst.Draft)
                    item.NoticeStatus = 2;
                else
                    item.NoticeStatus = 1;
            }

            return result;
        }
        /// <summary>
        /// 撤销通知
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> RecallAsync(int id)
        {
            try
            {
                using (TransactionScope transactionScope = new TransactionScope())
                {
                    //删除middle
                    await _middleRepository.DeleteAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), id).Where(nameof(Entity.Middle.Middle.FolderType),GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode()).Where(nameof(Entity.Middle.Middle.IsAdmin), false));
                    //修改为草稿箱
                    await _middleRepository.UpdateAsync(Q.Where(nameof(Entity.Middle.Middle.MiddleId), id).Where(nameof(Entity.Middle.Middle.FolderType), GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode()).Where(nameof(Entity.Middle.Middle.IsAdmin), true).Set(nameof(Entity.Middle.Middle.NoticeAddresseeType), 2));
                    //修改
                    await _noticeRepository.UpdateAsync(Q.Where(nameof(Entity.Notice.Id), id).Set(nameof(Entity.Notice.NoticeType), NoticeTypeConst.Draft.GetHashCode()));
                    transactionScope.Complete();
                }
            }
            catch (System.Exception ex)
            {
                throw new BusinessException(ex.Message);
            }
            return true;
        }
    }
}
