Commit 2c25e2a1 by lcx

提取数据连接数据库方法修改

parent cf59fc2e
......@@ -18,6 +18,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Performance.Api.Controllers
......@@ -230,14 +231,18 @@ public ApiResponse NewExtractData([CustomizeValidator, FromBody] ExtractRequest
if (isSingle)
{
logger.LogInformation("同一项目中进行提取");
Task.Run(() =>
{
using (var scope = serviceScopeFactory.CreateScope())
{
var scopedServices = scope.ServiceProvider.GetRequiredService<ExtractService>();
logger.LogInformation("提取绩效数据参数:" + JsonHelper.Serialize(new { allotId = allot.ID, hospitalId = allot.HospitalId, userId = claim.GetUserId() }));
string extractFilePath = scopedServices.Main(allot.ID, allot.HospitalId, email, "User" + claim.GetUserId(), filePath, isSingle);
}
});
Thread.Sleep(1000);
}
else
{
......
......@@ -75,10 +75,10 @@ public string Main(int allotId, int hospitalId, string email, string groupName,
IWorkbook workbook = null;
try
{
logService.ReturnTheLog(allotId, groupName, 2, "等待提取", $"确认配置信息是否可完成数据提取...", 1, isSingle);
logService.ClearExtractLog(allotId);
logService.ReturnTheLog(allotId, groupName, 2, "等待提取", $"确认配置信息是否可完成数据提取...", 1, isSingle);
var allots = perallotRepository.GetEntities(t => t.HospitalId == hospitalId);
if (allots == null || !allots.Any(t => t.ID == allotId)) throw new Exception("绩效不存在");
......
using Dapper;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.Logging;
using Performance.DtoModels;
using Performance.EntityModels;
using Performance.Infrastructure;
using Performance.Repository;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
......@@ -191,12 +194,23 @@ private List<ex_result> ExtractModuleData(per_allot allot, string groupName, boo
logService.ReturnTheLog(allot.ID, groupName, 3, "", ratio > 20 ? 20 : ratio, 1, isSingle);
logService.ReturnTheLog(allot.ID, groupName, 2, "提取数据", $"开始提取模块“{string.Join("、", thisModules.Select(t => t.ModuleName))}”的数据", 1, isSingle);
Dictionary<int, IDbConnection> pools = new Dictionary<int, IDbConnection>();
foreach (var script in scripts.Where(t => t.TypeId == typeId))
{
var config = configs.FirstOrDefault(t => t.Id == script.ConfigId) ?? configs.FirstOrDefault(t => t.DataBaseType == script.DatabaseType);
if (config == null) continue;
var querydata = QueryData(config, allot, script.ExecScript);
try
{
if(!pools.ContainsKey(config.Id))
pools.Add(config.Id, ConnectionBuilder.Create((DatabaseType)config.DataBaseType, config.DbSource, config.DbName, config.DbUser, config.DbPassword));
IDbConnection connection = pools[config.Id];
var parameters = GetParameters(allot);
var querydata = QueryData(connection, parameters, script.ExecScript);
if (querydata != null && querydata.Any())
{
thisModules.ForEach(f =>
......@@ -218,10 +232,15 @@ private List<ex_result> ExtractModuleData(per_allot allot, string groupName, boo
data.AddRange(result);
});
}
}
logService.ReturnTheLog(allot.ID, groupName, 2, "提取数据", $"模块“{string.Join("、", thisModules.Select(t => t.ModuleName))}”的数据已完成提取", 1, isSingle);
}
catch (Exception)
{
logService.ReturnTheLog(allot.ID, groupName, 2, "数据库连接", $"数据库“{config.DbName}”连接失败", 3, isSingle);
}
}
}
}
return data;
......@@ -372,6 +391,20 @@ public IEnumerable<ExtractDto> QueryData(sys_hospitalconfig config, per_allot al
var parameters = GetParameters(allot);
using (var connection = ConnectionBuilder.Create((DatabaseType)config.DataBaseType, config.DbSource, config.DbName, config.DbUser, config.DbPassword))
{
return QueryData(connection, parameters, execsql);
}
}
/// <summary>
/// 查询数据
/// </summary>
/// <param name="config"></param>
/// <param name="allot"></param>
/// <param name="execsql"></param>
/// <param name="source"></param>
/// <param name="category"></param>
/// <returns></returns>
public IEnumerable<ExtractDto> QueryData(IDbConnection connection, Dictionary<string, string> parameters, string execsql)
{
foreach (var item in parameters)
{
execsql = Regex.Replace(execsql, item.Key, item.Value, RegexOptions.IgnoreCase);
......@@ -383,7 +416,6 @@ public IEnumerable<ExtractDto> QueryData(sys_hospitalconfig config, per_allot al
return result;
}
}
/// <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