using FreeSql; using LiveForum.IService.Posts; using LiveForum.Model; using System; using System.Threading.Tasks; namespace LiveForum.Service.Posts { /// /// 发帖/回复时间间隔校验服务实现 /// public class PostReplyIntervalService : IPostReplyIntervalService { private readonly IBaseRepository _intervalsRepository; private readonly IBaseRepository _usersRepository; private readonly IBaseRepository _postsRepository; private readonly IBaseRepository _commentsRepository; /// /// 默认间隔值(未配置时使用),0 表示不限制 /// private const int DEFAULT_INTERVAL = 0; /// /// 构造函数 /// public PostReplyIntervalService( IBaseRepository intervalsRepository, IBaseRepository usersRepository, IBaseRepository postsRepository, IBaseRepository commentsRepository) { _intervalsRepository = intervalsRepository; _usersRepository = usersRepository; _postsRepository = postsRepository; _commentsRepository = commentsRepository; } /// /// 获取用户的发帖间隔配置(秒) /// public async Task GetPostIntervalAsync(long userId) { var user = await _usersRepository.Select .Where(x => x.Id == userId) .FirstAsync(); if (user?.CertifiedType == null || user.CertifiedType <= 0) return DEFAULT_INTERVAL; var config = await _intervalsRepository.Select .Where(x => x.CertificationTypeId == user.CertifiedType.Value) .FirstAsync(); return config?.PostInterval ?? DEFAULT_INTERVAL; } /// /// 获取用户的回复间隔配置(秒) /// public async Task GetReplyIntervalAsync(long userId) { var user = await _usersRepository.Select .Where(x => x.Id == userId) .FirstAsync(); if (user?.CertifiedType == null || user.CertifiedType <= 0) return DEFAULT_INTERVAL; var config = await _intervalsRepository.Select .Where(x => x.CertificationTypeId == user.CertifiedType.Value) .FirstAsync(); return config?.ReplyInterval ?? DEFAULT_INTERVAL; } /// /// 获取用户最近一次成功发帖时间 /// public async Task GetLastPostTimeAsync(long userId) { var lastPost = await _postsRepository.Select .Where(x => x.UserId == userId && !x.IsDeleted) .OrderByDescending(x => x.CreatedAt) .FirstAsync(); return lastPost?.CreatedAt; } /// /// 获取用户最近一次成功回复时间 /// public async Task GetLastCommentTimeAsync(long userId) { var lastComment = await _commentsRepository.Select .Where(x => x.UserId == userId && !x.IsDeleted) .OrderByDescending(x => x.CreatedAt) .FirstAsync(); return lastComment?.CreatedAt; } /// /// 校验发帖间隔,返回 null 表示通过,否则返回剩余秒数 /// public async Task CheckPostIntervalAsync(long userId) { var interval = await GetPostIntervalAsync(userId); if (interval <= 0) return null; // 不限制 var lastTime = await GetLastPostTimeAsync(userId); if (lastTime == null) return null; // 从未发帖 var elapsed = (DateTime.Now - lastTime.Value).TotalSeconds; if (elapsed >= interval) return null; // 已满足间隔 return (int)Math.Ceiling(interval - elapsed); // 返回剩余秒数 } /// /// 校验回复间隔,返回 null 表示通过,否则返回剩余秒数 /// public async Task CheckReplyIntervalAsync(long userId) { var interval = await GetReplyIntervalAsync(userId); if (interval <= 0) return null; // 不限制 var lastTime = await GetLastCommentTimeAsync(userId); if (lastTime == null) return null; // 从未回复 var elapsed = (DateTime.Now - lastTime.Value).TotalSeconds; if (elapsed >= interval) return null; // 已满足间隔 return (int)Math.Ceiling(interval - elapsed); // 返回剩余秒数 } } }