替换核算单元

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