using IRaCIS.Application.Contracts; using IRaCIS.Core.Domain.Share; using IRaCIS.Core.Application.Filter; using Microsoft.AspNetCore.Mvc; using IRaCIS.Core.Application.Contracts.DTO; using IRaCIS.Core.Application.Contracts; using IRaCIS.Core.Application.Interfaces; using IRaCIS.Core.Application.Service; using Microsoft.AspNetCore.Authorization; using IRaCIS.Core.Application.Auth; namespace IRaCIS.Core.Application.Services { [ApiExplorerSettings(GroupName = "Trial")] public class TrialMaintenanceService : BaseService, ITrialSiteService { private readonly IRepository _trialSiteRepository; private readonly IRepository _trialSiteUserRepository; private readonly IRepository _siteRepository; private readonly IRepository _trialRepository; public TrialMaintenanceService(IRepository trialSiteRepository, IRepository trialSiteUserRepository , IRepository siteRepository, IRepository trialRepository) { _trialSiteRepository = trialSiteRepository; _trialSiteUserRepository = trialSiteUserRepository; _siteRepository = siteRepository; _trialRepository = trialRepository; } #region 导出列表 /// /// Site用户列表导出 /// /// /// /// /// [HttpPost] [AllowAnonymous] public async Task TrialSiteUserListExport(SiteCRCExportQueryDTO param, [FromServices] IRepository _commonDocumentRepository) { var exportInfo = (await _trialRepository.Where(t => t.Id == param.TrialId).IgnoreQueryFilters().ProjectTo(_mapper.ConfigurationProvider).FirstOrDefaultAsync()).IfNullThrowException(); exportInfo.TrialSiteUserList = await _trialSiteUserRepository.Where(t => t.TrialId == param.TrialId).IgnoreQueryFilters() .WhereIf(param.IsDeleted != null, t => t.IsDeleted == param.IsDeleted) .WhereIf(!string.IsNullOrWhiteSpace(param.SiteName), t => t.Site.SiteName.Contains(param.SiteName)) .WhereIf(!string.IsNullOrWhiteSpace(param.TrialSiteAliasName), t => t.TrialSite.TrialSiteAliasName.Contains(param.TrialSiteAliasName)) .WhereIf(!string.IsNullOrWhiteSpace(param.TrialSiteCode), t => t.TrialSite.TrialSiteCode.Contains(param.TrialSiteCode)) .WhereIf(_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ClinicalResearchCoordinator, t => t.UserId == _userInfo.Id) .WhereIf(!string.IsNullOrWhiteSpace(param.UserKeyInfo), t => (t.User.FullName).Contains(param.UserKeyInfo) || t.User.UserName.Contains(param.UserKeyInfo) || t.User.EMail.Contains(param.UserKeyInfo)) .ProjectTo(_mapper.ConfigurationProvider).ToListAsync(); var no = 1; exportInfo.TrialSiteUserList.ForEach(t => t.No = no++); return await ExcelExportHelper.DataExportAsync(StaticData.Export.TrialSiteUserList_Export, exportInfo, exportInfo.TrialCode, _commonDocumentRepository, _hostEnvironment); } /// /// Site用户汇总表导出 /// /// /// /// /// /// /// [HttpPost] [AllowAnonymous] public async Task TrialSiteUserSummaryListExport(TrialSiteUserSurveyExportQueryDto queryParam, [FromServices] IRepository _commonDocumentRepository, [FromServices] IRepository _trialSiteSurveyRepository, [FromServices] IRepository _trialSiteUserSurveyRepository ) { var data = (await _trialRepository.Where(t => t.Id == queryParam.TrialId).IgnoreQueryFilters().ProjectTo(_mapper.ConfigurationProvider).FirstOrDefaultAsync()).IfNullThrowException(); var groupSelectIdQuery = _trialSiteSurveyRepository.Where(t => t.TrialId == queryParam.TrialId) .WhereIf(queryParam.SiteId != null, t => t.SiteId == queryParam.SiteId) .WhereIf(!string.IsNullOrEmpty(queryParam.FormWriterKeyInfo), t => (t.UserName).Contains(queryParam.FormWriterKeyInfo) || t.Email.Contains(queryParam.FormWriterKeyInfo) || t.Phone.Contains(queryParam.FormWriterKeyInfo)) .GroupBy(t => t.SiteId) .Select(g => g.OrderByDescending(u => u.CreateTime).Select(t => t.Id).First()); var query = _trialSiteUserSurveyRepository .Where(t => groupSelectIdQuery.Contains(t.TrialSiteSurveyId)) .WhereIf(queryParam.UserTypeId != null, t => t.UserTypeId == queryParam.UserTypeId) .WhereIf(queryParam.IsGenerateAccount != null, t => t.IsGenerateAccount == queryParam.IsGenerateAccount) .WhereIf(queryParam.TrialRoleNameId != null, t => t.TrialRoleNameId == queryParam.TrialRoleNameId) .WhereIf(queryParam.State != null && queryParam.State != TrialSiteUserStateEnum.OverTime, t => t.InviteState == queryParam.State) .WhereIf(queryParam.State != null && queryParam.State == TrialSiteUserStateEnum.OverTime, t => t.InviteState == TrialSiteUserStateEnum.HasSend && t.ExpireTime < DateTime.Now) .WhereIf(!string.IsNullOrEmpty(queryParam.UserName), t => (t.LastName + " / " + t.FirstName).Contains(queryParam.UserName)) .WhereIf(!string.IsNullOrEmpty(queryParam.OrganizationName), t => t.OrganizationName.Contains(queryParam.OrganizationName)) .ProjectTo(_mapper.ConfigurationProvider); data.TrialSiteUserList = await query.ToListAsync(); var exportInfo = data; //处理翻译 var no = 1; exportInfo.TrialSiteUserList.ForEach(t => t.No = no++); return await ExcelExportHelper.DataExportAsync(StaticData.Export.TrialSiteUserSummary_Export, exportInfo, exportInfo.TrialCode, _commonDocumentRepository, _hostEnvironment); } #endregion /// Pannel 进去 SiteTab [HttpPost] public async Task> GetSiteCRCList(SiteCrcQueryDTO param) { var siteStatQuery = _trialSiteRepository.Where(t => t.TrialId == param.TrialId, ignoreQueryFilters: true) .WhereIf(param.IsDeleted != null, t => t.IsDeleted == param.IsDeleted) .WhereIf(!string.IsNullOrWhiteSpace(param.SiteName), t => t.Site.SiteName.Contains(param.SiteName)) .WhereIf(!string.IsNullOrWhiteSpace(param.TrialSiteAliasName), t => t.TrialSiteAliasName.Contains(param.TrialSiteAliasName)) .WhereIf(!string.IsNullOrWhiteSpace(param.TrialSiteCode), t => t.TrialSiteCode.Contains(param.TrialSiteCode)) .WhereIf(_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ClinicalResearchCoordinator, t => t.CRCUserList.Any(k => k.UserId == _userInfo.Id)) .WhereIf(!string.IsNullOrWhiteSpace(param.UserKeyInfo), t => t.CRCUserList.Any(k => (k.User.FullName).Contains(param.UserKeyInfo) || k.User.UserName.Contains(param.UserKeyInfo) || k.User.EMail.Contains(param.UserKeyInfo))) .ProjectTo(_mapper.ConfigurationProvider); return await siteStatQuery.ToPagedListAsync(param.PageIndex, param.PageSize, string.IsNullOrWhiteSpace(param.SortField) ? "SiteCode" : param.SortField, param.Asc); } /// [new] setting页面Site列表,和getSiteCRCList对比 没有统计数据,增加了一些site信息 [HttpPost] public async Task> GetSiteCRCSimpleList(SiteCrcQueryDTO param) { var siteStatQuery = _trialSiteRepository.Where(t => t.TrialId == param.TrialId).IgnoreQueryFilters() .WhereIf(param.IsDeleted != null, t => t.IsDeleted == param.IsDeleted) .WhereIf(!string.IsNullOrWhiteSpace(param.SiteName), t => t.Site.SiteName.Contains(param.SiteName)) .WhereIf(!string.IsNullOrWhiteSpace(param.TrialSiteAliasName), t => t.TrialSiteAliasName.Contains(param.TrialSiteAliasName)) .WhereIf(!string.IsNullOrWhiteSpace(param.TrialSiteCode), t => t.TrialSiteCode.Contains(param.TrialSiteCode)) .WhereIf(_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ClinicalResearchCoordinator, t => t.CRCUserList.Any(k => k.UserId == _userInfo.Id)) .WhereIf(!string.IsNullOrWhiteSpace(param.UserKeyInfo), t => t.CRCUserList.Any(k => (k.User.FullName).Contains(param.UserKeyInfo) || k.User.UserName.Contains(param.UserKeyInfo) || k.User.EMail.Contains(param.UserKeyInfo))) .ProjectTo(_mapper.ConfigurationProvider); var result = await siteStatQuery.ToPagedListAsync(param.PageIndex, param.PageSize, string.IsNullOrWhiteSpace(param.SortField) ? "Site" : param.SortField, param.Asc); return result; } /// 获取某一Site下面的负责的CRC列表 [HttpGet, Route("{trialId:guid}/{siteId:guid}")] public async Task> GetTrialSiteCRCList(Guid trialId, Guid siteId) { var query = _trialSiteUserRepository.Where(t => t.TrialId == trialId && t.SiteId == siteId).IgnoreQueryFilters() .ProjectTo(_mapper.ConfigurationProvider); return await query.ToListAsync(); } /// [new] Setting页面 Site勾选列表( [HttpPost] public async Task> GetTrialSiteScreeningList(TrialSiteQuery searchModel) { // 之前选择了的不能再次出现在列表,做的时候我就不建议这样搞,搞好了 现在又要改回去。。。 瞎折腾。。。。 var siteQueryable = _siteRepository.AsQueryable(true) .WhereIf(!string.IsNullOrWhiteSpace(searchModel.SiteName), t => t.SiteName.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)) .ProjectTo(_mapper.ConfigurationProvider, new { trialId = searchModel.TrialId }); return await siteQueryable.ToPagedListAsync(searchModel.PageIndex, searchModel.PageSize, string.IsNullOrWhiteSpace(searchModel.SortField) ? "SiteName" : searchModel.SortField, searchModel.Asc); } /// Setting页面 Site批量添加 [HttpPost] [UnitOfWork] [Authorize(Policy = IRaCISPolicy.PM_APM)] [TypeFilter(typeof(TrialResourceFilter))] public async Task AddTrialSites(List trialSites) { var addArray = _mapper.Map>(trialSites); await _repository.AddRangeAsync(addArray); return ResponseOutput.Result(await _repository.SaveChangesAsync()); } /// /// 项目site 编辑接口 New 可以设置为启用不启用 不启用 不会验证Code 重复 /// /// /// [HttpPut] [Authorize(Policy = IRaCISPolicy.PM_APM)] public async Task EditTrialSite(EditTrialSiteCommand editTrialSiteCommand) { var dbEntity = await _trialSiteRepository.FirstOrDefaultAsync(t => t.Id == editTrialSiteCommand.Id, true); if (dbEntity == null) return Null404NotFound(dbEntity); var trialId = dbEntity.TrialId; var siteId = dbEntity.SiteId; if (editTrialSiteCommand.IsDeleted) { //if (await _repository.AnyAsync(t => t.TrialId == trialId && t.SiteId == siteId)) //{ // return ResponseOutput.NotOk("The site has been associated with CRC, and couldn't be deleted."); //} if (await _repository.AnyAsync(t => t.SiteId == siteId && t.TrialId == trialId)) { return ResponseOutput.NotOk("The subjects has been added to this site, and couldn't be disable."); } //if (await _repository.AnyAsync(t => t.SiteId == siteId && t.TrialId == trialId)) //{ // return ResponseOutput.NotOk("The site has been uploaded study, and couldn't be deleted."); //} } else { if (await _trialSiteRepository.AnyAsync(t => t.Id != editTrialSiteCommand.Id && t.TrialSiteCode == editTrialSiteCommand.TrialSiteCode && t.TrialId == editTrialSiteCommand.TrialId)) { return ResponseOutput.NotOk("Code is not allowed to be repeated"); } } _mapper.Map(editTrialSiteCommand, dbEntity); await _trialSiteRepository.SaveChangesAsync(); return ResponseOutput.Ok(); } /// 批量添加Site下 CRC的负责人 [HttpPost] [TypeFilter(typeof(TrialResourceFilter))] [Authorize(Policy = IRaCISPolicy.PM_APM)] public async Task AssignSiteCRC(List trialSiteCRCList) { var addArray = _mapper.Map>(trialSiteCRCList); await _trialSiteUserRepository.AddRangeAsync(addArray); await _trialSiteUserRepository.SaveChangesAsync(); return ResponseOutput.Result(true); } /// 删除CRC人员 [HttpDelete, Route("{id:guid}/{trialId:guid}/{isDelete:bool}")] [TypeFilter(typeof(TrialResourceFilter))] [Authorize(Policy = IRaCISPolicy.PM_APM)] public async Task DeleteSiteCRC(Guid id, bool isDelete) { await _trialSiteUserRepository.UpdatePartialFromQueryAsync(t=>t.Id==id, u => new TrialSiteUser(){ IsDeleted = isDelete, DeletedTime = isDelete ? DateTime.Now : null },true,true); return ResponseOutput.Ok(); } /// /// 获取项目下的 site 下拉框数据 CRC只看到他负责的 /// /// /// [HttpGet("{trialId:guid}")] public async Task> GetTrialSiteSelect(Guid trialId) { //CRC只看到他负责的 var list = await _trialSiteRepository.Where(t => t.TrialId == trialId) .WhereIf(_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ClinicalResearchCoordinator || _userInfo.UserTypeEnumInt == (int)UserTypeEnum.CRA, t => t.CRCUserList.Any(t => t.UserId == _userInfo.Id)) .ProjectTo(_mapper.ConfigurationProvider).OrderBy(t => t.TrialSiteCode).ToListAsync(); return list; } public async Task> GetTrialSiteCodeSelect(Guid trialId,bool isIncludeVirtualSite=true) { //CRC只看到他负责的 var list = await _trialSiteRepository.Where(t => t.TrialId == trialId) .WhereIf(_userInfo.UserTypeEnumInt == (int)UserTypeEnum.ClinicalResearchCoordinator || _userInfo.UserTypeEnumInt == (int)UserTypeEnum.CRA, t => t.CRCUserList.Any(t => t.UserId == _userInfo.Id)) .Select(t=> t.TrialSiteCode).ToListAsync(); var virtualList = await _repository.Where(t => t.IsSelfAnalysis==true && t.TrialId==trialId).Select(t => t.BlindTrialSiteCode).Distinct().ToListAsync(); return isIncludeVirtualSite? list.Concat(virtualList): list; } /// 删除 项目 下某一site [HttpDelete("{id:guid}/{trialId:guid}")] [TrialAudit(AuditType.TrialAudit, AuditOptType.DeleteTrialSite)] [TypeFilter(typeof(TrialResourceFilter))] [Obsolete] public async Task DeleteTrialSite(Guid id) { var relation = await _trialSiteRepository.FirstOrDefaultAsync(t => t.Id == id); if (relation == null) return Null404NotFound(relation); var trialId = relation.TrialId; var siteId = relation.SiteId; if (await _repository.AnyAsync(t => t.TrialId == trialId && t.SiteId == siteId)) { return ResponseOutput.NotOk("The site has been associated with CRC, and couldn't be deleted."); } if (await _repository.AnyAsync(t => t.SiteId == siteId && t.TrialId == trialId)) { return ResponseOutput.NotOk("The subjects has been added to this site, and couldn't be deleted."); } if (await _repository.AnyAsync(t => t.SiteId == siteId && t.TrialId == trialId)) { return ResponseOutput.NotOk("The site has been uploaded study, and couldn't be deleted."); } await _repository.DeleteAsync(relation); return ResponseOutput.Result(await _repository.SaveChangesAsync()); } } }