using Microsoft.AspNetCore.Mvc;
using IRaCIS.Core.Application.Interfaces;
using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Core.Infra.EFCore.Common;
using MassTransit;
using IRaCIS.Core.Infrastructure;
using IRaCIS.Core.Domain.Share;
using IRaCIS.Core.Application.Service.Reading.Dto;
using Panda.DynamicWebApi.Attributes;

namespace IRaCIS.Core.Application.Service.TA
{


    [ApiExplorerSettings(GroupName = "Reading")]
    public class ReadingQuestionService : BaseService
    {
        private readonly IRepository<TumorAssessment> _tumorAssessmentRepository;
    
        public ReadingQuestionService(      IRepository<TumorAssessment> tumorAssessmentRepository         )
        {
            this._tumorAssessmentRepository = tumorAssessmentRepository;          
        }


        /// <summary>
        /// 获取疗效对照
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<TumorAssessmentView>> GetTumorAssessmentList(GetTumorAssessmentListInDto inDto)
        {
            var result = await _tumorAssessmentRepository
                .Where(x => x.CriterionId == inDto.CriterionId)
                .WhereIf(inDto.OverallEfficacy != null, x => x.OverallEfficacy == inDto.OverallEfficacy)
                 .WhereIf(inDto.TargetLesion != null, x => x.TargetLesion == inDto.TargetLesion)
                  .WhereIf(inDto.NonTargetLesions != null, x => x.NonTargetLesions == inDto.NonTargetLesions)
                   .WhereIf(inDto.NewLesion != null, x => x.NewLesion == inDto.NewLesion)
                  .ProjectTo<TumorAssessmentView>(_mapper.ConfigurationProvider).ToListAsync();
            return result;
        }

        /// <summary>
        /// 获取疗效对照
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageOutput<TumorAssessmentView>> GetTumorAssessmentPageList(GetTumorAssessmentListInDto inQuery)
        {
            var query = _tumorAssessmentRepository
                .Where(x => x.CriterionId == inQuery.CriterionId)
                .WhereIf(inQuery.OverallEfficacy != null, x => x.OverallEfficacy == inQuery.OverallEfficacy)
                .WhereIf(inQuery.TargetLesion != null, x => x.TargetLesion == inQuery.TargetLesion)
                .WhereIf(inQuery.NonTargetLesions != null, x => x.NonTargetLesions == inQuery.NonTargetLesions)
                .WhereIf(inQuery.NewLesion != null, x => x.NewLesion == inQuery.NewLesion)
                .ProjectTo<TumorAssessmentView>(_mapper.ConfigurationProvider);

            return await query.ToPagedListAsync(inQuery.PageIndex, inQuery.PageSize, string.IsNullOrWhiteSpace(inQuery.SortField) ? nameof(TumorAssessmentView.Id) : inQuery.SortField, inQuery.Asc);
        }


        /// <summary>
        /// 新增修改疗效对照
        /// </summary>
        /// <param name="indto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IResponseOutput> AddOrUpdateTumorAssessment(AddOrUpdateTumorAssessmentInDto indto)
        {

            var entity = await _tumorAssessmentRepository.InsertOrUpdateAsync(indto, true);

            return ResponseOutput.Ok(entity.Id.ToString());

        }

        /// <summary>
        /// 删除疗效对照
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete("{Id:guid}")]
        public async Task<IResponseOutput> DeleteTumorAssessment(Guid Id)
        {
            await _tumorAssessmentRepository.DeleteFromQueryAsync(t => t.Id == Id);
            var success = await _tumorAssessmentRepository.SaveChangesAsync();
            return ResponseOutput.Result(success);
        }

    }
}