后台任务执行提取

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 filePath = extractService.ExtractData(request.ID);
if (!string.IsNullOrEmpty(filePath) && FileHelper.IsExistFile(filePath))
{
var user = claim.At(request.Token); 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")}成功提取。"); BackgroundJob.Enqueue(() => extractService.ExtractData(request.ID, user.Mail, hospital));
} return new ApiResponse(ResponseType.OK, "HIS绩效数据提取任务正在执行,稍后我们将以邮件的通知您!");
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,10 +64,12 @@ public class ExtractService : IAutoInjection ...@@ -57,10 +64,12 @@ 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>();
try
{
var allotList = perforPerallotRepository.GetEntities(t => t.HospitalId == hospitalId); var allotList = perforPerallotRepository.GetEntities(t => t.HospitalId == hospitalId);
var configList = perforHospitalconfigRepository.GetEntities(t => t.HospitalId == hospitalId); var configList = perforHospitalconfigRepository.GetEntities(t => t.HospitalId == hospitalId);
var firstList = perforPerfirstRepository.GetEntities(t => t.HospitalId == hospitalId); var firstList = perforPerfirstRepository.GetEntities(t => t.HospitalId == hospitalId);
...@@ -94,8 +103,32 @@ public string ExtractData(int hospitalId) ...@@ -94,8 +103,32 @@ public string ExtractData(int hospitalId)
string path = Path.Combine(dpath, $"绩效数据{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.xlsx"); string path = Path.Combine(dpath, $"绩效数据{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.xlsx");
//根据SHEET页信息,列头信息,创建EXCEL文件 //根据SHEET页信息,列头信息,创建EXCEL文件
if (WriteExcel(path, originalPath, sheetList, hospitalConfig, hospitalId, out string filepath)) if (WriteExcel(path, originalPath, sheetList, hospitalConfig, hospitalId, out string filepath))
return filepath; SendEmail(mail, filepath, $"{hospital.HosName}HIS数据提取成功", $"{hospital.HosName}{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}成功提取。");
throw new PerformanceException("绩效数据提取失败"); }
catch (Exception ex)
{
logger.LogError(ex.ToString());
SendEmail(mail, "", $"{hospital.HosName}HIS数据提取失败", $"{hospital.HosName}提取数据过程中出现异常情况,我们将尽快解决问题。给您带来的不便我们深感歉意!");
}
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="path"></param>
/// <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