using DocumentFormat.OpenXml.Vml.Office;
using DotNetCore.CAP;
using Hotline.Application.Snapshot.Contracts;
using Hotline.Caching.Interfaces;
using Hotline.Orders;
using Hotline.Settings;
using Hotline.Share.Attributes;
using Hotline.Share.Dtos.Article;
using Hotline.Share.Dtos.Snapshot;
using Hotline.Share.Enums.Article;
using Hotline.Share.Enums.Snapshot;
using Hotline.Share.Tools;
using Hotline.Snapshot;
using Hotline.Snapshot.Contracts;
using Hotline.Snapshot.IRepository;
using Mapster;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using XF.Domain.Authentications;
using XF.Domain.Dependency;
using XF.Domain.Exceptions;
namespace Hotline.Application.Snapshot;
public class SnapshotBulletinApplication : ISnapshotBulletinApplication, IScopeDependency
{
private readonly ISystemSettingCacheManager _systemSettingCacheManager;
private readonly ISnapshotBulletinRepository _bulletinRepository;
private readonly INotificationDomainService _notificationDomainService;
private readonly ISessionContext _sessionContext;
private readonly ISafetyTypeRepository _safetyTypeRepository;
private readonly ICapPublisher _capPublisher;
public SnapshotBulletinApplication(ISystemSettingCacheManager systemSettingCacheManager, ISnapshotBulletinRepository bulletinRepository, INotificationDomainService notificationDomainService, ISessionContext sessionContext, ISafetyTypeRepository safetyTypeRepository, ICapPublisher capPublisher)
{
_systemSettingCacheManager = systemSettingCacheManager;
_bulletinRepository = bulletinRepository;
_notificationDomainService = notificationDomainService;
_sessionContext = sessionContext;
_safetyTypeRepository = safetyTypeRepository;
_capPublisher = capPublisher;
}
public async Task ExamineBulletinAsync(ExamineBulletinDto dto, CancellationToken token)
{
var bulletin = await _bulletinRepository.GetAsync(dto.Id, token)
?? throw UserFriendlyException.SameMessage("无效数据");
if (bulletin.BulletinState != EBulletinState.InReview)
throw UserFriendlyException.SameMessage("当前状态不能审核");
if (dto.IsPass == false)
{
bulletin.ExaminOpinion = dto.Reason;
bulletin.ExaminTime = DateTime.Now;
bulletin.ExaminManId = _sessionContext.RequiredUserId;
bulletin.BulletinState = EBulletinState.ReviewNoPass;
await _bulletinRepository.UpdateAsync(bulletin, token);
return;
}
bulletin.BulletinState = EBulletinState.ReviewPass;
bulletin.BulletinTime = DateTime.Now;
bulletin.ExaminOpinion = dto.Reason;
bulletin.ExaminTime = DateTime.Now;
bulletin.ExaminManId = _sessionContext.RequiredUserId;
await _bulletinRepository.UpdateAsync(bulletin, token);
await _capPublisher.PublishAsync(Share.Mq.EventNames.BulletinIsPass, bulletin.Id, cancellationToken: token);
}
///
/// 执行发送通知公告
///
///
///
///
public async Task NotifyUserAsync(string bullutionId, CancellationToken token)
{
await _bulletinRepository.GetAsync(bullutionId, token)
.Then(async bulletion =>
{
if (bulletion!.IsSnapshot == false) return;
if (bulletion!.SafetyTypeId!.IsNullOrEmpty()) return;
foreach (var safetyTypeId in bulletion!.SafetyTypeId!)
{
await _safetyTypeRepository.Queryable()
.LeftJoin((safety, relation) => safety.Id == relation.SafetyTypeId)
.LeftJoin((safety, relation, citizen) => relation.CitizenId == citizen.Id)
.Where((safety, relation, citizen) => safety.Id == safetyTypeId)
.Select((safety, relation, citizen) => relation.CitizenId)
.ToListAsync(token)
.Then(async citizenIds =>
{
var inDto = bulletion.Adapt();
inDto.UserIds = citizenIds;
if (bulletion.VideoPath.NotNullOrEmpty())
{
inDto.NotifyType = ENotificationType.Video;
}
await _notificationDomainService.AddNotifyAsync(inDto, token);
});
}
});
}
///
/// 处理通知公告图片附件路径
///
///
///
public string GetSiteUrls(string sHtmlText)
{
sHtmlText = sHtmlText.Replace("<", "<").Replace(">", ">");
//临时内容
System.Text.StringBuilder sb = new StringBuilder();
sb.Append(sHtmlText);
// 定义正则表达式用来匹配 img 标签
Regex regImg = new Regex(@"
]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
// 搜索匹配的字符串
MatchCollection matches = regImg.Matches(sHtmlText);
// 定义正则表达式用来匹配 video 标签
Regex regvideo = new Regex(@"