using Abp.Auditing;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.EntityFrameworkCore.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ToolLibrary;
using ToolLibrary.LogHelper;
using YunDa.SOMS.Application.Core;
using YunDa.SOMS.Application.Core.Configuration;
using YunDa.SOMS.Application.Core.Session;
using YunDa.SOMS.Application.Core.SwaggerHelper;
using YunDa.SOMS.DataTransferObject;
using YunDa.SOMS.DataTransferObject.Account;
using YunDa.SOMS.DataTransferObject.DataMonitoring.SecondaryCircuitInspection;
using YunDa.SOMS.DataTransferObject.DataMonitoring.SecondaryCircuitInspection.Configurations;
using YunDa.SOMS.DataTransferObject.DataMonitoring.SecondaryCircuitInspection.Results;
using YunDa.SOMS.DataTransferObject.DataMonitoring.TelemeteringConfigurationDto;
using YunDa.SOMS.DataTransferObject.DataMonitoring.TelesignalisationConfigurationDto;
using YunDa.SOMS.DataTransferObject.GeneralInformation.EquipmentLiveDataDto;
using YunDa.SOMS.Entities.DataMonitoring;
using YunDa.SOMS.Entities.DataMonitoring.SecondaryCircuitInspection;
using YunDa.SOMS.Entities.GeneralInformation;
using YunDa.SOMS.Redis.Repositories;
namespace YunDa.SOMS.Application.DataMonitoring.SecondaryCircuitInspection
{
///
/// 二次回路事件驱动配置应用服务实现
///
[Description("二次回路事件驱动配置管理服务")]
public class SecondaryCircuitEventDrivenConfigAppService : SOMSAppServiceBase, ISecondaryCircuitEventDrivenConfigAppService
{
private readonly IRepository _eventDrivenConfigRepository;
private readonly IRepository _inspectionItemRepository;
private readonly IRepository _inspectionEventItemRepository;
private readonly IRepository _telemetryConfigRepository;
private readonly IRepository _telesignalConfigRepository;
private readonly IRedisRepository _telemeteringRedisRepository;
private readonly IRedisRepository _telesignalisationRedisRepository;
private readonly IRepository _equipmentInfoRepository;
private readonly IRepository _telemeteringConfigurationRepository;
private readonly IRepository _telesignalisationConfigurationRepository;
private LoginUserOutput _currentUser = null;
private IUnitOfWorkManager _unitOfWorkManager = null;
private string _ISMSGateWayIp = "http://127.0.0.1:38094";
public SecondaryCircuitEventDrivenConfigAppService(
IRepository eventDrivenConfigRepository,
IRepository inspectionItemRepository,
IRepository inspectionEventItemRepository,
IRepository telemetryConfigRepository,
IRepository telesignalConfigRepository,
IRedisRepository telemeteringRedisRepository,
IRedisRepository telesignalisationRedisRepository,
IRepository equipmentInfoRepository,
IRepository telemeteringConfigurationRepository,
IRepository telesignalisationConfigurationRepository,
IUnitOfWorkManager unitOfWorkManager,
ISessionAppService sessionAppService,
IAppServiceConfiguration appServiceConfiguration) : base(sessionAppService, appServiceConfiguration)
{
_unitOfWorkManager = unitOfWorkManager;
_eventDrivenConfigRepository = eventDrivenConfigRepository;
_inspectionItemRepository = inspectionItemRepository;
_inspectionEventItemRepository = inspectionEventItemRepository;
_telemetryConfigRepository = telemetryConfigRepository;
_telesignalConfigRepository = telesignalConfigRepository;
_telemeteringRedisRepository = telemeteringRedisRepository;
_telesignalisationRedisRepository = telesignalisationRedisRepository;
_equipmentInfoRepository = equipmentInfoRepository;
_telemeteringConfigurationRepository = telemeteringConfigurationRepository;
_telesignalisationConfigurationRepository = telesignalisationConfigurationRepository;
_ISMSGateWayIp = appServiceConfiguration.IsmsGateWayIp;
}
#region 增/改
///
/// 创建或更新事件驱动配置
///
[HttpPost, Audited, Description("创建或更新事件驱动配置")]
[ShowApi]
[AllowAnonymous]
public async Task> CreateOrUpdateAsync(
EditSecondaryCircuitEventDrivenConfigInput input,
CancellationToken cancellationToken = default)
{
if (input == null) return null;
if (_currentUser == null)
_currentUser = base.GetCurrentUser();
return input.Id.HasValue ?
await this.UpdateAsync(input, cancellationToken).ConfigureAwait(false) :
await this.CreateAsync(input, cancellationToken).ConfigureAwait(false);
}
///
/// 创建事件驱动配置
///
private async Task> CreateAsync(
EditSecondaryCircuitEventDrivenConfigInput input,
CancellationToken cancellationToken = default)
{
var rst = new RequestResult();
try
{
// 验证配置名称是否重复(排除当前记录)
var nameExists = await _eventDrivenConfigRepository.GetAll()
.AnyAsync(x => x.Name == input.Name && x.Id != input.Id, cancellationToken);
if (nameExists)
{
rst.Message = "已存在相同名称的事件驱动配置";
return rst;
}
input.CreationTime = DateTime.Now;
input.CreatorUserId = _currentUser.Id;
SecondaryCircuitEventDrivenConfig entity = ObjectMapper.Map(input);
using var uow = _unitOfWorkManager.Begin();
entity = await _eventDrivenConfigRepository.InsertAsync(entity).ConfigureAwait(false);
uow.Complete();
// 创建巡检项关联
if (input.SecondaryCircuitInspectionEventItems != null && input.SecondaryCircuitInspectionEventItems.Count > 0)
{
var secondaryCircuitInspectionEventItems = _inspectionItemRepository.GetAll()
.Where(t => input.SecondaryCircuitInspectionEventItems.Contains(t.Id))
.Select(t => t.Id);
foreach (var item in secondaryCircuitInspectionEventItems)
{
var secondaryCircuitInspectionEventItem = new SecondaryCircuitInspectionEventItem(entity.Id, item);
_inspectionEventItemRepository.Insert(secondaryCircuitInspectionEventItem);
}
}
// 创建遥测配置关联
if (input.TelemetryConfigs != null && input.TelemetryConfigs.Count > 0)
{
foreach (var telemetryConfigId in input.TelemetryConfigs)
{
var telemetryConfig = new SecondaryCircuitInspectionTelemetryConfig
{
SecondaryCircuitEventDrivenConfigId = entity.Id,
TelemetryConfigurationId = telemetryConfigId,
IsActive = true
};
await _telemetryConfigRepository.InsertAsync(telemetryConfig).ConfigureAwait(false);
}
}
// 创建遥信配置关联
if (input.TelesignalConfigs != null && input.TelesignalConfigs.Count > 0)
{
foreach (var telesignalConfigId in input.TelesignalConfigs)
{
var telesignalConfig = new SecondaryCircuitInspectionTelesignalConfig
{
SecondaryCircuitEventDrivenConfigId = entity.Id,
TelesignalConfigurationId = telesignalConfigId,
IsActive = true
};
await _telesignalConfigRepository.InsertAsync(telesignalConfig).ConfigureAwait(false);
}
}
rst.ResultData = ObjectMapper.Map(entity);
rst.Flag = true;
}
catch (Exception ex)
{
rst.Message = ex.ToString();
Log4Helper.Error(this.GetType(), "创建事件驱动配置", ex);
}
return rst;
}
///
/// 更新事件驱动配置
///
private async Task> UpdateAsync(
EditSecondaryCircuitEventDrivenConfigInput input,
CancellationToken cancellationToken = default)
{
var rst = new RequestResult();
try
{
var entity = await _eventDrivenConfigRepository.FirstOrDefaultAsync(u => u.Id == input.Id).ConfigureAwait(false);
if (entity == null)
{
rst.Message = "事件驱动配置不存在";
return rst;
}
input.CreationTime = entity.CreationTime;
input.CreatorUserId = entity.CreatorUserId.Value;
input.LastModificationTime = DateTime.Now;
input.LastModifierUserId = _currentUser.Id;
if (!string.IsNullOrEmpty(input.Name))
{
entity.Name = input.Name;
}
if (!string.IsNullOrEmpty(input.TriggerExpression))
{
entity.TriggerExpression = input.TriggerExpression;
}
if (input.DelayTriggerSeconds.HasValue)
{
entity.DelayTriggerSeconds = input.DelayTriggerSeconds.Value;
}
if (input.MandatoryWaitSeconds.HasValue)
{
entity.MandatoryWaitSeconds = input.MandatoryWaitSeconds.Value;
}
if (input.TimeWindowSeconds.HasValue)
{
entity.TimeWindowSeconds = input.TimeWindowSeconds.Value;
}
await _eventDrivenConfigRepository.UpdateAsync(entity);
// 重新插入(仅当有数据时)
if (input.SecondaryCircuitInspectionEventItems != null && input.SecondaryCircuitInspectionEventItems.Count > 0)
{
// 更新巡检项关联(删除后重新插入)
// 删除现有关联
await _inspectionEventItemRepository.BatchDeleteAsync(t => t.InpectionEventDrivenId == entity.Id);
var secondaryCircuitInspectionEventItems = _inspectionItemRepository.GetAll()
.Where(t => input.SecondaryCircuitInspectionEventItems.Contains(t.Id))
.Select(t => t.Id);
foreach (var item in secondaryCircuitInspectionEventItems)
{
var secondaryCircuitInspectionEventItem = new SecondaryCircuitInspectionEventItem(entity.Id, item);
_inspectionEventItemRepository.Insert(secondaryCircuitInspectionEventItem);
}
}
// 重新插入遥测配置关联
if (input.TelemetryConfigs != null && input.TelemetryConfigs.Count > 0)
{
// 更新遥测配置关联(删除后重新插入)
// 删除现有遥测配置关联
await _telemetryConfigRepository.BatchDeleteAsync(t => t.SecondaryCircuitEventDrivenConfigId == entity.Id);
foreach (var telemetryConfigId in input.TelemetryConfigs)
{
var telemetryConfig = new SecondaryCircuitInspectionTelemetryConfig
{
SecondaryCircuitEventDrivenConfigId = entity.Id,
TelemetryConfigurationId = telemetryConfigId,
IsActive = true
};
await _telemetryConfigRepository.InsertAsync(telemetryConfig).ConfigureAwait(false);
}
}
// 重新插入遥信配置关联
if (input.TelesignalConfigs != null && input.TelesignalConfigs.Count > 0)
{
// 更新遥信配置关联(删除后重新插入)
// 删除现有遥信配置关联
await _telesignalConfigRepository.BatchDeleteAsync(t => t.SecondaryCircuitEventDrivenConfigId == entity.Id);
foreach (var telesignalConfigId in input.TelesignalConfigs)
{
var telesignalConfig = new SecondaryCircuitInspectionTelesignalConfig
{
SecondaryCircuitEventDrivenConfigId = entity.Id,
TelesignalConfigurationId = telesignalConfigId,
IsActive = true
};
await _telesignalConfigRepository.InsertAsync(telesignalConfig).ConfigureAwait(false);
}
}
rst.ResultData = ObjectMapper.Map(entity);
rst.Flag = true;
}
catch (Exception ex)
{
rst.Message = ex.ToString();
Log4Helper.Error(this.GetType(), "更新事件驱动配置", ex);
}
return rst;
}
#endregion
#region 查
///
/// 查询所有事件驱动配置(不分页)
///
[HttpPost, Description("查询所有事件驱动配置(不分页)")]
[ShowApi]
[AllowAnonymous]
public RequestResult> GetListAsync(
SecondaryCircuitEventDrivenConfigSearchInput searchCondition)
{
var rst = new RequestResult>();
try
{
var query = _eventDrivenConfigRepository.GetAll()
.Include(x => x.SecondaryCircuitInspectionEventItems)
.ThenInclude(t => t.InspectionItem)
.ThenInclude(t => t.TelemetryConfigs)
.ThenInclude(t => t.TelemeteringConfiguration)
.Include(x => x.SecondaryCircuitInspectionEventItems)
.ThenInclude(t => t.InspectionItem)
.ThenInclude(t => t.TelesignalConfigs)
.ThenInclude(t => t.TelesignalisationConfiguration)
.Include(x => x.TelemetryConfigs)
.ThenInclude(t => t.TelemeteringConfiguration)
.Include(x => x.TelesignalConfigs)
.ThenInclude(t => t.TelesignalisationConfiguration)
.Where(x => x.IsActive);
// 提前加载所有设备信息并转换为字典以提高查询效率
var equipmentInfoDict = _equipmentInfoRepository.GetAll()
.ToDictionary(e => e.Id, e => e.Name);
// 应用搜索条件
if (searchCondition != null)
{
if (!string.IsNullOrWhiteSpace(searchCondition.Name))
{
query = query.Where(x => x.Name.Contains(searchCondition.Name) ||
x.Remark.Contains(searchCondition.Name));
}
if (searchCondition.Priority.HasValue)
{
query = query.Where(x => x.Priority == searchCondition.Priority.Value);
}
}
var entities = query.OrderBy(x => x.SortOrder).ThenBy(x => x.Name).ToList();
// 映射到输出DTO
var outputList = new List();
foreach (var entity in entities)
{
var output = ObjectMapper.Map(entity);
output.SecondaryCircuitInspectionEventItems = ObjectMapper.Map>(entity.SecondaryCircuitInspectionEventItems.Select(t => t.InspectionItem));
// Enrich nested inspection items with telemetry and telesignal config details
foreach (var eventItem in entity.SecondaryCircuitInspectionEventItems)
{
if (eventItem.InspectionItem == null) continue;
var outputItem = output.SecondaryCircuitInspectionEventItems.FirstOrDefault(x => x.Id == eventItem.InspectionItem.Id);
if (outputItem == null) continue;
// Enrich telemetry configs for nested inspection item
if (eventItem.InspectionItem.TelemetryConfigs != null && eventItem.InspectionItem.TelemetryConfigs.Any())
{
outputItem.TelemetryConfigs = eventItem.InspectionItem.TelemetryConfigs.Select(tc => new SecondaryCircuitInspectionTelemetryConfigOutput
{
Id = tc.Id,
EquipmentInfoId = tc.TelemeteringConfiguration.EquipmentInfoId.Value,
EquipmentInfoName = equipmentInfoDict.ContainsKey(tc.TelemeteringConfiguration.EquipmentInfoId.Value)
? equipmentInfoDict[tc.TelemeteringConfiguration.EquipmentInfoId.Value]
: string.Empty,
TelemetryConfigurationId = tc.TelemetryConfigurationId ?? Guid.Empty,
TelemetryConfigurationName = tc.TelemeteringConfiguration?.Name,
TelemetryConfigurationIsmsId = tc.TelemeteringConfiguration?.ismsbaseYCId
}).ToList();
outputItem.TelemetryConfigCount = outputItem.TelemetryConfigs.Count;
}
// Enrich telesignal configs for nested inspection item
if (eventItem.InspectionItem.TelesignalConfigs != null && eventItem.InspectionItem.TelesignalConfigs.Any())
{
outputItem.TelesignalConfigs = eventItem.InspectionItem.TelesignalConfigs.Select(tc => new SecondaryCircuitInspectionTelesignalConfigOutput
{
Id = tc.Id,
EquipmentInfoId = tc.TelesignalisationConfiguration.EquipmentInfoId.Value,
EquipmentInfoName = equipmentInfoDict.ContainsKey(tc.TelesignalisationConfiguration.EquipmentInfoId.Value)
? equipmentInfoDict[tc.TelesignalisationConfiguration.EquipmentInfoId.Value]
: string.Empty,
TelesignalConfigurationId = tc.TelesignalConfigurationId ?? Guid.Empty,
TelesignalConfigurationName = tc.TelesignalisationConfiguration?.Name,
TelesignalConfigurationIsmsId = tc.TelesignalisationConfiguration?.ismsbaseYXId
}).ToList();
outputItem.TelesignalConfigCount = outputItem.TelesignalConfigs.Count;
}
}
// 映射遥测配置关联
if (entity.TelemetryConfigs != null && entity.TelemetryConfigs.Any())
{
output.TelemetryConfigs = entity.TelemetryConfigs.Select(tc => new SecondaryCircuitInspectionTelemetryConfigOutput
{
Id = tc.Id,
EquipmentInfoId = tc.TelemeteringConfiguration.EquipmentInfoId.Value,
EquipmentInfoName = equipmentInfoDict.ContainsKey(tc.TelemeteringConfiguration.EquipmentInfoId.Value)
? equipmentInfoDict[tc.TelemeteringConfiguration.EquipmentInfoId.Value]
: string.Empty,
TelemetryConfigurationId = tc.TelemetryConfigurationId ?? Guid.Empty,
TelemetryConfigurationName = tc.TelemeteringConfiguration?.Name,
TelemetryConfigurationIsmsId = tc.TelemeteringConfiguration?.ismsbaseYCId
}).ToList();
}
else
{
output.TelemetryConfigs = new List();
}
// 映射遥信配置关联
if (entity.TelesignalConfigs != null && entity.TelesignalConfigs.Any())
{
output.TelesignalConfigs = entity.TelesignalConfigs.Select(tc => new SecondaryCircuitInspectionTelesignalConfigOutput
{
Id = tc.Id,
EquipmentInfoId = tc.TelesignalisationConfiguration.EquipmentInfoId.Value,
EquipmentInfoName = equipmentInfoDict.ContainsKey(tc.TelesignalisationConfiguration.EquipmentInfoId.Value)
? equipmentInfoDict[tc.TelesignalisationConfiguration.EquipmentInfoId.Value]
: string.Empty,
TelesignalConfigurationId = tc.TelesignalConfigurationId ?? Guid.Empty,
TelesignalConfigurationName = tc.TelesignalisationConfiguration?.Name,
TelesignalConfigurationIsmsId = tc.TelesignalisationConfiguration?.ismsbaseYXId
}).ToList();
}
else
{
output.TelesignalConfigs = new List();
}
outputList.Add(output);
}
rst.ResultData = outputList;
rst.Flag = true;
}
catch (Exception ex)
{
rst.Message = ex.Message;
Log4Helper.Error(this.GetType(), "查询所有事件驱动配置(不分页)", ex);
}
return rst;
}
#endregion
#region 删
///
/// 删除事件驱动配置
///
[HttpGet, Description("删除事件驱动配置")]
[ShowApi]
[AllowAnonymous]
public async Task DeleteAsync(
Guid id,
CancellationToken cancellationToken = default)
{
var rst = new RequestEasyResult();
try
{
var entity = await _eventDrivenConfigRepository.FirstOrDefaultAsync(u => u.Id == id).ConfigureAwait(false);
if (entity == null)
{
rst.Message = "事件驱动配置不存在";
return rst;
}
//var items = _inspectionEventItemRepository.GetAll().Where(t => t.InpectionEventDrivenId == id).Select(t=>t.Id);
//if (items!=null&& items.Any())
//{
// _inspectionEventItemRepository.BatchDeleteAsync(t=>);
// foreach (var item in items)
// {
// }
//}
await _eventDrivenConfigRepository.DeleteAsync(entity).ConfigureAwait(false);
rst.Flag = true;
}
catch (Exception ex)
{
rst.Message = ex.ToString();
Log4Helper.Error(this.GetType(), "删除事件驱动配置", ex);
}
return rst;
}
///
/// 批量删除事件驱动配置
///
[HttpPost, Description("批量删除事件驱动配置")]
[ShowApi]
public async Task BatchDeleteAsync(
List ids,
CancellationToken cancellationToken = default)
{
var rst = new RequestEasyResult();
try
{
if (ids == null || !ids.Any())
{
rst.Message = "请选择要删除的配置";
return rst;
}
var entities = await _eventDrivenConfigRepository.GetAll()
.Where(x => ids.Contains(x.Id))
.ToListAsync(cancellationToken);
foreach (var entity in entities)
{
await _eventDrivenConfigRepository.DeleteAsync(entity);
}
rst.Flag = true;
rst.Message = $"成功删除 {entities.Count} 个事件驱动配置";
}
catch (Exception ex)
{
rst.Message = ex.ToString();
Log4Helper.Error(this.GetType(), "批量删除事件驱动配置", ex);
}
return rst;
}
#endregion
#region 其他操作
///
/// 启用/禁用事件驱动配置
///
[HttpPost, Description("启用/禁用事件驱动配置")]
[ShowApi]
public async Task SetActiveStatusAsync(
Guid id,
bool isActive,
CancellationToken cancellationToken = default)
{
var rst = new RequestEasyResult();
try
{
var entity = await _eventDrivenConfigRepository.FirstOrDefaultAsync(u => u.Id == id).ConfigureAwait(false);
if (entity == null)
{
rst.Message = "事件驱动配置不存在";
return rst;
}
entity.IsActive = isActive;
await _eventDrivenConfigRepository.UpdateAsync(entity);
rst.Flag = true;
rst.Message = isActive ? "启用成功" : "禁用成功";
}
catch (Exception ex)
{
rst.Message = ex.ToString();
Log4Helper.Error(this.GetType(), "启用/禁用事件驱动配置", ex);
}
return rst;
}
#endregion
#region 事件驱动巡检项管理
///
/// 获取事件驱动配置的巡检项列表
///
[HttpPost, Description("获取事件驱动配置的巡检项列表")]
[ShowApi]
public async Task>> GetEventItemsAsync(
Guid eventDrivenConfigId,
CancellationToken cancellationToken = default)
{
var rst = new RequestResult>();
try
{
var items = await _inspectionEventItemRepository.GetAll()
.Include(x => x.InspectionItem)
.Where(x => x.InpectionEventDrivenId == eventDrivenConfigId)
.ToListAsync(cancellationToken);
rst.ResultData = ObjectMapper.Map>(items);
rst.Flag = true;
}
catch (Exception ex)
{
rst.Message = ex.Message;
Log4Helper.Error(this.GetType(), "获取事件驱动配置的巡检项列表", ex);
}
return rst;
}
///
/// 删除事件驱动配置的巡检项
///
[HttpPost, Description("删除事件驱动配置的巡检项")]
[ShowApi]
public async Task RemoveEventItemAsync(
Guid eventItemId,
CancellationToken cancellationToken = default)
{
var rst = new RequestEasyResult();
try
{
await _inspectionEventItemRepository.DeleteAsync(eventItemId);
await CurrentUnitOfWork.SaveChangesAsync();
rst.Flag = true;
rst.Message = "删除成功";
}
catch (Exception ex)
{
rst.Message = ex.ToString();
Log4Helper.Error(this.GetType(), "删除事件驱动配置的巡检项", ex);
}
return rst;
}
#endregion
#region 获取实时数据
///
/// 获取事件驱动配置关联的遥测实时数据
///
/// 事件驱动配置ID
/// 遥测实时数据列表
[HttpGet, Audited]
[ShowApi]
[AllowAnonymous]
public async Task>> GetTelemetryInfoData(Guid eventDrivenConfigId)
{
RequestResult> rst = new RequestResult>();
try
{
// 获取事件驱动配置以获取时间窗口设置
var eventDrivenConfig = await _eventDrivenConfigRepository.FirstOrDefaultAsync(e => e.Id == eventDrivenConfigId);
if (eventDrivenConfig == null)
{
rst.Message = "事件驱动配置不存在";
return rst;
}
// 查询所有关联的巡检事件项
var eventItems = await _inspectionEventItemRepository.GetAll()
.Include(t => t.InspectionItem)
.ThenInclude(t => t.TelemetryConfigs)
.ThenInclude(t => t.TelemeteringConfiguration)
.ThenInclude(t => t.EquipmentInfo)
.Where(t => t.InpectionEventDrivenId == eventDrivenConfigId && t.IsActive)
.ToListAsync();
if (eventItems == null || !eventItems.Any())
{
rst.Message = "未找到关联的巡检项";
rst.Flag = true;
rst.ResultData = new List();
return rst;
}
// 获取所有遥测配置(仅综自数据源)
var telemetryConfigs = _telemeteringConfigurationRepository.GetAll()
.Where(t => t.DataSourceCategory == DataSourceCategoryEnum.Zongzi);
string url = $"{_ISMSGateWayIp}/api/CallYCByDataId";
var ids = new List();
var equipmentNameMap = new Dictionary();
var telemetryNameMap = new Dictionary();
// 收集所有遥测ID
foreach (var eventItem in eventItems)
{
if (eventItem.InspectionItem?.TelemetryConfigs != null && eventItem.InspectionItem.TelemetryConfigs.Any())
{
foreach (var item in eventItem.InspectionItem.TelemetryConfigs)
{
var telemetryConfig = telemetryConfigs.FirstOrDefault(t => t.Id == item.TelemetryConfigurationId);
if (telemetryConfig != null && !string.IsNullOrEmpty(telemetryConfig.ismsbaseYCId))
{
if (!ids.Contains(telemetryConfig.ismsbaseYCId))
{
ids.Add(telemetryConfig.ismsbaseYCId);
}
// 映射设备名称
if (!equipmentNameMap.ContainsKey(telemetryConfig.ismsbaseYCId))
{
equipmentNameMap[telemetryConfig.ismsbaseYCId] =
item.TelemeteringConfiguration?.EquipmentInfo?.Name ?? string.Empty;
}
// 映射遥测名称
if (!telemetryNameMap.ContainsKey(telemetryConfig.ismsbaseYCId))
{
telemetryNameMap[telemetryConfig.ismsbaseYCId] =
item.TelemeteringConfiguration?.Name ?? string.Empty;
}
}
}
}
}
if (ids.Count == 0)
{
rst.Message = "未找到有效的遥测配置";
rst.Flag = true;
rst.ResultData = new List();
return rst;
}
var obj = new
{
id = ids,
times = eventDrivenConfig.TimeWindowSeconds,
timeWindowType = TimeWindowTypeEnum.Both // 使用前后时间窗口
};
var str = HttpHelper.HttpPostRequest(url, obj);
if (str != null)
{
if (bool.Parse(str["success"].ToString()) == true)
{
List telemetryInfoOutputs = str["data"].ToObject>();
// 填充设备名称
foreach (var telemetryInfo in telemetryInfoOutputs)
{
if (equipmentNameMap.ContainsKey(telemetryInfo.id))
{
telemetryInfo.deviceName = equipmentNameMap[telemetryInfo.id];
}
}
rst.ResultData = telemetryInfoOutputs;
rst.Flag = true;
}
else
{
rst.Message = "API返回失败: " + str["message"]?.ToString();
}
}
else
{
rst.Message = "HTTP请求失败";
}
}
catch (Exception ex)
{
rst.Message = ex.Message;
Log4Helper.Error(this.GetType(), "获取事件驱动配置关联的遥测实时数据", ex);
}
return rst;
}
///
/// 获取事件驱动配置关联的遥信实时数据
///
/// 事件驱动配置ID
/// 遥信实时数据列表
[HttpGet, Audited]
[ShowApi]
[AllowAnonymous]
public async Task>> GetTeleSignalData(Guid eventDrivenConfigId)
{
RequestResult> rst = new RequestResult>();
try
{
// 获取事件驱动配置以获取时间窗口设置
var eventDrivenConfig = await _eventDrivenConfigRepository.FirstOrDefaultAsync(e => e.Id == eventDrivenConfigId);
if (eventDrivenConfig == null)
{
rst.Message = "事件驱动配置不存在";
return rst;
}
// 查询所有关联的巡检事件项
var eventItems = await _inspectionEventItemRepository.GetAll()
.Include(t => t.InspectionItem)
.ThenInclude(t => t.TelesignalConfigs)
.ThenInclude(t => t.TelesignalisationConfiguration)
.ThenInclude(t => t.EquipmentInfo)
.Where(t => t.InpectionEventDrivenId == eventDrivenConfigId && t.IsActive)
.ToListAsync();
if (eventItems == null || !eventItems.Any())
{
rst.Message = "未找到关联的巡检项";
rst.Flag = true;
rst.ResultData = new List();
return rst;
}
// 获取所有遥信配置(仅综自数据源)
var telesignalConfigs = _telesignalisationConfigurationRepository.GetAll()
.Where(t => t.DataSourceCategory == DataSourceCategoryEnum.Zongzi);
string url = $"{_ISMSGateWayIp}/api/CallYXByDataId";
var ids = new List();
var equipmentNameMap = new Dictionary();
var telesignalNameMap = new Dictionary();
// 收集所有遥信ID
foreach (var eventItem in eventItems)
{
if (eventItem.InspectionItem?.TelesignalConfigs != null && eventItem.InspectionItem.TelesignalConfigs.Any())
{
foreach (var item in eventItem.InspectionItem.TelesignalConfigs)
{
var telesignalConfig = telesignalConfigs.FirstOrDefault(t => t.Id == item.TelesignalConfigurationId);
if (telesignalConfig != null && !string.IsNullOrEmpty(telesignalConfig.ismsbaseYXId))
{
if (!ids.Contains(telesignalConfig.ismsbaseYXId))
{
ids.Add(telesignalConfig.ismsbaseYXId);
}
// 映射设备名称
if (!equipmentNameMap.ContainsKey(telesignalConfig.ismsbaseYXId))
{
equipmentNameMap[telesignalConfig.ismsbaseYXId] =
item.TelesignalisationConfiguration?.EquipmentInfo?.Name ?? string.Empty;
}
// 映射遥信名称
if (!telesignalNameMap.ContainsKey(telesignalConfig.ismsbaseYXId))
{
telesignalNameMap[telesignalConfig.ismsbaseYXId] =
item.TelesignalisationConfiguration?.Name ?? string.Empty;
}
}
}
}
}
if (ids.Count == 0)
{
rst.Message = "未找到有效的遥信配置";
rst.Flag = true;
rst.ResultData = new List();
return rst;
}
var obj = new
{
id = ids,
times = eventDrivenConfig.TimeWindowSeconds,
timeWindowType = TimeWindowTypeEnum.Both // 使用前后时间窗口
};
var str = HttpHelper.HttpPostRequest(url, obj);
if (str != null)
{
if (bool.Parse(str["success"].ToString()) == true)
{
List telesignalInfoOutputs = str["data"].ToObject>();
// 填充设备名称
foreach (var telesignalInfo in telesignalInfoOutputs)
{
if (equipmentNameMap.ContainsKey(telesignalInfo.id))
{
telesignalInfo.deviceName = equipmentNameMap[telesignalInfo.id];
}
}
rst.ResultData = telesignalInfoOutputs;
rst.Flag = true;
}
else
{
rst.Message = "API返回失败: " + str["message"]?.ToString();
}
}
else
{
rst.Message = "HTTP请求失败";
}
}
catch (Exception ex)
{
rst.Message = ex.Message;
Log4Helper.Error(this.GetType(), "获取事件驱动配置关联的遥信实时数据", ex);
}
return rst;
}
#endregion
#region 私有方法
public RequestPageResult FindDatas(PageSearchCondition searchCondition)
{
throw new NotImplementedException();
}
#endregion
#region 辅助类
///
/// 事件驱动巡检项输出DTO
///
public class SecondaryCircuitInspectionEventItemOutput
{
public Guid Id { get; set; }
public Guid? InpectionEventDrivenId { get; set; }
public Guid? InspectionItemId { get; set; }
public string InspectionItemName { get; set; }
public int ExecutionOrder { get; set; }
public bool IsActive { get; set; }
public decimal WeightCoefficient { get; set; }
public DateTime? LastExecutionTime { get; set; }
public SecondaryCircuitInspectionResultStatus LastExecutionResult { get; set; }
public string LastExecutionErrorMessage { get; set; }
public int TotalExecutionCount { get; set; }
public int SuccessExecutionCount { get; set; }
public int AbnormalExecutionCount { get; set; }
public int ErrorExecutionCount { get; set; }
public long AverageExecutionDurationMs { get; set; }
}
#endregion
}
}