98 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			98 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.Domain.Share;
 | |
| using Medallion.Threading;
 | |
| 
 | |
| namespace IRaCIS.Application.Services
 | |
| {
 | |
|     [ApiExplorerSettings(GroupName = "Institution")]
 | |
|     public class SiteService : BaseService, ISiteService
 | |
|     {
 | |
|         private readonly IRepository<Site> _siteRepository;
 | |
|         private readonly IRepository<TrialSiteUser> _trialSiteUserRepository;
 | |
|         private readonly IDistributedLockProvider _distributedLockProvider;
 | |
| 
 | |
|         public SiteService(IRepository<Site> siteRepository, IRepository<TrialSiteUser> trialSiteUserRepository, IDistributedLockProvider distributedLockProvider)
 | |
|         {
 | |
|             _siteRepository = siteRepository;
 | |
|             _trialSiteUserRepository = trialSiteUserRepository;
 | |
|             _distributedLockProvider = distributedLockProvider;
 | |
|         }
 | |
| 
 | |
|         /// <summary> 分页获取研究中心列表 </summary>
 | |
|         [HttpPost]
 | |
|         public async Task<PageOutput<SiteSelectDTO>> GetSiteList(SiteQueryParam searchModel)
 | |
|         {
 | |
| 
 | |
|             var siteQueryable = _siteRepository
 | |
|                    .WhereIf(!string.IsNullOrWhiteSpace(searchModel.SiteName), t => t.SiteName.Contains(searchModel.SiteName) || t.SiteNameCN.Contains(searchModel.SiteName))
 | |
|                    .WhereIf(!string.IsNullOrWhiteSpace(searchModel.AliasName), t => t.AliasName.Contains(searchModel.AliasName))
 | |
|                    .WhereIf(!string.IsNullOrWhiteSpace(searchModel.City), t => t.City.Contains(searchModel.City))
 | |
|                    .WhereIf(!string.IsNullOrWhiteSpace(searchModel.Country), t => t.Country.Contains(searchModel.Country))
 | |
|                    .WhereIf(!string.IsNullOrWhiteSpace(searchModel.Province), t => t.Province.Contains(searchModel.Province))
 | |
|                    .ProjectTo<SiteSelectDTO>(_mapper.ConfigurationProvider, new {  isEn_Us = _userInfo.IsEn_Us });
 | |
| 
 | |
| 
 | |
|             return await siteQueryable.ToPagedListAsync(searchModel.PageIndex, searchModel.PageSize, string.IsNullOrWhiteSpace(searchModel.SortField) ? "SiteName" : searchModel.SortField, searchModel.Asc);
 | |
| 
 | |
| 
 | |
|         }
 | |
| 
 | |
|         public async Task<IEnumerable<SiteSelectionDTO>> GetAllSiteList()
 | |
|         {
 | |
|             return await _siteRepository.ProjectTo<SiteSelectionDTO>(_mapper.ConfigurationProvider).ToListAsync();
 | |
|         }
 | |
| 
 | |
| 
 | |
|         /// <summary> 添加研究中心 </summary>
 | |
| 
 | |
|         public async Task<IResponseOutput> AddOrUpdateSite(SiteCommand siteCommand)
 | |
|         {
 | |
| 
 | |
|             var exp = new EntityVerifyExp<Site>()
 | |
|             {
 | |
|                 VerifyExp = h => h.SiteName.Equals(siteCommand.SiteName) || h.SiteCode.Equals(siteCommand.SiteCode),
 | |
|                 //---已经存在同名的中心,请确认。
 | |
|                 VerifyMsg = _localizer["Site_DupName"]
 | |
|             };
 | |
| 
 | |
|             var @lock = _distributedLockProvider.CreateLock($"SiteAdd");
 | |
| 
 | |
|             using (await @lock.AcquireAsync())
 | |
|             {
 | |
|                 if (siteCommand.Id == null)
 | |
|                 {
 | |
| 
 | |
|                     siteCommand.Code = await _siteRepository.Select(t => t.Code).DefaultIfEmpty().MaxAsync() + 1;
 | |
| 
 | |
|                     siteCommand.SiteCode = AppSettings.GetCodeStr(siteCommand.Code, nameof(Site));
 | |
|                 }
 | |
| 
 | |
|                 var site = await _siteRepository.InsertOrUpdateAsync(siteCommand, true, exp);
 | |
| 
 | |
|                 return ResponseOutput.Ok(site.Id.ToString());
 | |
| 
 | |
|             }
 | |
| 
 | |
|         }
 | |
| 
 | |
|         /// <summary> 删除研究中心 </summary>
 | |
| 
 | |
|         [HttpDelete("{siteId:guid}")]
 | |
|         public async Task<IResponseOutput> DeleteSite(Guid siteId)
 | |
|         {
 | |
| 
 | |
|             if (await _trialSiteUserRepository.AnyAsync(t => t.SiteId == siteId))
 | |
|             {
 | |
|                 //---该中心已经加入项目,不可以被删除。
 | |
|                 return ResponseOutput.NotOk(_localizer["Site_InProject"]);
 | |
|             }
 | |
| 
 | |
|             var success = await _siteRepository.BatchDeleteNoTrackingAsync(x => x.Id == siteId);
 | |
|             return ResponseOutput.Result(success);
 | |
|         }
 | |
|     }
 | |
| }
 |