162 lines
5.9 KiB
C#
162 lines
5.9 KiB
C#
using Model;
|
|
using Model.Dto.Config;
|
|
using Model.Entity.Config;
|
|
using Model.Mapper;
|
|
using ORM;
|
|
using Service.Interface.Config;
|
|
using SqlSugar;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Service.Implement.Config
|
|
{
|
|
/// <summary>
|
|
/// IOT设备管理 服务实现
|
|
/// </summary>
|
|
public class DeviceService : IDeviceService
|
|
{
|
|
/// <summary>
|
|
/// 分页查询设备列表(支持关键字搜索编号/名称/类型)
|
|
/// </summary>
|
|
public async Task<Result<List<IotDeviceDto>>> GetPagedAsync(int pageIndex, int pageSize, RefAsync<int> total, string? keyword)
|
|
{
|
|
try
|
|
{
|
|
var list = await SqlSugarContext.DbContext.Queryable<IotDeviceEntity>()
|
|
.Where(x => x.IsDel == 0)
|
|
.WhereIF(!string.IsNullOrWhiteSpace(keyword),
|
|
x => x.Code.Contains(keyword!) || x.Name.Contains(keyword!) || x.DeviceType.Contains(keyword!))
|
|
.OrderBy(x => x.CreateTime, OrderByType.Desc)
|
|
.ToPageListAsync(pageIndex, pageSize, total);
|
|
|
|
return Result<List<IotDeviceDto>>.Success(list.ToDtoList());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result<List<IotDeviceDto>>.Error("查询设备列表失败", ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据 Id 获取设备详情
|
|
/// </summary>
|
|
public async Task<Result<IotDeviceDto>> GetByIdAsync(long id)
|
|
{
|
|
try
|
|
{
|
|
var entity = await SqlSugarContext.DbContext.Queryable<IotDeviceEntity>()
|
|
.Where(x => x.Id == id && x.IsDel == 0)
|
|
.FirstAsync();
|
|
if (entity == null)
|
|
return Result<IotDeviceDto>.Error("设备不存在或已被删除");
|
|
return Result<IotDeviceDto>.Success(entity.ToDto());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result<IotDeviceDto>.Error("查询设备详情失败", ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增设备(校验编号唯一性)
|
|
/// </summary>
|
|
public async Task<Result> AddAsync(IotDeviceDto dto)
|
|
{
|
|
if (dto == null || string.IsNullOrWhiteSpace(dto.Code))
|
|
return Result.Error("设备编号不能为空");
|
|
if (string.IsNullOrWhiteSpace(dto.Name))
|
|
return Result.Error("设备名称不能为空");
|
|
|
|
try
|
|
{
|
|
bool exists = await SqlSugarContext.DbContext.Queryable<IotDeviceEntity>()
|
|
.AnyAsync(x => x.Code == dto.Code && x.IsDel == 0);
|
|
if (exists)
|
|
return Result.Error($"设备编号【{dto.Code}】已存在");
|
|
|
|
var entity = dto.ToEntity();
|
|
entity.CreateTime = DateTime.Now;
|
|
// 初始离线
|
|
entity.OnlineStatus = IotDeviceOnlineStatusEnum.Offline;
|
|
await SqlSugarContext.DbContext.Insertable(entity).ExecuteCommandAsync();
|
|
return Result.Success();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result.Error("新增设备失败", ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改设备(在线状态等运行字段不允许被前端覆盖)
|
|
/// </summary>
|
|
public async Task<Result> UpdateAsync(IotDeviceDto dto)
|
|
{
|
|
var entity = dto?.ToEntity();
|
|
if (entity == null || entity.Id <= 0)
|
|
return Result.Error("设备Id无效");
|
|
|
|
try
|
|
{
|
|
// 编号变更时校验唯一性
|
|
bool exists = await SqlSugarContext.DbContext.Queryable<IotDeviceEntity>()
|
|
.AnyAsync(x => x.Code == entity.Code && x.IsDel == 0 && x.Id != entity.Id);
|
|
if (exists)
|
|
return Result.Error($"设备编号【{entity.Code}】已存在");
|
|
|
|
await SqlSugarContext.DbContext.Updateable(entity)
|
|
.IgnoreColumns(x => new { x.CreateTime, x.IsDel, x.OnlineStatus, x.LastCollectTime, x.LastError })
|
|
.ExecuteCommandAsync();
|
|
return Result.Success();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result.Error("修改设备失败", ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除设备(软删除:IsDel 置 1)
|
|
/// </summary>
|
|
public async Task<Result> DeleteAsync(long id)
|
|
{
|
|
if (id <= 0)
|
|
return Result.Error("设备Id无效");
|
|
|
|
try
|
|
{
|
|
await SqlSugarContext.DbContext.Updateable<IotDeviceEntity>()
|
|
.SetColumns(x => x.IsDel == 1)
|
|
.Where(x => x.Id == id)
|
|
.ExecuteCommandAsync();
|
|
return Result.Success();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result.Error("删除设备失败", ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页查询指定设备的日志(设备独享日志)
|
|
/// </summary>
|
|
public async Task<Result<List<DeviceLogDto>>> GetDeviceLogsAsync(long deviceId, int pageIndex, int pageSize, RefAsync<int> total)
|
|
{
|
|
try
|
|
{
|
|
var list = await SqlSugarContext.DbContext.Queryable<DeviceLogEntity>()
|
|
.Where(x => x.DeviceId == deviceId && x.IsDel == 0)
|
|
.OrderBy(x => x.LogTime, OrderByType.Desc)
|
|
.ToPageListAsync(pageIndex, pageSize, total);
|
|
|
|
return Result<List<DeviceLogDto>>.Success(list.ToDtoList());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Result<List<DeviceLogDto>>.Error("查询设备日志失败", ex);
|
|
}
|
|
}
|
|
}
|
|
}
|