SnapshotBulletinApplication.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using DocumentFormat.OpenXml.Vml.Office;
  2. using DotNetCore.CAP;
  3. using Hotline.Application.Snapshot.Contracts;
  4. using Hotline.Caching.Interfaces;
  5. using Hotline.Orders;
  6. using Hotline.Settings;
  7. using Hotline.Share.Attributes;
  8. using Hotline.Share.Dtos.Article;
  9. using Hotline.Share.Dtos.Snapshot;
  10. using Hotline.Share.Enums.Article;
  11. using Hotline.Share.Enums.Snapshot;
  12. using Hotline.Share.Tools;
  13. using Hotline.Snapshot;
  14. using Hotline.Snapshot.Contracts;
  15. using Hotline.Snapshot.IRepository;
  16. using Mapster;
  17. using Microsoft.AspNetCore.Http;
  18. using SqlSugar;
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Text.RegularExpressions;
  24. using System.Threading.Tasks;
  25. using XF.Domain.Authentications;
  26. using XF.Domain.Dependency;
  27. using XF.Domain.Exceptions;
  28. namespace Hotline.Application.Snapshot;
  29. public class SnapshotBulletinApplication : ISnapshotBulletinApplication, IScopeDependency
  30. {
  31. private readonly ISystemSettingCacheManager _systemSettingCacheManager;
  32. private readonly ISnapshotBulletinRepository _bulletinRepository;
  33. private readonly INotificationDomainService _notificationDomainService;
  34. private readonly ISessionContext _sessionContext;
  35. private readonly ISafetyTypeRepository _safetyTypeRepository;
  36. private readonly ICapPublisher _capPublisher;
  37. public SnapshotBulletinApplication(ISystemSettingCacheManager systemSettingCacheManager, ISnapshotBulletinRepository bulletinRepository, INotificationDomainService notificationDomainService, ISessionContext sessionContext, ISafetyTypeRepository safetyTypeRepository, ICapPublisher capPublisher)
  38. {
  39. _systemSettingCacheManager = systemSettingCacheManager;
  40. _bulletinRepository = bulletinRepository;
  41. _notificationDomainService = notificationDomainService;
  42. _sessionContext = sessionContext;
  43. _safetyTypeRepository = safetyTypeRepository;
  44. _capPublisher = capPublisher;
  45. }
  46. public async Task ExamineBulletinAsync(ExamineBulletinDto dto, CancellationToken token)
  47. {
  48. var bulletin = await _bulletinRepository.GetAsync(dto.Id, token)
  49. ?? throw UserFriendlyException.SameMessage("无效数据");
  50. if (bulletin.BulletinState != EBulletinState.InReview)
  51. throw UserFriendlyException.SameMessage("当前状态不能审核");
  52. if (dto.IsPass == false)
  53. {
  54. bulletin.ExaminOpinion = dto.Reason;
  55. bulletin.ExaminTime = DateTime.Now;
  56. bulletin.ExaminManId = _sessionContext.RequiredUserId;
  57. bulletin.BulletinState = EBulletinState.ReviewNoPass;
  58. await _bulletinRepository.UpdateAsync(bulletin, token);
  59. return;
  60. }
  61. bulletin.BulletinState = EBulletinState.ReviewPass;
  62. bulletin.BulletinTime = DateTime.Now;
  63. bulletin.ExaminOpinion = dto.Reason;
  64. bulletin.ExaminTime = DateTime.Now;
  65. bulletin.ExaminManId = _sessionContext.RequiredUserId;
  66. await _bulletinRepository.UpdateAsync(bulletin, token);
  67. await _capPublisher.PublishAsync(Share.Mq.EventNames.BulletinIsPass, bulletin.Id, cancellationToken: token);
  68. }
  69. /// <summary>
  70. /// 执行发送通知公告
  71. /// </summary>
  72. /// <param name="bullutionId"></param>
  73. /// <param name="token"></param>
  74. /// <returns></returns>
  75. public async Task NotifyUserAsync(string bullutionId, CancellationToken token)
  76. {
  77. await _bulletinRepository.GetAsync(bullutionId, token)
  78. .Then(async bulletion =>
  79. {
  80. if (bulletion!.IsSnapshot == false) return;
  81. if (bulletion!.SafetyTypeId!.IsNullOrEmpty()) return;
  82. foreach (var safetyTypeId in bulletion!.SafetyTypeId!)
  83. {
  84. await _safetyTypeRepository.Queryable()
  85. .LeftJoin<CitizenRelationSafetyType>((safety, relation) => safety.Id == relation.SafetyTypeId)
  86. .LeftJoin<Citizen>((safety, relation, citizen) => relation.CitizenId == citizen.Id)
  87. .Where((safety, relation, citizen) => safety.Id == safetyTypeId)
  88. .Select((safety, relation, citizen) => relation.CitizenId)
  89. .ToListAsync(token)
  90. .Then(async citizenIds =>
  91. {
  92. var inDto = bulletion.Adapt<AddNotifyInDto>();
  93. inDto.UserIds = citizenIds;
  94. if (bulletion.VideoPath.NotNullOrEmpty())
  95. {
  96. inDto.NotifyType = ENotificationType.Video;
  97. }
  98. await _notificationDomainService.AddNotifyAsync(inDto, token);
  99. });
  100. }
  101. });
  102. }
  103. /// <summary>
  104. /// 处理通知公告图片附件路径
  105. /// </summary>
  106. /// <param name="sHtmlText"></param>
  107. /// <returns></returns>
  108. public string GetSiteUrls(string sHtmlText)
  109. {
  110. sHtmlText = sHtmlText.Replace("&lt;", "<").Replace("&gt;", ">");
  111. //临时内容
  112. System.Text.StringBuilder sb = new StringBuilder();
  113. sb.Append(sHtmlText);
  114. // 定义正则表达式用来匹配 img 标签
  115. Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
  116. // 搜索匹配的字符串
  117. MatchCollection matches = regImg.Matches(sHtmlText);
  118. // 定义正则表达式用来匹配 video 标签
  119. Regex regvideo = new Regex(@"<video\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
  120. // 搜索匹配的字符串
  121. MatchCollection matchesvideo = regvideo.Matches(sHtmlText);
  122. var strSiteUrl = _systemSettingCacheManager.GetSetting(SettingConstants.OldFilesUrls)?.SettingValue[0];
  123. // 取得匹配项列表 视频
  124. foreach (Match match in matchesvideo)
  125. {
  126. if (-1 == match.Groups["imgUrl"].Value.IndexOf("http"))
  127. {
  128. sb.Replace(match.Groups["imgUrl"].Value, strSiteUrl + match.Groups["imgUrl"].Value);
  129. sb.Replace(strSiteUrl + strSiteUrl + match.Groups["imgUrl"].Value, strSiteUrl + match.Groups["imgUrl"].Value);
  130. }
  131. }
  132. // 取得匹配项列表
  133. foreach (Match match in matches)
  134. {
  135. if (-1 == match.Groups["imgUrl"].Value.IndexOf("http"))
  136. {
  137. sb.Replace(match.Groups["imgUrl"].Value, strSiteUrl + match.Groups["imgUrl"].Value);
  138. sb.Replace(strSiteUrl + strSiteUrl + match.Groups["imgUrl"].Value, strSiteUrl + match.Groups["imgUrl"].Value);
  139. }
  140. }
  141. return sb.ToString();
  142. }
  143. [ExportExcel("随手拍公告")]
  144. public ISugarQueryable<SnapshotBulletinItemsOutDto> QueryBulletinItems(SnapshotBulletinItemsInDto dto)
  145. {
  146. var query = _bulletinRepository.Queryable()
  147. .Includes(x => x.ExaminMan)
  148. .WhereIF(!string.IsNullOrEmpty(dto.SnapshotBulletinTypeName), d => d.SnapshotBulletinTypeName.Contains(dto.SnapshotBulletinTypeName))
  149. .WhereIF(!string.IsNullOrEmpty(dto.Title), d => d.Title.Contains(dto.Title))
  150. .WhereIF(dto.BeginCreationTime.HasValue, d => d.BulletinTime >= dto.BeginCreationTime)
  151. .WhereIF(dto.EndCreationTime.HasValue, d => d.BulletinTime <= dto.EndCreationTime)
  152. .WhereIF(dto.State.HasValue, d => d.BulletinState == dto.State)
  153. .OrderByDescending(d => d.CreationTime)
  154. .Select<SnapshotBulletinItemsOutDto>();
  155. return query;
  156. }
  157. public async Task CommitBulletinAsync(string id, CancellationToken requestAborted)
  158. {
  159. var bulletin = await _bulletinRepository.GetAsync(id, requestAborted);
  160. if (bulletin == null)
  161. throw UserFriendlyException.SameMessage("无效数据");
  162. if (bulletin.BulletinState != EBulletinState.Draft && bulletin.BulletinState != EBulletinState.ReviewNoPass)
  163. throw UserFriendlyException.SameMessage("当前状态不能提交");
  164. bulletin.BulletinState = EBulletinState.InReview;
  165. bulletin.CommitTime = DateTime.Now;
  166. await _bulletinRepository.UpdateAsync(bulletin, requestAborted);
  167. }
  168. public async Task<string> AddBulletinAsync(AddSnapshotBulletinInDto dto, CancellationToken token)
  169. {
  170. var model = dto.Adapt<SnapshotBulletin>();
  171. model.BulletinState = EBulletinState.Draft;
  172. model.ReadedNum = 0;
  173. if (model.BulletinTime.HasValue == false) model.BulletinTime = DateTime.Now;
  174. return await _bulletinRepository.AddAsync(model, token);
  175. }
  176. }