后台任务执行提取

parent b53eca2a
using FluentValidation.AspNetCore; using FluentValidation.AspNetCore;
using Hangfire;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Internal; using Microsoft.AspNetCore.Http.Internal;
...@@ -103,15 +104,10 @@ public ApiResponse ExtractData([CustomizeValidator(RuleSet = "Delete"), FromBody ...@@ -103,15 +104,10 @@ public ApiResponse ExtractData([CustomizeValidator(RuleSet = "Delete"), FromBody
{ {
var hospital = hospitalService.GetHopital(request.ID); var hospital = hospitalService.GetHopital(request.ID);
if (hospital == null) if (hospital == null)
return new ApiResponse(ResponseType.Fail, "hospitalid不存在"); return new ApiResponse(ResponseType.Fail, "医院无效");
var user = claim.At(request.Token);
var filePath = extractService.ExtractData(request.ID); BackgroundJob.Enqueue(() => extractService.ExtractData(request.ID, user.Mail, hospital));
if (!string.IsNullOrEmpty(filePath) && FileHelper.IsExistFile(filePath)) return new ApiResponse(ResponseType.OK, "HIS绩效数据提取任务正在执行,稍后我们将以邮件的通知您!");
{
var user = claim.At(request.Token);
templateService.SendEmail(new List<string> { user.Mail }, filePath, $"{hospital.HosName}提取数据", $"{hospital.HosName}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}成功提取。");
}
return new ApiResponse(ResponseType.OK, "OK", filePath);
} }
} }
} }
\ No newline at end of file
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using NPOI.HSSF.UserModel; using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel; using NPOI.SS.UserModel;
...@@ -23,7 +24,9 @@ namespace Performance.Services ...@@ -23,7 +24,9 @@ namespace Performance.Services
/// </summary> /// </summary>
public class ExtractService : IAutoInjection public class ExtractService : IAutoInjection
{ {
private readonly ILogger<ExtractService> logger;
private readonly IHostingEnvironment environment; private readonly IHostingEnvironment environment;
private readonly IEmailService emailService;
private readonly PerSheetService perSheetService; private readonly PerSheetService perSheetService;
private readonly PerHeaderService perHeaderService; private readonly PerHeaderService perHeaderService;
private readonly PerforPersheetRepository perforPersheetRepository; private readonly PerforPersheetRepository perforPersheetRepository;
...@@ -34,7 +37,9 @@ public class ExtractService : IAutoInjection ...@@ -34,7 +37,9 @@ public class ExtractService : IAutoInjection
private readonly PerforPerallotRepository perforPerallotRepository; private readonly PerforPerallotRepository perforPerallotRepository;
private readonly PerforHospitalconfigRepository perforHospitalconfigRepository; private readonly PerforHospitalconfigRepository perforHospitalconfigRepository;
public ExtractService(IHostingEnvironment environment, public ExtractService(ILogger<ExtractService> logger,
IHostingEnvironment environment,
IEmailService emailService,
PerSheetService perSheetService, PerSheetService perSheetService,
PerHeaderService perHeaderService, PerHeaderService perHeaderService,
PerforPersheetRepository perforPersheetRepository, PerforPersheetRepository perforPersheetRepository,
...@@ -45,7 +50,9 @@ public class ExtractService : IAutoInjection ...@@ -45,7 +50,9 @@ public class ExtractService : IAutoInjection
PerforPerallotRepository perforPerallotRepository, PerforPerallotRepository perforPerallotRepository,
PerforHospitalconfigRepository perforHospitalconfigRepository) PerforHospitalconfigRepository perforHospitalconfigRepository)
{ {
this.logger = logger;
this.environment = environment; this.environment = environment;
this.emailService = emailService;
this.perSheetService = perSheetService; this.perSheetService = perSheetService;
this.perHeaderService = perHeaderService; this.perHeaderService = perHeaderService;
this.perforPersheetRepository = perforPersheetRepository; this.perforPersheetRepository = perforPersheetRepository;
...@@ -57,45 +64,71 @@ public class ExtractService : IAutoInjection ...@@ -57,45 +64,71 @@ public class ExtractService : IAutoInjection
this.perforHospitalconfigRepository = perforHospitalconfigRepository; this.perforHospitalconfigRepository = perforHospitalconfigRepository;
} }
public string ExtractData(int hospitalId) public void ExtractData(int hospitalId, string mail, sys_hospital hospital)
{ {
List<PerSheet> sheetList = new List<PerSheet>(); List<PerSheet> sheetList = new List<PerSheet>();
var allotList = perforPerallotRepository.GetEntities(t => t.HospitalId == hospitalId); try
var configList = perforHospitalconfigRepository.GetEntities(t => t.HospitalId == hospitalId); {
var firstList = perforPerfirstRepository.GetEntities(t => t.HospitalId == hospitalId); var allotList = perforPerallotRepository.GetEntities(t => t.HospitalId == hospitalId);
var scriptList = perforExtractRepository.GetEntities(t => t.HospitalId == hospitalId); var configList = perforHospitalconfigRepository.GetEntities(t => t.HospitalId == hospitalId);
var firstList = perforPerfirstRepository.GetEntities(t => t.HospitalId == hospitalId);
var scriptList = perforExtractRepository.GetEntities(t => t.HospitalId == hospitalId);
if (configList == null || !configList.Any()) if (configList == null || !configList.Any())
throw new PerformanceException($"暂不支持自动提取绩效数据"); throw new PerformanceException($"暂不支持自动提取绩效数据");
if (scriptList == null || !scriptList.Any()) if (scriptList == null || !scriptList.Any())
throw new PerformanceException($"暂无该医院的配置信息和执行sql,无法进行数据提取"); throw new PerformanceException($"暂无该医院的配置信息和执行sql,无法进行数据提取");
var hospitalConfig = configList.First(); var hospitalConfig = configList.First();
string originalPath = ""; string originalPath = "";
//判断是否是首次 //判断是否是首次
if ((allotList == null || !allotList.Any()) && (firstList != null || !firstList.Any())) if ((allotList == null || !allotList.Any()) && (firstList != null || !firstList.Any()))
{ {
var first = firstList.OrderByDescending(t => t.CreateDate).FirstOrDefault(); var first = firstList.OrderByDescending(t => t.CreateDate).FirstOrDefault();
//首次 从excel中获取人员信息,SHEET页信息,列头信息 //首次 从excel中获取人员信息,SHEET页信息,列头信息
sheetList = GetFileData(first.Path); sheetList = GetFileData(first.Path);
originalPath = first.Path; originalPath = first.Path;
}
else
{
//非首次 从数据库中获取人员信息,SHEET页信息,列头信息
var allot = allotList.OrderByDescending(t => t.Year).ThenByDescending(t => t.Month).First();
sheetList = GetRepositoryData(allot.ID);
originalPath = allot.Path;
}
var dpath = Path.Combine(environment.ContentRootPath, "Files", $"{hospitalId}", "autoextract");
FileHelper.CreateDirectory(dpath);
string path = Path.Combine(dpath, $"绩效数据{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.xlsx");
//根据SHEET页信息,列头信息,创建EXCEL文件
if (WriteExcel(path, originalPath, sheetList, hospitalConfig, hospitalId, out string filepath))
SendEmail(mail, filepath, $"{hospital.HosName}HIS数据提取成功", $"{hospital.HosName}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}成功提取。");
} }
else catch (Exception ex)
{ {
//非首次 从数据库中获取人员信息,SHEET页信息,列头信息 logger.LogError(ex.ToString());
var allot = allotList.OrderByDescending(t => t.Year).ThenByDescending(t => t.Month).First(); SendEmail(mail, "", $"{hospital.HosName}HIS数据提取失败", $"{hospital.HosName}提取数据过程中出现异常情况,我们将尽快解决问题。给您带来的不便我们深感歉意!");
sheetList = GetRepositoryData(allot.ID);
originalPath = allot.Path;
} }
var dpath = Path.Combine(environment.ContentRootPath, "Files", $"{hospitalId}", "autoextract"); }
FileHelper.CreateDirectory(dpath);
string path = Path.Combine(dpath, $"绩效数据{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.xlsx"); /// <summary>
//根据SHEET页信息,列头信息,创建EXCEL文件 /// 发送邮件
if (WriteExcel(path, originalPath, sheetList, hospitalConfig, hospitalId, out string filepath)) /// </summary>
return filepath; /// <param name="path"></param>
throw new PerformanceException("绩效数据提取失败"); /// <param name="subject"></param>
/// <param name="body"></param>
private void SendEmail(string mail, string path, string subject, string body)
{
var message = new EmailMessage
{
To = new List<string> { mail },
Attachments = new List<string> { path },
DisplayName = "溯直健康",
Subject = subject,
Body = body
};
emailService.Send(message);
} }
/// <summary> /// <summary>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment