Files
BuYuHouDuan/AdminSrc/AdminService/Faq/FaqService.cs
T
2026-10-12 16:22:04 +08:00

158 lines
5.4 KiB
C#

using CommonDomain;
using MongoRepository;
namespace AdminService.Faq
{
public class FaqService : IFaqService
{
private readonly IMongoBaseRepository<FaqCategory> _categoryRepo;
private readonly IMongoBaseRepository<FaqQuestion> _questionRepo;
public FaqService(
IMongoBaseRepository<FaqCategory> categoryRepo,
IMongoBaseRepository<FaqQuestion> questionRepo)
{
_categoryRepo = categoryRepo;
_questionRepo = questionRepo;
}
public async Task<List<FaqCategory>> GetAllCategories()
{
var list = await _categoryRepo.FilterByAsync(x => x.IsEnable);
return list.OrderBy(x => x.Sort).ToList();
}
public async Task<List<FaqCategoryTreeNode>> GetCategoryTree()
{
var allCategories = await _categoryRepo.FilterByAsync(x => true);
var sortedList = allCategories.OrderBy(x => x.Sort).ToList();
var nodeDict = sortedList.ToDictionary(
x => x.Id,
x => new FaqCategoryTreeNode
{
Id = x.Id,
Name = x.Name,
Icon = x.Icon,
Sort = x.Sort,
IsEnable = x.IsEnable,
Children = new List<FaqCategoryTreeNode>()
}
);
var rootNodes = new List<FaqCategoryTreeNode>();
foreach (var category in sortedList)
{
var node = nodeDict[category.Id!];
if (string.IsNullOrEmpty(category.ParentId))
{
rootNodes.Add(node);
}
else if (nodeDict.ContainsKey(category.ParentId))
{
nodeDict[category.ParentId!].Children.Add(node);
}
else
{
rootNodes.Add(node);
}
}
return rootNodes;
}
public async Task<FaqCategory?> GetCategoryById(string id)
{
return await _categoryRepo.FindOneAsync(x => x.Id == id);
}
public async Task<(bool ok, string msg)> AddCategory(FaqCategory category)
{
return await _categoryRepo.InsertOneAsync(category);
}
public async Task<(bool ok, string msg)> UpdateCategory(FaqCategory category)
{
var existing = await _categoryRepo.FindOneAsync(x => x.Id == category.Id);
if (existing == null)
return (false, "板块不存在");
existing.ParentId = category.ParentId;
existing.Name = category.Name;
existing.Icon = category.Icon;
existing.Sort = category.Sort;
existing.IsEnable = category.IsEnable;
return await _categoryRepo.ReplaceOneAsync(existing);
}
public async Task<(bool ok, string msg)> DeleteCategory(string id)
{
var hasChildren = await _categoryRepo.FindOneAsync(x => x.ParentId == id);
if (hasChildren != null)
return (false, "该分类下存在子分类,请先删除子分类");
var questions = await _questionRepo.FilterByAsync(x => x.CategoryId == id);
if (questions.Any())
return (false, "该板块下存在问题,请先删除相关问题");
return await _categoryRepo.DeleteByIdAsync(id);
}
public async Task<List<FaqQuestion>> GetQuestionsByCategory(string categoryId)
{
var list = await _questionRepo.FilterByAsync(x => x.CategoryId == categoryId && x.IsEnable);
return list.OrderBy(x => x.Sort).ToList();
}
public async Task<(List<FaqQuestion> list, long count)> QueryQuestions(string? categoryId, int pageIndex, int pageSize)
{
var result = await _questionRepo.QueryPageAsync(
filterExpression: x =>
(string.IsNullOrEmpty(categoryId) || x.CategoryId == categoryId),
orderExpression: x => x.Sort,
desc: false,
pageSize: pageSize,
pageIndex: pageIndex
);
return (result.list?.OrderBy(x => x.Sort).ToList() ?? new List<FaqQuestion>(), result.count);
}
public async Task<FaqQuestion?> GetQuestionById(string id)
{
return await _questionRepo.FindOneAsync(x => x.Id == id);
}
public async Task<(bool ok, string msg)> AddQuestion(FaqQuestion question)
{
question.CreateTime = DateTime.Now;
return await _questionRepo.InsertOneAsync(question);
}
public async Task<(bool ok, string msg)> UpdateQuestion(FaqQuestion question)
{
var existing = await _questionRepo.FindOneAsync(x => x.Id == question.Id);
if (existing == null)
return (false, "问题不存在");
existing.CategoryId = question.CategoryId;
existing.Title = question.Title;
existing.Content = question.Content;
existing.Sort = question.Sort;
existing.IsEnable = question.IsEnable;
return await _questionRepo.ReplaceOneAsync(existing);
}
public async Task<(bool ok, string msg)> DeleteQuestion(string id)
{
return await _questionRepo.DeleteByIdAsync(id);
}
}
}