2024-12-09 14:44:33 +08:00

423 lines
18 KiB
C#

using Abp.Auditing;
using Abp.Authorization;
using Abp.Collections.Extensions;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using iText.Layout.Element;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Driver.Linq;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using ToolLibrary.LogHelper;
using YunDa.ISAS.Application.Core;
using YunDa.ISAS.Application.Core.Session;
using YunDa.ISAS.Application.Core.SwaggerHelper;
using YunDa.ISAS.Application.GeneralInformation.SecondaryCircuitInfo;
using YunDa.ISAS.DataTransferObject;
using YunDa.ISAS.Entities.GeneralInformation;
using YunDa.ISAS.Redis.Repositories;
using YunDa.ISMS.BASE.Entities.Models;
using YunDa.SOMS.DataTransferObject.GeneralInformation.ProtectionDeviceInfoDto;
using YunDa.SOMS.DataTransferObject.GeneralInformation.ProtectionDeviceInfoDto.SearchCondition;
using YunDa.SOMS.DataTransferObject.GeneralInformation.ProtectionSettingDto;
using YunDa.SOMS.Entities.GeneralInformation;
namespace YunDa.ISAS.Application.GeneralInformation.ProtectionSettingInfo
{
/// <summary>
/// 保护定值
/// </summary>
public class ProtectionSettingAppService : ISASAppServiceBase, IProtectionSettingAppService
{
private readonly IRepository<ImDeviceDz, string> _imDeviceDzRepository;
private readonly IRepository<ProtectionDeviceInfo, Guid> _protectionDeviceInfoRepository;
private readonly IRepository<ProtectionSetting, Guid> _protectionSettingRepository;
private readonly IRepository<ProtectionSettingType, Guid> _protectionSettingTypeRepository;
private readonly IRepository<ImDztype, int> _imDztypeRepository;
private readonly IRedisRepository<ImDeviceDzOutput, string> _imDeviceDzRedis;
private readonly IUnitOfWorkManager _unitOfWorkManager;
public ProtectionSettingAppService(ISessionAppService sessionAppService,
IRepository<ImDeviceDz, string> imDeviceDzRepository,
IRepository<ImDztype, int> imDztypeRepository,
IRepository<ProtectionDeviceInfo, Guid> protectionDeviceInfoRepository,
IRepository<ProtectionSetting, Guid> protectionSettingRepository,
IRepository<ProtectionSettingType, Guid> protectionSettingTypeRepository,
IRedisRepository<ImDeviceDzOutput, string> imDeviceDzRedis,
IUnitOfWorkManager unitOfWorkManager
) : base(sessionAppService)
{
_unitOfWorkManager = unitOfWorkManager;
_imDeviceDzRepository = imDeviceDzRepository;
_protectionSettingRepository = protectionSettingRepository;
_protectionDeviceInfoRepository = protectionDeviceInfoRepository;
_protectionSettingTypeRepository = protectionSettingTypeRepository;
_imDztypeRepository = imDztypeRepository;
_imDeviceDzRedis = imDeviceDzRedis;
}
public Task<RequestResult<ProtectionSettingOutput>> CreateOrUpdateAsync(EditProtectionSettingInput input)
{
throw new NotImplementedException();
}
public Task<RequestEasyResult> DeleteByIdAsync(Guid id)
{
throw new NotImplementedException();
}
public Task<RequestEasyResult> DeleteByIdsAsync(List<Guid> ids)
{
throw new NotImplementedException();
}
/// <summary>
/// 查询保护装置定值信息
/// </summary>
/// <param name="searchCondition"></param>
/// <returns></returns>
[ShowApi]
[AbpAllowAnonymous]
public RequestPageResult<ProtectionSettingOutput> FindDatas(PageSearchCondition<ProtectionSettingSearchConditionInput> searchCondition)
{
RequestPageResult < ProtectionSettingOutput > rst = new RequestPageResult<ProtectionSettingOutput >();
try
{
var repo = _protectionSettingRepository.GetAll()
.WhereIf(searchCondition.SearchCondition.ProtectionDeviceInfoId.HasValue, t => t.ProtectionDeviceInfoId == searchCondition.SearchCondition.ProtectionDeviceInfoId);
rst.TotalCount = repo.Count();
List<ProtectionSettingOutput> datas = new List<ProtectionSettingOutput>();
if (repo.Count() > 0)
{
rst.PageSize = searchCondition.PageSize;
rst.PageIndex = searchCondition.PageIndex;
if (searchCondition.PageSize > 0)
{
var dataTemps = repo.Skip((searchCondition.PageIndex - 1) * searchCondition.PageSize).Take(searchCondition.PageSize);
datas = ObjectMapper.Map<List<ProtectionSettingOutput>>(dataTemps);
}
else
{
datas = ObjectMapper.Map<List<ProtectionSettingOutput>>(repo);
}
}
rst.ResultData = datas;
rst.Flag = true;
}
catch (Exception ex)
{
rst.Message = ex.Message;
rst.Flag = false;
Log4Helper.Error(this.GetType(), "查询定值信息", ex);
}
return rst;
}
/// <summary>
/// 使用装置地址从后台数据库中获取定值信息
/// </summary>
/// <param name="deviceAddr"></param>
/// <param name="cpuIndex"></param>
/// <returns></returns>
[ShowApi]
[AbpAllowAnonymous]
[HttpGet]
public RequestResult<List<ImDeviceDzOutput>> GetDzByDeviceAddr(int deviceAddr,int cpuIndex)
{
RequestResult<List<ImDeviceDzOutput>> rst = new RequestResult<List<ImDeviceDzOutput>>();
try
{
var data = _imDeviceDzRepository.GetAllIncluding(t => t.Device,t=>t.DzTypeNavigation).Where(t => t.Device.DeviceAddr == deviceAddr)
.Where(t => t.CpuIndex == cpuIndex).OrderBy(t=>t.DzIndex);
rst.ResultData = MapDzPutput(data);
rst.Flag = true;
}
catch (Exception ex)
{
rst.Message = ex.Message;
rst.Flag = false;
Log4Helper.Error(this.GetType(), "线路管理服务", ex);
}
return rst;
}
/// <summary>
/// 查询保护装置定值信息
/// </summary>
/// <returns></returns>
[ShowApi]
[AbpAllowAnonymous]
[DisableAuditing]
[HttpGet]
public async Task<RequestResult<List<ImDeviceDzOutput>>> FindDZDataByEquipmentInfoId(Guid? equipmentInfoId, string dzType)
{
RequestResult<List<ImDeviceDzOutput>> rst = new();
try
{
ProtectionDeviceInfo entity = null;
entity = _protectionDeviceInfoRepository.GetAllIncluding()
.FirstOrDefault(t => t.EquipmentInfoId == equipmentInfoId);
if (entity != null)
{
if (dzType.ToLower() == "system")
{
var list = await _imDeviceDzRedis.HashSetGetAllAsync($"DZ_{entity.DeviceAddress.ToString("X2")}_System");
rst.ResultData = list.OrderBy(t=>t.DzIndex).ToList();
}
else
{
var list = await _imDeviceDzRedis.HashSetGetAllAsync($"DZ_{entity.DeviceAddress.ToString("X2")}_User");
rst.ResultData = list.OrderBy(t => t.DzIndex).ToList();
}
rst.Flag = true;
}
}
catch (Exception ex)
{
rst.Message = ex.Message;
rst.Flag = false;
Log4Helper.Error(this.GetType(), "保护装置信息管理服务", ex);
}
return rst;
}
/// <summary>
/// 从综自后台数据库迁移数据到运维数据库中(保护装置数据)
/// </summary>
/// <returns></returns>
[ShowApi]
[AbpAllowAnonymous]
[HttpGet]
[UnitOfWork(isTransactional: false)]
public RequestEasyResult MigreteProtectSettingDataFromISMS()
{
RequestEasyResult rst = new RequestEasyResult();
try
{
List<ProtectionSettingType> protectionSettingTypes = null;
Dictionary<string, ProtectionDeviceInfo> porotectionDeviceInfos = default;
List<ImDeviceDz > ImDeviceDzs = default;
Dictionary<string, ProtectionSetting> protectionSettings = default;
using (var unitOfWork = _unitOfWorkManager.Begin())
{
ImDeviceDzs = _imDeviceDzRepository.GetAll().ToList();
unitOfWork.Complete();
}
using (var unitOfWork = _unitOfWorkManager.Begin())
{
porotectionDeviceInfos = _protectionDeviceInfoRepository.GetAllIncluding().ToDictionary(t => t.ISMS_DeviceId);
protectionSettingTypes = _protectionSettingTypeRepository.GetAll().ToList();
protectionSettings = _protectionSettingRepository.GetAll().ToDictionary(t => t.ISMS_DeviceDZId);
unitOfWork.Complete();
}
int count = 0;
using (var unitOfWork = _unitOfWorkManager.Begin())
{
foreach (var imDeviceDz in ImDeviceDzs)
{
if (protectionSettings.ContainsKey(imDeviceDz.Id))
{
continue;
}
if (porotectionDeviceInfos.ContainsKey(imDeviceDz.DeviceId))
{
var protectionDeviceInfo = porotectionDeviceInfos[imDeviceDz.DeviceId];
// 匹配 ProtectionSettingType
var protectionSettingType = protectionSettingTypes.FirstOrDefault(t => t.Description == imDeviceDz.DzType.ToString());
// 创建新的 ProtectionSetting 实体并进行字段映射
ProtectionSetting entity = new ProtectionSetting()
{
CreationTime = DateTime.Now,
CreatorUserId = base.GetCurrentUser().Id,
IsActive = true,
ISMS_DeviceId = imDeviceDz.DeviceId,
EquipmentInfoId = protectionDeviceInfo.EquipmentInfoId, // 映射到 ProtectionDeviceInfo 的 ID
CpuIndex = imDeviceDz.CpuIndex,
SettingIndex = imDeviceDz.DzIndex,
Name = imDeviceDz.DzName,
SettingComment = imDeviceDz.DzComment,
SettingRange = imDeviceDz.DzRange,
Min = (float?)imDeviceDz.DzMin,
Max = (float?)imDeviceDz.DzMax,
SettingUnit = imDeviceDz.DzUnit,
SettingCoeff = imDeviceDz.DzCoeff,
SettingPrecision = imDeviceDz.DzPrecise,
SettingUnit1 = imDeviceDz.DzUnit1,
SettingCoeff1 = imDeviceDz.DzCoeff1,
SettingPrecision1 = imDeviceDz.DzPrecise1,
CtrlWordTypeId = imDeviceDz.CtrlWordTypeId,
ProtectionSettingTypeId = protectionSettingType?.Id ?? Guid.Empty, // 匹配定值类型
EnumTypeId = imDeviceDz.EnumTypeId,
UnitConversionCoeff = imDeviceDz.DzUnitCvtCoeff,
RelatedPtId = imDeviceDz.RelatePtId,
RelatedCtId = imDeviceDz.RelateCtId,
IsReadOnly = imDeviceDz.ReadOnly == 1, // 0: false, 1: true
IsHidden = imDeviceDz.Hidden == "是", // "是" 表示隐藏
ISMS_DeviceDZId = imDeviceDz.Id,
ProtectionDeviceInfoId = protectionDeviceInfo.Id,
};
_protectionSettingRepository.Insert(entity);
count++;
}
}
unitOfWork.Complete();
}
rst.Flag = true;
rst.Message = $"共完成{count}信息迁移";
}
catch (Exception ex)
{
rst.Message = ex.Message;
rst.Flag = false;
Log4Helper.Error(this.GetType(), "线路管理服务", ex);
}
return rst;
}
/// <summary>
/// 从综自后台数据库迁移数据到运维数据库中(保护装置数据)
/// </summary>
/// <returns></returns>
[ShowApi]
[AbpAllowAnonymous]
[HttpGet]
[UnitOfWork(isTransactional: false)]
public RequestEasyResult MigreteProtectSttingTypeDataFromISMS()
{
RequestEasyResult rst = new RequestEasyResult();
try
{
List<ImDztype> ImDztypes = null;
using (var unitOfWork = _unitOfWorkManager.Begin())
{
ImDztypes = _imDztypeRepository.GetAll().ToList();
unitOfWork.Complete();
}
int count = 0;
using (var unitOfWork = _unitOfWorkManager.Begin())
{
foreach (var imDztype in ImDztypes)
{
var entity = new ProtectionSettingType
{
Name = imDztype.Dztype,
CreationTime = DateTime.Now,
CreatorUserId = base.GetCurrentUser().Id,
Description = imDztype.Id.ToString(),
};
_protectionSettingTypeRepository.Insert(entity);
count++;
}
unitOfWork.Complete();
}
rst.Flag = true;
rst.Message = $"共完成{count}信息迁移";
}
catch (Exception ex)
{
rst.Message = ex.Message;
rst.Flag = false;
Log4Helper.Error(this.GetType(), "线路管理服务", ex);
}
return rst;
}
/// <summary>
/// 生成模拟数据
/// </summary>
/// <returns></returns>
[ShowApi]
[AbpAllowAnonymous]
[HttpGet]
public async Task<RequestEasyResult> SpwaProtectDeviceSettingTestData()
{
RequestEasyResult rst = new RequestEasyResult();
try
{
//_imDeviceDzRedis.HashSetUpdateManyAsync($"DZ_{address.ToString("X2")}_System", userDzs.Select(t => t.Id).ToList(), userDzs);
var dzs = await _imDeviceDzRepository.GetAllIncludingAsync(t=>t.DzTypeNavigation,t=>t.Device);
var addrDzs = dzs.GroupBy(t => t.Device.DeviceAddr);
foreach (var dz in addrDzs)
{
if (true)
{
var list = dz.Where(t => t.CpuIndex == 9).OrderBy(t=>t.DzIndex);
// 手动转换为 ImDeviceDzOutput 列表
List<ImDeviceDzOutput> imDeviceDzOutputs = MapDzPutput(list);
List<string> redisSystems = imDeviceDzOutputs.Select(t => t.Id).ToList();
await _imDeviceDzRedis.HashSetUpdateManyAsync($"DZ_{dz.Key.ToString("X2")}_System", redisSystems, imDeviceDzOutputs);
}
if (true)
{
var list = dz.Where(t => t.CpuIndex == 1).OrderBy(t => t.DzIndex);
List<ImDeviceDzOutput> imDeviceDzOutputs = MapDzPutput(list);
List<string> redisSystems = imDeviceDzOutputs.Select(t => t.Id).ToList();
await _imDeviceDzRedis.HashSetUpdateManyAsync($"DZ_{dz.Key.ToString("X2")}_User", redisSystems, imDeviceDzOutputs);
}
}
rst.Flag = true;
}
catch (Exception ex)
{
rst.Message = ex.Message;
}
return rst;
}
private List<ImDeviceDzOutput> MapDzPutput(IEnumerable<ImDeviceDz> list)
{
return list.Select(t => new ImDeviceDzOutput
{
Id = t.Id,
DeviceId = t.DeviceId,
CpuIndex = t.CpuIndex,
DzIndex = t.DzIndex,
DzName = t.DzName,
DzComment = t.DzComment,
DzRange = t.DzRange,
DzMin = t.DzMin,
DzMax = t.DzMax,
DzUnit = t.DzUnit,
DzCoeff = t.DzCoeff,
DzPrecise = t.DzPrecise,
DzUnit1 = t.DzUnit1,
DzCoeff1 = t.DzCoeff1,
DzPrecise1 = t.DzPrecise1,
CtrlWordTypeId = t.CtrlWordTypeId,
DzType = t.DzType,
PUCtgyCode = t.Device.PuctgyCode, // 假设这个字段由某些条件决定
EnumTypeId = t.EnumTypeId,
DzUnitCvtCoeff = t.DzUnitCvtCoeff,
RelatePtId = t.RelatePtId,
RelateCtId = t.RelateCtId,
ReadOnly = t.ReadOnly,
Hidden = t.Hidden,
DzTypeNavigation = new ImDztypeOutput { /* 映射 ImDztype 到 ImDztypeOutput 的属性 */ },
Value = (new Random().Next(0, 10)).ToString() // 随机值
}).ToList();
}
}
}