971 lines
43 KiB
C#
971 lines
43 KiB
C#
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;
|
||
using YunDa.SOMS.Application.DataMonitoring.SecondaryCircuitInspection.Test;
|
||
|
||
namespace YunDa.SOMS.Application.DataMonitoring.SecondaryCircuitInspection
|
||
{
|
||
/// <summary>
|
||
/// 二次回路事件驱动配置应用服务实现
|
||
/// </summary>
|
||
[Description("二次回路事件驱动配置管理服务")]
|
||
public class SecondaryCircuitEventDrivenConfigAppService : SOMSAppServiceBase, ISecondaryCircuitEventDrivenConfigAppService
|
||
{
|
||
private readonly IRepository<SecondaryCircuitEventDrivenConfig, Guid> _eventDrivenConfigRepository;
|
||
private readonly IRepository<SecondaryCircuitInspectionItem, Guid> _inspectionItemRepository;
|
||
private readonly IRepository<SecondaryCircuitInspectionEventItem, Guid> _inspectionEventItemRepository;
|
||
private readonly IRepository<SecondaryCircuitInspectionTelemetryConfig, Guid> _telemetryConfigRepository;
|
||
private readonly IRepository<SecondaryCircuitInspectionTelesignalConfig, Guid> _telesignalConfigRepository;
|
||
private readonly IRedisRepository<TelemeteringModel, string> _telemeteringRedisRepository;
|
||
private readonly IRedisRepository<TelesignalisationModel, string> _telesignalisationRedisRepository;
|
||
private readonly IRepository<EquipmentInfo, Guid> _equipmentInfoRepository;
|
||
private LoginUserOutput _currentUser = null;
|
||
private IUnitOfWorkManager _unitOfWorkManager = null;
|
||
private string _ISMSGateWayIp = "http://127.0.0.1:38094";
|
||
|
||
// 测试数据服务(懒加载)
|
||
private SecondaryCircuitInspectionTestDataService _testDataService;
|
||
private SecondaryCircuitInspectionTestDataService TestDataService
|
||
{
|
||
get
|
||
{
|
||
if (_testDataService == null)
|
||
{
|
||
_testDataService = new SecondaryCircuitInspectionTestDataService();
|
||
}
|
||
return _testDataService;
|
||
}
|
||
}
|
||
|
||
public SecondaryCircuitEventDrivenConfigAppService(
|
||
IRepository<SecondaryCircuitEventDrivenConfig, Guid> eventDrivenConfigRepository,
|
||
IRepository<SecondaryCircuitInspectionItem, Guid> inspectionItemRepository,
|
||
IRepository<SecondaryCircuitInspectionEventItem, Guid> inspectionEventItemRepository,
|
||
IRepository<SecondaryCircuitInspectionTelemetryConfig, Guid> telemetryConfigRepository,
|
||
IRepository<SecondaryCircuitInspectionTelesignalConfig, Guid> telesignalConfigRepository,
|
||
IRedisRepository<TelemeteringModel, string> telemeteringRedisRepository,
|
||
IRedisRepository<TelesignalisationModel, string> telesignalisationRedisRepository,
|
||
IRepository<EquipmentInfo, Guid> equipmentInfoRepository,
|
||
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;
|
||
_ISMSGateWayIp = appServiceConfiguration.IsmsGateWayIp;
|
||
}
|
||
|
||
#region 增/改
|
||
|
||
/// <summary>
|
||
/// 创建或更新事件驱动配置
|
||
/// </summary>
|
||
[HttpPost, Audited, Description("创建或更新事件驱动配置")]
|
||
[ShowApi]
|
||
[AllowAnonymous]
|
||
public async Task<RequestResult<SecondaryCircuitEventDrivenConfigOutput>> 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);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 创建事件驱动配置
|
||
/// </summary>
|
||
private async Task<RequestResult<SecondaryCircuitEventDrivenConfigOutput>> CreateAsync(
|
||
EditSecondaryCircuitEventDrivenConfigInput input,
|
||
CancellationToken cancellationToken = default)
|
||
|
||
{
|
||
var rst = new RequestResult<SecondaryCircuitEventDrivenConfigOutput>();
|
||
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<SecondaryCircuitEventDrivenConfig>(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<SecondaryCircuitEventDrivenConfigOutput>(entity);
|
||
rst.Flag = true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
rst.Message = ex.ToString();
|
||
Log4Helper.Error(this.GetType(), "创建事件驱动配置", ex);
|
||
}
|
||
return rst;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新事件驱动配置
|
||
/// </summary>
|
||
private async Task<RequestResult<SecondaryCircuitEventDrivenConfigOutput>> UpdateAsync(
|
||
EditSecondaryCircuitEventDrivenConfigInput input,
|
||
CancellationToken cancellationToken = default)
|
||
{
|
||
var rst = new RequestResult<SecondaryCircuitEventDrivenConfigOutput>();
|
||
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 )
|
||
{
|
||
// 更新巡检项关联(删除后重新插入)
|
||
// 删除现有关联
|
||
await _inspectionEventItemRepository.BatchDeleteAsync(t => t.InpectionEventDrivenId == entity.Id);
|
||
if (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 )
|
||
{
|
||
// 更新遥测配置关联(删除后重新插入)
|
||
// 删除现有遥测配置关联
|
||
await _telemetryConfigRepository.BatchDeleteAsync(t => t.SecondaryCircuitEventDrivenConfigId == entity.Id);
|
||
if (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 )
|
||
{
|
||
// 更新遥信配置关联(删除后重新插入)
|
||
// 删除现有遥信配置关联
|
||
await _telesignalConfigRepository.BatchDeleteAsync(t => t.SecondaryCircuitEventDrivenConfigId == entity.Id);
|
||
if (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<SecondaryCircuitEventDrivenConfigOutput>(entity);
|
||
rst.Flag = true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
rst.Message = ex.ToString();
|
||
Log4Helper.Error(this.GetType(), "更新事件驱动配置", ex);
|
||
}
|
||
return rst;
|
||
}
|
||
|
||
|
||
|
||
#endregion
|
||
|
||
#region 查
|
||
|
||
/// <summary>
|
||
/// 查询所有事件驱动配置(不分页)
|
||
/// </summary>
|
||
[HttpPost, Description("查询所有事件驱动配置(不分页)")]
|
||
[ShowApi]
|
||
[AllowAnonymous]
|
||
public RequestResult<List<SecondaryCircuitEventDrivenConfigOutput>> GetListAsync(
|
||
SecondaryCircuitEventDrivenConfigSearchInput searchCondition)
|
||
{
|
||
var rst = new RequestResult<List<SecondaryCircuitEventDrivenConfigOutput>>();
|
||
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<SecondaryCircuitEventDrivenConfigOutput>();
|
||
foreach (var entity in entities)
|
||
{
|
||
var output = ObjectMapper.Map<SecondaryCircuitEventDrivenConfigOutput>(entity);
|
||
output.SecondaryCircuitInspectionEventItems = ObjectMapper.Map<List<SecondaryCircuitInspectionItemOutput>>(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<SecondaryCircuitInspectionTelemetryConfigOutput>();
|
||
}
|
||
|
||
// 映射遥信配置关联
|
||
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<SecondaryCircuitInspectionTelesignalConfigOutput>();
|
||
}
|
||
|
||
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 删
|
||
|
||
/// <summary>
|
||
/// 删除事件驱动配置
|
||
/// </summary>
|
||
[HttpGet, Description("删除事件驱动配置")]
|
||
[ShowApi]
|
||
[AllowAnonymous]
|
||
public async Task<RequestEasyResult> 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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 批量删除事件驱动配置
|
||
/// </summary>
|
||
[HttpPost, Description("批量删除事件驱动配置")]
|
||
[ShowApi]
|
||
public async Task<RequestEasyResult> BatchDeleteAsync(
|
||
List<Guid> 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 其他操作
|
||
|
||
/// <summary>
|
||
/// 启用/禁用事件驱动配置
|
||
/// </summary>
|
||
[HttpPost, Description("启用/禁用事件驱动配置")]
|
||
[ShowApi]
|
||
public async Task<RequestEasyResult> 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 事件驱动巡检项管理
|
||
|
||
/// <summary>
|
||
/// 获取事件驱动配置的巡检项列表
|
||
/// </summary>
|
||
[HttpPost, Description("获取事件驱动配置的巡检项列表")]
|
||
[ShowApi]
|
||
public async Task<RequestResult<List<SecondaryCircuitInspectionEventItemOutput>>> GetEventItemsAsync(
|
||
Guid eventDrivenConfigId,
|
||
CancellationToken cancellationToken = default)
|
||
{
|
||
var rst = new RequestResult<List<SecondaryCircuitInspectionEventItemOutput>>();
|
||
try
|
||
{
|
||
var items = await _inspectionEventItemRepository.GetAll()
|
||
.Include(x => x.InspectionItem)
|
||
.Where(x => x.InpectionEventDrivenId == eventDrivenConfigId)
|
||
|
||
.ToListAsync(cancellationToken);
|
||
|
||
rst.ResultData = ObjectMapper.Map<List<SecondaryCircuitInspectionEventItemOutput>>(items);
|
||
rst.Flag = true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
rst.Message = ex.Message;
|
||
Log4Helper.Error(this.GetType(), "获取事件驱动配置的巡检项列表", ex);
|
||
}
|
||
return rst;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 删除事件驱动配置的巡检项
|
||
/// </summary>
|
||
[HttpPost, Description("删除事件驱动配置的巡检项")]
|
||
[ShowApi]
|
||
public async Task<RequestEasyResult> 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 获取实时数据
|
||
|
||
/// <summary>
|
||
/// 获取事件驱动配置关联的遥测实时数据
|
||
/// </summary>
|
||
/// <param name="eventDrivenConfigId">事件驱动配置ID</param>
|
||
/// <returns>遥测实时数据列表</returns>
|
||
[HttpGet, Audited]
|
||
[ShowApi]
|
||
[AllowAnonymous]
|
||
public async Task<RequestResult<List<TelemetryInfoOutput>>> GetTelemetryInfoData(Guid eventDrivenConfigId)
|
||
{
|
||
RequestResult<List<TelemetryInfoOutput>> rst = new RequestResult<List<TelemetryInfoOutput>>();
|
||
try
|
||
{
|
||
// 获取事件驱动配置以获取时间窗口设置
|
||
var eventDrivenConfig = await _eventDrivenConfigRepository.FirstOrDefaultAsync(e => e.Id == eventDrivenConfigId);
|
||
if (eventDrivenConfig == null)
|
||
{
|
||
rst.Message = "事件驱动配置不存在";
|
||
return rst;
|
||
}
|
||
|
||
// 获取所有遥测配置(仅综自数据源)
|
||
var telemetryConfigs = await _telemetryConfigRepository.GetAll()
|
||
.Include(t => t.TelemeteringConfiguration)
|
||
.ThenInclude(t => t.EquipmentInfo)
|
||
.Where(t => t.SecondaryCircuitEventDrivenConfigId == eventDrivenConfigId)
|
||
.Where(t => t.DataSourceCategory == DataSourceCategoryEnum.Zongzi)
|
||
.ToListAsync();
|
||
|
||
string url = $"{_ISMSGateWayIp}/api/CallYCByDataId";
|
||
var ids = new List<string>();
|
||
var equipmentNameMap = new Dictionary<string, string>();
|
||
var telemetryNameMap = new Dictionary<string, string>();
|
||
|
||
// 收集所有遥测ID
|
||
foreach (var telemetryConfig in telemetryConfigs)
|
||
{
|
||
if (telemetryConfig.TelemeteringConfiguration != null &&
|
||
!string.IsNullOrEmpty(telemetryConfig.TelemeteringConfiguration.ismsbaseYCId))
|
||
{
|
||
var ismsId = telemetryConfig.TelemeteringConfiguration.ismsbaseYCId;
|
||
|
||
if (!ids.Contains(ismsId))
|
||
{
|
||
ids.Add(ismsId);
|
||
}
|
||
|
||
// 映射设备名称
|
||
if (!equipmentNameMap.ContainsKey(ismsId))
|
||
{
|
||
equipmentNameMap[ismsId] =
|
||
telemetryConfig.TelemeteringConfiguration.EquipmentInfo?.Name ?? string.Empty;
|
||
}
|
||
|
||
// 映射遥测名称
|
||
if (!telemetryNameMap.ContainsKey(ismsId))
|
||
{
|
||
telemetryNameMap[ismsId] =
|
||
telemetryConfig.TelemeteringConfiguration.Name ?? string.Empty;
|
||
}
|
||
}
|
||
}
|
||
|
||
if (ids.Count == 0)
|
||
{
|
||
rst.Message = "未找到有效的遥测配置";
|
||
rst.Flag = true;
|
||
rst.ResultData = new List<TelemetryInfoOutput>();
|
||
return rst;
|
||
}
|
||
|
||
var obj = new
|
||
{
|
||
id = ids,
|
||
times = eventDrivenConfig.TimeWindowSeconds,
|
||
timeWindowType = TimeWindowTypeEnum.Both // 使用前后时间窗口
|
||
};
|
||
|
||
var str = HttpHelper.HttpPostRequest<JObject>(url, obj);
|
||
if (str != null)
|
||
{
|
||
if (bool.Parse(str["success"].ToString()) == true)
|
||
{
|
||
List<TelemetryInfoOutput> telemetryInfoOutputs = str["data"].ToObject<List<TelemetryInfoOutput>>();
|
||
// 填充设备名称
|
||
foreach (var telemetryInfo in telemetryInfoOutputs)
|
||
{
|
||
if (equipmentNameMap.ContainsKey(telemetryInfo.id))
|
||
{
|
||
telemetryInfo.deviceName = equipmentNameMap[telemetryInfo.id];
|
||
}
|
||
}
|
||
rst.ResultData = telemetryInfoOutputs;
|
||
rst.Flag = true;
|
||
}
|
||
else
|
||
{
|
||
// API返回失败,使用测试数据
|
||
rst.ResultData = TestDataService.GenerateTelemetryTestData(
|
||
ids,
|
||
telemetryNameMap,
|
||
eventDrivenConfig.TimeWindowSeconds,
|
||
TimeWindowTypeEnum.Both,
|
||
-1,
|
||
equipmentNameMap);
|
||
rst.Flag = true;
|
||
rst.Message = "使用测试数据(API返回失败)";
|
||
}
|
||
}
|
||
else
|
||
{
|
||
// HTTP请求失败,使用测试数据
|
||
rst.ResultData = TestDataService.GenerateTelemetryTestData(
|
||
ids,
|
||
telemetryNameMap,
|
||
eventDrivenConfig.TimeWindowSeconds,
|
||
TimeWindowTypeEnum.Both,
|
||
-1,
|
||
equipmentNameMap);
|
||
rst.Flag = true;
|
||
rst.Message = "使用测试数据(HTTP请求失败)";
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
rst.Message = ex.Message;
|
||
Log4Helper.Error(this.GetType(), "获取事件驱动配置关联的遥测实时数据", ex);
|
||
}
|
||
return rst;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取事件驱动配置关联的遥信实时数据
|
||
/// </summary>
|
||
/// <param name="eventDrivenConfigId">事件驱动配置ID</param>
|
||
/// <returns>遥信实时数据列表</returns>
|
||
[HttpGet, Audited]
|
||
[ShowApi]
|
||
[AllowAnonymous]
|
||
public async Task<RequestResult<List<TeleSignalOutput>>> GetTeleSignalData(Guid eventDrivenConfigId)
|
||
{
|
||
RequestResult<List<TeleSignalOutput>> rst = new RequestResult<List<TeleSignalOutput>>();
|
||
try
|
||
{
|
||
// 获取事件驱动配置以获取时间窗口设置
|
||
var eventDrivenConfig = await _eventDrivenConfigRepository.FirstOrDefaultAsync(e => e.Id == eventDrivenConfigId);
|
||
if (eventDrivenConfig == null)
|
||
{
|
||
rst.Message = "事件驱动配置不存在";
|
||
return rst;
|
||
}
|
||
|
||
// 获取所有遥信配置(仅综自数据源)
|
||
var telesignalConfigs = await _telesignalConfigRepository.GetAll()
|
||
.Include(t => t.TelesignalisationConfiguration)
|
||
.ThenInclude(t => t.EquipmentInfo)
|
||
.Where(t => t.SecondaryCircuitEventDrivenConfigId == eventDrivenConfigId)
|
||
.Where(t => t.DataSourceCategory == DataSourceCategoryEnum.Zongzi)
|
||
.ToListAsync();
|
||
|
||
string url = $"{_ISMSGateWayIp}/api/CallYXByDataId";
|
||
var ids = new List<string>();
|
||
var equipmentNameMap = new Dictionary<string, string>();
|
||
var telesignalNameMap = new Dictionary<string, string>();
|
||
|
||
// 收集所有遥信ID
|
||
foreach (var telesignalConfig in telesignalConfigs)
|
||
{
|
||
if (telesignalConfig.TelesignalisationConfiguration != null &&
|
||
!string.IsNullOrEmpty(telesignalConfig.TelesignalisationConfiguration.ismsbaseYXId))
|
||
{
|
||
var ismsId = telesignalConfig.TelesignalisationConfiguration.ismsbaseYXId;
|
||
|
||
if (!ids.Contains(ismsId))
|
||
{
|
||
ids.Add(ismsId);
|
||
}
|
||
|
||
// 映射设备名称
|
||
if (!equipmentNameMap.ContainsKey(ismsId))
|
||
{
|
||
equipmentNameMap[ismsId] =
|
||
telesignalConfig.TelesignalisationConfiguration.EquipmentInfo?.Name ?? string.Empty;
|
||
}
|
||
|
||
// 映射遥信名称
|
||
if (!telesignalNameMap.ContainsKey(ismsId))
|
||
{
|
||
telesignalNameMap[ismsId] =
|
||
telesignalConfig.TelesignalisationConfiguration.Name ?? string.Empty;
|
||
}
|
||
}
|
||
}
|
||
|
||
if (ids.Count == 0)
|
||
{
|
||
rst.Message = "未找到有效的遥信配置";
|
||
rst.Flag = true;
|
||
rst.ResultData = new List<TeleSignalOutput>();
|
||
return rst;
|
||
}
|
||
|
||
var obj = new
|
||
{
|
||
id = ids,
|
||
times = eventDrivenConfig.TimeWindowSeconds,
|
||
timeWindowType = TimeWindowTypeEnum.Both // 使用前后时间窗口
|
||
};
|
||
|
||
var str = HttpHelper.HttpPostRequest<JObject>(url, obj);
|
||
if (str != null)
|
||
{
|
||
if (bool.Parse(str["success"].ToString()) == true)
|
||
{
|
||
List<TeleSignalOutput> telesignalInfoOutputs = str["data"].ToObject<List<TeleSignalOutput>>();
|
||
// 填充设备名称
|
||
foreach (var telesignalInfo in telesignalInfoOutputs)
|
||
{
|
||
if (equipmentNameMap.ContainsKey(telesignalInfo.id))
|
||
{
|
||
telesignalInfo.deviceName = equipmentNameMap[telesignalInfo.id];
|
||
}
|
||
}
|
||
rst.ResultData = telesignalInfoOutputs;
|
||
rst.Flag = true;
|
||
}
|
||
else
|
||
{
|
||
// API返回失败,使用测试数据
|
||
rst.ResultData = TestDataService.GenerateTeleSignalTestData(
|
||
ids,
|
||
telesignalNameMap,
|
||
eventDrivenConfig.TimeWindowSeconds,
|
||
TimeWindowTypeEnum.Both,
|
||
-1);
|
||
rst.Flag = true;
|
||
rst.Message = "使用测试数据(API返回失败)";
|
||
}
|
||
}
|
||
else
|
||
{
|
||
// HTTP请求失败,使用测试数据
|
||
rst.ResultData = TestDataService.GenerateTeleSignalTestData(
|
||
ids,
|
||
telesignalNameMap,
|
||
eventDrivenConfig.TimeWindowSeconds,
|
||
TimeWindowTypeEnum.Both,
|
||
-1);
|
||
rst.Flag = true;
|
||
rst.Message = "使用测试数据(HTTP请求失败)";
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
rst.Message = ex.Message;
|
||
Log4Helper.Error(this.GetType(), "获取事件驱动配置关联的遥信实时数据", ex);
|
||
}
|
||
return rst;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 私有方法
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
public RequestPageResult<SecondaryCircuitEventDrivenConfigOutput> FindDatas(PageSearchCondition<SecondaryCircuitEventDrivenConfigSearchInput> searchCondition)
|
||
{
|
||
throw new NotImplementedException();
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 辅助类
|
||
|
||
/// <summary>
|
||
/// 事件驱动巡检项输出DTO
|
||
/// </summary>
|
||
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
|
||
}
|
||
}
|