using GxPress.Service.Interface.Media; using GxPress.Repository.Interface.Category; using GxPress.Repository.Interface.Media; using System.Threading.Tasks; using GxPress.Result.Media; using AutoMapper; using System.Linq; using GxPress.Result.Category; namespace GxPress.Service.Implement.Media { public class MediaService : IMediaService { private readonly ICategoryRepository categoryRepository; private readonly IMediaRepository mediaRepository; private readonly IMapper _mapper; public MediaService(ICategoryRepository categoryRepository, IMediaRepository mediaRepository, IMapper _mapper) { this.categoryRepository = categoryRepository; this.mediaRepository = mediaRepository; this._mapper = _mapper; } public async Task InsertAsync(MediaResult result) { // if (result.CategoryId > 0) // { // result.CategoryName = await categoryRepository.GetCategoryParentAsync(result.CategoryId, result.CategoryName); // } return await mediaRepository.InsertAsync(result); } public async Task GetAsync(int id) { var result = new MediaResult(); //获取媒体 result = await mediaRepository.GetAsync(id); //获取类别 var categoryAll = await categoryRepository.GetAllAsync(); result.CategoryResults = categoryAll.Where(n => n.ParentId == 0).Select(n => _mapper.Map(n)).ToList(); foreach (var item in result.CategoryResults) { item.IsChildren = categoryAll.Any(n => n.ParentId == item.Id); item.Children = categoryAll.Where(n => n.ParentId == item.Id).Select(n => _mapper.Map(n)).ToList(); if (item.IsChildren) { foreach (var chidren in item.Children) { chidren.IsChildren = categoryAll.Any(n => n.ParentId == chidren.Id); chidren.Children = categoryAll.Where(n => n.ParentId == chidren.Id).Select(n => _mapper.Map(n)).ToList(); } } } return result; } // public List GetAllCategoryResult(IEnumerable categorys, int id, List categoryResults) // { // var result = new List(); // result = categorys.Where(n => n.ParentId == id).Select(n => _mapper.Map(n)).ToList(); // foreach (var item in result) // { // item.IsChildren = categorys.Any(n => n.ParentId == item.Id); // item.Children = categorys.Where(n => n.ParentId == item.Id).Select(n => _mapper.Map(n)).ToList(); // foreach (var children in item.Children) // { // GetAllCategoryResult(categorys,children.Id,categoryResults); // } // } // return result; // } } }