Commit c614ea10 by 纪旭 韦

Merge branch 'develop' into 手工录入_

parents 6777d66a d424bda0
......@@ -13,6 +13,7 @@
using Performance.Infrastructure;
using Performance.Services;
using Performance.Services.ExtractExcelService;
using Performance.Services.Queues;
using System;
using System.Collections.Generic;
using System.IO;
......@@ -37,6 +38,8 @@ public class TemplateController : Controller
private readonly AllotService allotService;
private readonly LogManageService logService;
private readonly IServiceScopeFactory serviceScopeFactory;
private readonly IBackgroundTaskQueue _backgroundTaskQueue;
private readonly ExtractService extractService;
public TemplateController(
ILogger<ExceptionsFilter> logger,
......@@ -50,7 +53,9 @@ public class TemplateController : Controller
HospitalService hospitalService,
AllotService allotService,
LogManageService logService,
IServiceScopeFactory serviceScopeFactory)
IBackgroundTaskQueue backgroundTaskQueue,
IServiceScopeFactory serviceScopeFactory,
ExtractService extractService)
{
this.logger = logger;
this.env = env;
......@@ -64,6 +69,8 @@ public class TemplateController : Controller
this.allotService = allotService;
this.logService = logService;
this.serviceScopeFactory = serviceScopeFactory;
_backgroundTaskQueue = backgroundTaskQueue;
this.extractService = extractService;
}
/// <summary>
......@@ -226,22 +233,22 @@ public ApiResponse NewExtractData([CustomizeValidator, FromBody] ExtractRequest
allot.IsExtracting = 1;
allot.ExtractTime = DateTime.Now;
allotService.Update(allot);
string email = claim.GetUserClaim(JwtClaimTypes.Mail);
// string email = claim.GetUserClaim(JwtClaimTypes.Mail);
string email = "";
if (isSingle)
{
logger.LogInformation("同一项目中进行提取");
Task.Run(() =>
_backgroundTaskQueue.QueueBackgroundWorkItem(async token =>
{
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);
logger.LogInformation("提取绩效数据参数:" + JsonHelper.Serialize(new { allotId = allot.ID, hospitalId = allot.HospitalId }));
string extractFilePath = scopedServices.Main(allot.ID, allot.HospitalId, email, allot.ID.ToString(), filePath, isSingle);
}
});
Thread.Sleep(1000);
}
else
{
......@@ -252,8 +259,7 @@ public ApiResponse NewExtractData([CustomizeValidator, FromBody] ExtractRequest
{
AllotId = request.AllotId,
HospitalId = request.HospitalId,
Email = email,
UserId = claim.GetUserId()
Email = email
};
string json = JsonHelper.Serialize(obj);
......@@ -284,6 +290,12 @@ public ApiResponse NewExtractData([CustomizeValidator, FromBody] ExtractRequest
// B 使用配置作为模板
}
[HttpPost("extract/allot/file")]
public ApiResponse ExtractAllotFile(ExtractRequest request)
{
extractService.Main(request.AllotId, request.HospitalId, request.Email, request.AllotId.ToString(), "", true);
return new ApiResponse(ResponseType.OK);
}
/// <summary>
/// 提取日志
......
using Microsoft.Data.SqlClient;
using MySql.Data.MySqlClient;
using MySql.Data.MySqlClient;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Data;
using System.Data.SqlClient;
namespace Performance.Repository
{
......
......@@ -10,6 +10,7 @@
using Performance.Repository;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
......@@ -117,7 +118,10 @@ public Comparison CheckAccountingUnitRealGiveFeeDiff(int allotId, string searchQ
TotalCount = DapperQuery<int>(queryCount, new { allotId, searchQuery, parm = $"%{searchQuery}%" })?.FirstOrDefault() ?? 0,
};
}
}
public IDbConnection GetDbConnection()
{
return context.Database.GetDbConnection();
}
}
}
......@@ -5,8 +5,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="4.0.0" />
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="3.21.4" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
</ItemGroup>
......
......@@ -775,7 +775,7 @@ public string Judge(int allotId, int hospitalId, int useTemplate, ref bool isSin
if (conf == null)
continue;
var data = queryService.QueryData(conf, allot, scr.ExecScript);
var data = queryService.QueryData<ExtractDto>(conf, scr.ExecScript, allot, false);
if (data == null || !data.Any()) continue;
foreach (var module in moduleSheet.Where(t => t.CheckScriptId == scr.Id))
......
......@@ -71,7 +71,7 @@ public void Handler(int hospitalId, per_allot allot, string groupName, bool isSi
var typeIds = types.Where(t => t.Source == (int)SheetType.Employee).Select(t => t.Id);
var script = scripts.Where(t => typeIds.Contains(t.TypeId));
if (script != null)
Employee(allot, configs, scripts);
Employee(allot, configs, scripts, isSingle);
}
if (types.Any(t => t.Source == (int)SheetType.OnlyExtract)) //不写入Excel的提取数据
{
......@@ -84,7 +84,7 @@ public void Handler(int hospitalId, per_allot allot, string groupName, bool isSi
foreach (var script in thisTypeScripts)
{
logger.LogInformation($"提取sql: {script.ExecScript}");
ExResult(allot, configs.FirstOrDefault(t => t.Id == script.ConfigId), script.ExecScript, type.EName);
ExResult(allot, configs.FirstOrDefault(t => t.Id == script.ConfigId), script.ExecScript, type.EName, isSingle);
}
}
}
......@@ -96,7 +96,7 @@ public void Handler(int hospitalId, per_allot allot, string groupName, bool isSi
foreach (var item in hisScrips)
{
logService.ReturnTheLog(allot.ID, groupName, 2, "提取数据", $"提取{item.SourceType} - {item.Category}数据", isSingle: isSingle);
HisData(allot, configs.FirstOrDefault(t => t.Id == item.ConfigId), item);
HisData(allot, configs.FirstOrDefault(t => t.Id == item.ConfigId), item, isSingle);
}
}
catch (Exception)
......@@ -106,7 +106,7 @@ public void Handler(int hospitalId, per_allot allot, string groupName, bool isSi
}
}
private void Employee(per_allot allot, List<sys_hospitalconfig> configs, IEnumerable<ex_script> scripts)
private void Employee(per_allot allot, List<sys_hospitalconfig> configs, IEnumerable<ex_script> scripts, bool isSingle)
{
try
{
......@@ -116,12 +116,12 @@ private void Employee(per_allot allot, List<sys_hospitalconfig> configs, IEnumer
var sql = script.ExecScript;
if (config == null || string.IsNullOrEmpty(sql)) return;
var limitData = queryService.QueryData<dynamic>(config, allot, sql);
var limitData = queryService.QueryData<dynamic>(config, sql, allot, isSingle);
if (limitData == null || !limitData.Any()) return;
var columns = ((IDictionary<string, object>)limitData.ElementAt(0)).Select(t => t.Key.ToLower()).ToList();
var data = queryService.QueryData<per_employee>(config, allot, sql).ToList();
var data = queryService.QueryData<per_employee>(config, sql, allot, isSingle).ToList();
data.ForEach(t =>
{
t.AllotId = allot.ID;
......@@ -223,13 +223,13 @@ private void JudgeDataEqual(List<string> columns, List<per_employee> emps, List<
}
}
private void HisData(per_allot allot, sys_hospitalconfig config, his_script script)
private void HisData(per_allot allot, sys_hospitalconfig config, his_script script, bool isSingle)
{
try
{
if (config == null || string.IsNullOrEmpty(script.ExecScript)) return;
var data = queryService.QueryData<HisData>(config, allot, script.ExecScript);
var data = queryService.QueryData<HisData>(config, script.ExecScript, allot, isSingle);
if (data == null || !data.Any()) return;
var hisdata = hisdataRepository.GetEntities(t => t.AllotId == allot.ID && t.SourceType == script.SourceType && t.Category == script.Category);
......@@ -265,13 +265,13 @@ private void Department()
{
}
private void ExResult(per_allot allot, sys_hospitalconfig config, string sql, string enmae)
private void ExResult(per_allot allot, sys_hospitalconfig config, string sql, string enmae, bool isSingle)
{
try
{
if (config == null || string.IsNullOrEmpty(sql)) return;
var data = queryService.QueryData(config, allot, sql);
var data = queryService.QueryData<ExtractDto>(config, sql, allot, isSingle);
if (data == null || !data.Any()) return;
var createTime = DateTime.Now;
......
using Microsoft.Extensions.Logging;
using Dapper;
using Microsoft.Extensions.Logging;
using NPOI.SS.UserModel;
using Performance.DtoModels;
using Performance.EntityModels;
......@@ -7,6 +8,7 @@
using Performance.Services.ExtractExcelService.SheetDataWrite;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
......@@ -73,9 +75,11 @@ PerforCofdrugtypefactorRepository drugtypefactorRepository
/// <param name="isSingle">抽取是否在同一项目</param>
public string Main(int allotId, int hospitalId, string email, string groupName, string filePath = null, bool isSingle = false)
{
groupName = allotId.ToString();
string extractFilePath = "";
per_allot allot = null;
IWorkbook workbook = null;
IDbConnection connection = peremployeeRepository.GetDbConnection();
try
{
logService.ClearExtractLog(allotId);
......@@ -114,6 +118,11 @@ public string Main(int allotId, int hospitalId, string email, string groupName,
allot.ExtractPath = extractFilePath;
workbook.EvaluateAll();
using (FileStream file = new FileStream(extractFilePath, FileMode.OpenOrCreate))
{
workbook.Write(file);
}
workbook.Close();
}
catch (Exception ex)
{
......@@ -124,12 +133,7 @@ public string Main(int allotId, int hospitalId, string email, string groupName,
finally
{
logService.ReturnTheLog(allotId, groupName, 3, "", 100, 5, isSingle);
using (FileStream file = new FileStream(extractFilePath, FileMode.OpenOrCreate))
{
workbook.Write(file);
}
workbook.Close();
perallotRepository.Update(allot);
UpdateAllot(connection, allot);
}
return extractFilePath;
}
......@@ -333,6 +337,25 @@ private List<ExtractTransDto> StandDataFormat(int hospitalId, List<ex_result> re
return groupdata.ToList();
}
private void UpdateAllot(IDbConnection connection, per_allot allot)
{
try
{
string sql = "update per_allot set isextracting = @isextracting, extractpath = @extractpath where id = @id";
if (connection == null) return;
if (connection.State == ConnectionState.Closed)
connection.Open();
connection.Execute(sql, allot);
}
catch (Exception ex)
{
logger.LogError(ex.Message);
}
}
private readonly List<SheetType> OtherConfigSheet = new List<SheetType>
{
SheetType.AccountExtra,
......
......@@ -59,6 +59,7 @@ PerforPerallotRepository perallotRepository
}
private readonly DateTime CreateTime = DateTime.Now;
private static Dictionary<int, IDbConnection> pools = new Dictionary<int, IDbConnection>();
/// <summary>
/// 获取抽取数据
......@@ -194,8 +195,6 @@ 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);
......@@ -203,20 +202,7 @@ private List<ex_result> ExtractModuleData(per_allot allot, string groupName, boo
try
{
try
{
if (!pools.ContainsKey(config.Id))
pools.Add(config.Id, ConnectionBuilder.Create((DatabaseType)config.DataBaseType, config.DbSource, config.DbName, config.DbUser, config.DbPassword));
}
catch
{
logService.ReturnTheLog(allot.ID, groupName, 2, "数据库连接", $"数据库“{config.DbName}”连接失败", 3, isSingle);
}
IDbConnection connection = pools[config.Id];
var parameters = GetParameters(allot);
var querydata = QueryData(connection, parameters, script.ExecScript);
var querydata = QueryData<ExtractDto>(config, script.ExecScript, allot, isSingle);
if (querydata != null && querydata.Any())
{
......@@ -287,7 +273,7 @@ private List<ex_result> ExtractItemData(per_allot allot, string groupName, bool
var config = configs.FirstOrDefault(t => t.Id == script.ConfigId);
if (config == null) continue;
var querydata = QueryData(config, allot, script.ExecScript);
var querydata = QueryData<ExtractDto>(config, script.ExecScript, allot, isSingle);
if (querydata != null && querydata.Any())
{
thisItems.ForEach(f =>
......@@ -351,7 +337,7 @@ private List<ex_result> ExtractSpecialData(per_allot allot, string groupName, bo
var config = configs.FirstOrDefault(t => t.Id == script.ConfigId);
if (config == null) continue;
var querydata = QueryData(config, allot, script.ExecScript);
var querydata = QueryData<ExtractDto>(config, script.ExecScript, allot, isSingle);
if (querydata != null && querydata.Any())
{
thisSpecials.ForEach(f =>
......@@ -396,62 +382,36 @@ private List<ex_result> ExtractSpecialData(per_allot allot, string groupName, bo
/// <param name="source"></param>
/// <param name="category"></param>
/// <returns></returns>
public IEnumerable<ExtractDto> QueryData(sys_hospitalconfig config, per_allot allot, string execsql)
public IEnumerable<T> QueryData<T>(sys_hospitalconfig config, string execsql, per_allot allot, bool isSingle)
{
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);
}
logger.LogInformation($"提取绩效数据SQL脚本{execsql}");
var result = connection.Query<ExtractDto>(execsql, commandTimeout: 20000);
logger.LogInformation($"提取绩效数据执行脚本获取数据{result?.Count() ?? 0}条记录");
IDbConnection connection = null;
try
{
if (!pools.ContainsKey(config.Id))
pools.Add(config.Id, ConnectionBuilder.Create((DatabaseType)config.DataBaseType, config.DbSource, config.DbName, config.DbUser, config.DbPassword));
connection = pools[config.Id];
return result;
}
if (connection == null) return Enumerable.Empty<T>();
/// <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<T> QueryData<T>(sys_hospitalconfig config, per_allot allot, string execsql)
{
var parameters = GetParameters(allot);
using (var connection = ConnectionBuilder.Create((DatabaseType)config.DataBaseType, config.DbSource, config.DbName, config.DbUser, config.DbPassword))
if (connection.State == ConnectionState.Closed)
connection.Open();
}
catch
{
foreach (var item in parameters)
{
execsql = Regex.Replace(execsql, item.Key, item.Value, RegexOptions.IgnoreCase);
}
logService.ReturnTheLog(allot.ID, allot.ID.ToString(), 2, "数据库连接", $"数据库“{config.DbName}”连接失败", 3, isSingle);
}
logger.LogInformation($"提取绩效数据SQL脚本{execsql}");
var result = connection.Query<T>(execsql, commandTimeout: 20000);
logger.LogInformation($"提取绩效数据执行脚本获取数据{result?.Count() ?? 0}条记录");
logger.LogInformation($"提取绩效数据SQL脚本{execsql}");
var result = connection.Query<T>(execsql, commandTimeout: 20000);
logger.LogInformation($"提取绩效数据执行脚本获取数据{result?.Count() ?? 0}条记录");
return result;
}
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