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 { /// /// 保护定值 /// public class ProtectionSettingAppService : ISASAppServiceBase, IProtectionSettingAppService { private readonly IRepository _imDeviceDzRepository; private readonly IRepository _protectionDeviceInfoRepository; private readonly IRepository _protectionSettingRepository; private readonly IRepository _protectionSettingTypeRepository; private readonly IRepository _imDztypeRepository; private readonly IRedisRepository _imDeviceDzRedis; private readonly IUnitOfWorkManager _unitOfWorkManager; public ProtectionSettingAppService(ISessionAppService sessionAppService, IRepository imDeviceDzRepository, IRepository imDztypeRepository, IRepository protectionDeviceInfoRepository, IRepository protectionSettingRepository, IRepository protectionSettingTypeRepository, IRedisRepository imDeviceDzRedis, IUnitOfWorkManager unitOfWorkManager ) : base(sessionAppService) { _unitOfWorkManager = unitOfWorkManager; _imDeviceDzRepository = imDeviceDzRepository; _protectionSettingRepository = protectionSettingRepository; _protectionDeviceInfoRepository = protectionDeviceInfoRepository; _protectionSettingTypeRepository = protectionSettingTypeRepository; _imDztypeRepository = imDztypeRepository; _imDeviceDzRedis = imDeviceDzRedis; } public Task> CreateOrUpdateAsync(EditProtectionSettingInput input) { throw new NotImplementedException(); } public Task DeleteByIdAsync(Guid id) { throw new NotImplementedException(); } public Task DeleteByIdsAsync(List ids) { throw new NotImplementedException(); } /// /// 查询保护装置定值信息 /// /// /// [ShowApi] [AbpAllowAnonymous] public RequestPageResult FindDatas(PageSearchCondition searchCondition) { RequestPageResult < ProtectionSettingOutput > rst = new RequestPageResult(); try { var repo = _protectionSettingRepository.GetAll() .WhereIf(searchCondition.SearchCondition.ProtectionDeviceInfoId.HasValue, t => t.ProtectionDeviceInfoId == searchCondition.SearchCondition.ProtectionDeviceInfoId); rst.TotalCount = repo.Count(); List datas = new List(); 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>(dataTemps); } else { datas = ObjectMapper.Map>(repo); } } rst.ResultData = datas; rst.Flag = true; } catch (Exception ex) { rst.Message = ex.Message; rst.Flag = false; Log4Helper.Error(this.GetType(), "查询定值信息", ex); } return rst; } /// /// 使用装置地址从后台数据库中获取定值信息 /// /// /// /// [ShowApi] [AbpAllowAnonymous] [HttpGet] public RequestResult> GetDzByDeviceAddr(int deviceAddr,int cpuIndex) { RequestResult> rst = new RequestResult>(); 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; } /// /// 查询保护装置定值信息 /// /// [ShowApi] [AbpAllowAnonymous] [DisableAuditing] [HttpGet] public async Task>> FindDZDataByEquipmentInfoId(Guid? equipmentInfoId, string dzType) { RequestResult> 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; } /// /// 从综自后台数据库迁移数据到运维数据库中(保护装置数据) /// /// [ShowApi] [AbpAllowAnonymous] [HttpGet] [UnitOfWork(isTransactional: false)] public RequestEasyResult MigreteProtectSettingDataFromISMS() { RequestEasyResult rst = new RequestEasyResult(); try { List protectionSettingTypes = null; Dictionary porotectionDeviceInfos = default; List ImDeviceDzs = default; Dictionary 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; } /// /// 从综自后台数据库迁移数据到运维数据库中(保护装置数据) /// /// [ShowApi] [AbpAllowAnonymous] [HttpGet] [UnitOfWork(isTransactional: false)] public RequestEasyResult MigreteProtectSttingTypeDataFromISMS() { RequestEasyResult rst = new RequestEasyResult(); try { List 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; } /// /// 生成模拟数据 /// /// [ShowApi] [AbpAllowAnonymous] [HttpGet] public async Task 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 imDeviceDzOutputs = MapDzPutput(list); List 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 imDeviceDzOutputs = MapDzPutput(list); List 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 MapDzPutput(IEnumerable 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(); } } }