人员群组 查询优化

master
xiaochanghai 4 months ago
parent 0905b91c21
commit 764701490a
  1. 125
      Tiobon.Core.Services/Ghre/Ghre_StaffGroupServices.cs

@ -1,5 +1,6 @@
using MySqlX.XDevAPI.Common;
using Org.BouncyCastle.Utilities;
using System.Data;
namespace Tiobon.Core.Services;
@ -17,7 +18,111 @@ public class Ghre_StaffGroupServices : BaseServices<Ghre_StaffGroup, Ghre_StaffG
}
public override async Task<ServicePageResult<Ghre_StaffGroupDto>> QueryFilterPage(QueryBody filter, string condition, bool? IsEnable = true)
{
var result = await base.QueryFilterPage(filter, condition, IsEnable);
if (string.IsNullOrWhiteSpace(filter.orderBy))
filter.orderBy = "CreateTime1 DESC";
if (filter.pageSize == 0)
filter.pageSize = 10000;
Type entityType = typeof(Ghre_StaffGroup);
var countSql = @$" SELECT COUNT(1) FROM {entityType.GetEntityTableName()} A";
var sql1 = @$"DECLARE @langId INT = {filter.langId};";
var sql = @$" SELECT *,
ISNULL ((SELECT CASE WHEN @langId = 1 THEN UserName ELSE UserEname END
FROM Ghrs_User B
WHERE B.UserId = A.CreateBy),
'') CreateDataInfo,
ISNULL ((SELECT CASE WHEN @langId = 1 THEN UserName ELSE UserEname END
FROM Ghrs_User B
WHERE B.UserId = A.UpdateBy),
'') UpdateDataInfo, ISNULL(A.UpdateTime, A.CreateTime) CreateTime1
FROM {entityType.GetEntityTableName()} A";
string conditions = " WHERE 1=1 ";
if (IsEnable == true)
conditions += " AND IsEnable = 1";
else if (IsEnable == false)
conditions += " AND IsEnable = 0";
if (!string.IsNullOrWhiteSpace(condition))
conditions += " AND " + condition;
var properties = entityType.GetGenericProperties();
if (filter.jsonParam != null)
foreach (JProperty jProperty in filter.jsonParam.Properties())
{
var name = jProperty.Name;
var value = jProperty.Value.ToString();
if (name == "page" || name == "pageSize" || !properties.Any(x => x.Name == name))
continue;
if (name == "StaffType1" || name == "StaffType2")
{
var jsonParam = JsonHelper.JsonToObj<JsonParam>(value);
switch (jsonParam.operationKey)
{
case "EqualAny"://
if (jsonParam.columnValue != null)
{
//var ids1 = JsonHelper.JsonToObj<List<string>>(jsonParam.columnValue.ToString());
conditions += $" AND Exists(select 1 from openjson(A.{name}) depts where Exists(select 1 from openjson('{jsonParam.columnValue.ToString()}') where value =depts.[value] ) )";
}
break;
case "NotEqualAny"://
if (jsonParam.columnValue != null)
{
conditions += $" AND NOT Exists(select 1 from openjson(A.{name}) depts where Exists(select 1 from openjson('{jsonParam.columnValue.ToString()}') where value =depts.[value] ) )";
}
break;
default:
break;
}
continue;
}
if (name == "ZoneId" || name == "DeptId" || name == "JobId" || name == "GradeId" || name == "TitleId" || name == "StaffId")
{
var jsonParam = JsonHelper.JsonToObj<JsonParam>(value);
switch (jsonParam.operationKey)
{
case "EqualAny"://
if (jsonParam.columnValue != null)
conditions += $" AND (Exists(select 1 from openjson(A.{name}) depts where Exists(select 1 from openjson('{jsonParam.columnValue.ToString()}') where value =depts.[value] ) ) OR " +
$"Exists(select 1 from openjson(A.Excl{name}) depts where Exists(select 1 from openjson('{jsonParam.columnValue.ToString()}') where value =depts.[value] ) ))";
break;
case "NotEqualAny"://
if (jsonParam.columnValue != null)
conditions += $" AND NOT (Exists(select 1 from openjson(A.{name}) depts where Exists(select 1 from openjson('{jsonParam.columnValue.ToString()}') where value =depts.[value] ) ) AND " +
$"Exists(select 1 from openjson(A.Excl{name}) depts where Exists(select 1 from openjson('{jsonParam.columnValue.ToString()}') where value =depts.[value] ) ))";
break;
default:
break;
}
continue;
}
if (!string.IsNullOrWhiteSpace(value))
conditions = DealConditions(conditions, name, value);
}
sql += conditions;
countSql += conditions;
int total = await Db.Ado.GetIntAsync(countSql);
sql = "SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY " + filter.orderBy + ") NUM FROM (SELECT * FROM (" + sql + " ";
sql += ") A ) B ) C";
sql += " WHERE NUM <= " + filter.pageNum * filter.pageSize + " AND NUM >" + (filter.pageNum - 1) * filter.pageSize;
sql = sql1 + sql;
var entitys = await Db.Ado.SqlQueryAsync<Ghre_StaffGroupDto>(sql);
var result = new ServicePageResult<Ghre_StaffGroupDto>(filter.pageNum, total, filter.pageSize, entitys);
var zoneIds = new List<int>();
var exclZoneIds = new List<int>();
@ -62,6 +167,22 @@ public class Ghre_StaffGroupServices : BaseServices<Ghre_StaffGroup, Ghre_StaffG
staffIds.AddRange(x.ExclStaffIds);
x.StaffType1 = null;
x.StaffType2 = null;
x.ZoneId = null;
x.ExclZoneId = null;
x.DeptId = null;
x.ExclDeptId = null;
x.TitleId = null;
x.ExclTitleId = null;
x.GradeId = null;
x.ExclGradeId = null;
x.JobId = null;
x.ExclJobId = null;
x.StaffId = null;
x.ExclStaffId = null;
x.GroupType = await GetParaLabel("TrainingGroupType", x.GroupType);
var StaffType1s = new List<string>();
@ -79,7 +200,6 @@ public class Ghre_StaffGroupServices : BaseServices<Ghre_StaffGroup, Ghre_StaffG
x.StaffType2 = string.Join(",", StaffType2s);
});
if (zoneIds.Any())
{
var zones = await Db.Queryable<Ghra_Zone>().Where(x => zoneIds.Contains(x.ZoneID.Value)).ToListAsync();
@ -193,7 +313,6 @@ public class Ghre_StaffGroupServices : BaseServices<Ghre_StaffGroup, Ghre_StaffG
return result;
}
public override async Task<long> Add(InsertGhre_StaffGroupInput entity)
{
entity.StaffType1 = JsonHelper.ObjToJson(entity.StaffType1s);

Loading…
Cancel
Save