Files
BuYuHouDuan/AdminSrc/AdminService/Notices/NoticeService.cs
T

150 lines
4.9 KiB
C#
Raw Normal View History

2026-10-12 16:22:04 +08:00
using AdminService.Notices.Param;
using AutoMapper;
using Common;
using Common.Utils;
using Microsoft.Extensions.Logging;
using MongoDB.Bson;
using MongoRepository;
using UserDomain.Entity;
using System.Text.RegularExpressions;
namespace AdminService.Notices
{
public class NoticeService : INoticeService
{
private readonly IMongoBaseRepository<Notice> _noticeMongo;
private readonly IMapper _mapper;
public NoticeService(IMongoBaseRepository<Notice> noticeMongo, IMapper mapper)
{
_noticeMongo = noticeMongo;
_mapper = mapper;
}
private void MoveTempFiles(Notice notice)
{
var wwwroot = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
var tempDir = Path.Combine(wwwroot, "uploads", "temp");
var noticeDir = Path.Combine(wwwroot, "uploads", "notice");
if (!Directory.Exists(noticeDir)) Directory.CreateDirectory(noticeDir);
var paths = new List<string>();
if (!string.IsNullOrEmpty(notice.Content))
{
foreach (Match m in Regex.Matches(notice.Content, @"/uploads/temp/([^""'\s]+)"))
{
paths.Add(m.Value);
}
}
if (!string.IsNullOrEmpty(notice.TopImage) && notice.TopImage.Contains("/uploads/temp/"))
paths.Add(notice.TopImage);
if (notice.ContentImages != null)
{
foreach (var img in notice.ContentImages)
{
if (!string.IsNullOrEmpty(img) && img.Contains("/uploads/temp/"))
paths.Add(img);
}
}
foreach (var path in paths)
{
var cleanPath = path.Split('?', '#')[0];
var fileName = Path.GetFileName(cleanPath);
if (string.IsNullOrWhiteSpace(fileName))
continue;
var src = Path.Combine(tempDir, fileName);
var dest = Path.Combine(noticeDir, fileName);
if (File.Exists(src))
{
File.Move(src, dest, overwrite: true);
}
else
{
}
}
if (!string.IsNullOrEmpty(notice.Content))
notice.Content = notice.Content.Replace("/uploads/temp/", "/uploads/notice/");
if (!string.IsNullOrEmpty(notice.TopImage))
notice.TopImage = notice.TopImage.Replace("/uploads/temp/", "/uploads/notice/");
if (notice.ContentImages != null)
{
for (int i = 0; i < notice.ContentImages.Count; i++)
{
if (!string.IsNullOrEmpty(notice.ContentImages[i]))
notice.ContentImages[i] = notice.ContentImages[i].Replace("/uploads/temp/", "/uploads/notice/");
}
}
}
public async Task<(bool isok, string msg)> Add(AddNoticeParam param)
{
var model = _mapper.Map<Notice>(param);
model.Id = ObjectId.GenerateNewId().ToString();
model.CreateTime = DateTime.Now.ToTimeStamp();
MoveTempFiles(model);
return await _noticeMongo.InsertOneAsync(model);
}
public async Task<(bool isok, string msg)> Update(UpdateNoticeParam param)
{
var model = await Get(param.Id);
if (model == null)
return (false, "该条公告不存在");
model.Title = param.Title;
model.Content = param.Content;
model.Recommend = param.Recommend;
model.TopImage = param.TopImage;
model.ContentImages = param.ContentImages;
MoveTempFiles(model);
return await _noticeMongo.ReplaceOneAsync(model);
}
public async Task<(bool isok, string msg)> Delete(string id)
{
var model = await Get(id);
if (model == null)
return (false, "该条公告不存在");
model.IsDelete = true;
return await _noticeMongo.ReplaceOneAsync(model);
}
public async Task<Notice> Get(string id)
{
var model = await _noticeMongo.FindByIdAsync(id);
if (model == null)
return null;
if (model.IsDelete)
return null;
return model;
}
public async Task<(List<Notice> list, long count)> GetList(GetNoticeListParam param)
{
var where = LinqExtensions.True<Notice>().And(e => e.IsDelete == false);
if (!string.IsNullOrWhiteSpace(param.KeyWord))
where = where.And(e => e.Title.Contains(param.KeyWord));
var result = await _noticeMongo.QueryPageAsync(where, c => c.CreateTime, true, param.PageSize ?? 0, param.PageIndex ?? 0);
return (result.list, result.count);
}
}
}