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

using IRaCIS.Core.Application.Interfaces;
using IRaCIS.Core.Application.ViewModel;
using Microsoft.AspNetCore.Mvc;
namespace IRaCIS.Core.Application.Service
{
    /// <summary>
    /// TrialSiteDicomAEService
    /// </summary>	
    [ApiExplorerSettings(GroupName = "Trial")]
    public class TrialSiteDicomAEService(IRepository<TrialSiteDicomAE> _trialSiteDicomAERepository, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService, ITrialSiteDicomAEService
    {



        [HttpPost]
        public async Task<List<TrialSiteDicomAEView>> GetTrialSiteDicomAEList(TrialSiteDicomAEQuery inQuery)
        {

            var trialSiteDicomAEQueryable =

               _trialSiteDicomAERepository.Where(t => t.TrialSiteId == inQuery.TrialSiteId)
                .WhereIf(!string.IsNullOrWhiteSpace(inQuery.IP), t => t.IP.Contains(inQuery.IP))
                .WhereIf(!string.IsNullOrWhiteSpace(inQuery.Port), t => t.Port.Contains(inQuery.Port))
                .WhereIf(!string.IsNullOrWhiteSpace(inQuery.Description), t => t.Description.Contains(inQuery.Description))
                .WhereIf(!string.IsNullOrWhiteSpace(inQuery.CallingAE), t => t.CallingAE.Contains(inQuery.CallingAE))
             .ProjectTo<TrialSiteDicomAEView>(_mapper.ConfigurationProvider);

            //var pageList = await trialSiteDicomAEQueryable
            //.ToPagedListAsync(inQuery.PageIndex, inQuery.PageSize, string.IsNullOrWhiteSpace(inQuery.SortField) ? nameof(TrialSiteDicomAEView.Id) : inQuery.SortField,
            //inQuery.Asc);

            var list = await trialSiteDicomAEQueryable.ToListAsync();

            return list;
        }


        public async Task<IResponseOutput> AddOrUpdateTrialSiteDicomAE(TrialSiteDicomAEAddOrEdit addOrEditTrialSiteDicomAE)
        {
            var verifyExp1 = new EntityVerifyExp<TrialSiteDicomAE>()
            {
                VerifyExp = u => u.CallingAE == addOrEditTrialSiteDicomAE.CallingAE && u.TrialId == addOrEditTrialSiteDicomAE.TrialId
                && u.TrialSiteId == addOrEditTrialSiteDicomAE.TrialSiteId,

                //"不允许添加相同的记录"
                VerifyMsg = _localizer["TrialSiteDicomAE_RepeatRecord"]
            };
            var verifyExp2 = new EntityVerifyExp<TrialSiteDicomAE>()
            {
                VerifyExp = u => u.CallingAE == addOrEditTrialSiteDicomAE.CallingAE && u.TrialId == addOrEditTrialSiteDicomAE.TrialId && u.TrialSiteId != addOrEditTrialSiteDicomAE.TrialSiteId,

                // "其他中心已有该CallingAE"
                VerifyMsg = _localizer["TrialSiteDicomAE_OtherSiteExistCallingAE"]
            };


            var entity = await _trialSiteDicomAERepository.InsertOrUpdateAsync(addOrEditTrialSiteDicomAE, true, verifyExp1, verifyExp2);

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

        }


        [HttpDelete("{trialSiteDicomAEId:guid}")]
        public async Task<IResponseOutput> DeleteTrialSiteDicomAE(Guid trialSiteDicomAEId)
        {
            var success = await _trialSiteDicomAERepository.DeleteFromQueryAsync(t => t.Id == trialSiteDicomAEId, true);
            return ResponseOutput.Ok();
        }


    }
}
