替换核算单元

parent e4c901e6
......@@ -23,6 +23,7 @@ public class CustomExtractService : IAutoInjection
private readonly UserService _userService;
private readonly RoleService _roleService;
private readonly PerforPerallotRepository _perallotRepository;
private readonly PerforPerdeptdicRepository _perforPerdeptdicRepository;
private readonly PerforHospitalconfigRepository _perforHospitalconfigRepository;
private readonly PerforcustscriptRepository _perforcustscriptRepository;
......@@ -32,6 +33,7 @@ public class CustomExtractService : IAutoInjection
UserService userService,
RoleService roleService,
PerforPerallotRepository perallotRepository,
PerforPerdeptdicRepository perforPerdeptdicRepository,
PerforHospitalconfigRepository perforHospitalconfigRepository,
PerforcustscriptRepository perforcustscriptRepository)
{
......@@ -40,6 +42,7 @@ public class CustomExtractService : IAutoInjection
_userService = userService;
_roleService = roleService;
_perallotRepository = perallotRepository;
_perforPerdeptdicRepository = perforPerdeptdicRepository;
_perforHospitalconfigRepository = perforHospitalconfigRepository;
_perforcustscriptRepository = perforcustscriptRepository;
}
......@@ -50,7 +53,7 @@ public bool CheckConfigScript(int userId, int allotId)
var scripts = _perforcustscriptRepository.GetEntities(w => w.HospitalId == allot.HospitalId && w.IsEnable == 1);
scripts = (IsSecondAdmin(userId))
scripts = (IsSecondAdmin(userId, out string[] unitType))
? scripts?.Where(w => w.IsSecondAllot == 1).ToList()
: scripts?.Where(w => w.IsOnceAllot == 1).ToList();
......@@ -68,7 +71,7 @@ public bool ExtractData(int userId, int allotId, out string resultFilePath)
var scripts = _perforcustscriptRepository.GetEntities(w => w.HospitalId == allot.HospitalId && w.IsEnable == 1);
scripts = (IsSecondAdmin(userId))
scripts = (IsSecondAdmin(userId, out string[] unitType))
? scripts?.Where(w => w.IsSecondAllot == 1).ToList()
: scripts?.Where(w => w.IsOnceAllot == 1).ToList();
......@@ -77,6 +80,7 @@ public bool ExtractData(int userId, int allotId, out string resultFilePath)
result = false;
return result;
}
var depts = _perforPerdeptdicRepository.GetEntities(w => w.HospitalId == allot.HospitalId);
IWorkbook workbook = null;
try
......@@ -84,7 +88,7 @@ public bool ExtractData(int userId, int allotId, out string resultFilePath)
workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
ExcelStyle style = new ExcelStyle(workbook);
WriteDataToFile(userId, allot, scripts, workbook);
WriteDataToFile(userId, allot, scripts, workbook, depts);
}
catch (Exception ex)
{
......@@ -104,13 +108,28 @@ public bool ExtractData(int userId, int allotId, out string resultFilePath)
return result;
}
private void WriteDataToFile(int userId, per_allot allot, List<cust_script> scripts, IWorkbook workbook)
/// <summary>
/// 返回需要关注的列头索引,找不到-1
/// </summary>
/// <param name="heads"></param>
/// <returns></returns>
private (int deptIndex, int sourceIndex, int unitIndex) GetCruxHeader(IEnumerable<string> heads)
{
var deptIndex = heads.ToList().IndexOf("科室名称");
var sourceIndex = heads.ToList().IndexOf("来源");
var unitIndex = heads.ToList().IndexOf("组别");
return (deptIndex, sourceIndex, unitIndex);
}
private void WriteDataToFile(int userId, per_allot allot, List<cust_script> scripts, IWorkbook workbook, List<per_dept_dic> depts)
{
var configs = _perforHospitalconfigRepository.GetEntities(t => t.HospitalId == allot.HospitalId)
?? throw new PerformanceException("当前医院没有数据库地址配置");
var parameters = GetParameters(allot);
var (isSecondAdmin, department, unitType) = GetUserDepartment(userId);
parameters.Add("@department", $"'{department}'");
var parameters = GetParameters(allot, userId);
foreach (var item in scripts)
{
var conf = configs.FirstOrDefault(w => w.Id == item.ConfigId);
......@@ -128,51 +147,80 @@ private void WriteDataToFile(int userId, per_allot allot, List<cust_script> scri
}
var sheet = workbook.CreateSheet(item.Name);
// 没数据跳过
if (dynamics == null || dynamics.Count() == 0)
continue;
var headers = ((IDictionary<string, object>)dynamics.ElementAt(0)).Keys;
var hrow = sheet.GetOrCreate(0);
for (int col = 0; col < headers.Count; col++)
{
var cell = hrow.CreateCell(col);
cell.SetCellOValue(headers.ElementAt(col));
sheet.SetValue(1, col + 4, headers.ElementAt(col));
}
// 列头坐标信息
var (deptIndex, sourceIndex, unitIndex) = GetCruxHeader(headers);
for (int col = 0; col < headers.Count; col++)
int row = 2;
for (int r = 0; r < dynamics.Count(); r++)
{
for (int row = 0; row < dynamics.Count(); row++)
{
var key = headers.ElementAt(col);
var data = dynamics.ElementAt(row);
var temp = (IDictionary<string, object>)dynamics.ElementAt(r); // 行数据
var temp = (IDictionary<string, object>)data;
#region 替换原始科室名称及跳过写入EXCEL逻辑
var value = temp[key]?.ToString() ?? "";
string accountUnit = "";
// “科室名称”必须存在 “来源”必须存在
if (deptIndex > -1 && sourceIndex > -1)
{
string[] atUnitTypeList = new string[] { };
string atSource = headers.ElementAt(sourceIndex); // 当前行“来源”
string atDepartment = temp.ElementAt(deptIndex).Value?.ToString() ?? ""; // 当前行“科室名称”
string atUnitType = (unitIndex > -1) ? headers.ElementAt(unitIndex) : ""; // 当前行“核算单元类别”
var srow = sheet.GetOrCreate(row + 1);
var cell = srow.CreateCell(col);
SetCellValue(cell, value);
// 如果是科主任护士长 则取角色的 核算单元类别
if (isSecondAdmin && unitType.Any())
{
atUnitTypeList = unitType;
atSource = headers.ElementAt(sourceIndex);
}
// 如果是核算办 则取数据中 核算单元类别
else if (unitIndex > -1)
{
atUnitTypeList = new string[] { atUnitType };
}
// 替换原始科室名称
if (atUnitTypeList.Any() && !string.IsNullOrEmpty(atSource) && !string.IsNullOrEmpty(atDepartment))
{
accountUnit = depts.FirstOrDefault(w => atUnitTypeList.Contains(w.UnitType) && w.Source == atSource && w.HISDeptName == atDepartment)
?.AccountingUnit ?? "";
}
catch (Exception ex)
// 跳过写入EXCEL逻辑
if (isSecondAdmin)
{
_logger.LogError($"提取绩效数据写入错误:{item.Name}{ex}");
if (!string.IsNullOrEmpty(accountUnit) && accountUnit != department) continue;
if (!string.IsNullOrEmpty(atUnitType) && !atUnitTypeList.Contains(atUnitType)) continue;
}
}
#endregion
int col = 4;
for (int c = 0; c < headers.Count; c++)
{
var value = temp.ElementAt(c).Value;
// 替换原始科室名称
if (deptIndex > -1 && deptIndex == c)
value = accountUnit;
sheet.SetValue(row, col, value);
col++;
}
row++;
}
public void SetCellValue(ICell cell, string value)
{
switch (value)
}
catch (Exception ex)
{
case string reg when Regex.IsMatch(reg, @"^[+-]?\d*[.]?\d*$"):
cell.SetCellValue(ConvertHelper.To<double>(value));
break;
case string reg when Regex.IsMatch(reg, @"^[+-]?\d*$"):
cell.SetCellValue(ConvertHelper.To<int>(value));
break;
default:
cell.SetCellValue(value);
break;
_logger.LogError($"提取绩效数据写入错误:{item.Name}{ex}");
}
}
}
}
......@@ -215,7 +263,7 @@ public IEnumerable<dynamic> QueryData(sys_hospitalconfig config, string execsql,
/// </summary>
/// <param name="allot"></param>
/// <returns></returns>
private Dictionary<string, string> GetParameters(per_allot allot, int userId)
private Dictionary<string, string> GetParameters(per_allot allot)
{
DateTime beginTime = new DateTime(allot.Year, allot.Month, 1);
......@@ -223,7 +271,6 @@ public IEnumerable<dynamic> QueryData(sys_hospitalconfig config, string execsql,
{
{ "@beginTime", $"'{beginTime.ToString("yyyy-MM-dd")}'" },
{ "@endTime", $"'{beginTime.AddMonths(1).ToString("yyyy-MM-dd")}'"},
{ "@department", $"'{GetUserDepartment(userId)}'" },
};
return pairs;
}
......@@ -233,12 +280,24 @@ public IEnumerable<dynamic> QueryData(sys_hospitalconfig config, string execsql,
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
private bool IsSecondAdmin(int userId)
private bool IsSecondAdmin(int userId, out string[] unitType)
{
var role = _roleService.GetRole(userId);
int[] roleArray = new int[] { _options.Value.NurseRole, _options.Value.DirectorRole, _options.Value.SpecialRole, _options.Value.OfficeRole };
Dictionary<int, string[]> pairs = new Dictionary<int, string[]>
{
{ _options.Value.NurseRole, new string[] { "护理组" } },
{ _options.Value.DirectorRole, new string[] { "医生组", "医技组" } },
{ _options.Value.SpecialRole, new string[] { "特殊核算组" } },
{ _options.Value.OfficeRole, new string[] { "行政后勤" } },
};
return roleArray.Contains(role?.FirstOrDefault().ID ?? 0);
var roleId = _roleService.GetRole(userId)?.FirstOrDefault().ID ?? 0;
if (pairs.ContainsKey(roleId))
{
unitType = pairs[roleId];
return true;
}
unitType = new string[] { };
return false;
}
/// <summary>
......@@ -246,13 +305,13 @@ private bool IsSecondAdmin(int userId)
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
private string GetUserDepartment(int userId)
private (bool isSecondAdmin, string department, string[] unitType) GetUserDepartment(int userId)
{
var user = _userService.GetUser(userId)
?? throw new PerformanceException("当前用户信息无效");
return IsSecondAdmin(userId) ? string.Empty : (user.Department ?? "");
//return "妇产科";
var isSecondAdmin = IsSecondAdmin(userId, out string[] unitType);
var department = isSecondAdmin ? string.Empty : (user.Department ?? "");
return (isSecondAdmin, department, unitType);
}
}
}
\ No newline at end of file
......@@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace Performance.Services.ExtractExcelService
{
......@@ -86,6 +87,22 @@ public static void SetCellValue<T>(this ICell cell, object value, T defaultValue
cell.SetCellValue("");
}
}
public static void SetValue(this ISheet sheet, int row, int column, object value)
{
var icell = sheet.GetOrCreate(row).GetOrCreate(column);
switch (value)
{
case string reg when Regex.IsMatch(reg, @"^[+-]?\d*[.]?\d*$"):
value = ConvertHelper.To<double>(value);
break;
case string reg when Regex.IsMatch(reg, @"^[+-]?\d*$"):
value = ConvertHelper.To<int>(value);
break;
}
SetCellOValue(icell, value);
}
public static void SetCellOValue(this ICell cell, object value)
{
......
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