using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Common; using CommonDomain; using CommonInfrastructure; using Microsoft.AspNetCore.Mvc; using AdminService.Faq; namespace AdminApi.Faq { [ApiController, Route("adminapi/[controller]")] public class FaqController : BaseController { private readonly IFaqService _faqService; public FaqController(IFaqService faqService) { _faqService = faqService; } [HttpGet("Categories")] public async Task GetCategories() { var list = await _faqService.GetAllCategories(); if (list == null) return new ListResponse { Code = CommonResponseCodeEnum.Success, Data = new List() }; var dtos = list.Select(MapToCategoryDto).ToList(); return new ListResponse { Code = CommonResponseCodeEnum.Success, Info = "获取成功", Data = dtos }; } [HttpGet("CategoryTree")] public async Task GetCategoryTree() { var tree = await _faqService.GetCategoryTree(); var dtos = tree.Select(MapToTreeNodeDto).ToList(); return new ListResponse { Code = CommonResponseCodeEnum.Success, Info = "获取成功", Data = dtos }; } [HttpGet("CategoryDetail")] public async Task> GetCategoryDetail(string id) { var category = await _faqService.GetCategoryById(id); if (category == null) return this.ToFailResult("板块不存在"); return this.ToResult(MapToCategoryDto(category)); } [HttpPost("AddCategory")] public async Task> AddCategory([FromBody] FaqCategory category) { var result = await _faqService.AddCategory(category); if (result.ok) return this.ToResult(result.msg); else return this.ToFailResult(result.msg); } [HttpPost("UpdateCategory")] public async Task> UpdateCategory([FromBody] FaqCategory category) { var result = await _faqService.UpdateCategory(category); if (result.ok) return this.ToResult(result.msg); else return this.ToFailResult(result.msg); } [HttpPost("DeleteCategory")] public async Task> DeleteCategory(string id) { var result = await _faqService.DeleteCategory(id); if (result.ok) return this.ToResult(result.msg); else return this.ToFailResult(result.msg); } [HttpGet("Questions")] public async Task GetQuestions(string categoryId) { var list = await _faqService.GetQuestionsByCategory(categoryId); if (list == null) return new ListResponse { Code = CommonResponseCodeEnum.Success, Data = new List() }; var dtos = list.Select(MapToQuestionDto).ToList(); return new ListResponse { Code = CommonResponseCodeEnum.Success, Info = "获取成功", Data = dtos }; } [HttpPost("QuestionList")] public async Task GetQuestionList([FromBody] FaqQueryParam param) { var result = await _faqService.QueryQuestions(param.CategoryId, param.PageIndex, param.PageSize); var dtos = result.list.Select(MapToQuestionDto).ToList(); return new PageDataResponse { Code = CommonResponseCodeEnum.Success, Info = "获取成功", Data = dtos, Count = (int)result.count }; } [HttpGet("QuestionDetail")] public async Task> GetQuestionDetail(string id) { var question = await _faqService.GetQuestionById(id); if (question == null) return this.ToFailResult("问题不存在"); return this.ToResult(MapToQuestionDto(question)); } [HttpPost("AddQuestion")] public async Task> AddQuestion([FromBody] FaqQuestion question) { var result = await _faqService.AddQuestion(question); if (result.ok) return this.ToResult(result.msg); else return this.ToFailResult(result.msg); } [HttpPost("UpdateQuestion")] public async Task> UpdateQuestion([FromBody] FaqQuestion question) { var result = await _faqService.UpdateQuestion(question); if (result.ok) return this.ToResult(result.msg); else return this.ToFailResult(result.msg); } [HttpPost("DeleteQuestion")] public async Task> DeleteQuestion(string id) { var result = await _faqService.DeleteQuestion(id); if (result.ok) return this.ToResult(result.msg); else return this.ToFailResult(result.msg); } private static FaqCategoryDto MapToCategoryDto(FaqCategory entity) { if (entity == null) return new FaqCategoryDto(); return new FaqCategoryDto { Id = entity.Id ?? "", ParentId = entity.ParentId ?? "", Name = entity.Name ?? "", Icon = entity.Icon ?? "", Sort = entity.Sort, IsEnable = entity.IsEnable }; } private static FaqCategoryTreeNodeDto MapToTreeNodeDto(FaqCategoryTreeNode entity) { if (entity == null) return new FaqCategoryTreeNodeDto(); return new FaqCategoryTreeNodeDto { Id = entity.Id ?? "", Name = entity.Name ?? "", Icon = entity.Icon ?? "", Sort = entity.Sort, IsEnable = entity.IsEnable, Children = entity.Children?.Select(MapToTreeNodeDto).ToList() ?? new List(), QuestionCount = entity.QuestionCount }; } private static FaqQuestionDto MapToQuestionDto(FaqQuestion entity) { if (entity == null) return new FaqQuestionDto(); return new FaqQuestionDto { Id = entity.Id ?? "", CategoryId = entity.CategoryId ?? "", Title = entity.Title ?? "", Content = entity.Content ?? "", Sort = entity.Sort, IsEnable = entity.IsEnable, CreateTime = entity.CreateTime }; } } public class FaqCategoryDto { public string Id { get; set; } public string ParentId { get; set; } public string Name { get; set; } public string Icon { get; set; } public int Sort { get; set; } public bool IsEnable { get; set; } } public class FaqCategoryTreeNodeDto { public string Id { get; set; } public string Name { get; set; } public string Icon { get; set; } public int Sort { get; set; } public bool IsEnable { get; set; } public List Children { get; set; } = new(); public int QuestionCount { get; set; } } public class FaqQuestionDto { public string Id { get; set; } public string CategoryId { get; set; } public string Title { get; set; } public string Content { get; set; } public int Sort { get; set; } public bool IsEnable { get; set; } public DateTime CreateTime { get; set; } } public class FaqQueryParam { public string? CategoryId { get; set; } public int PageIndex { get; set; } = 1; public int PageSize { get; set; } = 10; } }