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; using GxPress.Service.Interface.CommonSqlKata; 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; private readonly ICommonSqlKataService commonSqlKataService; 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, ICommonSqlKataService commonSqlKataService) { _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; this.commonSqlKataService = commonSqlKataService; } /// /// 获取通知的点赞列表 /// /// /// /// public async Task 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(notice); //获取新版 result.AddresseeUserMiddles = await _userMiddleService.FindUserMiddlesAsync(10, id); result.CcUserMiddles = await _userMiddleService.FindUserMiddlesAsync(20, id); //json var contentJsonData = JsonConvert.DeserializeObject>(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 = GxPress.EnumConst.AllTypeConst.Notice.GetHashCode(); 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 = AllTypeConst.Notice.GetHashCode(), Page = 1, PerPage = 3 }; result.NoticePraisePagedList = await _analyzeService.GetPraisePageAsync(request); //是否是当前用户创建 result.IsAdmin = notice.UserId == userId; return result; } /// /// 根据通知ID获取通知详情 /// /// /// public async Task NoticeEditDetailAsync(int id) { var notice = await _noticeRepository.GetAsync(id); var result = _mapper.Map(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(n)).ToList(); var noticeCcUsers = await _userRepository.UserListInsAsync(noticeCcList.Select(n => n.UserId).ToList()); result.NoticeCcUsers = noticeCcUsers.Select(n => _mapper.Map(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>(result.Content); foreach (var item in jsonData) item.File = StringUtils.AddDomain(item.File); result.Data = jsonData; return result; } /// /// 获取通知详情 /// /// /// public async Task 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(notice); //获取新版 result.AddresseeUserMiddles = await _userMiddleService.FindUserMiddlesAsync(10, id); result.CcUserMiddles = await _userMiddleService.FindUserMiddlesAsync(20, id); //json var contentJsonData = JsonConvert.DeserializeObject>(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; } /// /// 编辑通知 /// /// /// public async Task 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; } /// /// 根据已读未读查询通知数据 /// /// /// public async Task> NoticeReadListAsync(NoticeReadListRequest request) { var noticeReadListResults = await _addresseeRepository.GetNoticeAddresseeByUserIdIdAsync(request); return noticeReadListResults; } /// /// 获取通知列表 /// /// /// public async Task> GetNoticeListPageAsync(NoticePageSearchRequest request) { var noticeTypeValue = GxPress.EnumConst.AllTypeConst.Inbox.GetHashCode(); var query = new SqlKata.Query("tede_middle"); query.LeftJoin("tede_notice", "tede_notice.Id", "tede_middle.MiddleId"); query.LeftJoin("tede_user", "tede_user.Id", "tede_notice.UserId"); query.LeftJoin("tede_addressee", "tede_addressee.Id", "tede_middle.MiddleSonId"); if (request.SearchType == 1) { query.Where("tede_middle.IsAdmin", true); query.Where("tede_middle.NoticeAddresseeType", 1); query.Where("tede_middle.IsDelete", false); } else if (request.SearchType == 2) { query.Where("tede_middle.AttributeValue", 1); query.Where("tede_middle.NoticeAddresseeType", 2); query.Where("tede_middle.IsDelete", false); } else if (request.SearchType == 3) { query.Where("tede_middle.AttributeValue", 1); query.Where("tede_middle.IsDelete", true); } else if (request.SearchType > 3) { if (request.SearchType == 16) { query.WhereIn("tede_notice.TypeValue", new List { AllTypeConst.Notice.GetHashCode(), AllTypeConst.Missive.GetHashCode(), request.SearchType, AllTypeConst.Anonymity.GetHashCode() }); query.Where("tede_middle.IsAdmin", true); } else query.Where("tede_notice.TypeValue", request.SearchType); query.WhereNot("tede_middle.NoticeAddresseeType", 2); query.Where("tede_middle.IsDelete", false); } else { query.WhereNot("tede_middle.NoticeAddresseeType", 2); query.Where("tede_middle.IsDelete", false); } if (!string.IsNullOrEmpty(request.Keyword)) { if (request.NoticeFolderId > 0) query.Where("tede_middle.ParentId", request.NoticeFolderId); var sourceIdQuery = new SqlKata.Query("tede_addressee"); sourceIdQuery.Join("tede_user", "tede_user.Id", "tede_addressee.UserId"); sourceIdQuery.WhereColumns("tede_addressee.SourceId", "=", "tede_middle.MiddleId"); sourceIdQuery.WhereLike("tede_user.Name", $"%{request.Keyword}%"); sourceIdQuery.Select("tede_addressee.SourceId"); query.WhereNot("tede_middle.AttributeValue", 2); query.Where(n => n.OrWhereLike("tede_notice.Title", $"%{request.Keyword}%").OrWhereLike("tede_notice.Content", $"%{request.Keyword}%") .OrWhere(n => n.WhereIn("tede_middle.MiddleId", sourceIdQuery))); } else { if (request.SearchType == 0) query.Where("tede_middle.ParentId", request.NoticeFolderId); } if (request.SearchType > 0) query.Where("tede_middle.IsRecall", false); query.Where("tede_middle.UserId", request.UserId); query.Where("tede_middle.FolderType", noticeTypeValue); //条数 var countQuery = query; //收件箱 query = commonSqlKataService.NoticeQueryAsync(query, request.UserId); //通用 //query = commonSqlKataService.GetCommonQueryAsync(noticeTypeValue, request.UserId, query, "tede_middle.MiddleId"); query.OrderByDesc("tede_middle.AttributeValue"); query.OrderByDesc("tede_middle.IsTop"); query.OrderByDesc("tede_middle.LastModifiedDate"); //query.OrderByDesc("tede_middle.Sort"); query.ForPage(request.Page, request.PerPage); query.Select( "tede_middle.Id", "tede_middle.MiddleId", "tede_middle.IsTop", "tede_middle.AttributeValue", "tede_middle.FolderName", "tede_middle.CreatedDate" , "tede_notice.{UserId,UserName,SourceId,Title,TypeValue,IsRecall}" , "tede_user.{AvatarUrl}" , "tede_addressee.{Id as NoticeAddresseeId, SourceId as NoticeId}"); var result = await _noticeRepository.GetNoticeListPageAsync(query, countQuery); foreach (var item in result.Items) { item.IsAdmin = item.UserId == request.UserId; var typeValueIds = new List{AllTypeConst.MyChecked.GetHashCode() ,AllTypeConst.MyCc.GetHashCode(),AllTypeConst.MySubmitted.GetHashCode()}; if (typeValueIds.Contains(item.TypeValue)) { item.IsAdmin = item.IsFlowAdmin; item.TodoId = item.SonId; item.MiddleId = item.SourceId; //item.UserName = item.SendUserName; item.ReadCount = item.CheckedCount + 1; item.AllCount = item.UCheckedCount + 1; item.EnjoyUser = item.FlowUserName; //我发起的 if (item.TypeValue == AllTypeConst.MySubmitted.GetHashCode()) item.IsRead = true; //我抄送 if (item.TypeValue == AllTypeConst.MyCc.GetHashCode()) item.IsRead = true; //我审批 if (item.TypeValue == AllTypeConst.MyChecked.GetHashCode()) item.IsRead = item.IsFlowChecked; if (item.IsDone && item.IsChecked) item.FlowState = FlowState.Checked; else if (item.IsDone && !item.IsChecked) { if (item.DoneType == 1) item.FlowState = FlowState.Meeting; else if (item.DoneType == 2) item.FlowState = FlowState.Wait; else item.FlowState = FlowState.Denied; } else if (item.IsDone && !item.IsChecked) item.FlowState = FlowState.Denied; else { item.FlowState = FlowState.Checking; } } // item.Title = _contentJsonService.GetTitile(item.Title, item.Content); if (request.SearchType == 3) item.NoticeStatus = 0; else if (request.SearchType == 2) item.NoticeStatus = 2; else item.NoticeStatus = 1; if (item.IsRecall) item.IsRead = true; } return result; } /// /// 撤销通知 /// /// /// public async Task RecallAsync(int id) { try { using (TransactionScope transactionScope = new TransactionScope()) { var query = Q.NewQuery(); //修改通知 await _noticeRepository.UpdateAsync(Q.Where(nameof(Entity.Notice.Id), id).Set(nameof(Entity.Notice.IsRecall), true)); //删除middle query.Where(nameof(Entity.Middle.Middle.MiddleId), id); query.Where(nameof(Entity.Middle.Middle.FolderType), GxPress.EnumConst.AllTypeConst.Inbox.GetHashCode()); query.Where(nameof(Entity.Middle.Middle.IsAdmin), false); //query.Set(nameof(Entity.Middle.Middle.IsRecall), true); await _middleRepository.DeleteAsync(query); // //修改middle // query = Q.NewQuery(); // query.Where(nameof(Entity.Middle.Middle.MiddleId), id); // query.Where(nameof(Entity.Middle.Middle.FolderType), GxPress.EnumConst.AllTypeConst.Inbox.GetHashCode()); // query.Where(nameof(Entity.Middle.Middle.IsAdmin), true); // query.Set(nameof(Entity.Middle.Middle.IsRecall), true); // await _middleRepository.UpdateAsync(query); // //修改为草稿箱 // query = Q.NewQuery(); // query.Where(nameof(Entity.Middle.Middle.MiddleId), id); // query.Where(nameof(Entity.Middle.Middle.FolderType), GxPress.EnumConst.FolderTypeConst.Notice.GetHashCode()); // query.Where(nameof(Entity.Middle.Middle.IsAdmin), true); // query.Set(nameof(Entity.Middle.Middle.NoticeAddresseeType), 2); // await _middleRepository.UpdateAsync(query); //修改未撤回状态 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; } public async Task UpdateNoticeTitleAsync() { var query = Q.NewQuery(); query.Where(n => n.OrWhereNull(nameof(Entity.Notice.Title)).OrWhere(nameof(Entity.Notice.Title), "=", "")); var result = await _noticeRepository.GetAllAsync(query); foreach (var item in result) { item.Title = _contentJsonService.GetTitile(item.Title, item.Content); await _noticeRepository.UpdateAsync(item); } return true; } } }