提取绩效数据初稿

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