通知告警规则引擎
This commit is contained in:
@@ -1,14 +1,67 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Model.Dto.Inspection;
|
||||||
|
using Service.Interface;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace WebAPI.Controllers
|
namespace WebAPI.Controllers
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 告警规则
|
/// 告警规则(规则实例:查看所有设备告警的规则列表)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("api/inspection/alert-rule")]
|
[Route("api/inspection/alert-rule")]
|
||||||
public class AlertRuleController : ControllerBase
|
public class AlertRuleController : ControllerBase
|
||||||
{
|
{
|
||||||
// TODO: 实现 告警规则 相关接口
|
private readonly IAlertRuleService _alertRuleService;
|
||||||
|
|
||||||
|
public AlertRuleController(IAlertRuleService alertRuleService)
|
||||||
|
{
|
||||||
|
_alertRuleService = alertRuleService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取全部告警规则列表
|
||||||
|
/// </summary>
|
||||||
|
[HttpGet("list")]
|
||||||
|
public async Task<IActionResult> GetList()
|
||||||
|
{
|
||||||
|
return Ok(await _alertRuleService.GetListAsync());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 新增告警规则(前端传 DTO)
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost("add")]
|
||||||
|
public async Task<IActionResult> Add([FromBody] AlertRuleDto dto)
|
||||||
|
{
|
||||||
|
return Ok(await _alertRuleService.AddAsync(dto));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 修改告警规则(前端传 DTO)
|
||||||
|
/// </summary>
|
||||||
|
[HttpPut("update")]
|
||||||
|
public async Task<IActionResult> Update([FromBody] AlertRuleDto dto)
|
||||||
|
{
|
||||||
|
return Ok(await _alertRuleService.UpdateAsync(dto));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除告警规则(软删除)
|
||||||
|
/// </summary>
|
||||||
|
[HttpDelete("{id}")]
|
||||||
|
public async Task<IActionResult> Delete(long id)
|
||||||
|
{
|
||||||
|
return Ok(await _alertRuleService.DeleteAsync(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启用/停用告警规则
|
||||||
|
/// </summary>
|
||||||
|
[HttpPut("{id}/enabled")]
|
||||||
|
public async Task<IActionResult> SetEnabled(long id, [FromQuery] bool enabled)
|
||||||
|
{
|
||||||
|
return Ok(await _alertRuleService.SetEnabledAsync(id, enabled));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Model.Dto.Inspection;
|
||||||
|
using Service.Interface;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace WebAPI.Controllers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 数据转发(配置数据转发规则,支持通过消息通知的方式定时转发数据)
|
||||||
|
/// </summary>
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/inspection/data-forward")]
|
||||||
|
public class DataForwardController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly IDataForwardService _dataForwardService;
|
||||||
|
|
||||||
|
public DataForwardController(IDataForwardService dataForwardService)
|
||||||
|
{
|
||||||
|
_dataForwardService = dataForwardService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取全部转发规则列表
|
||||||
|
/// </summary>
|
||||||
|
[HttpGet("list")]
|
||||||
|
public async Task<IActionResult> GetList()
|
||||||
|
{
|
||||||
|
return Ok(await _dataForwardService.GetListAsync());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 新增转发规则(前端传 DTO)
|
||||||
|
/// </summary>
|
||||||
|
[HttpPost("add")]
|
||||||
|
public async Task<IActionResult> Add([FromBody] DataForwardRuleDto dto)
|
||||||
|
{
|
||||||
|
return Ok(await _dataForwardService.AddAsync(dto));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 修改转发规则(前端传 DTO)
|
||||||
|
/// </summary>
|
||||||
|
[HttpPut("update")]
|
||||||
|
public async Task<IActionResult> Update([FromBody] DataForwardRuleDto dto)
|
||||||
|
{
|
||||||
|
return Ok(await _dataForwardService.UpdateAsync(dto));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除转发规则(软删除)
|
||||||
|
/// </summary>
|
||||||
|
[HttpDelete("{id}")]
|
||||||
|
public async Task<IActionResult> Delete(long id)
|
||||||
|
{
|
||||||
|
return Ok(await _dataForwardService.DeleteAsync(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启用/停用转发规则
|
||||||
|
/// </summary>
|
||||||
|
[HttpPut("{id}/enabled")]
|
||||||
|
public async Task<IActionResult> SetEnabled(long id, [FromQuery] bool enabled)
|
||||||
|
{
|
||||||
|
return Ok(await _dataForwardService.SetEnabledAsync(id, enabled));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
-1
@@ -31,7 +31,11 @@ namespace WebAPI
|
|||||||
DatabaseConfig.SetTenant(tenantId);
|
DatabaseConfig.SetTenant(tenantId);
|
||||||
DatabaseConfig.CreateDatabaseAndCheckConnection(createDatabase: true, checkConnection: true);
|
DatabaseConfig.CreateDatabaseAndCheckConnection(createDatabase: true, checkConnection: true);
|
||||||
SqlSugarContext.InitDatabase();
|
SqlSugarContext.InitDatabase();
|
||||||
builder.Services.AddControllers()
|
builder.Services.AddControllers(options =>
|
||||||
|
{
|
||||||
|
// 非空字符串属性(如 Remark)缺失时不报 400,前端可以只传部分字段
|
||||||
|
options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true;
|
||||||
|
})
|
||||||
.AddJsonOptions(options =>
|
.AddJsonOptions(options =>
|
||||||
{
|
{
|
||||||
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
|
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
namespace Model.Dto.Inspection
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 告警规则 DTO(Id 由 long 改为 string,避免前端精度丢失)
|
||||||
|
/// </summary>
|
||||||
|
public class AlertRuleDto
|
||||||
|
{
|
||||||
|
/// <summary>主键 Id(long → string)</summary>
|
||||||
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>删除状态(0、未删除;1、已删除)</summary>
|
||||||
|
public byte IsDel { get; set; }
|
||||||
|
|
||||||
|
/// <summary>创建时间</summary>
|
||||||
|
public DateTime? CreateTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>规则名称</summary>
|
||||||
|
public string RuleName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>适用设备类型(空表示全部设备)</summary>
|
||||||
|
public string DeviceType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>监测指标(Temperature / Humidity 等)</summary>
|
||||||
|
public string Metric { get; set; }
|
||||||
|
|
||||||
|
/// <summary>比较运算符(> >= < <= = ≠)</summary>
|
||||||
|
public string Operator { get; set; }
|
||||||
|
|
||||||
|
/// <summary>阈值</summary>
|
||||||
|
public double? Threshold { get; set; }
|
||||||
|
|
||||||
|
/// <summary>告警级别(信息/警告/紧急)</summary>
|
||||||
|
public string AlarmLevel { get; set; }
|
||||||
|
|
||||||
|
/// <summary>是否启用</summary>
|
||||||
|
public bool Enabled { get; set; }
|
||||||
|
|
||||||
|
/// <summary>触发次数</summary>
|
||||||
|
public int TriggerCount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>最近触发时间</summary>
|
||||||
|
public DateTime? LastAlarmTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>备注</summary>
|
||||||
|
public string Remark { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
namespace Model.Dto.Inspection
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 数据转发规则 DTO(Id 由 long 改为 string,避免前端精度丢失)
|
||||||
|
/// </summary>
|
||||||
|
public class DataForwardRuleDto
|
||||||
|
{
|
||||||
|
/// <summary>主键 Id(long → string)</summary>
|
||||||
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>删除状态(0、未删除;1、已删除)</summary>
|
||||||
|
public byte IsDel { get; set; }
|
||||||
|
|
||||||
|
/// <summary>创建时间</summary>
|
||||||
|
public DateTime? CreateTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>转发规则名称</summary>
|
||||||
|
public string ForwardName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>源设备类型(空表示全部设备)</summary>
|
||||||
|
public string SourceDeviceType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>转发间隔(秒)</summary>
|
||||||
|
public int ForwardInterval { get; set; }
|
||||||
|
|
||||||
|
/// <summary>通知方式(Email / Sms / Http)</summary>
|
||||||
|
public string NotifyType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>目标地址(邮箱 / 手机号 / 接口URL)</summary>
|
||||||
|
public string TargetAddress { get; set; }
|
||||||
|
|
||||||
|
/// <summary>是否启用</summary>
|
||||||
|
public bool Enabled { get; set; }
|
||||||
|
|
||||||
|
/// <summary>最近转发时间</summary>
|
||||||
|
public DateTime? LastForwardTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>备注</summary>
|
||||||
|
public string Remark { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Model.Entity.Inspection
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 告警规则(规则实例:查看所有设备告警的规则列表)
|
||||||
|
/// </summary>
|
||||||
|
public class AlertRuleEntity : BaseEntity
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 规则名称
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "规则名称", Length = 100)]
|
||||||
|
public string RuleName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 适用设备类型(空表示全部设备,如 TemperatureBox 温度箱)
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "适用设备类型", Length = 64, IsNullable = true)]
|
||||||
|
public string DeviceType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 监测指标(Temperature 温度 / Humidity 湿度等)
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "监测指标", Length = 64)]
|
||||||
|
public string Metric { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 比较运算符(> >= < <= = ≠)
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "比较运算符", Length = 10)]
|
||||||
|
public string Operator { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 阈值
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "阈值", IsNullable = true)]
|
||||||
|
public double? Threshold { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 告警级别(信息/警告/紧急)
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "告警级别", Length = 32)]
|
||||||
|
public string AlarmLevel { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否启用
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "是否启用")]
|
||||||
|
public bool Enabled { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 触发次数(系统统计)
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "触发次数")]
|
||||||
|
public int TriggerCount { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 最近触发时间(系统统计)
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "最近触发时间", IsNullable = true)]
|
||||||
|
public DateTime? LastAlarmTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 备注
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "备注", Length = 500, IsNullable = true)]
|
||||||
|
public string Remark { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
using SqlSugar;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Model.Entity.Inspection
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 数据转发规则(配置数据转发规则,支持通过消息通知的方式定时转发数据)
|
||||||
|
/// </summary>
|
||||||
|
public class DataForwardRuleEntity : BaseEntity
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 转发规则名称
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "转发规则名称", Length = 100)]
|
||||||
|
public string ForwardName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 源设备类型(空表示全部设备,如 TemperatureBox 温度箱)
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "源设备类型", Length = 64, IsNullable = true)]
|
||||||
|
public string SourceDeviceType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 转发间隔(秒)
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "转发间隔(秒)")]
|
||||||
|
public int ForwardInterval { get; set; } = 60;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 通知方式(Email 邮件 / Sms 短信 / Http 接口推送)
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "通知方式", Length = 32)]
|
||||||
|
public string NotifyType { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 目标地址(邮箱 / 手机号 / 接口URL)
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "目标地址", Length = 200)]
|
||||||
|
public string TargetAddress { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否启用
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "是否启用")]
|
||||||
|
public bool Enabled { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 最近转发时间(系统统计)
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "最近转发时间", IsNullable = true)]
|
||||||
|
public DateTime? LastForwardTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 备注
|
||||||
|
/// </summary>
|
||||||
|
[SugarColumn(ColumnDescription = "备注", Length = 500, IsNullable = true)]
|
||||||
|
public string Remark { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -194,5 +194,260 @@ namespace Model.Mapper
|
|||||||
return entities?.Select(e => e.ToDto()).ToList() ?? new List<TemperatureBoxDataDto>();
|
return entities?.Select(e => e.ToDto()).ToList() ?? new List<TemperatureBoxDataDto>();
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region 告警规则
|
||||||
|
/// <summary>
|
||||||
|
/// AlertRuleEntity → AlertRuleDto
|
||||||
|
/// </summary>
|
||||||
|
public static AlertRuleDto ToDto(this AlertRuleEntity entity)
|
||||||
|
{
|
||||||
|
if (entity == null) return null;
|
||||||
|
return new AlertRuleDto
|
||||||
|
{
|
||||||
|
Id = entity.Id.ToString(),
|
||||||
|
IsDel = entity.IsDel,
|
||||||
|
CreateTime = entity.CreateTime,
|
||||||
|
RuleName = entity.RuleName,
|
||||||
|
DeviceType = entity.DeviceType,
|
||||||
|
Metric = entity.Metric,
|
||||||
|
Operator = entity.Operator,
|
||||||
|
Threshold = entity.Threshold,
|
||||||
|
AlarmLevel = entity.AlarmLevel,
|
||||||
|
Enabled = entity.Enabled,
|
||||||
|
TriggerCount = entity.TriggerCount,
|
||||||
|
LastAlarmTime = entity.LastAlarmTime,
|
||||||
|
Remark = entity.Remark
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// List<AlertRuleEntity> → List<AlertRuleDto>
|
||||||
|
/// </summary>
|
||||||
|
public static List<AlertRuleDto> ToDtoList(this List<AlertRuleEntity> entities)
|
||||||
|
{
|
||||||
|
return entities?.Select(e => e.ToDto()).ToList() ?? new List<AlertRuleDto>();
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 数据转发规则
|
||||||
|
/// <summary>
|
||||||
|
/// DataForwardRuleEntity → DataForwardRuleDto
|
||||||
|
/// </summary>
|
||||||
|
public static DataForwardRuleDto ToDto(this DataForwardRuleEntity entity)
|
||||||
|
{
|
||||||
|
if (entity == null) return null;
|
||||||
|
return new DataForwardRuleDto
|
||||||
|
{
|
||||||
|
Id = entity.Id.ToString(),
|
||||||
|
IsDel = entity.IsDel,
|
||||||
|
CreateTime = entity.CreateTime,
|
||||||
|
ForwardName = entity.ForwardName,
|
||||||
|
SourceDeviceType = entity.SourceDeviceType,
|
||||||
|
ForwardInterval = entity.ForwardInterval,
|
||||||
|
NotifyType = entity.NotifyType,
|
||||||
|
TargetAddress = entity.TargetAddress,
|
||||||
|
Enabled = entity.Enabled,
|
||||||
|
LastForwardTime = entity.LastForwardTime,
|
||||||
|
Remark = entity.Remark
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// List<DataForwardRuleEntity> → List<DataForwardRuleDto>
|
||||||
|
/// </summary>
|
||||||
|
public static List<DataForwardRuleDto> ToDtoList(this List<DataForwardRuleEntity> entities)
|
||||||
|
{
|
||||||
|
return entities?.Select(e => e.ToDto()).ToList() ?? new List<DataForwardRuleDto>();
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 反向转换 DTO → Entity(前端入参转实体入库用,Id 由 string 转回 long)
|
||||||
|
/// <summary>
|
||||||
|
/// string Id 安全转 long(空/非法返回 0,新增时由雪花算法自动赋值)
|
||||||
|
/// </summary>
|
||||||
|
private static long ParseId(string id)
|
||||||
|
{
|
||||||
|
return long.TryParse(id, out var value) ? value : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// EquipmentDto → EquipmentEntity
|
||||||
|
/// </summary>
|
||||||
|
public static EquipmentEntity ToEntity(this EquipmentDto dto)
|
||||||
|
{
|
||||||
|
if (dto == null) return null;
|
||||||
|
return new EquipmentEntity
|
||||||
|
{
|
||||||
|
Id = ParseId(dto.Id),
|
||||||
|
IsDel = dto.IsDel,
|
||||||
|
CreateTime = dto.CreateTime,
|
||||||
|
Code = dto.Code,
|
||||||
|
Name = dto.Name,
|
||||||
|
CategoryId = dto.CategoryId,
|
||||||
|
Type = dto.Type,
|
||||||
|
Brand = dto.Brand,
|
||||||
|
Model = dto.Model,
|
||||||
|
Specifications = dto.Specifications,
|
||||||
|
Manufacturer = dto.Manufacturer,
|
||||||
|
Supplier = dto.Supplier,
|
||||||
|
ImageUrl = dto.ImageUrl,
|
||||||
|
QrCode = dto.QrCode,
|
||||||
|
Description = dto.Description,
|
||||||
|
Department = dto.Department,
|
||||||
|
Location = dto.Location,
|
||||||
|
ResponsiblePerson = dto.ResponsiblePerson,
|
||||||
|
ContactPhone = dto.ContactPhone,
|
||||||
|
Custodian = dto.Custodian,
|
||||||
|
PurchaseDate = dto.PurchaseDate,
|
||||||
|
PurchasePrice = dto.PurchasePrice,
|
||||||
|
WarrantyExpireDate = dto.WarrantyExpireDate,
|
||||||
|
AcceptanceDate = dto.AcceptanceDate,
|
||||||
|
EnableDate = dto.EnableDate,
|
||||||
|
ServiceLifeYears = dto.ServiceLifeYears,
|
||||||
|
ScrapDate = dto.ScrapDate,
|
||||||
|
Status = dto.Status,
|
||||||
|
CalibrationStatus = dto.CalibrationStatus,
|
||||||
|
LastCalibrationDate = dto.LastCalibrationDate,
|
||||||
|
NextCalibrationDate = dto.NextCalibrationDate,
|
||||||
|
InspectionDate = dto.InspectionDate,
|
||||||
|
NextInspectionDate = dto.NextInspectionDate,
|
||||||
|
MaintenanceStatus = dto.MaintenanceStatus,
|
||||||
|
LastMaintenanceDate = dto.LastMaintenanceDate,
|
||||||
|
NextMaintenanceDate = dto.NextMaintenanceDate,
|
||||||
|
Remark = dto.Remark,
|
||||||
|
CreateBy = dto.CreateBy,
|
||||||
|
UpdateBy = dto.UpdateBy,
|
||||||
|
UpdateTime = dto.UpdateTime
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// EquipmentAttachmentDto → EquipmentAttachmentEntity
|
||||||
|
/// </summary>
|
||||||
|
public static EquipmentAttachmentEntity ToEntity(this EquipmentAttachmentDto dto)
|
||||||
|
{
|
||||||
|
if (dto == null) return null;
|
||||||
|
return new EquipmentAttachmentEntity
|
||||||
|
{
|
||||||
|
Id = ParseId(dto.Id),
|
||||||
|
IsDel = dto.IsDel,
|
||||||
|
CreateTime = dto.CreateTime,
|
||||||
|
EquipmentId = dto.EquipmentId,
|
||||||
|
FileName = dto.FileName,
|
||||||
|
FileType = dto.FileType,
|
||||||
|
FileExt = dto.FileExt,
|
||||||
|
FileUrl = dto.FileUrl,
|
||||||
|
FileSize = dto.FileSize,
|
||||||
|
Uploader = dto.Uploader,
|
||||||
|
Remark = dto.Remark
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// EquipmentCategoryDto → EquipmentCategoryEntity
|
||||||
|
/// </summary>
|
||||||
|
public static EquipmentCategoryEntity ToEntity(this EquipmentCategoryDto dto)
|
||||||
|
{
|
||||||
|
if (dto == null) return null;
|
||||||
|
return new EquipmentCategoryEntity
|
||||||
|
{
|
||||||
|
Id = ParseId(dto.Id),
|
||||||
|
IsDel = dto.IsDel,
|
||||||
|
CreateTime = dto.CreateTime,
|
||||||
|
ParentId = dto.ParentId,
|
||||||
|
Name = dto.Name,
|
||||||
|
Code = dto.Code,
|
||||||
|
Level = dto.Level,
|
||||||
|
Sort = dto.Sort,
|
||||||
|
Remark = dto.Remark
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// EquipmentStatusRecordDto → EquipmentStatusRecordEntity
|
||||||
|
/// </summary>
|
||||||
|
public static EquipmentStatusRecordEntity ToEntity(this EquipmentStatusRecordDto dto)
|
||||||
|
{
|
||||||
|
if (dto == null) return null;
|
||||||
|
return new EquipmentStatusRecordEntity
|
||||||
|
{
|
||||||
|
Id = ParseId(dto.Id),
|
||||||
|
IsDel = dto.IsDel,
|
||||||
|
CreateTime = dto.CreateTime,
|
||||||
|
EquipmentId = dto.EquipmentId,
|
||||||
|
FromStatus = dto.FromStatus,
|
||||||
|
ToStatus = dto.ToStatus,
|
||||||
|
ChangeTime = dto.ChangeTime,
|
||||||
|
Operator = dto.Operator,
|
||||||
|
Remark = dto.Remark
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// TemperatureBoxDataDto → TemperatureBoxDataEntity
|
||||||
|
/// </summary>
|
||||||
|
public static TemperatureBoxDataEntity ToEntity(this TemperatureBoxDataDto dto)
|
||||||
|
{
|
||||||
|
if (dto == null) return null;
|
||||||
|
return new TemperatureBoxDataEntity
|
||||||
|
{
|
||||||
|
Id = ParseId(dto.Id),
|
||||||
|
IsDel = dto.IsDel,
|
||||||
|
CreateTime = dto.CreateTime,
|
||||||
|
DeviceCode = dto.DeviceCode,
|
||||||
|
DeviceType = dto.DeviceType,
|
||||||
|
DeviceTemperature = dto.DeviceTemperature,
|
||||||
|
DeviceHumidity = dto.DeviceHumidity,
|
||||||
|
Status = dto.Status,
|
||||||
|
AlarmInfo = dto.AlarmInfo
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// AlertRuleDto → AlertRuleEntity
|
||||||
|
/// </summary>
|
||||||
|
public static AlertRuleEntity ToEntity(this AlertRuleDto dto)
|
||||||
|
{
|
||||||
|
if (dto == null) return null;
|
||||||
|
return new AlertRuleEntity
|
||||||
|
{
|
||||||
|
Id = ParseId(dto.Id),
|
||||||
|
IsDel = dto.IsDel,
|
||||||
|
CreateTime = dto.CreateTime,
|
||||||
|
RuleName = dto.RuleName,
|
||||||
|
DeviceType = dto.DeviceType,
|
||||||
|
Metric = dto.Metric,
|
||||||
|
Operator = dto.Operator,
|
||||||
|
Threshold = dto.Threshold,
|
||||||
|
AlarmLevel = dto.AlarmLevel,
|
||||||
|
Enabled = dto.Enabled,
|
||||||
|
TriggerCount = dto.TriggerCount,
|
||||||
|
LastAlarmTime = dto.LastAlarmTime,
|
||||||
|
Remark = dto.Remark
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// DataForwardRuleDto → DataForwardRuleEntity
|
||||||
|
/// </summary>
|
||||||
|
public static DataForwardRuleEntity ToEntity(this DataForwardRuleDto dto)
|
||||||
|
{
|
||||||
|
if (dto == null) return null;
|
||||||
|
return new DataForwardRuleEntity
|
||||||
|
{
|
||||||
|
Id = ParseId(dto.Id),
|
||||||
|
IsDel = dto.IsDel,
|
||||||
|
CreateTime = dto.CreateTime,
|
||||||
|
ForwardName = dto.ForwardName,
|
||||||
|
SourceDeviceType = dto.SourceDeviceType,
|
||||||
|
ForwardInterval = dto.ForwardInterval,
|
||||||
|
NotifyType = dto.NotifyType,
|
||||||
|
TargetAddress = dto.TargetAddress,
|
||||||
|
Enabled = dto.Enabled,
|
||||||
|
LastForwardTime = dto.LastForwardTime,
|
||||||
|
Remark = dto.Remark
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,120 @@
|
|||||||
|
using Model;
|
||||||
|
using Model.Dto.Inspection;
|
||||||
|
using Model.Entity.Inspection;
|
||||||
|
using Model.Mapper;
|
||||||
|
using ORM;
|
||||||
using Service.Interface;
|
using Service.Interface;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Service.Implement
|
namespace Service.Implement
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 告警规则 服务实现
|
/// 告警规则 服务实现(规则实例:查看所有设备告警的规则列表)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class AlertRuleService : IAlertRuleService
|
public class AlertRuleService : IAlertRuleService
|
||||||
{
|
{
|
||||||
// TODO: 实现 告警规则 相关方法
|
/// <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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,124 @@
|
|||||||
|
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 DataForwardService : IDataForwardService
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取全部转发规则列表
|
||||||
|
/// </summary>
|
||||||
|
public async Task<Result<List<DataForwardRuleDto>>> GetListAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var list = await SqlSugarContext.DbContext.Queryable<DataForwardRuleEntity>()
|
||||||
|
.Where(x => x.IsDel == 0)
|
||||||
|
.OrderBy(x => x.CreateTime, SqlSugar.OrderByType.Desc)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
// Entity → DTO(Id 转 string)
|
||||||
|
return Result<List<DataForwardRuleDto>>.Success(list.ToDtoList());
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result<List<DataForwardRuleDto>>.Error("查询转发规则失败", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 新增转发规则(DTO → Entity,string Id 转 long)
|
||||||
|
/// </summary>
|
||||||
|
public async Task<Result> AddAsync(DataForwardRuleDto dto)
|
||||||
|
{
|
||||||
|
if (dto == null || string.IsNullOrWhiteSpace(dto.ForwardName))
|
||||||
|
return Result.Error("转发规则名称不能为空");
|
||||||
|
if (string.IsNullOrWhiteSpace(dto.TargetAddress))
|
||||||
|
return Result.Error("目标地址不能为空");
|
||||||
|
if (dto.ForwardInterval <= 0)
|
||||||
|
return Result.Error("转发间隔必须大于 0 秒");
|
||||||
|
|
||||||
|
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(DataForwardRuleDto 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.LastForwardTime })
|
||||||
|
.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<DataForwardRuleEntity>()
|
||||||
|
.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<DataForwardRuleEntity>()
|
||||||
|
.SetColumns(x => x.Enabled == enabled)
|
||||||
|
.Where(x => x.Id == id)
|
||||||
|
.ExecuteCommandAsync();
|
||||||
|
return Result.Success();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Error("更新启用状态失败", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,39 @@
|
|||||||
|
using Model;
|
||||||
|
using Model.Dto.Inspection;
|
||||||
|
using Model.Entity.Inspection;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Service.Interface
|
namespace Service.Interface
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 告警规则 服务接口
|
/// 告警规则 服务接口(规则实例:查看所有设备告警的规则列表)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IAlertRuleService
|
public interface IAlertRuleService
|
||||||
{
|
{
|
||||||
// TODO: 定义 告警规则 相关方法
|
/// <summary>
|
||||||
|
/// 获取全部告警规则列表
|
||||||
|
/// </summary>
|
||||||
|
Task<Result<List<AlertRuleDto>>> GetListAsync();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 新增告警规则(前端传 DTO,Id 为 string)
|
||||||
|
/// </summary>
|
||||||
|
Task<Result> AddAsync(AlertRuleDto dto);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 修改告警规则(前端传 DTO,Id 为 string)
|
||||||
|
/// </summary>
|
||||||
|
Task<Result> UpdateAsync(AlertRuleDto dto);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除告警规则(软删除)
|
||||||
|
/// </summary>
|
||||||
|
Task<Result> DeleteAsync(long id);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启用/停用告警规则
|
||||||
|
/// </summary>
|
||||||
|
Task<Result> SetEnabledAsync(long id, bool enabled);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
using Model;
|
||||||
|
using Model.Dto.Inspection;
|
||||||
|
using Model.Entity.Inspection;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Service.Interface
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 数据转发 服务接口(配置数据转发规则,支持通过消息通知的方式定时转发数据)
|
||||||
|
/// </summary>
|
||||||
|
public interface IDataForwardService
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取全部转发规则列表
|
||||||
|
/// </summary>
|
||||||
|
Task<Result<List<DataForwardRuleDto>>> GetListAsync();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 新增转发规则(前端传 DTO,Id 为 string)
|
||||||
|
/// </summary>
|
||||||
|
Task<Result> AddAsync(DataForwardRuleDto dto);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 修改转发规则(前端传 DTO,Id 为 string)
|
||||||
|
/// </summary>
|
||||||
|
Task<Result> UpdateAsync(DataForwardRuleDto dto);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除转发规则(软删除)
|
||||||
|
/// </summary>
|
||||||
|
Task<Result> DeleteAsync(long id);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启用/停用转发规则
|
||||||
|
/// </summary>
|
||||||
|
Task<Result> SetEnabledAsync(long id, bool enabled);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user