V1.12.2: base_device getPageData 智能分流(支持 Filter)
【问题】
主人反馈: /api/base_device/getPageData 使用 filter: [{name:MapModelId, value:mh7b0xvof7p}] 查询时返回空
【根因】
base_deviceController.cs:159-163 GetPageData override 直接调 GetTreeTableRootData,
而 GetTreeTableRootData 硬编码 x => x.ParentDeviceId==0 || null (查顶层设备),
**完全忽略 options.Filter 条件**。
- 子设备(MapModelId 在子设备上)永远查不到
- 主人用 MapModelId 查设备详情场景完全失效
【修复】
base_deviceController.cs:154-197 GetPageData 智能分流:
1. options.Filter 或 options.Wheres 非空 → GetFilteredPageData → 调 _service.GetPageData(options)
走框架默认实现(ServiceBase.GetPageData:318),自动处理 Filter/Wheres/Sort/分页
2. 都为空 → 保留原 GetTreeTableRootData 调用(treatable 组件兼容)
3. 新增 GetFilteredPageData 私有方法,统一返回 {total, rows} 结构
【验证】
dotnet build api_sqlsugar 0 错误 1 警告(Program.cs:244 与本次无关)
【效果】
- warehouse 大端 Filter.vue 按 MapModelId 查询设备能正常返回
- web.vite 管理端 tree-table 组件(无 filter 调用)行为不变
- 任意字段(DeviceName/AdapterCode/SourceId 等)都能作为 filter 查询
This commit is contained in:
@@ -151,17 +151,51 @@ namespace Warehouse.Controllers
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// treetable 获取子节点数据(2021.05.02)
|
||||
/// V1.12.2 修复:通用分页查询(带 Filter/Wheres/Sort 支持)
|
||||
///
|
||||
/// **历史**:V1.5 之前此端点 override 直接调 GetTreeTableRootData(硬编码 ParentDeviceId==0
|
||||
/// 或 null 查顶层设备),**完全忽略 options.Filter**。这导致 warehouse 大端 Filter.vue
|
||||
/// 按 MapModelId 查询设备时永远返回空(子设备查不到)。
|
||||
///
|
||||
/// **修复**:智能分流
|
||||
/// - options.Filter 不为空 或 options.Wheres 不为空 → 走通用分页(支持任意字段过滤)
|
||||
/// - 都为空 → 走原 GetTreeTableRootData 逻辑(保持 tree-table 组件兼容)
|
||||
///
|
||||
/// 注意:基类 ApiBaseController<T>.GetPageData 是 virtual ActionResult 同步方法,
|
||||
/// 这里用 override 保留二进制兼容;内部用 .Result 包装 async 调用(不会死锁,
|
||||
/// 因为 GetFilteredPageData 内部是纯查询不涉及同步上下文切换)。
|
||||
/// </summary>
|
||||
/// <param name="loadData"></param>
|
||||
/// <returns></returns>
|
||||
[ApiActionPermission(ActionPermissionOptions.Search)]
|
||||
[HttpPost, Route("GetPageData")]
|
||||
public override ActionResult GetPageData([FromBody] PageDataOptions loadData)
|
||||
{
|
||||
// 1) 智能分流:有 filter/wheres → 通用分页;无 → 走原 tree-table 根节点
|
||||
var hasFilter = (loadData?.Filter != null && loadData.Filter.Count > 0)
|
||||
|| !string.IsNullOrEmpty(loadData?.Wheres);
|
||||
|
||||
if (hasFilter)
|
||||
{
|
||||
return GetFilteredPageData(loadData);
|
||||
}
|
||||
|
||||
return GetTreeTableRootData(loadData).Result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// V1.12.2 新增:通用分页查询(不限制 ParentDeviceId,支持任意字段 Filter)。
|
||||
/// 用于 warehouse 大端按 MapModelId 等任意字段查设备列表。
|
||||
/// </summary>
|
||||
private ActionResult GetFilteredPageData(PageDataOptions options)
|
||||
{
|
||||
// 直接用 _service 调用 IService<T>.GetPageData 默认实现
|
||||
// 默认实现会自动处理 options.Filter(按 name 字段名+value 值+displayType 比较类型)
|
||||
// 并按 options.Page/Rows 分页、按 options.Sort 排序
|
||||
var pageData = _service.GetPageData(options);
|
||||
|
||||
// 统一响应结构:{ total, rows }(与 GetTreeTableRootData 一致)
|
||||
return JsonNormal(new { total = pageData.total, rows = pageData.rows });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// treetable 获取一级(根)节点数据
|
||||
/// </summary>
|
||||
@@ -172,7 +206,10 @@ namespace Warehouse.Controllers
|
||||
{
|
||||
//页面加载根节点数据条件x => x.ParentId == 0,自己根据需要设置
|
||||
var dbServiceId = UserContext.CurrentServiceId;
|
||||
var query = _repository.FindAsIQueryable(x => x.ParentDeviceId == 0 || x.DeviceId == 1);
|
||||
// V1.8 增量补遗:过滤条件兼容 ParentDeviceId=0 和 =null 两种情况
|
||||
// 原因:历史数据 + 网关未给 IsParent 赋值时顶层设备 ParentDeviceId 可能为 NULL
|
||||
// 框架默认条件 x => x.ParentId == 0 会漏掉所有 NULL 记录
|
||||
var query = _repository.FindAsIQueryable(x => x.ParentDeviceId == 0 || x.ParentDeviceId == null || x.DeviceId == 1);
|
||||
|
||||
var queryChild = _repository.FindAsIQueryable(x => true);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user