diff --git a/IRaCIS.Core.Application/Service/Common/ExcelExportHelper.cs b/IRaCIS.Core.Application/Service/Common/ExcelExportHelper.cs new file mode 100644 index 000000000..1ef7cb155 --- /dev/null +++ b/IRaCIS.Core.Application/Service/Common/ExcelExportHelper.cs @@ -0,0 +1,59 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc; +using MiniExcelLibs; + +namespace IRaCIS.Core.Application.Service; + +public static class ExcelExportHelper +{ + //MiniExcel_Export + public static async Task DataExportAsync(string code, object data, IRepository _commonDocumentRepository, IWebHostEnvironment _hostEnvironment) + { + var doc = _commonDocumentRepository.AsQueryable(true).FirstOrDefault(t => t.Code == code); + + if (doc == null) + { + throw new Exception("当前code 没要找到对应的导出模板文件"); + } + + var rootPath = Directory.GetParent(_hostEnvironment.ContentRootPath.TrimEnd('\\'))?.FullName; + + var filePath = Path.Combine(rootPath, doc.Path.Trim('/')); + + if (!System.IO.File.Exists(filePath)) + { + throw new Exception("服务器本地不存在该路径文件"); + } + + //模板路径 + var tplPath = filePath; + + #region MiniExcel + + var memoryStream = new MemoryStream(); + + await MiniExcel.SaveAsByTemplateAsync(memoryStream, tplPath, data); + + memoryStream.Seek(0, SeekOrigin.Begin); + + return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") + { + FileDownloadName = $"{doc.Name}_{DateTime.Now.ToString("yyyy-MM-dd:hh:mm:ss")}.xlsx" + }; + + #endregion + + #region Magicodes 模板规则不一样 + + ////创建Excel导出对象 + //IExportFileByTemplate exporter = new ExcelExporter(); + + ////根据模板导出 + //var result = await exporter.ExportBytesByTemplate(exportInfo, tplPath); + + + //return new XlsxFileResult(bytes: result, fileDownloadName: $"{doc.Name}_{DateTime.Now.ToString("yyyy-MM-dd:hh:mm:ss")}.xlsx"); + #endregion + + } +} \ No newline at end of file diff --git a/IRaCIS.Core.Application/Service/SiteSurvey/DTO/TrialSiteSurveyViewModel.cs b/IRaCIS.Core.Application/Service/SiteSurvey/DTO/TrialSiteSurveyViewModel.cs index 1ec1da3f1..b9b66810e 100644 --- a/IRaCIS.Core.Application/Service/SiteSurvey/DTO/TrialSiteSurveyViewModel.cs +++ b/IRaCIS.Core.Application/Service/SiteSurvey/DTO/TrialSiteSurveyViewModel.cs @@ -62,6 +62,8 @@ namespace IRaCIS.Core.Application.Contracts public TrialSiteSurveyView TrialSiteSurvey { get; set; } } + + public class TrialSiteUserSurveyAllQuery : PageInput { [NotDefault] @@ -78,7 +80,6 @@ namespace IRaCIS.Core.Application.Contracts public string FormWriterKeyInfo { get; set; } = string.Empty; - //public string UserKeyInfo { get; set; } = string.Empty; public string UserName { get; set; } = string.Empty; diff --git a/IRaCIS.Core.Application/Service/TrialSiteUser/DTO/TrialSiteViewModel.cs b/IRaCIS.Core.Application/Service/TrialSiteUser/DTO/TrialSiteViewModel.cs index 30cdcb7cc..a448b9825 100644 --- a/IRaCIS.Core.Application/Service/TrialSiteUser/DTO/TrialSiteViewModel.cs +++ b/IRaCIS.Core.Application/Service/TrialSiteUser/DTO/TrialSiteViewModel.cs @@ -22,6 +22,19 @@ namespace IRaCIS.Core.Application.Contracts.DTO } + public class SiteCRCExportQueryDTO + { + public Guid TrialId { get; set; } = Guid.Empty; + public string SiteName { get; set; } = String.Empty; + public string TrialSiteAliasName { get; set; } = String.Empty; + + public bool? IsDeleted { get; set; } + + public string TrialSiteCode { get; set; } = String.Empty; + + public string UserKeyInfo { get; set; } = String.Empty; + } + public class TrialSiteCommand { public Guid TrialId { get; set; } diff --git a/IRaCIS.Core.Application/Service/TrialSiteUser/DTO/UserTrialViewModel.cs b/IRaCIS.Core.Application/Service/TrialSiteUser/DTO/UserTrialViewModel.cs index 81a063086..92d3c8841 100644 --- a/IRaCIS.Core.Application/Service/TrialSiteUser/DTO/UserTrialViewModel.cs +++ b/IRaCIS.Core.Application/Service/TrialSiteUser/DTO/UserTrialViewModel.cs @@ -211,13 +211,10 @@ namespace IRaCIS.Application.Contracts - - - public class TrialMaintenanceQuery : PageInput + public class TrialMaintenanceExportQuery { public Guid TrialId { get; set; } = Guid.Empty; public string UserRealName { get; set; } = string.Empty; - //public string UserType { get; set; } = string.Empty; public Guid? UserTypeId { get; set; } @@ -226,11 +223,20 @@ namespace IRaCIS.Application.Contracts public string OrganizationName { get; set; } = String.Empty; public bool? IsDeleted { get; set; } + } - //public DateTime? RemoveTime { get; set; } + public class TrialMaintenanceQuery : PageInput + { + public Guid TrialId { get; set; } = Guid.Empty; + public string UserRealName { get; set; } = string.Empty; - //public DateTime? JoinTime { get; set; } + public Guid? UserTypeId { get; set; } + public string UserName { get; set; } = string.Empty; + + public string OrganizationName { get; set; } = String.Empty; + + public bool? IsDeleted { get; set; } } diff --git a/IRaCIS.Core.Application/Service/TrialSiteUser/TrialMaintenanceService.cs b/IRaCIS.Core.Application/Service/TrialSiteUser/TrialMaintenanceService.cs index 6d2ae915d..8ebf84419 100644 --- a/IRaCIS.Core.Application/Service/TrialSiteUser/TrialMaintenanceService.cs +++ b/IRaCIS.Core.Application/Service/TrialSiteUser/TrialMaintenanceService.cs @@ -1,15 +1,11 @@ using IRaCIS.Application.Contracts; -using IRaCIS.Core.Infra.EFCore; using IRaCIS.Core.Application.Filter; using IRaCIS.Core.Domain.Share; using Microsoft.AspNetCore.Mvc; using IRaCIS.Core.Application.Contracts.DTO; using IRaCIS.Application.Interfaces; -using Magicodes.ExporterAndImporter.Core; -using Magicodes.ExporterAndImporter.Excel; -using Magicodes.ExporterAndImporter.Excel.AspNetCore; -using Microsoft.AspNetCore.Hosting; -using MiniExcelLibs; +using IRaCIS.Core.Application.Service; + using Microsoft.AspNetCore.Authorization; namespace IRaCIS.Application.Services @@ -29,67 +25,36 @@ namespace IRaCIS.Application.Services /// /// 项目参与人员导出 /// - /// + /// /// /// /// - [HttpGet] + [HttpPost] [AllowAnonymous] - public async Task TrialUserListExport(Guid trialId, [FromServices] IRepository _commonDocumentRepository) + public async Task TrialUserListExport(TrialMaintenanceExportQuery param, [FromServices] IRepository _commonDocumentRepository) { - var doc = _commonDocumentRepository.AsQueryable(true).FirstOrDefault(t => t.Code == "TrialUserList_Export"); - if (doc == null) - { - throw new Exception("当前code 没要找到对应的导出模板文件"); - } + var exportInfo = (await _trialRepository.Where(t => t.Id == param.TrialId).IgnoreQueryFilters().ProjectTo(_mapper.ConfigurationProvider).FirstOrDefaultAsync()).IfNullThrowException(); - var rootPath = Directory.GetParent(_hostEnvironment.ContentRootPath.TrimEnd('\\'))?.FullName; + exportInfo.TrialUserList = await _trialUseRepository.Where(t => t.TrialId == param.TrialId).IgnoreQueryFilters() + .WhereIf(param.UserTypeId != null, t => t.User.UserTypeId == param.UserTypeId) + .WhereIf(!string.IsNullOrWhiteSpace(param.UserName), t => t.User.UserName.Contains(param.UserName)) - var filePath = Path.Combine(rootPath, doc.Path.Trim('/')); + .WhereIf(param.IsDeleted != null, t => t.IsDeleted == param.IsDeleted) + .WhereIf(!string.IsNullOrWhiteSpace(param.OrganizationName), + t => t.User.OrganizationName.Contains(param.OrganizationName)) + .WhereIf(!string.IsNullOrWhiteSpace(param.UserRealName), + t => (t.User.LastName + " / " + t.User.FirstName).Contains(param.UserRealName)) + .ProjectTo(_mapper.ConfigurationProvider).ToListAsync(); - if (!System.IO.File.Exists(filePath)) - { - throw new Exception("服务器本地不存在该路径文件"); - } + return await ExcelExportHelper.DataExportAsync(StaticData.TrialUserList_Export, exportInfo, _commonDocumentRepository, _hostEnvironment); - var exportInfo = await _trialRepository.Where(t => t.Id == trialId).IgnoreQueryFilters().ProjectTo(_mapper.ConfigurationProvider).FirstOrDefaultAsync(); - //模板路径 - var tplPath = filePath; - #region MiniExcel - - var memoryStream = new MemoryStream(); - - MiniExcel.SaveAsByTemplate(memoryStream, tplPath, exportInfo); - - memoryStream.Seek(0, SeekOrigin.Begin); - - return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") - { - FileDownloadName = $"{doc.Name}_{DateTime.Now.ToString("yyyy-MM-dd:hh:mm:ss")}.xlsx" - }; - - #endregion - - - #region Magicodes 模板规则不一样 - - ////创建Excel导出对象 - //IExportFileByTemplate exporter = new ExcelExporter(); - - ////根据模板导出 - //var result = await exporter.ExportBytesByTemplate(exportInfo, tplPath); - - - //return new XlsxFileResult(bytes: result, fileDownloadName: $"{doc.Name}_{DateTime.Now.ToString("yyyy-MM-dd:hh:mm:ss")}.xlsx"); - #endregion - } diff --git a/IRaCIS.Core.Application/Service/TrialSiteUser/TrialSiteService.cs b/IRaCIS.Core.Application/Service/TrialSiteUser/TrialSiteService.cs index d8e06b2ac..f94bedc8e 100644 --- a/IRaCIS.Core.Application/Service/TrialSiteUser/TrialSiteService.cs +++ b/IRaCIS.Core.Application/Service/TrialSiteUser/TrialSiteService.cs @@ -1,13 +1,12 @@ using IRaCIS.Application.Contracts; using IRaCIS.Core.Domain.Share; -using IRaCIS.Core.Infrastructure.ExpressionExtend; 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 MiniExcelLibs; namespace IRaCIS.Core.Application.Services { @@ -29,91 +28,86 @@ namespace IRaCIS.Core.Application.Services } + #region 导出列表 + /// /// Site用户列表导出 /// - /// - /// isAllSiteUser true : site用户列表,false :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.LastName + " / " + t.User.FirstName).Contains(param.UserKeyInfo) + || t.User.UserName.Contains(param.UserKeyInfo) || t.User.EMail.Contains(param.UserKeyInfo)) + + .ProjectTo(_mapper.ConfigurationProvider).ToListAsync(); + + + return await ExcelExportHelper.DataExportAsync(StaticData.TrialSiteUserList_Export, exportInfo, _commonDocumentRepository, _hostEnvironment); + + } + + /// + /// Site用户汇总表导出 + /// /// /// /// /// /// - [HttpGet, Route("{trialId:guid}/{isSiteUserNotAll:bool}")] + [HttpGet, Route("{trialId:guid}")] [AllowAnonymous] - public async Task TrialSiteUserListExport(Guid trialId, bool isSiteUserNotAll, + public async Task TrialSiteUserSummaryListExport(Guid trialId, [FromServices] IRepository _commonDocumentRepository, [FromServices] IRepository _trialSiteSurveyRepository, [FromServices] IRepository _trialSiteUserSurveyRepository - ) + ) { - var code = isSiteUserNotAll ? "TrialSiteUserList_Export":"TrialSiteUserSummary_Export"; - var doc = _commonDocumentRepository.AsQueryable(true).FirstOrDefault(t => t.Code == code); + var data = (await _trialRepository.Where(t => t.Id == trialId).IgnoreQueryFilters().ProjectTo(_mapper.ConfigurationProvider).FirstOrDefaultAsync()).IfNullThrowException(); - if (doc == null) - { - throw new Exception("当前code 没要找到对应的导出模板文件"); - } + var groupSelectIdQuery = + _trialSiteSurveyRepository.Where(t => t.TrialId == trialId) + .GroupBy(t => t.SiteId) + .Select(g => g.OrderByDescending(u => u.CreateTime).Select(t => t.Id).First()); - var rootPath = Directory.GetParent(_hostEnvironment.ContentRootPath.TrimEnd('\\')).FullName; + var query = _trialSiteUserSurveyRepository + .Where(t => groupSelectIdQuery.Contains(t.TrialSiteSurveyId)) + .ProjectTo(_mapper.ConfigurationProvider); - var filePath = Path.Combine(rootPath, doc.Path.Trim('/')); + data.TrialSiteUserList = await query.ToListAsync(); - if (!System.IO.File.Exists(filePath)) - { - throw new Exception("服务器本地不存在该路径文件"); - } - - object exportInfo = default; - - if (isSiteUserNotAll) - { - exportInfo = (await _trialRepository.Where(t => t.Id == trialId).IgnoreQueryFilters().ProjectTo(_mapper.ConfigurationProvider).FirstOrDefaultAsync()).IfNullThrowException(); - - } - else - { - - var data = (await _trialRepository.Where(t => t.Id == trialId).IgnoreQueryFilters().ProjectTo(_mapper.ConfigurationProvider).FirstOrDefaultAsync()).IfNullThrowException(); - - var groupSelectIdQuery = - _trialSiteSurveyRepository.Where(t => t.TrialId == trialId && t.IsDeleted == false) - .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)) - .ProjectTo(_mapper.ConfigurationProvider); - - data.TrialSiteUserList = await query.ToListAsync(); - - exportInfo = data; - } + var exportInfo = data; - - //模板路径 - var tplPath = filePath; - - #region MiniExcel - - var memoryStream = new MemoryStream(); - - MiniExcel.SaveAsByTemplate(memoryStream, tplPath, exportInfo); - - memoryStream.Seek(0, SeekOrigin.Begin); - - return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") - { - FileDownloadName = $"{doc.Name}_{DateTime.Now.ToString("yyyy-MM-dd:hh:mm:ss")}.xlsx" - }; - - #endregion + return await ExcelExportHelper.DataExportAsync(StaticData.TrialSiteUserSummary_Export, exportInfo, _commonDocumentRepository, _hostEnvironment); } + + + #endregion + + + + diff --git a/IRaCIS.Core.Application/Service/TrialSiteUser/_MapConfig.cs b/IRaCIS.Core.Application/Service/TrialSiteUser/_MapConfig.cs index f7d49e140..368755ad0 100644 --- a/IRaCIS.Core.Application/Service/TrialSiteUser/_MapConfig.cs +++ b/IRaCIS.Core.Application/Service/TrialSiteUser/_MapConfig.cs @@ -200,11 +200,17 @@ namespace IRaCIS.Core.Application.Service CreateMap(); - CreateMap(); - CreateMap(); + CreateMap() + .ForMember(t=>t.TrialUserList,u=>u.Ignore()); + + + + CreateMap() + .ForMember(t => t.TrialSiteUserList, u => u.Ignore()); + CreateMap().IncludeMembers(t=>t.User) .ForMember(t => t.TrialSiteCode, u => u.MapFrom(c => c.TrialSite.TrialSiteCode)) .ForMember(t => t.TrialSiteAliasName, u => u.MapFrom(c => c.TrialSite.TrialSiteAliasName)) diff --git a/IRaCIS.Core.Domain.Share/_StaticData.cs b/IRaCIS.Core.Domain.Share/_StaticData.cs index 6cd5c90f7..75c6b0581 100644 --- a/IRaCIS.Core.Domain.Share/_StaticData.cs +++ b/IRaCIS.Core.Domain.Share/_StaticData.cs @@ -67,6 +67,19 @@ public const string Anonymize_AddIRCInfoFiled = "Anonymize_AddIRCInfoFiled"; + #endregion + + + + #region 项目人员导出 + + public const string TrialSiteUserList_Export = "TrialSiteUserList_Export"; + + public const string TrialSiteUserSummary_Export = "TrialSiteUserSummary_Export"; + + public const string TrialUserList_Export = "TrialUserList_Export"; + + #endregion }