后台任务执行提取

parent b53eca2a
using FluentValidation.AspNetCore;
using Hangfire;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Internal;
......@@ -103,15 +104,10 @@ public ApiResponse ExtractData([CustomizeValidator(RuleSet = "Delete"), FromBody
{
var hospital = hospitalService.GetHopital(request.ID);
if (hospital == null)
return new ApiResponse(ResponseType.Fail, "hospitalid不存在");
var filePath = extractService.ExtractData(request.ID);
if (!string.IsNullOrEmpty(filePath) && FileHelper.IsExistFile(filePath))
{
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);
return new ApiResponse(ResponseType.Fail, "医院无效");
var user = claim.At(request.Token);
BackgroundJob.Enqueue(() => extractService.ExtractData(request.ID, user.Mail, hospital));
return new ApiResponse(ResponseType.OK, "HIS绩效数据提取任务正在执行,稍后我们将以邮件的通知您!");
}
}
}
\ No newline at end of file
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
......@@ -23,7 +24,9 @@ namespace Performance.Services
/// </summary>
public class ExtractService : IAutoInjection
{
private readonly ILogger<ExtractService> logger;
private readonly IHostingEnvironment environment;
private readonly IEmailService emailService;
private readonly PerSheetService perSheetService;
private readonly PerHeaderService perHeaderService;
private readonly PerforPersheetRepository perforPersheetRepository;
......@@ -34,7 +37,9 @@ public class ExtractService : IAutoInjection
private readonly PerforPerallotRepository perforPerallotRepository;
private readonly PerforHospitalconfigRepository perforHospitalconfigRepository;
public ExtractService(IHostingEnvironment environment,
public ExtractService(ILogger<ExtractService> logger,
IHostingEnvironment environment,
IEmailService emailService,
PerSheetService perSheetService,
PerHeaderService perHeaderService,
PerforPersheetRepository perforPersheetRepository,
......@@ -45,7 +50,9 @@ public class ExtractService : IAutoInjection
PerforPerallotRepository perforPerallotRepository,
PerforHospitalconfigRepository perforHospitalconfigRepository)
{
this.logger = logger;
this.environment = environment;
this.emailService = emailService;
this.perSheetService = perSheetService;
this.perHeaderService = perHeaderService;
this.perforPersheetRepository = perforPersheetRepository;
......@@ -57,45 +64,71 @@ public class ExtractService : IAutoInjection
this.perforHospitalconfigRepository = perforHospitalconfigRepository;
}
public string ExtractData(int hospitalId)
public void ExtractData(int hospitalId, string mail, sys_hospital hospital)
{
List<PerSheet> sheetList = new List<PerSheet>();
var allotList = perforPerallotRepository.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);
try
{
var allotList = perforPerallotRepository.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())
throw new PerformanceException($"暂不支持自动提取绩效数据");
if (configList == null || !configList.Any())
throw new PerformanceException($"暂不支持自动提取绩效数据");
if (scriptList == null || !scriptList.Any())
throw new PerformanceException($"暂无该医院的配置信息和执行sql,无法进行数据提取");
if (scriptList == null || !scriptList.Any())
throw new PerformanceException($"暂无该医院的配置信息和执行sql,无法进行数据提取");
var hospitalConfig = configList.First();
string originalPath = "";
//判断是否是首次
if ((allotList == null || !allotList.Any()) && (firstList != null || !firstList.Any()))
{
var first = firstList.OrderByDescending(t => t.CreateDate).FirstOrDefault();
//首次 从excel中获取人员信息,SHEET页信息,列头信息
sheetList = GetFileData(first.Path);
originalPath = first.Path;
var hospitalConfig = configList.First();
string originalPath = "";
//判断是否是首次
if ((allotList == null || !allotList.Any()) && (firstList != null || !firstList.Any()))
{
var first = firstList.OrderByDescending(t => t.CreateDate).FirstOrDefault();
//首次 从excel中获取人员信息,SHEET页信息,列头信息
sheetList = GetFileData(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页信息,列头信息
var allot = allotList.OrderByDescending(t => t.Year).ThenByDescending(t => t.Month).First();
sheetList = GetRepositoryData(allot.ID);
originalPath = allot.Path;
logger.LogError(ex.ToString());
SendEmail(mail, "", $"{hospital.HosName}HIS数据提取失败", $"{hospital.HosName}提取数据过程中出现异常情况,我们将尽快解决问题。给您带来的不便我们深感歉意!");
}
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))
return filepath;
throw new PerformanceException("绩效数据提取失败");
}
/// <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>
......
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