121 lines
3.9 KiB
C#
121 lines
3.9 KiB
C#
using Model;
|
||
using Model.Dto.Inspection;
|
||
using Model.Entity.Inspection;
|
||
using Model.Mapper;
|
||
using ORM;
|
||
using Service.Interface;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Service.Implement
|
||
{
|
||
/// <summary>
|
||
/// 告警规则 服务实现(规则实例:查看所有设备告警的规则列表)
|
||
/// </summary>
|
||
public class AlertRuleService : IAlertRuleService
|
||
{
|
||
/// <summary>
|
||
/// 获取全部告警规则列表
|
||
/// </summary>
|
||
public async Task<Result<List<AlertRuleDto>>> GetListAsync()
|
||
{
|
||
try
|
||
{
|
||
var list = await SqlSugarContext.DbContext.Queryable<AlertRuleEntity>()
|
||
.Where(x => x.IsDel == 0)
|
||
.OrderBy(x => x.CreateTime, SqlSugar.OrderByType.Desc)
|
||
.ToListAsync();
|
||
|
||
// Entity → DTO(Id 转 string)
|
||
return Result<List<AlertRuleDto>>.Success(list.ToDtoList());
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return Result<List<AlertRuleDto>>.Error("查询告警规则失败", ex);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 新增告警规则(DTO → Entity,string Id 转 long)
|
||
/// </summary>
|
||
public async Task<Result> AddAsync(AlertRuleDto dto)
|
||
{
|
||
if (dto == null || string.IsNullOrWhiteSpace(dto.RuleName))
|
||
return Result.Error("规则名称不能为空");
|
||
|
||
try
|
||
{
|
||
var entity = dto.ToEntity();
|
||
entity.CreateTime = DateTime.Now;
|
||
await SqlSugarContext.DbContext.Insertable(entity).ExecuteCommandAsync();
|
||
return Result.Success();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return Result.Error("新增告警规则失败", ex);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 修改告警规则(触发次数等系统字段不允许被前端覆盖)
|
||
/// </summary>
|
||
public async Task<Result> UpdateAsync(AlertRuleDto dto)
|
||
{
|
||
var entity = dto?.ToEntity();
|
||
if (entity == null || entity.Id <= 0)
|
||
return Result.Error("规则Id无效");
|
||
|
||
try
|
||
{
|
||
await SqlSugarContext.DbContext.Updateable(entity)
|
||
.IgnoreColumns(x => new { x.CreateTime, x.IsDel, x.TriggerCount, x.LastAlarmTime })
|
||
.ExecuteCommandAsync();
|
||
return Result.Success();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return Result.Error("修改告警规则失败", ex);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除告警规则(软删除:IsDel 置 1)
|
||
/// </summary>
|
||
public async Task<Result> DeleteAsync(long id)
|
||
{
|
||
try
|
||
{
|
||
await SqlSugarContext.DbContext.Updateable<AlertRuleEntity>()
|
||
.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> SetEnabledAsync(long id, bool enabled)
|
||
{
|
||
try
|
||
{
|
||
await SqlSugarContext.DbContext.Updateable<AlertRuleEntity>()
|
||
.SetColumns(x => x.Enabled == enabled)
|
||
.Where(x => x.Id == id)
|
||
.ExecuteCommandAsync();
|
||
return Result.Success();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return Result.Error("更新启用状态失败", ex);
|
||
}
|
||
}
|
||
}
|
||
}
|