104 lines
4.0 KiB
C#
104 lines
4.0 KiB
C#
using IRaCIS.Application.Interfaces;
|
|
using IRaCIS.Application.Contracts;
|
|
using IRaCIS.Core.Infra.EFCore;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using IRaCIS.Core.Infrastructure;
|
|
using Microsoft.Data.Sqlite;
|
|
|
|
namespace IRaCIS.Application.Services
|
|
{
|
|
[ApiExplorerSettings(GroupName = "Institution")]
|
|
public class HospitalService : BaseService, IHospitalService
|
|
{
|
|
private readonly IRepository<Hospital> _hospitalRepository;
|
|
private readonly IRepository<Doctor> _doctorRepository;
|
|
private readonly IRepository<Site> _siteRepository;
|
|
|
|
public HospitalService(IRepository<Hospital> hospitalRepository, IRepository<Doctor> doctorRepository, IRepository<Site> siteRepository)
|
|
{
|
|
_hospitalRepository = hospitalRepository;
|
|
_doctorRepository = doctorRepository;
|
|
_siteRepository = siteRepository;
|
|
}
|
|
|
|
/// <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);
|
|
|
|
//手动解绑医院与site的关系
|
|
if (hospitalCommand.SiteId == null && hospital.SiteId != null)
|
|
{
|
|
await _siteRepository.BatchUpdateNoTrackingAsync(t => t.Id == hospital.SiteId, u => new Site() { HospitalId = null });
|
|
}
|
|
|
|
//手动绑
|
|
if (hospitalCommand.SiteId != null)
|
|
{
|
|
await _siteRepository.BatchUpdateNoTrackingAsync(t => t.Id == hospitalCommand.SiteId, u => new Site() { HospitalId = hospital.Id});
|
|
}
|
|
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(_localizer["Hospital_HasDoctors"]);
|
|
}
|
|
|
|
|
|
var success = await _hospitalRepository.BatchDeleteNoTrackingAsync(x => x.Id == hospitalId);
|
|
|
|
return ResponseOutput.Result(success);
|
|
}
|
|
|
|
/// <summary> 分页获取医院列表 </summary>
|
|
[HttpPost]
|
|
public async Task<PageOutput<HospitalDTO>> GetHospitalPageList(HospitalQueryDTO inQuery)
|
|
{
|
|
|
|
var hospitalQueryable =
|
|
_hospitalRepository
|
|
.WhereIf(inQuery.HospitalName != null, t => t.HospitalName.Contains(inQuery.HospitalName!) || t.HospitalNameCN.Contains(inQuery.HospitalName!))
|
|
.WhereIf(inQuery.City != null, t => t.City.Contains(inQuery.City!) || t.HospitalNameCN.Contains(inQuery.City!))
|
|
.WhereIf(inQuery.Province != null, t => t.Province.Contains(inQuery.Province!) || t.HospitalNameCN.Contains(inQuery.Province!))
|
|
.ProjectTo<HospitalDTO>(_mapper.ConfigurationProvider);
|
|
|
|
//优化后
|
|
return await hospitalQueryable.ToPagedListAsync(inQuery);
|
|
|
|
|
|
|
|
}
|
|
}
|
|
}
|