MediaService.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. namespace GxPress.Service.Implement.Media
  10. {
  11. public class MediaService : IMediaService
  12. {
  13. private readonly ICategoryRepository categoryRepository;
  14. private readonly IMediaRepository mediaRepository;
  15. private readonly IMapper _mapper;
  16. public MediaService(ICategoryRepository categoryRepository, IMediaRepository mediaRepository, IMapper _mapper)
  17. {
  18. this.categoryRepository = categoryRepository;
  19. this.mediaRepository = mediaRepository;
  20. this._mapper = _mapper;
  21. }
  22. public async Task<bool> InsertAsync(MediaResult result)
  23. {
  24. // if (result.CategoryId > 0)
  25. // {
  26. // result.CategoryName = await categoryRepository.GetCategoryParentAsync(result.CategoryId, result.CategoryName);
  27. // }
  28. return await mediaRepository.InsertAsync(result);
  29. }
  30. public async Task<MediaResult> GetAsync(int id)
  31. {
  32. var result = new MediaResult();
  33. //获取媒体
  34. result = await mediaRepository.GetAsync(id);
  35. //获取类别
  36. var categoryAll = await categoryRepository.GetAllAsync();
  37. result.CategoryResults = categoryAll.Where(n => n.ParentId == 0).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
  38. foreach (var item in result.CategoryResults)
  39. {
  40. item.IsChildren = categoryAll.Any(n => n.ParentId == item.Id);
  41. item.Children = categoryAll.Where(n => n.ParentId == item.Id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
  42. if (item.IsChildren)
  43. {
  44. foreach (var chidren in item.Children)
  45. {
  46. chidren.IsChildren = categoryAll.Any(n => n.ParentId == chidren.Id);
  47. chidren.Children = categoryAll.Where(n => n.ParentId == chidren.Id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
  48. }
  49. }
  50. }
  51. return result;
  52. }
  53. // public List<CategoryResult> GetAllCategoryResult(IEnumerable<Entity.tede2.Category.Category> categorys, int id, List<CategoryResult> categoryResults)
  54. // {
  55. // var result = new List<CategoryResult>();
  56. // result = categorys.Where(n => n.ParentId == id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
  57. // foreach (var item in result)
  58. // {
  59. // item.IsChildren = categorys.Any(n => n.ParentId == item.Id);
  60. // item.Children = categorys.Where(n => n.ParentId == item.Id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
  61. // foreach (var children in item.Children)
  62. // {
  63. // GetAllCategoryResult(categorys,children.Id,categoryResults);
  64. // }
  65. // }
  66. // return result;
  67. // }
  68. }
  69. }