using IRaCIS.Application.Interfaces;
using IRaCIS.Application.Contracts;
using IRaCIS.Core.Infra.EFCore;
using Microsoft.AspNetCore.Mvc;

namespace IRaCIS.Application.Services
{
    [ ApiExplorerSettings(GroupName = "Institution")]
    public class SponsorService : BaseService, ISponsorService
    {
        private readonly IRepository<Sponsor> _sponsorRepository;
        private readonly IRepository<Trial> _trialRepository;

        public SponsorService(IRepository<Sponsor> sponsorRepository, IRepository<Trial> trialRepository)
        {
            _sponsorRepository = sponsorRepository;
            this._trialRepository = trialRepository;
        }

        /// <summary> 分页获取申办方列表 </summary>
       [HttpPost]
        public async Task<PageOutput<SponsorDTO>> GetSponsorList(SponsorQueryDTO sponsorSearchModel)
        {
           
            var sponsorQueryable = _sponsorRepository
                .WhereIf(!string.IsNullOrWhiteSpace(sponsorSearchModel.SponsorName),t => t.SponsorName.Contains(sponsorSearchModel.SponsorName)||  t.SponsorNameCN.Contains(sponsorSearchModel.SponsorName))
                .WhereIf(!string.IsNullOrEmpty(sponsorSearchModel.SponsorCode), t => t.SponsorCode.Contains(sponsorSearchModel.SponsorCode))
                .ProjectTo<SponsorDTO>(_mapper.ConfigurationProvider);

            return await sponsorQueryable.ToPagedListAsync(sponsorSearchModel.PageIndex,
                sponsorSearchModel.PageSize, string.IsNullOrWhiteSpace(sponsorSearchModel.SortField) ? "Id" : sponsorSearchModel.SortField, sponsorSearchModel.Asc);
           
        }

        /// <summary> 分页获取申办方列表 </summary>
        public async  Task<IEnumerable<SponsorSelectDTO>> GetAllSponsorList()
        {
            //Expression<Func<Sponsor, bool>> sponsorLambda = x => true;
            //if (!string.IsNullOrWhiteSpace(sponsorSearchModel.SponsorName))
            //{
            //    sponsorLambda = sponsorLambda.And(t => t.SponsorName.Contains(sponsorSearchModel.SponsorName.Trim()));
            //}
            var sponsorQueryable = _sponsorRepository.ProjectTo<SponsorSelectDTO>(_mapper.ConfigurationProvider,new { isEn_Us= _userInfo.IsEn_Us});
            return await sponsorQueryable.ToListAsync();
        }

        /// <summary> 添加申办方 </summary>
        public async Task<IResponseOutput> AddOrUpdateSponsor(SponsorCommand sponsorCommand)
        {
            var exp = new EntityVerifyExp<Sponsor>()
            {
                VerifyExp = h => h.SponsorName.Equals(sponsorCommand.SponsorName),
                //---已经存在同名的申办方，请确认。
                VerifyMsg = _localizer["Sponsor_DupName"]
            };

            var sponsor = await _sponsorRepository.InsertOrUpdateAsync(sponsorCommand, true, exp);

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

          
           
        }

        /// <summary> 删除申办方 </summary>
        [HttpDelete("{sponsorId:guid}")]
       
        public async  Task<IResponseOutput> DeleteSponsor(Guid sponsorId)
        {
            if (await _trialRepository.AnyAsync(t => t.CROId == sponsorId))
            {
                //---该申办方已经加入项目，不允许删除。
                return ResponseOutput.NotOk(_localizer["Sponsor_InProject"]);
            }
            //if (_userRepository.Find().Any(t => t.OrganizationId == sponsorId))
            //{
            //    return ResponseOutput.NotOk("该申办方下存在用户，暂时无法删除。");
            //}
          

            var success = await _sponsorRepository.BatchDeleteNoTrackingAsync(x => x.Id == sponsorId);
            return ResponseOutput.Result(success);
        }      
    }
}
