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;
        private readonly IRepository<Doctor> _doctorRepository;

        public HospitalService(IRepository<Hospital> hospitalRepository, IRepository<Doctor> doctorRepository)
        {
            _hospitalRepository = hospitalRepository;
            this._doctorRepository = doctorRepository;
        }

        /// <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 = "已经存在同名的医院，请确认。"
            };

            if (await _hospitalRepository.AnyAsync(x => x.SiteId == hospitalCommand.SiteId && hospitalCommand.SiteId != null))
            {
                throw new BusinessValidationFailedException("当前Site已经添加到其他Hospital了");
            }

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



        /// <summary> 删除医院信息 </summary>
        [HttpDelete("{hospitalId:guid}")]
        public async Task<IResponseOutput> DeleteHospital(Guid hospitalId)
        {
            if (await _doctorRepository.AnyAsync(t => t.Id == 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);



        }
    }
}