102 lines
3.2 KiB
C#
102 lines
3.2 KiB
C#
using CommonDomain;
|
|
using MongoRepository;
|
|
|
|
namespace UserService.Notices
|
|
{
|
|
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<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);
|
|
}
|
|
|
|
|
|
}
|
|
}
|