设备类添加产品字段、添加物模型类,新增产品CRUD方法

This commit is contained in:
2026-09-03 17:16:28 +08:00
parent cfa8e78d33
commit bcd6484f45
29 changed files with 1611 additions and 23 deletions
+36 -4
View File
@@ -7,6 +7,7 @@ using Service.Interface.Config;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Service.Implement.Config
@@ -17,9 +18,9 @@ namespace Service.Implement.Config
public class DeviceService : IDeviceService
{
/// <summary>
/// 分页查询设备列表(支持关键字搜索编号/名称/类型)
/// 分页查询设备列表(支持关键字搜索编号/名称/类型,可按所属产品筛选
/// </summary>
public async Task<Result<List<IotDeviceDto>>> GetPagedAsync(int pageIndex, int pageSize, RefAsync<int> total, string? keyword)
public async Task<Result<List<IotDeviceDto>>> GetPagedAsync(int pageIndex, int pageSize, RefAsync<int> total, string? keyword, long? productId = null)
{
try
{
@@ -27,10 +28,13 @@ namespace Service.Implement.Config
.Where(x => x.IsDel == 0)
.WhereIF(!string.IsNullOrWhiteSpace(keyword),
x => x.Code.Contains(keyword!) || x.Name.Contains(keyword!) || x.DeviceType.Contains(keyword!))
.WhereIF(productId.HasValue && productId.Value > 0, x => x.ProductId == productId!.Value)
.OrderBy(x => x.CreateTime, OrderByType.Desc)
.ToPageListAsync(pageIndex, pageSize, total);
return Result<List<IotDeviceDto>>.Success(list.ToDtoList());
var dtos = list.ToDtoList();
await FillProductNames(dtos);
return Result<List<IotDeviceDto>>.Success(dtos);
}
catch (Exception ex)
{
@@ -50,7 +54,10 @@ namespace Service.Implement.Config
.FirstAsync();
if (entity == null)
return Result<IotDeviceDto>.Error("设备不存在或已被删除");
return Result<IotDeviceDto>.Success(entity.ToDto());
var dto = entity.ToDto();
await FillProductNames(new List<IotDeviceDto> { dto });
return Result<IotDeviceDto>.Success(dto);
}
catch (Exception ex)
{
@@ -157,5 +164,30 @@ namespace Service.Implement.Config
return Result<List<DeviceLogDto>>.Error("查询设备日志失败", ex);
}
}
/// <summary>
/// 给设备 DTO 填充所属产品的型号/名称(ProductName 展示用)
/// </summary>
private async Task FillProductNames(List<IotDeviceDto> dtos)
{
if (dtos == null || dtos.Count == 0)
return;
var ids = dtos.Where(x => long.TryParse(x.ProductId, out var pid) && pid > 0)
.Select(x => long.Parse(x.ProductId!)).Distinct().ToList();
if (ids.Count == 0)
return;
var products = await SqlSugarContext.DbContext.Queryable<ProductEntity>()
.Where(x => ids.Contains(x.Id) && x.IsDel == 0)
.ToListAsync();
var map = products.ToDictionary(p => p.Id, p => p.Name ?? p.Model);
foreach (var d in dtos)
{
if (long.TryParse(d.ProductId, out var pid) && map.TryGetValue(pid, out var name))
d.ProductName = name;
}
}
}
}