提取绩效数据初稿

parent d0cb1d7c
......@@ -16,17 +16,26 @@ namespace Performance.Api.Controllers
public class TemplateController : Controller
{
private readonly TemplateService templateService;
private readonly ExtractService extractService;
public TemplateController(TemplateService templateService)
public TemplateController(TemplateService templateService,
ExtractService extractService)
{
this.templateService = templateService;
this.extractService = extractService;
}
public ApiResponse Download()
/// <summary>
/// 提取绩效数据
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[Route("extractdata")]
[HttpPost]
public ApiResponse ExtractData([FromBody]ApiRequest request)
{
//templateService.Download();
return new ApiResponse(ResponseType.OK);
var filePath = extractService.ExtractData(24);
return new ApiResponse(ResponseType.OK, "OK", filePath);
}
}
}
\ No newline at end of file
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Options;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
......@@ -21,6 +22,7 @@ namespace Performance.Services
/// </summary>
public class ExtractService : IAutoInjection
{
private readonly IHostingEnvironment environment;
private readonly PerSheetService perSheetService;
private readonly PerHeaderService perHeaderService;
private readonly PerforPersheetRepository perforPersheetRepository;
......@@ -31,7 +33,8 @@ public class ExtractService : IAutoInjection
private readonly PerforPerallotRepository perforPerallotRepository;
private readonly PerforHospitalconfigRepository perforHospitalconfigRepository;
public ExtractService(PerSheetService perSheetService,
public ExtractService(IHostingEnvironment environment,
PerSheetService perSheetService,
PerHeaderService perHeaderService,
PerforPersheetRepository perforPersheetRepository,
PerforImheaderRepository perforImheaderRepository,
......@@ -41,6 +44,7 @@ public class ExtractService : IAutoInjection
PerforPerallotRepository perforPerallotRepository,
PerforHospitalconfigRepository perforHospitalconfigRepository)
{
this.environment = environment;
this.perSheetService = perSheetService;
this.perHeaderService = perHeaderService;
this.perforPersheetRepository = perforPersheetRepository;
......@@ -51,7 +55,8 @@ public class ExtractService : IAutoInjection
this.perforPerallotRepository = perforPerallotRepository;
this.perforHospitalconfigRepository = perforHospitalconfigRepository;
}
public void Download(int hospitalId)
public string ExtractData(int hospitalId)
{
List<PerSheet> sheetList = new List<PerSheet>();
......@@ -63,66 +68,81 @@ public void Download(int hospitalId)
throw new PerformanceException($"暂不支持自动提取绩效数据");
var hospitalConfig = configList.First();
string originalPath = "";
//判断是否是首次
if ((allotList == null || !allotList.Any()) && first != null)
{
//首次 从excel中获取人员信息,SHEET页信息,列头信息
sheetList = GetFileData(first.Path);
originalPath = first.Path;
}
else
{
//非首次 从数据库中获取人员信息,SHEET页信息,列头信息
var allot = allotList.OrderByDescending(t => t.Year).ThenBy(t => t.Month).First();
var allot = allotList.OrderByDescending(t => t.Year).ThenByDescending(t => t.Month).First();
sheetList = GetRepositoryData(allot.ID);
originalPath = allot.Path;
}
string path = @"C:\Users\ry\Desktop\test.xlsx";
FileHelper.DeleteFile(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文件
WriteExcel(path, sheetList, hospitalConfig, hospitalId);
//人员信息由EXCEL中提供
//SHEET页在SQL提取中出现时,执行SQL脚本获得结果向EXCEL中填充
//向EXCEL中填充数据,填充时值与列头必须匹配
throw new NotImplementedException();
if (WriteExcel(path, originalPath, sheetList, hospitalConfig, hospitalId))
return dpath;
throw new PerformanceException("绩效数据提取失败");
}
/// <summary>
/// 向EXCEL中写入数据
/// 人员信息由EXCEL中提供,或从上次绩效中提供
/// SHEET页在SQL提取中出现时,执行SQL脚本获得结果向EXCEL中填充
/// 向EXCEL中填充数据,填充时值与列头必须匹配
/// </summary>
/// <param name="path"></param>
/// <param name="newpath"></param>
/// <param name="originalPath"></param>
/// <param name="sheetList"></param>
/// <param name="hospitalConfig"></param>
/// <param name="hospitalId"></param>
private void WriteExcel(string path, List<PerSheet> sheetList, sys_hospitalconfig hospitalConfig, int hospitalId)
private bool WriteExcel(string newpath, string originalPath, List<PerSheet> sheetList, sys_hospitalconfig hospitalConfig, int hospitalId)
{
if (string.IsNullOrEmpty(originalPath))
throw new PerformanceException($"{originalPath}文件路径无效");
var scriptList = perforExtractRepository.GetEntities(t => t.HospitalId == hospitalId);
var connection = ConnectionBuilder.Create(DatabaseType.SqlServer, hospitalConfig.DbSource, hospitalConfig.DbName, hospitalConfig.DbUser, hospitalConfig.DbPassword);
//根据SHEET页信息,列头信息,创建EXCEL文件
IWorkbook workbook = new XSSFWorkbook(path);
IWorkbook workbook = new XSSFWorkbook(originalPath);
foreach (var sheet in sheetList)
{
var importSheet = workbook.CreateSheet(sheet.SheetName);
var importSheet = workbook.GetSheet(sheet.SheetName);
if (importSheet == null) continue;
//var importSheet = workbook.CreateSheet(sheet.SheetName);
//创建列头
foreach (var pointRow in sheet.PerHeader.Select(t => t.PointRow).Distinct().OrderBy(t => t))
{
var importRow = importSheet.CreateRow(pointRow);
//写入单元格
foreach (var perHeader in sheet.PerHeader.Where(t => t.PointRow == pointRow))
{
importRow.CreateCell(perHeader.PointCell).SetCellValue(perHeader.CellValue);
//设置合并单元格
if (perHeader.IsMerge)
//foreach (var pointRow in sheet.PerHeader.Select(t => t.PointRow).Distinct().OrderBy(t => t))
//{
// var importRow = importSheet.CreateRow(pointRow);
// //写入单元格
// foreach (var perHeader in sheet.PerHeader.Where(t => t.PointRow == pointRow))
// {
// importRow.CreateCell(perHeader.PointCell).SetCellValue(perHeader.CellValue);
// //设置合并单元格
// if (perHeader.IsMerge)
// {
// var cellRange = new CellRangeAddress(perHeader.PointRow, perHeader.PointRow + perHeader.MergeRow, perHeader.PointCell, perHeader.PointCell + perHeader.MergeCell);
// importSheet.AddMergedRegion(cellRange);
// }
// }
//}
var maxHeaderRowNumber = sheet.PerHeader.Max(t => t.PointRow);
//清空数据行
for (int i = maxHeaderRowNumber + 1; i < importSheet.LastRowNum + 1; i++)
{
var cellRange = new CellRangeAddress(perHeader.PointRow, perHeader.PointRow + perHeader.MergeRow, perHeader.PointCell, perHeader.PointCell + perHeader.MergeCell);
importSheet.AddMergedRegion(cellRange);
var importRow = importSheet.GetRow(i);
if (importRow != null)
importSheet.RemoveRow(importRow);
}
}
}
var maxHeaderRowNumber = sheet.PerHeader.Max(t => t.PointRow);
//填充人员信息
if (SheetType.Employee == sheet.SheetType && sheet.PerData != null && sheet.PerData.Any())
{
......@@ -153,7 +173,7 @@ private void WriteExcel(string path, List<PerSheet> sheetList, sys_hospitalconfi
var headInfo = sheet.PerHeader.FirstOrDefault(t => t.CellValue == item);
if (headInfo != null)
{
var value = keyValues[item].Invoke(dataList[i]).ToString();
var value = (keyValues[item].Invoke(dataList[i]) ?? "").ToString();
importRow.CreateCell(headInfo.PointCell).SetCellValue(value);
}
}
......@@ -183,6 +203,9 @@ private void WriteExcel(string path, List<PerSheet> sheetList, sys_hospitalconfi
}
}
}
using (FileStream file = new FileStream(newpath, FileMode.Create))
workbook.Write(file);
return true;
}
/// <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