82 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			82 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			C#
		
	
	
| 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 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 = "A hospital with the same name already existed in the table. Please confirm."
 | |
|             };
 | |
| 
 | |
|             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("There are already doctors registered in this hospital,and it couldn't be deleted");
 | |
|             }
 | |
|             //if (_userRepository.Find().Any(t => t.OrganizationId == hospitalId))
 | |
|             //{
 | |
|             //    return ResponseOutput.NotOk("该医院下存在用户,暂时无法删除。");
 | |
|             //}
 | |
| 
 | |
|             var success = await _hospitalRepository.DeleteFromQueryAsync(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);
 | |
| 
 | |
| 
 | |
| 
 | |
|         }
 | |
|     }
 | |
| } |