fix 子查询补充过滤

This commit is contained in:
2025-12-17 11:04:56 +08:00
parent b409f6f375
commit 4bca8859ef
4 changed files with 55 additions and 44 deletions

View File

@@ -446,52 +446,53 @@ public class DevicePermissionService : IDevicePermissionService
); );
break; break;
case AssignTypeEnumType.Group: case AssignTypeEnumType.Group:
{ {
var codes = assignValues.Select(Guid.Parse); var codes = assignValues.Select(Guid.Parse);
ids.AddRange(await _dbContext.Queryable<GainfoGroupEntity>() ids.AddRange(await _dbContext.Queryable<GainfoGroupEntity>()
.LeftJoin<GainfoGroupRelationshipEntity>((a, b) => a.Code == b.GroupCode) .LeftJoin<GainfoGroupRelationshipEntity>((a, b) => a.Code == b.GroupCode)
.LeftJoin<GainfoEntity>((a, b, c) => b.UserNo == c.No) .LeftJoin<GainfoEntity>((a, b, c) => b.UserNo == c.No)
.Where(a => codes.Contains(a.Code)) .Where(a => codes.Contains(a.Code))
.Select((a, b, c) => c.Id) .Select((a, b, c) => c.Id)
.ToListAsync() .ToListAsync()
); );
} }
break; break;
case AssignTypeEnumType.Department: case AssignTypeEnumType.Department:
{
//递归向上获取 assignValue = tenant:department
var tenantDepartments = assignValues.Select(x =>
{ {
var arr = x.Split(":"); //递归向上获取 assignValue = tenant:department
var currentDepartmentNo = arr.Last(); var tenantDepartments = assignValues.Select(x =>
var departmentNos = RecursionHelper.HandlerDepartmentNextCodes(currentDepartmentNo, departments);
return new
{ {
tenantId = arr.First(), departmentNos var arr = x.Split(":");
}; var currentDepartmentNo = arr.Last();
}); var departmentNos = RecursionHelper.HandlerDepartmentNextCodes(currentDepartmentNo, departments);
return new
{
tenantId = arr.First(),
departmentNos
};
});
var where = LinqExpressionExtensions.False<GainfoEntity>(); var where = LinqExpressionExtensions.False<GainfoEntity>();
foreach (var tenantDepartment in tenantDepartments) foreach (var tenantDepartment in tenantDepartments)
{ {
where = where.Or(x => x.Factory == tenantDepartment.tenantId && tenantDepartment.departmentNos.Contains(x.DepartmentNo)); where = where.Or(x => x.Factory == tenantDepartment.tenantId && tenantDepartment.departmentNos.Contains(x.DepartmentNo));
}
ids.AddRange(await _dbContext.Queryable<GainfoEntity>()
.Where(where)
.Select(x => x.Id)
.ToListAsync()
);
} }
ids.AddRange(await _dbContext.Queryable<GainfoEntity>()
.Where(where)
.Select(x => x.Id)
.ToListAsync()
);
}
break; break;
case AssignTypeEnumType.Tenant: case AssignTypeEnumType.Tenant:
{ {
ids.AddRange(await _dbContext.Queryable<GainfoEntity>() ids.AddRange(await _dbContext.Queryable<GainfoEntity>()
.Where(x => assignValues.Contains(x.Factory)) .Where(x => assignValues.Contains(x.Factory))
.Select(x => x.Id) .Select(x => x.Id)
.ToListAsync() .ToListAsync()
); );
} }
break; break;
default: default:
throw new ArgumentOutOfRangeException($"不支持的权限类型:{assignType}"); throw new ArgumentOutOfRangeException($"不支持的权限类型:{assignType}");
@@ -603,8 +604,10 @@ public class DevicePermissionService : IDevicePermissionService
var devicePermissionManuallyUserNos = await _dbContext.Queryable<GainfoEntity>() var devicePermissionManuallyUserNos = await _dbContext.Queryable<GainfoEntity>()
.Where(x => SqlFunc.Subqueryable<DevicePermissionManuallyEntity>() .Where(x => SqlFunc.Subqueryable<DevicePermissionManuallyEntity>()
.Where(p => p.DeviceCode == device.Code) .EnableTableFilter()
.Where(p => p.UserNo == x.No).Any()) .Where(p => p.DeviceCode == device.Code)
.Where(p => p.UserNo == x.No)
.Any())
.Select(x => x.No) .Select(x => x.No)
.ToListAsync(); .ToListAsync();
@@ -626,6 +629,7 @@ public class DevicePermissionService : IDevicePermissionService
// 获取与指定设备关联的分组编码 // 获取与指定设备关联的分组编码
var groupCodes = await _dbContext.Queryable<DeviceGroupEntity>() var groupCodes = await _dbContext.Queryable<DeviceGroupEntity>()
.Where(x => SqlFunc.Subqueryable<DeviceGroupRelationshipEntity>() .Where(x => SqlFunc.Subqueryable<DeviceGroupRelationshipEntity>()
.EnableTableFilter()
.Where(p => p.GroupCode == x.Code) .Where(p => p.GroupCode == x.Code)
.Where(p => p.DeviceCode == deviceCode) .Where(p => p.DeviceCode == deviceCode)
.Any()) .Any())
@@ -663,8 +667,10 @@ public class DevicePermissionService : IDevicePermissionService
{ {
return await _dbContext.Queryable<GainfoEntity>() return await _dbContext.Queryable<GainfoEntity>()
.Where(x => SqlFunc.Subqueryable<DevicePermissionManuallyEntity>() .Where(x => SqlFunc.Subqueryable<DevicePermissionManuallyEntity>()
.EnableTableFilter()
.Where(p => p.DeviceCode == deviceCode) .Where(p => p.DeviceCode == deviceCode)
.Where(p => p.UserNo == x.No).Any()) .Where(p => p.UserNo == x.No)
.Any())
.Select(x => x.No) .Select(x => x.No)
.ToListAsync(); .ToListAsync();
} }

