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

namespace IRaCIS.Application.Services
{
    [ ApiExplorerSettings(GroupName = "Institution")]
    public class HospitalService : BaseService, IHospitalService
    {
        private readonly IRepository<Hospital> _hospitalRepository;

        public HospitalService(IRepository<Hospital> hospitalRepository )
        {
            _hospitalRepository = hospitalRepository;
           
        }

        /// <summary> 获取所有医院列表 </summary>
        public async Task<IEnumerable<HospitalDTO>> GetHospitalList()
        {
            return await _hospitalRepository.ProjectTo<HospitalDTO>(_mapper.ConfigurationProvider).ToListAsync();
        }

        /// <summary> 添加医院 </summary>
        [HttpPost]
        public async Task<IResponseOutput> AddOrUpdateHospital(HospitalCommand hospitalCommand)
        {
            var exp = new EntityVerifyExp<Hospital>()
            {
                VerifyExp = h => h.HospitalName.Equals(hospitalCommand.HospitalName),
                //---已经存在同名的医院，请确认。
                VerifyMsg = _localizer["Hospital_DupName"]
            };

            var exp1 = new EntityVerifyExp<Hospital>()
            {
                VerifyExp = x => x.SiteId == hospitalCommand.SiteId && hospitalCommand.SiteId != null,
                //---已经存在同名的医院，请确认。
                VerifyMsg = _localizer["Hospital_SiteAdded"]
            };

          
            var hospital = await _hospitalRepository.InsertOrUpdateAsync(hospitalCommand, true, exp,exp1);
        
            return ResponseOutput.Ok(hospital.Id.ToString());
        
        }



        /// <summary> 删除医院信息 </summary>
        [HttpDelete("{hospitalId:guid}")]
        public async Task<IResponseOutput> DeleteHospital(Guid hospitalId)
        {
          
            //if (_userRepository.Find().Any(t => t.OrganizationId == hospitalId))
            //{
            //    return ResponseOutput.NotOk("该医院下存在用户，暂时无法删除。");
            //}

            var success = await _hospitalRepository.BatchDeleteNoTrackingAsync(x => x.Id == hospitalId);

            return ResponseOutput.Result(success);
        }

        /// <summary> 分页获取医院列表 </summary>
        [HttpPost]
        public  async Task<PageOutput<HospitalDTO>> GetHospitalPageList(HospitalQueryDTO hospitalSearchModel)
        {

            var hospitalQueryable =
                _hospitalRepository
                .WhereIf(hospitalSearchModel.HospitalName!=null, t => t.HospitalName.Contains(hospitalSearchModel.HospitalName!) || t.HospitalNameCN.Contains(hospitalSearchModel.HospitalName!))
                .WhereIf(hospitalSearchModel.City != null, t => t.City.Contains(hospitalSearchModel.City!) || t.HospitalNameCN.Contains(hospitalSearchModel.City!))
                .WhereIf(hospitalSearchModel.Province != null, t => t.Province.Contains(hospitalSearchModel.Province!) || t.HospitalNameCN.Contains(hospitalSearchModel.Province!))
                .ProjectTo<HospitalDTO>(_mapper.ConfigurationProvider);

            //优化后
            return await hospitalQueryable.ToPagedListAsync(hospitalSearchModel.PageIndex, hospitalSearchModel.PageSize, string.IsNullOrWhiteSpace(hospitalSearchModel.SortField) ? "HospitalName" : hospitalSearchModel.SortField,
                hospitalSearchModel.Asc);



        }
    }
}
