//--------------------------------------------------------------------
//     此代码由T4模板自动生成  byzhouhang 20210918
//	   生成时间 2022-03-03 15:28:32 
//     对此文件的更改可能会导致不正确的行为，并且如果重新生成代码，这些更改将会丢失。
//--------------------------------------------------------------------

using Dicom;
using IRaCIS.Core.Application.Interfaces;
using IRaCIS.Core.Application.MediatR.Handlers;
using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Core.Infra.EFCore;
using MediatR;
using Microsoft.AspNetCore.Mvc;
namespace IRaCIS.Core.Application.Service
{
    /// <summary>
    /// SystemAnonymizationService
    /// </summary>	
    [ApiExplorerSettings(GroupName = "Image")]
    public class SystemAnonymizationService : BaseService, ISystemAnonymizationService
    {
        private readonly IMediator _mediator;
        private readonly IRepository<SystemAnonymization> systemAnonymizationRepository;

        public SystemAnonymizationService(IMediator mediator, IRepository<SystemAnonymization> systemAnonymizationRepository)
        {
            _mediator = mediator;
            this.systemAnonymizationRepository = systemAnonymizationRepository;
        }

        [HttpPost]
        public async Task<PageOutput<SystemAnonymizationView>> GetSystemAnonymizationList(SystemAnonymizationQuery querySystemAnonymization)
        {

            var systemAnonymizationQueryable = systemAnonymizationRepository
                .WhereIf(!string.IsNullOrEmpty(querySystemAnonymization.Group), t => t.Group.Contains(querySystemAnonymization.Group))
                .WhereIf(!string.IsNullOrEmpty(querySystemAnonymization.Element), t => t.Element.Contains(querySystemAnonymization.Element))
                 .WhereIf(!string.IsNullOrEmpty(querySystemAnonymization.TagDescription), t => t.TagDescription.Contains(querySystemAnonymization.TagDescription))
                  .WhereIf(!string.IsNullOrEmpty(querySystemAnonymization.TagDescriptionCN), t => t.TagDescriptionCN.Contains(querySystemAnonymization.TagDescriptionCN))
                .WhereIf(querySystemAnonymization.IsAdd != null, t => t.IsAdd == querySystemAnonymization.IsAdd)
                .WhereIf(!string.IsNullOrEmpty(querySystemAnonymization.ValueRepresentation), t => t.ValueRepresentation.Contains(querySystemAnonymization.ValueRepresentation))
            .ProjectTo<SystemAnonymizationView>(_mapper.ConfigurationProvider);

            return await systemAnonymizationQueryable.ToPagedListAsync(querySystemAnonymization.PageIndex, querySystemAnonymization.PageSize, querySystemAnonymization.SortField, querySystemAnonymization.Asc);
        }


        public async Task<IResponseOutput> AddOrUpdateSystemAnonymization(SystemAnonymizationAddOrEdit addOrEditSystemAnonymization)
        {

            ushort result;
            if ((UInt16.TryParse(addOrEditSystemAnonymization.Group, System.Globalization.NumberStyles.HexNumber, null, out result)==false) || (UInt16.TryParse(addOrEditSystemAnonymization.Element, System.Globalization.NumberStyles.HexNumber, null, out result) == false))
            {
                //请核对DicomTag 配置的元素号或者组号是否符合要求
                return ResponseOutput.NotOk(_localizer["SystemAnonymization_InvalidGroupOrElement"]);
            }



            var entity = await _repository.InsertOrUpdateAsync<SystemAnonymization, SystemAnonymizationAddOrEdit>(addOrEditSystemAnonymization, true);

            await _mediator.Send(new AnonymizeCacheRequest());

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

        }


        [HttpDelete("{systemAnonymizationId:guid}")]
        public async Task<IResponseOutput> DeleteSystemAnonymization(Guid systemAnonymizationId)
        {
            var success = await systemAnonymizationRepository.BatchDeleteNoTrackingAsync(t => t.Id == systemAnonymizationId);

            return ResponseOutput.Result(success);
        }


    }
}