View File

@@ -131,7 +131,11 @@ public class GainfoOutsourceService : IGainfoOutsourceService
{ {
var outsources = await _dbContext.Queryable<GainfoEntity>() var outsources = await _dbContext.Queryable<GainfoEntity>()
.Where(x => x.Type == GainfoUserTypeEnumType.Outsource) .Where(x => x.Type == GainfoUserTypeEnumType.Outsource)
.Where(x => SqlFunc.Subqueryable<OutsourceWhiteListEntity>().Where(p => p.UserCode == x.Code).NotAny())//不存在白名单中 .Where(x => SqlFunc.Subqueryable<OutsourceWhiteListEntity>()
.Where(p => p.UserCode == x.Code)
.Where(p => p.IsDeleted == false)
.NotAny()
)//不存在白名单中
.Select(x => new { x.Id, x.Code, x.Factory, x.CreateTime }) .Select(x => new { x.Id, x.Code, x.Factory, x.CreateTime })
.ToListAsync(); .ToListAsync();

View File

@@ -50,7 +50,7 @@ public class EFenceService : IEFenceService
Name = x.Name, Name = x.Name,
Description = x.Description, Description = x.Description,
DirectionType = x.DirectionType, DirectionType = x.DirectionType,
DeviceCount = SqlFunc.Subqueryable<EFenceDeviceRelationEntity>().Where(p => p.EFenceCode == x.Code).Count(), DeviceCount = SqlFunc.Subqueryable<EFenceDeviceRelationEntity>().Where(p => p.EFenceCode == x.Code).Where(p => p.IsDeleted == false).Count(),
}) })
.ToPageListAsync(request); .ToPageListAsync(request);
@@ -84,6 +84,7 @@ public class EFenceService : IEFenceService
.Where(d => deviceCodes.Contains(d.Code)) .Where(d => deviceCodes.Contains(d.Code))
.Where(d => SqlFunc.Subqueryable<EFenceDeviceRelationEntity>() .Where(d => SqlFunc.Subqueryable<EFenceDeviceRelationEntity>()
.Where(p => p.DeviceCode == d.Code) .Where(p => p.DeviceCode == d.Code)
.Where(p => p.IsDeleted == false)
.NotAny() //不可重复分配 .NotAny() //不可重复分配
) )
.ToListAsync(); .ToListAsync();

View File

@@ -23,7 +23,7 @@
"Timeout": 30, "Timeout": 30,
"SlowSqlTime": 1, "SlowSqlTime": 1,
"EnableSqlLog": true, "EnableSqlLog": true,
"Generate": true "Generate": false
}, },
"SwaggerOptions": { "SwaggerOptions": {
"Enable": true, "Enable": true,