using Exam.Application.Interface.Exam;
using Exam.Infrastructure.Data.Entity;
using Exam.Infrastructure.Extensions;
using Exam.Share.ViewResponses.Sourceware;
using Hotline.Application.Exam.Constants.Messages;
using Hotline.Application.Exam.Extensions;
using Hotline.Application.Exam.QueryExtensions.ExamManages;
using Hotline.Exams.Sourcewares;
using Hotline.Repository.SqlSugar;
using Hotline.Repository.SqlSugar.Exam.Interfaces.ExamManages;
using Hotline.Share.Dtos.ExamManages;
using Hotline.Share.Requests.Exam;
using Hotline.Share.Requests.Sourceware;
using Hotline.Share.ViewResponses.Exam;
using Mapster;
using MapsterMapper;
using System.ComponentModel;
using Hotline.Application.Exam.Interface.ExamManages;
using Hotline.Exams.ExamManages;
using XF.Domain.Dependency;
using XF.Domain.Exceptions;
using Hotline.Repository.SqlSugar.Exam.Service;

namespace Hotline.Application.Exam.Service.ExamManages
{
    /// <summary>
    /// 考试标签服务
    /// </summary>
    [Description("考试标签服务")]
    public class ExamTagService : ApiService<ExamTag, AddExamTagDto,UpdateExamTagDto,HotlineDbContext>,IExamTagService, IScopeDependency
    {
        private readonly IExamTagRepository _repository;
        private readonly IMapper _mapper;
        public ExamTagService(IExamTagRepository repository,IMapper mapper) : base(repository, mapper)
        {
            _repository = repository;
            _mapper = mapper;
        }

        public async Task<ExamTagDto> GetAsync(EntityQueryRequest entityQueryRequest)
        {
            var entity = await _repository.GetAsync(entityQueryRequest.Id);

            return _mapper.Map<ExamTagDto>(entity);
        }

        public async Task<(int, List<ExamTagViewResponse>)> GetListAsync(ExamTagRequest queryRequest)
        {
            var query = _repository.Queryable();

            var result = await query.Select(m => new ExamTagViewResponse { 
                				Name = m.Name,
								ParentId = m.ParentId,
								Id = m.Id,
                                SortIndex = m.SortIndex,
                                Status = m.Status
				            }).ToListAsync();

            var total = await query.CountAsync();

            return (total,result);
        }

        public async Task<PageViewResponse<ExamTagViewResponse>> GetPagedListAsync(ExamTagRequest queryRequest)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 获取树状
        /// </summary>
        /// <param name="sourcewareCategoryDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<ExamTagViewResponse>> GetTreeAsync(ExamTagRequest examTagRequest)
        {
            var expression = examTagRequest.GetExpression();

            var sourcewareCategorys = await _repository.GetTreeAsync(expression);

            var sourcewareCategoryViewResponses = sourcewareCategorys.Select(m => new ExamTagViewResponse
            {
                Id = m.Id,
                Name = m.Name,
                ParentId = m.ParentId,
                SortIndex = m.SortIndex,
                Status = m.Status,
                Children = _mapper.Map<List<ExamTagViewResponse>>(m.Children)
            }).ToList();


            return sourcewareCategoryViewResponses;
        }

        /// <summary>
        /// 删除考试标签
        /// </summary>
        /// <param name="entityQueryRequest"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="UserFriendlyException"></exception>
        public override Task DeleteAsync(EntityQueryRequest entityQueryRequest, CancellationToken cancellationToken)
        {
            if (_repository.CheckHasChild(entityQueryRequest))
            {
                throw new UserFriendlyException(BusinessExamErrorMessage.ServiceError,string.Format(BusinessExamErrorMessage.HasChild, typeof(ExamTag).GetDescription()));
            }

            return base.DeleteAsync(entityQueryRequest, cancellationToken);
        }
    }
}


