using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using YGNT.QuestionLibrary.CognitiveAbilities.Dto;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Mapster;
using Microsoft.EntityFrameworkCore;
using YGNT.Infrastructure.Model;
using YGNT.QuestionLibrary.Core.CognitiveAbilities;

namespace YGNT.QuestionLibrary.CognitiveAbilities
{
    /// <summary>
    /// 认知能力
    /// </summary>
    public class CognitiveAbilityAppService : ICognitiveAbilityAppService
    {
        private readonly IRepository<CognitiveAbility> _cognitiveAbilityRepository;

        public CognitiveAbilityAppService(IRepository<CognitiveAbility> cognitiveAbilityRepository)
        {
            _cognitiveAbilityRepository = cognitiveAbilityRepository;
        }

        /// <summary>
        /// 查询认知能力列表
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<CognitiveAbilityDto>> GetListAsync(SearchBaseInputDto search)
        {
            var queryable = _cognitiveAbilityRepository.GetAll()
                .WhereIf(!search.KeyWord.IsNullOrEmpty(), x => x.Name.Contains(search.KeyWord))
                .OrderBy(x => x.Name);

            var result = new PagedResultDto<CognitiveAbilityDto>()
            {
                TotalCount = await queryable.CountAsync(),
                Items = await queryable.PageBy(search.SkipCount, search.MaxResultCount)
                    .Select(x => x.Adapt(new CognitiveAbilityDto()))
                    .ToListAsync()
            };

            return result;
        }

        /// <summary>
        /// 查询认知能力详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<CognitiveAbilityDto> GetOneAsync([Required] int id)
        {
            var cognitiveAbility = await _cognitiveAbilityRepository.GetAsync(id);
            return cognitiveAbility.Adapt<CognitiveAbilityDto>();
        }

        /// <summary>
        /// 创建新认知能力
        /// </summary>
        /// <param name="create"></param>
        /// <returns></returns>
        public async Task<int> CreateAsync(CreateCognitiveAbilityDto create)
        {
            var cognitiveAbility = create.Adapt<CognitiveAbility>();
            return await _cognitiveAbilityRepository.InsertAndGetIdAsync(cognitiveAbility);
        }

        /// <summary>
        /// 修改认知能力
        /// </summary>
        /// <param name="id"></param>
        /// <param name="update"></param>
        /// <returns></returns>
        public async Task UpdateAsync(int id, UpdateCognitiveAbilityDto update)
        {
            _cognitiveAbilityRepository.Update(id, x => update.Adapt(x));
            await Task.CompletedTask;
        }

        /// <summary>
        /// 删除认知能力
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync([Required] int id)
        {
            await _cognitiveAbilityRepository.DeleteAsync(id);
        }
    }
}