Commit 3b6d5ce7 by ruyun.zhang

更换EPPlus读取规则

parent 274f5acf
using Dapper;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Performance.Infrastructure.Extensions
{
public static class DapperExtensions
{
/// <summary>
/// 批量新增
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="connectionString"></param>
/// <param name="entities"></param>
/// <param name="tableName"></param>
/// <param name="transaction"></param>
/// <param name="commandTimeout"></param>
/// <param name="relevantColumns"></param>
/// <param name="ignoreColumns"></param>
/// <param name="batchCount"></param>
/// <returns></returns>
public static int BulkInsert<TEntity>(string connectionString,
List<TEntity> entities,
string? tableName = null,
IDbTransaction? transaction = null,
int? commandTimeout = null,
List<string>? relevantColumns = null,
List<string>? ignoreColumns = null,
int batchCount = 1000)
{
var type = typeof(TEntity);
var properties = type.GetProperties().AsEnumerable();
if (relevantColumns?.Any() == true)
properties = properties.Where(w => relevantColumns.Contains(w.Name));
if (ignoreColumns?.Any() == true)
properties = properties.Where(w => !ignoreColumns.Contains(w.Name));
var columnNames = properties.Select(w => w.Name).ToList();
for (int i = 0; i < Math.Ceiling((double)entities.Count / batchCount); i++)
{
var startIndex = batchCount * i;
var currEntities = entities.Skip(startIndex).Take(batchCount);
var strsql = new StringBuilder();
strsql.Append($"INSERT INTO {tableName}({string.Join(',', columnNames)}) VALUES");
DynamicParameters parameters = new DynamicParameters();
foreach (var item in currEntities.Select((row, index) => (row, index)))
{
var vNames = new List<string>();
foreach (var name in columnNames)
{
var pName = $"@{name}_{item.index}";
if (!vNames.Contains(pName)) vNames.Add(pName);
var value = GetValue<TEntity, object>(item.row, name);
if (value != null && value.GetType().IsClass)
value = value.ToString();
parameters.Add(pName, value);
}
tableName ??= typeof(TEntity).Name;
strsql.Append($"({string.Join(',', vNames)}),");
}
strsql.Remove(strsql.Length - 1, 1);
strsql.Append(';');
var inserSql = strsql.ToString();
using (var conn = new MySqlConnection(connectionString))
{
if (conn.State != ConnectionState.Open) conn.Open();
conn.Execute(inserSql, parameters, transaction, commandTimeout);
conn.Close();
}
}
return entities.Count;
}
/// <summary>
/// 对象取值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="item"></param>
/// <param name="propertyName"></param>
/// <returns></returns>
public static TValue GetValue<T, TValue>(T item, string propertyName)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "w");
MemberExpression member = Expression.Property(parameter, propertyName);
UnaryExpression conversion = Expression.Convert(member, typeof(TValue));
var lambda = Expression.Lambda<Func<T, TValue>>(conversion, parameter);
Func<T, TValue> getter = lambda.Compile();
return getter(item);
}
}
}
......@@ -8,6 +8,7 @@
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using Masuit.Tools.Models;
using Microsoft.EntityFrameworkCore;
using Performance.DtoModels;
using Performance.EntityModels;
......@@ -69,28 +70,16 @@ public new List<AccountUnit> GetEmployeeUnit(Expression<Func<per_employee, bool>
/// <returns></returns>
public DtoModels.Comparison<view_check_emp> CheckEmployeeRealGiveFeeDiff(ComparisonPagingRequest request)
{
var queryData = $@"
SELECT
HospitalId,Year,Month,AllotID,ComputeId,UnitType,AccountingUnit,JobNumber,MAX(EmployeeName) AS EmployeeName,
SUM(RealGiveFeeExecl) AS RealGiveFeeExecl,SUM(RealGiveFeeCompute) AS RealGiveFeeCompute,SUM(RealGiveFeeExecl) - SUM(RealGiveFeeCompute) AS Diff
FROM (
SELECT * FROM view_check_emp WHERE AllotId = @allotId
) TAB
WHERE if(@searchQuery='','',AccountingUnit) LIKE @parm OR if(@searchQuery='','',JobNumber) LIKE @parm OR if(@searchQuery='','',EmployeeName) LIKE @parm
GROUP BY HospitalId,Year,Month,AllotID,UnitType,AccountingUnit,JobNumber
ORDER BY HospitalId,Year,Month,ABS(SUM(RealGiveFeeExecl) - SUM(RealGiveFeeCompute)) DESC LIMIT {(request.PageIndex - 1) * request.PageSize},{request.PageSize}
";
var queryCount = @"
SELECT COUNT(0) FROM (
SELECT * FROM view_check_emp WHERE AllotId = @allotId
) TAB
WHERE if(@searchQuery='','',AccountingUnit) LIKE @parm OR if(@searchQuery='','',JobNumber) LIKE @parm OR if(@searchQuery='','',EmployeeName) LIKE @parm
";
var queryData = $@"CALL proc_allot_check_emp(@allotId);";
var checkEmps = DapperQuery<view_check_emp>(queryData, new { allotId = request.AllotId });
if (!string.IsNullOrEmpty(request.SearchQuery))
checkEmps = checkEmps.Where(w => w.UnitType.Contains(request.SearchQuery) || w.AccountingUnit.Contains(request.SearchQuery) || w.JobNumber.Contains(request.SearchQuery) || w.EmployeeName.Contains(request.SearchQuery));
checkEmps = checkEmps.OrderByDescending(w => Math.Abs(w.Diff ?? 0));
return new DtoModels.Comparison<view_check_emp>()
{
Datas = DapperQuery<view_check_emp>(queryData, new { allotId = request.AllotId, searchQuery = request.SearchQuery, parm = $"%{request.SearchQuery}%" }, commandTimeout: 10000)?.ToList() ?? new List<view_check_emp>(),
TotalCount = DapperQuery<int>(queryCount, new { allotId = request.AllotId, searchQuery = request.SearchQuery, parm = $"%{request.SearchQuery}%" }, commandTimeout: 10000)?.FirstOrDefault() ?? 0,
Datas = checkEmps.ToPagedList(request.PageIndex, request.PageSize).Data,
TotalCount = checkEmps.Count(),
};
}
......@@ -99,26 +88,16 @@ public DtoModels.Comparison<view_check_emp> CheckEmployeeRealGiveFeeDiff(Compari
/// </summary>
public DtoModels.Comparison<view_check_emp> CheckAccountingUnitRealGiveFeeDiff(ComparisonPagingRequest request)
{
var queryData = $@"
SELECT *,IFNULL(RealGiveFeeExecl,0) - IFNULL(RealGiveFeeCompute,0) AS Diff FROM (
SELECT * FROM view_check_dept_account WHERE AllotId = @allotId UNION ALL
SELECT * FROM view_check_dept_specialunit WHERE AllotId = @allotId
) TAB
WHERE if(@searchQuery='','',AccountingUnit) LIKE @parm
ORDER BY HospitalId,Year,Month,ABS(DIFF) DESC LIMIT {(request.PageIndex - 1) * request.PageSize},{request.PageSize}
";
var queryCount = @"
SELECT count(0) FROM (
SELECT * FROM view_check_dept_account WHERE AllotId = @allotId UNION ALL
SELECT * FROM view_check_dept_specialunit WHERE AllotId = @allotId
) TAB
WHERE if(@searchQuery='','',AccountingUnit) LIKE @parm
";
var queryData = $@"CALL proc_allot_check_dept(@allotId);";
var checkEmps = DapperQuery<view_check_emp>(queryData, new { allotId = request.AllotId });
if (!string.IsNullOrEmpty(request.SearchQuery))
checkEmps = checkEmps.Where(w => w.UnitType.Contains(request.SearchQuery) || w.AccountingUnit.Contains(request.SearchQuery) || w.JobNumber.Contains(request.SearchQuery) || w.EmployeeName.Contains(request.SearchQuery));
checkEmps = checkEmps.OrderByDescending(w => Math.Abs(w.Diff ?? 0));
return new DtoModels.Comparison<view_check_emp>()
{
Datas = DapperQuery<view_check_emp>(queryData, new { allotId = request.AllotId, searchQuery = request.SearchQuery, parm = $"%{request.SearchQuery}%" })?.ToList() ?? new List<view_check_emp>(),
TotalCount = DapperQuery<int>(queryCount, new { allotId = request.AllotId, searchQuery = request.SearchQuery, parm = $"%{request.SearchQuery}%" })?.FirstOrDefault() ?? 0,
Datas = checkEmps.ToPagedList(request.PageIndex, request.PageSize).Data,
TotalCount = checkEmps.Count(),
};
}
......@@ -127,17 +106,16 @@ public DtoModels.Comparison<view_check_emp> CheckAccountingUnitRealGiveFeeDiff(C
/// </summary>
public DtoModels.Comparison<DeptComparisonTotal> CheckView_check_deptUnitRealGiveFeeDiffTotal(int allotId)
{
var queryData = @"SELECT UnitType,Count(1) Count,Sum(RealGiveFeeCompute) SumFee FROM
(SELECT *,IFNULL(RealGiveFeeExecl,0) - IFNULL(RealGiveFeeCompute,0) AS Diff FROM (
SELECT * FROM view_check_dept_account UNION ALL
SELECT * FROM view_check_dept_specialunit
) TAB
ORDER BY HospitalId,Year,Month,ABS(DIFF) DESC )view_check_dept
where AllotID = @allotId and Diff <> 0 GROUP BY UnitType";
return new DtoModels.Comparison<DeptComparisonTotal>()
var queryData = $@"CALL proc_allot_check_dept(@allotId);";
var checkEmps = DapperQuery<view_check_emp>(queryData, new { allotId });
var datas = checkEmps.Where(w => w.Diff != 0).GroupBy(w => w.UnitType).Select(w => new DeptComparisonTotal
{
Datas = DapperQuery<DeptComparisonTotal>(queryData, new { allotId })?.ToList() ?? new List<DeptComparisonTotal>(),
};
UnitType = w.Key,
Count = w.Count(),
SumFee = w.Sum(p => p.RealGiveFeeCompute),
}).ToList();
return new DtoModels.Comparison<DeptComparisonTotal>() { Datas = datas };
}
public IDbConnection GetDbConnection()
......
......@@ -2,10 +2,12 @@
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Performance.DtoModels;
using Performance.DtoModels.AppSettings;
using Performance.EntityModels;
using Performance.Infrastructure.Extensions;
using Performance.Repository;
namespace Performance.Services.AllotCompute
......@@ -15,6 +17,7 @@ namespace Performance.Services.AllotCompute
/// </summary>
public class ProcessComputService : IAutoInjection
{
private readonly IConfiguration _configuration;
private readonly IMapper _mapper;
private readonly IOptions<Application> _options;
private readonly BudgetService _budgetService;
......@@ -34,6 +37,7 @@ public class ProcessComputService : IAutoInjection
private readonly PerforPerallotRepository perallotRepository;
public ProcessComputService(
IConfiguration configuration,
IMapper mapper,
IOptions<Application> options,
BudgetService budgetService,
......@@ -52,6 +56,7 @@ public class ProcessComputService : IAutoInjection
PerforHospitalRepository hospitalRepository,
PerforPerallotRepository perallotRepository)
{
_configuration = configuration;
_mapper = mapper;
_options = options;
_budgetService = budgetService;
......@@ -103,15 +108,14 @@ private void SaveComputeAccount(PerSheet sheet, int allotId)
perforPerSheetRepository.Add(imsheet);
var dataList = sheet.PerData.Select(t => (PerDataAccountBaisc)t);
List<res_account> addList = new List<res_account>();
foreach (var data in dataList)
List<res_account> addList = _mapper.Map<List<res_account>>(dataList);
foreach (var data in addList)
{
var imdata = _mapper.Map<res_account>(data);
imdata.SheetID = imsheet.ID;
imdata.AllotID = allotId;
addList.Add(imdata);
}
perforResaccountRepository.AddRange(addList.ToArray());
data.SheetID = imsheet.ID;
data.AllotID = allotId;
}
var connectionString = _configuration.GetValue("AppConnection:PerformanceConnectionString", "");
DapperExtensions.BulkInsert(connectionString, addList, tableName: "res_account", commandTimeout: 60 * 60 * 5);
}
/// <summary>
......@@ -122,6 +126,7 @@ private void SaveComputeAccount(PerSheet sheet, int allotId)
/// <returns></returns>
private void SaveCommon(PerSheet sheet, int allotId)
{
var connectionString = _configuration.GetValue("AppConnection:PerformanceConnectionString", "");
var imsheet = new per_sheet { AllotID = allotId, SheetName = sheet.SheetName, Source = 2, SheetType = (int)sheet.SheetType };
perforPerSheetRepository.Add(imsheet);
......@@ -143,19 +148,17 @@ private void SaveCommon(PerSheet sheet, int allotId)
addHeadList.Add(imheaderChild);
}
}
}
perforImHeaderRepository.AddRange(addHeadList.ToArray());
}
DapperExtensions.BulkInsert(connectionString, addHeadList, tableName: "im_header", commandTimeout: 60 * 60 * 5);
List<im_data> addDataList = new List<im_data>();
var dataList = sheet.PerData.Select(t => (PerData)t);
foreach (var data in dataList)
List<im_data> addDataList = _mapper.Map<List<im_data>>(dataList);
foreach (var data in addDataList)
{
var imdata = _mapper.Map<im_data>(data);
imdata.SheetID = imsheet.ID;
imdata.AllotID = allotId;
addDataList.Add(imdata);
data.SheetID = imsheet.ID;
data.AllotID = allotId;
}
perforImDataRepository.AddRange(addDataList.ToArray());
DapperExtensions.BulkInsert(connectionString, addDataList, tableName: "im_data", commandTimeout: 60 * 60 * 5);
}
/// <summary>
......
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using AutoMapper;
using AutoMapper;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OfficeOpenXml;
......@@ -16,6 +12,11 @@
using Performance.Repository;
using Performance.Services.AllotCompute;
using Performance.Services.ExtractExcelService;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
namespace Performance.Services
{
......@@ -23,6 +24,7 @@ public class AllotService : IAutoInjection
{
private BaiscNormService baiscNormService;
private ImportDataService importDataService;
private readonly EPImportDataService _epImportDataService;
private ProcessComputService processComputService;
private ResultComputeService resultComputeService;
private PerforLogdbugRepository logdbug;
......@@ -36,6 +38,7 @@ public class AllotService : IAutoInjection
private IEmailService emailService;
private readonly IOptions<Application> options;
private readonly IOptions<AppConnection> _appConnection;
private readonly IConfiguration _configuration;
private readonly ComputeDirector _computeDirector;
private readonly PerforRescomputeRepository _perforRescomputeRepository;
private readonly PerforImemployeeRepository _perforImEmployeeRepository;
......@@ -62,6 +65,7 @@ public class AllotService : IAutoInjection
PerforPerallotRepository allotRepository,
BaiscNormService baiscNormService,
ImportDataService importDataService,
EPImportDataService epImportDataService,
ProcessComputService processComputService,
ResultComputeService resultComputeService,
ConfigService configService,
......@@ -70,6 +74,7 @@ public class AllotService : IAutoInjection
IEmailService emailService,
IOptions<Application> options,
IOptions<AppConnection> appConnection,
IConfiguration configuration,
ComputeDirector computeDirector,
PerforRescomputeRepository perforRescomputeRepository,
PerforImemployeeRepository perforImEmployeeRepository,
......@@ -97,11 +102,13 @@ public class AllotService : IAutoInjection
_evn = evn;
this.baiscNormService = baiscNormService;
this.importDataService = importDataService;
_epImportDataService = epImportDataService;
this.processComputService = processComputService;
this.resultComputeService = resultComputeService;
this.emailService = emailService;
this.options = options;
_appConnection = appConnection;
_configuration = configuration;
_computeDirector = computeDirector;
_perforRescomputeRepository = perforRescomputeRepository;
_perforImEmployeeRepository = perforImEmployeeRepository;
......@@ -338,10 +345,22 @@ public void Generate(per_allot allot)
logManageService.WriteMsg("绩效开始执行", $"数据来源:用户上传的Excel。", 1, allot.ID, "ReceiveMessage", true);
configService.Clear(allot.ID);
// 关闭筛选功能
ExtractHelper.CloseAutoFilter(allot.Path);
// 导出数据
excel = importDataService.ReadDataAndSave(allot);
var useNewRead = _configuration.GetValue("AllotFileReadOption:UseNewRead", true);
if (useNewRead)
{
_epImportDataService.DeleteSheetData(allot);
_epImportDataService.ReadSheetData(allot);
_epImportDataService.SheetDataImport(allot);
excel = _epImportDataService.GetImportExcel(allot);
}
else
{
// 关闭筛选功能
ExtractHelper.CloseAutoFilter(allot.Path);
// 导出数据
excel = importDataService.ReadDataAndSave(allot);
}
//if (!checkDataService.Check(excel, allot))
//{
......
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Dapper;
using Microsoft.Extensions.Configuration;
using MySql.Data.MySqlClient;
using Performance.DtoModels;
using Performance.EntityModels;
using Performance.Repository;
......@@ -8,11 +13,12 @@ namespace Performance.Services
{
public class TaskService : IAutoInjection
{
private readonly IConfiguration _configuration;
private readonly PerforBgtaskRepository _taskRepository;
public TaskService(
PerforBgtaskRepository taskRepository)
public TaskService(IConfiguration configuration, PerforBgtaskRepository taskRepository)
{
_configuration = configuration;
_taskRepository = taskRepository;
}
......@@ -23,7 +29,13 @@ public class TaskService : IAutoInjection
/// <returns></returns>
public List<bg_task> GetTasks(int hours = -10)
{
return _taskRepository.GetEntities(w => w.CreateTime > DateTime.Now.AddHours(hours));
var connectionString = _configuration.GetValue("AppConnection:PerformanceConnectionString", "");
using var conn = new MySqlConnection(connectionString);
if (conn.State != ConnectionState.Open) conn.Open();
var sql = @"SELECT * FROM bg_task WHERE CreateTime > DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL CAST(@hours AS signed) hour);";
var tasks = conn.Query<bg_task>(sql, new { hours }).ToList();
return tasks;
}
public bool Add(Background.JobType type, string argument = "")
......
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