diff --git a/IOT_API/Controllers/Inspection/AlertRuleController.cs b/IOT_API/Controllers/Inspection/AlertRuleController.cs
index 3e9c71d..2f12f6c 100644
--- a/IOT_API/Controllers/Inspection/AlertRuleController.cs
+++ b/IOT_API/Controllers/Inspection/AlertRuleController.cs
@@ -1,14 +1,67 @@
using Microsoft.AspNetCore.Mvc;
+using Model.Dto.Inspection;
+using Service.Interface;
+using System.Threading.Tasks;
namespace WebAPI.Controllers
{
///
- /// 告警规则
+ /// 告警规则(规则实例:查看所有设备告警的规则列表)
///
[ApiController]
[Route("api/inspection/alert-rule")]
public class AlertRuleController : ControllerBase
{
- // TODO: 实现 告警规则 相关接口
+ private readonly IAlertRuleService _alertRuleService;
+
+ public AlertRuleController(IAlertRuleService alertRuleService)
+ {
+ _alertRuleService = alertRuleService;
+ }
+
+ ///
+ /// 获取全部告警规则列表
+ ///
+ [HttpGet("list")]
+ public async Task GetList()
+ {
+ return Ok(await _alertRuleService.GetListAsync());
+ }
+
+ ///
+ /// 新增告警规则(前端传 DTO)
+ ///
+ [HttpPost("add")]
+ public async Task Add([FromBody] AlertRuleDto dto)
+ {
+ return Ok(await _alertRuleService.AddAsync(dto));
+ }
+
+ ///
+ /// 修改告警规则(前端传 DTO)
+ ///
+ [HttpPut("update")]
+ public async Task Update([FromBody] AlertRuleDto dto)
+ {
+ return Ok(await _alertRuleService.UpdateAsync(dto));
+ }
+
+ ///
+ /// 删除告警规则(软删除)
+ ///
+ [HttpDelete("{id}")]
+ public async Task Delete(long id)
+ {
+ return Ok(await _alertRuleService.DeleteAsync(id));
+ }
+
+ ///
+ /// 启用/停用告警规则
+ ///
+ [HttpPut("{id}/enabled")]
+ public async Task SetEnabled(long id, [FromQuery] bool enabled)
+ {
+ return Ok(await _alertRuleService.SetEnabledAsync(id, enabled));
+ }
}
}
diff --git a/IOT_API/Controllers/Inspection/DataForwardController.cs b/IOT_API/Controllers/Inspection/DataForwardController.cs
new file mode 100644
index 0000000..fa7098d
--- /dev/null
+++ b/IOT_API/Controllers/Inspection/DataForwardController.cs
@@ -0,0 +1,67 @@
+using Microsoft.AspNetCore.Mvc;
+using Model.Dto.Inspection;
+using Service.Interface;
+using System.Threading.Tasks;
+
+namespace WebAPI.Controllers
+{
+ ///
+ /// 数据转发(配置数据转发规则,支持通过消息通知的方式定时转发数据)
+ ///
+ [ApiController]
+ [Route("api/inspection/data-forward")]
+ public class DataForwardController : ControllerBase
+ {
+ private readonly IDataForwardService _dataForwardService;
+
+ public DataForwardController(IDataForwardService dataForwardService)
+ {
+ _dataForwardService = dataForwardService;
+ }
+
+ ///
+ /// 获取全部转发规则列表
+ ///
+ [HttpGet("list")]
+ public async Task GetList()
+ {
+ return Ok(await _dataForwardService.GetListAsync());
+ }
+
+ ///
+ /// 新增转发规则(前端传 DTO)
+ ///
+ [HttpPost("add")]
+ public async Task Add([FromBody] DataForwardRuleDto dto)
+ {
+ return Ok(await _dataForwardService.AddAsync(dto));
+ }
+
+ ///
+ /// 修改转发规则(前端传 DTO)
+ ///
+ [HttpPut("update")]
+ public async Task Update([FromBody] DataForwardRuleDto dto)
+ {
+ return Ok(await _dataForwardService.UpdateAsync(dto));
+ }
+
+ ///
+ /// 删除转发规则(软删除)
+ ///
+ [HttpDelete("{id}")]
+ public async Task Delete(long id)
+ {
+ return Ok(await _dataForwardService.DeleteAsync(id));
+ }
+
+ ///
+ /// 启用/停用转发规则
+ ///
+ [HttpPut("{id}/enabled")]
+ public async Task SetEnabled(long id, [FromQuery] bool enabled)
+ {
+ return Ok(await _dataForwardService.SetEnabledAsync(id, enabled));
+ }
+ }
+}
diff --git a/IOT_API/Program.cs b/IOT_API/Program.cs
index f039c2f..a668778 100644
--- a/IOT_API/Program.cs
+++ b/IOT_API/Program.cs
@@ -31,7 +31,11 @@ namespace WebAPI
DatabaseConfig.SetTenant(tenantId);
DatabaseConfig.CreateDatabaseAndCheckConnection(createDatabase: true, checkConnection: true);
SqlSugarContext.InitDatabase();
- builder.Services.AddControllers()
+ builder.Services.AddControllers(options =>
+ {
+ // 非空字符串属性(如 Remark)缺失时不报 400,前端可以只传部分字段
+ options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true;
+ })
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
diff --git a/Model/Dto/Inspection/AlertRuleDto.cs b/Model/Dto/Inspection/AlertRuleDto.cs
new file mode 100644
index 0000000..e966545
--- /dev/null
+++ b/Model/Dto/Inspection/AlertRuleDto.cs
@@ -0,0 +1,47 @@
+namespace Model.Dto.Inspection
+{
+ ///
+ /// 告警规则 DTO(Id 由 long 改为 string,避免前端精度丢失)
+ ///
+ public class AlertRuleDto
+ {
+ /// 主键 Id(long → string)
+ public string Id { get; set; }
+
+ /// 删除状态(0、未删除;1、已删除)
+ public byte IsDel { get; set; }
+
+ /// 创建时间
+ public DateTime? CreateTime { get; set; }
+
+ /// 规则名称
+ public string RuleName { get; set; }
+
+ /// 适用设备类型(空表示全部设备)
+ public string DeviceType { get; set; }
+
+ /// 监测指标(Temperature / Humidity 等)
+ public string Metric { get; set; }
+
+ /// 比较运算符(> >= < <= = ≠)
+ public string Operator { get; set; }
+
+ /// 阈值
+ public double? Threshold { get; set; }
+
+ /// 告警级别(信息/警告/紧急)
+ public string AlarmLevel { get; set; }
+
+ /// 是否启用
+ public bool Enabled { get; set; }
+
+ /// 触发次数
+ public int TriggerCount { get; set; }
+
+ /// 最近触发时间
+ public DateTime? LastAlarmTime { get; set; }
+
+ /// 备注
+ public string Remark { get; set; }
+ }
+}
diff --git a/Model/Dto/Inspection/DataForwardRuleDto.cs b/Model/Dto/Inspection/DataForwardRuleDto.cs
new file mode 100644
index 0000000..3f9bd97
--- /dev/null
+++ b/Model/Dto/Inspection/DataForwardRuleDto.cs
@@ -0,0 +1,41 @@
+namespace Model.Dto.Inspection
+{
+ ///
+ /// 数据转发规则 DTO(Id 由 long 改为 string,避免前端精度丢失)
+ ///
+ public class DataForwardRuleDto
+ {
+ /// 主键 Id(long → string)
+ public string Id { get; set; }
+
+ /// 删除状态(0、未删除;1、已删除)
+ public byte IsDel { get; set; }
+
+ /// 创建时间
+ public DateTime? CreateTime { get; set; }
+
+ /// 转发规则名称
+ public string ForwardName { get; set; }
+
+ /// 源设备类型(空表示全部设备)
+ public string SourceDeviceType { get; set; }
+
+ /// 转发间隔(秒)
+ public int ForwardInterval { get; set; }
+
+ /// 通知方式(Email / Sms / Http)
+ public string NotifyType { get; set; }
+
+ /// 目标地址(邮箱 / 手机号 / 接口URL)
+ public string TargetAddress { get; set; }
+
+ /// 是否启用
+ public bool Enabled { get; set; }
+
+ /// 最近转发时间
+ public DateTime? LastForwardTime { get; set; }
+
+ /// 备注
+ public string Remark { get; set; }
+ }
+}
diff --git a/Model/Entity/Inspection/AlertRuleEntity.cs b/Model/Entity/Inspection/AlertRuleEntity.cs
new file mode 100644
index 0000000..fa56735
--- /dev/null
+++ b/Model/Entity/Inspection/AlertRuleEntity.cs
@@ -0,0 +1,71 @@
+using SqlSugar;
+using System;
+
+namespace Model.Entity.Inspection
+{
+ ///
+ /// 告警规则(规则实例:查看所有设备告警的规则列表)
+ ///
+ public class AlertRuleEntity : BaseEntity
+ {
+ ///
+ /// 规则名称
+ ///
+ [SugarColumn(ColumnDescription = "规则名称", Length = 100)]
+ public string RuleName { get; set; }
+
+ ///
+ /// 适用设备类型(空表示全部设备,如 TemperatureBox 温度箱)
+ ///
+ [SugarColumn(ColumnDescription = "适用设备类型", Length = 64, IsNullable = true)]
+ public string DeviceType { get; set; }
+
+ ///
+ /// 监测指标(Temperature 温度 / Humidity 湿度等)
+ ///
+ [SugarColumn(ColumnDescription = "监测指标", Length = 64)]
+ public string Metric { get; set; }
+
+ ///
+ /// 比较运算符(> >= < <= = ≠)
+ ///
+ [SugarColumn(ColumnDescription = "比较运算符", Length = 10)]
+ public string Operator { get; set; }
+
+ ///
+ /// 阈值
+ ///
+ [SugarColumn(ColumnDescription = "阈值", IsNullable = true)]
+ public double? Threshold { get; set; }
+
+ ///
+ /// 告警级别(信息/警告/紧急)
+ ///
+ [SugarColumn(ColumnDescription = "告警级别", Length = 32)]
+ public string AlarmLevel { get; set; }
+
+ ///
+ /// 是否启用
+ ///
+ [SugarColumn(ColumnDescription = "是否启用")]
+ public bool Enabled { get; set; } = true;
+
+ ///
+ /// 触发次数(系统统计)
+ ///
+ [SugarColumn(ColumnDescription = "触发次数")]
+ public int TriggerCount { get; set; }
+
+ ///
+ /// 最近触发时间(系统统计)
+ ///
+ [SugarColumn(ColumnDescription = "最近触发时间", IsNullable = true)]
+ public DateTime? LastAlarmTime { get; set; }
+
+ ///
+ /// 备注
+ ///
+ [SugarColumn(ColumnDescription = "备注", Length = 500, IsNullable = true)]
+ public string Remark { get; set; }
+ }
+}
diff --git a/Model/Entity/Inspection/DataForwardRuleEntity.cs b/Model/Entity/Inspection/DataForwardRuleEntity.cs
new file mode 100644
index 0000000..574024a
--- /dev/null
+++ b/Model/Entity/Inspection/DataForwardRuleEntity.cs
@@ -0,0 +1,59 @@
+using SqlSugar;
+using System;
+
+namespace Model.Entity.Inspection
+{
+ ///
+ /// 数据转发规则(配置数据转发规则,支持通过消息通知的方式定时转发数据)
+ ///
+ public class DataForwardRuleEntity : BaseEntity
+ {
+ ///
+ /// 转发规则名称
+ ///
+ [SugarColumn(ColumnDescription = "转发规则名称", Length = 100)]
+ public string ForwardName { get; set; }
+
+ ///
+ /// 源设备类型(空表示全部设备,如 TemperatureBox 温度箱)
+ ///
+ [SugarColumn(ColumnDescription = "源设备类型", Length = 64, IsNullable = true)]
+ public string SourceDeviceType { get; set; }
+
+ ///
+ /// 转发间隔(秒)
+ ///
+ [SugarColumn(ColumnDescription = "转发间隔(秒)")]
+ public int ForwardInterval { get; set; } = 60;
+
+ ///
+ /// 通知方式(Email 邮件 / Sms 短信 / Http 接口推送)
+ ///
+ [SugarColumn(ColumnDescription = "通知方式", Length = 32)]
+ public string NotifyType { get; set; }
+
+ ///
+ /// 目标地址(邮箱 / 手机号 / 接口URL)
+ ///
+ [SugarColumn(ColumnDescription = "目标地址", Length = 200)]
+ public string TargetAddress { get; set; }
+
+ ///
+ /// 是否启用
+ ///
+ [SugarColumn(ColumnDescription = "是否启用")]
+ public bool Enabled { get; set; } = true;
+
+ ///
+ /// 最近转发时间(系统统计)
+ ///
+ [SugarColumn(ColumnDescription = "最近转发时间", IsNullable = true)]
+ public DateTime? LastForwardTime { get; set; }
+
+ ///
+ /// 备注
+ ///
+ [SugarColumn(ColumnDescription = "备注", Length = 500, IsNullable = true)]
+ public string Remark { get; set; }
+ }
+}
diff --git a/Model/Mapper/EntityMapper.cs b/Model/Mapper/EntityMapper.cs
index ea077dc..7dd6272 100644
--- a/Model/Mapper/EntityMapper.cs
+++ b/Model/Mapper/EntityMapper.cs
@@ -194,5 +194,260 @@ namespace Model.Mapper
return entities?.Select(e => e.ToDto()).ToList() ?? new List();
}
#endregion
+
+ #region 告警规则
+ ///
+ /// AlertRuleEntity → AlertRuleDto
+ ///
+ 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
+ };
+ }
+
+ ///
+ /// List<AlertRuleEntity> → List<AlertRuleDto>
+ ///
+ public static List ToDtoList(this List entities)
+ {
+ return entities?.Select(e => e.ToDto()).ToList() ?? new List();
+ }
+ #endregion
+
+ #region 数据转发规则
+ ///
+ /// DataForwardRuleEntity → DataForwardRuleDto
+ ///
+ 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
+ };
+ }
+
+ ///
+ /// List<DataForwardRuleEntity> → List<DataForwardRuleDto>
+ ///
+ public static List ToDtoList(this List entities)
+ {
+ return entities?.Select(e => e.ToDto()).ToList() ?? new List();
+ }
+ #endregion
+
+ #region 反向转换 DTO → Entity(前端入参转实体入库用,Id 由 string 转回 long)
+ ///
+ /// string Id 安全转 long(空/非法返回 0,新增时由雪花算法自动赋值)
+ ///
+ private static long ParseId(string id)
+ {
+ return long.TryParse(id, out var value) ? value : 0;
+ }
+
+ ///
+ /// EquipmentDto → EquipmentEntity
+ ///
+ 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
+ };
+ }
+
+ ///
+ /// EquipmentAttachmentDto → EquipmentAttachmentEntity
+ ///
+ 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
+ };
+ }
+
+ ///
+ /// EquipmentCategoryDto → EquipmentCategoryEntity
+ ///
+ 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
+ };
+ }
+
+ ///
+ /// EquipmentStatusRecordDto → EquipmentStatusRecordEntity
+ ///
+ 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
+ };
+ }
+
+ ///
+ /// TemperatureBoxDataDto → TemperatureBoxDataEntity
+ ///
+ 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
+ };
+ }
+
+ ///
+ /// AlertRuleDto → AlertRuleEntity
+ ///
+ 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
+ };
+ }
+
+ ///
+ /// DataForwardRuleDto → DataForwardRuleEntity
+ ///
+ 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
}
}
diff --git a/Service/Implement/Inspection/AlertRuleService.cs b/Service/Implement/Inspection/AlertRuleService.cs
index 40e65d3..aaf748d 100644
--- a/Service/Implement/Inspection/AlertRuleService.cs
+++ b/Service/Implement/Inspection/AlertRuleService.cs
@@ -1,12 +1,120 @@
+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
{
///
- /// 告警规则 服务实现
+ /// 告警规则 服务实现(规则实例:查看所有设备告警的规则列表)
///
public class AlertRuleService : IAlertRuleService
{
- // TODO: 实现 告警规则 相关方法
+ ///
+ /// 获取全部告警规则列表
+ ///
+ public async Task>> GetListAsync()
+ {
+ try
+ {
+ var list = await SqlSugarContext.DbContext.Queryable()
+ .Where(x => x.IsDel == 0)
+ .OrderBy(x => x.CreateTime, SqlSugar.OrderByType.Desc)
+ .ToListAsync();
+
+ // Entity → DTO(Id 转 string)
+ return Result>.Success(list.ToDtoList());
+ }
+ catch (Exception ex)
+ {
+ return Result>.Error("查询告警规则失败", ex);
+ }
+ }
+
+ ///
+ /// 新增告警规则(DTO → Entity,string Id 转 long)
+ ///
+ public async Task 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);
+ }
+ }
+
+ ///
+ /// 修改告警规则(触发次数等系统字段不允许被前端覆盖)
+ ///
+ public async Task 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);
+ }
+ }
+
+ ///
+ /// 删除告警规则(软删除:IsDel 置 1)
+ ///
+ public async Task DeleteAsync(long id)
+ {
+ try
+ {
+ await SqlSugarContext.DbContext.Updateable()
+ .SetColumns(x => x.IsDel == 1)
+ .Where(x => x.Id == id)
+ .ExecuteCommandAsync();
+ return Result.Success();
+ }
+ catch (Exception ex)
+ {
+ return Result.Error("删除告警规则失败", ex);
+ }
+ }
+
+ ///
+ /// 启用/停用告警规则
+ ///
+ public async Task SetEnabledAsync(long id, bool enabled)
+ {
+ try
+ {
+ await SqlSugarContext.DbContext.Updateable()
+ .SetColumns(x => x.Enabled == enabled)
+ .Where(x => x.Id == id)
+ .ExecuteCommandAsync();
+ return Result.Success();
+ }
+ catch (Exception ex)
+ {
+ return Result.Error("更新启用状态失败", ex);
+ }
+ }
}
}
diff --git a/Service/Implement/Inspection/DataForwardService.cs b/Service/Implement/Inspection/DataForwardService.cs
new file mode 100644
index 0000000..a6912e8
--- /dev/null
+++ b/Service/Implement/Inspection/DataForwardService.cs
@@ -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
+{
+ ///
+ /// 数据转发 服务实现(配置数据转发规则,支持通过消息通知的方式定时转发数据)
+ ///
+ public class DataForwardService : IDataForwardService
+ {
+ ///
+ /// 获取全部转发规则列表
+ ///
+ public async Task>> GetListAsync()
+ {
+ try
+ {
+ var list = await SqlSugarContext.DbContext.Queryable()
+ .Where(x => x.IsDel == 0)
+ .OrderBy(x => x.CreateTime, SqlSugar.OrderByType.Desc)
+ .ToListAsync();
+
+ // Entity → DTO(Id 转 string)
+ return Result>.Success(list.ToDtoList());
+ }
+ catch (Exception ex)
+ {
+ return Result>.Error("查询转发规则失败", ex);
+ }
+ }
+
+ ///
+ /// 新增转发规则(DTO → Entity,string Id 转 long)
+ ///
+ public async Task 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);
+ }
+ }
+
+ ///
+ /// 修改转发规则(最近转发时间等系统字段不允许被前端覆盖)
+ ///
+ public async Task 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);
+ }
+ }
+
+ ///
+ /// 删除转发规则(软删除:IsDel 置 1)
+ ///
+ public async Task DeleteAsync(long id)
+ {
+ try
+ {
+ await SqlSugarContext.DbContext.Updateable()
+ .SetColumns(x => x.IsDel == 1)
+ .Where(x => x.Id == id)
+ .ExecuteCommandAsync();
+ return Result.Success();
+ }
+ catch (Exception ex)
+ {
+ return Result.Error("删除转发规则失败", ex);
+ }
+ }
+
+ ///
+ /// 启用/停用转发规则
+ ///
+ public async Task SetEnabledAsync(long id, bool enabled)
+ {
+ try
+ {
+ await SqlSugarContext.DbContext.Updateable()
+ .SetColumns(x => x.Enabled == enabled)
+ .Where(x => x.Id == id)
+ .ExecuteCommandAsync();
+ return Result.Success();
+ }
+ catch (Exception ex)
+ {
+ return Result.Error("更新启用状态失败", ex);
+ }
+ }
+ }
+}
diff --git a/Service/Interface/Inspection/IAlertRuleService.cs b/Service/Interface/Inspection/IAlertRuleService.cs
index 71d3da8..751e48f 100644
--- a/Service/Interface/Inspection/IAlertRuleService.cs
+++ b/Service/Interface/Inspection/IAlertRuleService.cs
@@ -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
{
///
- /// 告警规则 服务接口
+ /// 告警规则 服务接口(规则实例:查看所有设备告警的规则列表)
///
public interface IAlertRuleService
{
- // TODO: 定义 告警规则 相关方法
+ ///
+ /// 获取全部告警规则列表
+ ///
+ Task>> GetListAsync();
+
+ ///
+ /// 新增告警规则(前端传 DTO,Id 为 string)
+ ///
+ Task AddAsync(AlertRuleDto dto);
+
+ ///
+ /// 修改告警规则(前端传 DTO,Id 为 string)
+ ///
+ Task UpdateAsync(AlertRuleDto dto);
+
+ ///
+ /// 删除告警规则(软删除)
+ ///
+ Task DeleteAsync(long id);
+
+ ///
+ /// 启用/停用告警规则
+ ///
+ Task SetEnabledAsync(long id, bool enabled);
}
}
diff --git a/Service/Interface/Inspection/IDataForwardService.cs b/Service/Interface/Inspection/IDataForwardService.cs
new file mode 100644
index 0000000..9b28f4c
--- /dev/null
+++ b/Service/Interface/Inspection/IDataForwardService.cs
@@ -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
+{
+ ///
+ /// 数据转发 服务接口(配置数据转发规则,支持通过消息通知的方式定时转发数据)
+ ///
+ public interface IDataForwardService
+ {
+ ///
+ /// 获取全部转发规则列表
+ ///
+ Task>> GetListAsync();
+
+ ///
+ /// 新增转发规则(前端传 DTO,Id 为 string)
+ ///
+ Task AddAsync(DataForwardRuleDto dto);
+
+ ///
+ /// 修改转发规则(前端传 DTO,Id 为 string)
+ ///
+ Task UpdateAsync(DataForwardRuleDto dto);
+
+ ///
+ /// 删除转发规则(软删除)
+ ///
+ Task DeleteAsync(long id);
+
+ ///
+ /// 启用/停用转发规则
+ ///
+ Task SetEnabledAsync(long id, bool enabled);
+ }
+}