MediaService.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using GxPress.Service.Interface.Media;
  2. using GxPress.Repository.Interface.Category;
  3. using GxPress.Repository.Interface.Media;
  4. using System.Threading.Tasks;
  5. using GxPress.Result.Media;
  6. using AutoMapper;
  7. using System.Linq;
  8. using GxPress.Result.Category;
  9. using GxPress.Service.Interface.Epub;
  10. namespace GxPress.Service.Implement.Media
  11. {
  12. public class MediaService : IMediaService
  13. {
  14. private readonly ICategoryRepository categoryRepository;
  15. private readonly IMediaRepository mediaRepository;
  16. private readonly IMapper _mapper;
  17. private readonly IEpubService epubService;
  18. public MediaService(ICategoryRepository categoryRepository, IMediaRepository mediaRepository, IMapper _mapper,IEpubService epubService)
  19. {
  20. this.categoryRepository = categoryRepository;
  21. this.mediaRepository = mediaRepository;
  22. this._mapper = _mapper;
  23. this.epubService=epubService;
  24. }
  25. public async Task<bool> InsertAsync(MediaResult result)
  26. {
  27. // if (result.CategoryId > 0)
  28. // {
  29. // result.CategoryName = await categoryRepository.GetCategoryParentAsync(result.CategoryId, result.CategoryName);
  30. // }
  31. return await mediaRepository.InsertAsync(result);
  32. }
  33. public async Task<MediaResult> GetAsync(int id)
  34. {
  35. var result = new MediaResult();
  36. //获取媒体
  37. result = await mediaRepository.GetAsync(id);
  38. //获取类别
  39. var categoryAll = await categoryRepository.GetAllAsync();
  40. result.CategoryResults = categoryAll.Where(n => n.ParentId == 0).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
  41. foreach (var item in result.CategoryResults)
  42. {
  43. item.IsChildren = categoryAll.Any(n => n.ParentId == item.Id);
  44. item.Children = categoryAll.Where(n => n.ParentId == item.Id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
  45. if (item.IsChildren)
  46. {
  47. foreach (var chidren in item.Children)
  48. {
  49. chidren.IsChildren = categoryAll.Any(n => n.ParentId == chidren.Id);
  50. chidren.Children = categoryAll.Where(n => n.ParentId == chidren.Id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
  51. }
  52. }
  53. }
  54. //如果是书籍
  55. return result;
  56. }
  57. // public List<CategoryResult> GetAllCategoryResult(IEnumerable<Entity.tede2.Category.Category> categorys, int id, List<CategoryResult> categoryResults)
  58. // {
  59. // var result = new List<CategoryResult>();
  60. // result = categorys.Where(n => n.ParentId == id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
  61. // foreach (var item in result)
  62. // {
  63. // item.IsChildren = categorys.Any(n => n.ParentId == item.Id);
  64. // item.Children = categorys.Where(n => n.ParentId == item.Id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
  65. // foreach (var children in item.Children)
  66. // {
  67. // GetAllCategoryResult(categorys,children.Id,categoryResults);
  68. // }
  69. // }
  70. // return result;
  71. // }
  72. }
  73. }