using Abp.Auditing; using Abp.Authorization; using Abp.Collections.Extensions; using Abp.Domain.Repositories; using Abp.Domain.Uow; using Amazon.Runtime.Internal.Transform; using Microsoft.AspNetCore.Mvc; using MongoDB.Driver.Linq; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic.Core; using System.Threading.Tasks; using System.Xml.Serialization; using ToolLibrary.LogHelper; using YunDa.ISAS.Application.Core; using YunDa.ISAS.Application.Core.Session; using YunDa.ISAS.Application.Core.SwaggerHelper; using YunDa.ISAS.DataTransferObject; using YunDa.ISAS.Entities.GeneralInformation; using YunDa.ISAS.Redis.Repositories; using YunDa.ISMS.BASE.Entities.Models; using YunDa.SOMS.DataTransferObject.CommonDto; using YunDa.SOMS.DataTransferObject.GeneralInformation.ProtectionDeviceInfoDto; using YunDa.SOMS.DataTransferObject.GeneralInformation.ProtectionDeviceInfoDto.HistoryData; using YunDa.SOMS.DataTransferObject.GeneralInformation.ProtectionDeviceInfoDto.SearchCondition; using YunDa.SOMS.DataTransferObject.GeneralInformation.ProtectionSettingDto; using YunDa.SOMS.Entities.GeneralInformation; namespace YunDa.ISAS.Application.GeneralInformation { /// /// 保护装置履历信息管理类 /// public class BoardCardInfoAppService : ISASAppServiceBase, IBoardCardInfoAppService { private readonly IRepository _equipmentInfoRepository; private readonly IRepository _protectionDeviceInfoRepository; private readonly IRepository _protectionDeviceTypeRepository; private readonly IRepository _protectionDeviceHistoryRepository; private readonly IRepository _boardCardInfoRepository; private readonly IRepository _boardCardHistoryRepository; private readonly IRepository _manufacturerInfoRepository; private readonly IRepository _imProtectDeviceRepository; private readonly IUnitOfWorkManager _unitOfWorkManager; IRedisRepository, string> _deviceBoardStatesRedis; string deviceIORedisKey = "deviceIO"; IRedisRepository _deviceIOsRedis; // 定义 BoardType 与 BoardTypeId 的映射字典 Dictionary boardTypeMapping = new Dictionary { { "CPU插件", "J2CA" }, { "交流插件", "J2JA" }, { "开入插件", "J2IA" }, { "开出插件", "J2OA" }, { "操作插件", "J2OC" }, { "电源插件", "J2IA" }, { "液晶", "液晶" } }; public BoardCardInfoAppService( IRepository protectionDeviceInfoRepository, IUnitOfWorkManager unitOfWorkManager, IRepository imProtectDeviceRepository, IRepository equipmentInfoRepository, IRepository protectionDeviceHistoryRepository, IRepository boardCardInfoRepository, IRepository boardCardHistoryRepository, IRepository manufacturerInfoRepository, IRedisRepository, string> deviceBoardStatesRedis, IRedisRepository deviceIOsRedis, IRepository protectionDeviceTypeRepository, ISessionAppService sessionAppService ) : base(sessionAppService) { _unitOfWorkManager = unitOfWorkManager; _equipmentInfoRepository = equipmentInfoRepository; _imProtectDeviceRepository = imProtectDeviceRepository; _protectionDeviceInfoRepository = protectionDeviceInfoRepository; _protectionDeviceHistoryRepository = protectionDeviceHistoryRepository; _boardCardInfoRepository = boardCardInfoRepository; _boardCardHistoryRepository = boardCardHistoryRepository; _manufacturerInfoRepository = manufacturerInfoRepository; _deviceBoardStatesRedis = deviceBoardStatesRedis; _deviceIOsRedis = deviceIOsRedis; _protectionDeviceTypeRepository = protectionDeviceTypeRepository; } public async Task> CreateOrUpdateAsync(EditBoardCardInput input) { RequestResult rst = new RequestResult(); try { if (boardTypeMapping.ContainsKey(input.BoardType)) { input.BoardTypeId = boardTypeMapping[input.BoardType]; } if (input.Id.HasValue) { var entity = _boardCardInfoRepository.GetAll().FirstOrDefault(t => t.Id == input.Id); if (entity!=null) { entity.LastModificationTime = DateTime.Now; entity.LastModifierUserId = base.GetCurrentUser().Id; ObjectMapper.Map(input, entity); } } else { var entity = ObjectMapper.Map(input); entity.CreatorUserId = base.GetCurrentUser().Id; entity.CreationTime = DateTime.Now; await _boardCardInfoRepository.InsertAsync(entity); } rst.Flag = true; } catch (Exception ex) { rst.Message = ex.Message; rst.Flag = false; Log4Helper.Error(this.GetType(), "线路管理服务", ex); } return rst; } /// /// 扫码枪输入设备信息 /// /// /// [ShowApi] [AbpAllowAnonymous] [HttpPost] public RequestEasyResult ScanDeviceQRCode(EditBoardCardQrCodeInput input) { RequestEasyResult rst = new RequestEasyResult(); try { var repo = _protectionDeviceInfoRepository.GetAllIncluding(t => t.EquipmentInfo,t=>t.ProtectionDeviceType).ToList(); var boardCards = _boardCardInfoRepository.GetAll().ToList(); var protectionDevice = repo.FirstOrDefault(t => t.Name.Contains(input.ProtectionDeviceName)); if (protectionDevice != null) { var deviceBoardCard = boardCards.FirstOrDefault(t => t.ProtectionDeviceInfoId == protectionDevice.Id&&t.BoardId == input.BoardId); if (deviceBoardCard!=null) { if (deviceBoardCard.SerialNumber != input.SerialNumber) { var deviceBoardCardHistory = _boardCardHistoryRepository.GetAll().Where(t => t.BoardCardInfoId == deviceBoardCard.Id).ToList(); var json = JsonConvert.SerializeObject(deviceBoardCard); var boardHistory = new BoardCardHistory { ContentJson = json, CreationTime = DateTime.Now, CreatorUserId = base.GetCurrentUser().Id, Name = protectionDevice.Name, RecodeDate = DateTime.Now, BoardId = input.BoardId, BoardCardInfoId = deviceBoardCard.Id, ProtectionDeviceInfoId = protectionDevice.Id, SeqNo = deviceBoardCardHistory.Count > 0 ? deviceBoardCardHistory.Max(t => t.SeqNo) + 1 : 1 }; deviceBoardCard.SerialNumber = input.SerialNumber; deviceBoardCard.ProductionDate = input.ProductionDate; deviceBoardCard.VerificationDate = input.VerificationDate; deviceBoardCard.VerificationPerson = input.VerificationPerson; deviceBoardCard.VerificationRecords = input.VerificationRecords; var newjson = JsonConvert.SerializeObject(deviceBoardCard); boardHistory.ContentNewJson = newjson; _boardCardHistoryRepository.Insert(boardHistory); } } else { _boardCardInfoRepository.Insert(new BoardCardInfo { SeqNo = 1, BoardId=input.BoardId, SerialNumber=input.SerialNumber, BoardType=input.BoardType, ProductionDate=input.ProductionDate, VerificationDate=input.VerificationDate, VerificationPerson=input.VerificationPerson, VerificationRecords=input.VerificationRecords, ProtectionDeviceInfoId=deviceBoardCard.Id, }); //rst.Message = "未找到对应板卡"; } rst.Flag = true; } else { rst.Message = "未找到对应装置"; } } catch (Exception ex) { rst.Message = ex.Message; rst.Flag = false; Log4Helper.Error(this.GetType(), "", ex); } return rst; } public async Task DeleteByIdAsync(Guid id) { RequestEasyResult rst = new RequestEasyResult(); return rst; } /// /// 删除板卡信息 /// /// /// [HttpPost] public async Task DeleteByIdsAsync(List ids) { RequestEasyResult rst = new RequestEasyResult(); try { await _boardCardInfoRepository.DeleteAsync(t => ids.Contains(t.Id)); rst.Flag = true; } catch (Exception ex) { rst.Message = ex.Message; rst.Flag = false; Log4Helper.Error(this.GetType(), "保护装置信息管理服务", ex); } return rst; } /// /// 查询保护装置板卡信息 /// /// /// [ShowApi] [AbpAllowAnonymous] [DisableAuditing] [HttpPost] public RequestPageResult FindDatas(PageSearchCondition searchCondition) { RequestPageResult rst = new RequestPageResult(); try { var repo = _boardCardInfoRepository.GetAllIncluding(t=>t.ProtectionDeviceInfo,t=>t.ManufacturerInfo) .WhereIf(searchCondition.SearchCondition.EquipmentInfoId.HasValue, t => t.ProtectionDeviceInfo?.EquipmentInfoId == searchCondition.SearchCondition.EquipmentInfoId) .WhereIf(searchCondition.SearchCondition.ProtectionDeviceInfoId.HasValue, t => t.ProtectionDeviceInfoId == searchCondition.SearchCondition.ProtectionDeviceInfoId); rst.TotalCount = repo.Count(); if (searchCondition.PageSize>0&& searchCondition.PageIndex>0) { int skipCount = searchCondition.PageSize * (searchCondition.PageIndex - 1); var tmpData = repo.Skip(skipCount).Take(searchCondition.PageSize); rst.ResultData = ObjectMapper.Map>(tmpData); } else { rst.ResultData = ObjectMapper.Map>(repo); } 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 RequestResult> FindHistoryDataByEquipmentInfoId(Guid? equipmentInfoId) { RequestResult> rst = new RequestResult>(); try { List historys = new List(); var protectionDevice = _protectionDeviceInfoRepository.GetAllIncluding(t => t.EquipmentInfo) .FirstOrDefault(t => t.EquipmentInfoId == equipmentInfoId); if (protectionDevice != null) { var boards = _boardCardHistoryRepository.GetAll().Where(t => t.ProtectionDeviceInfoId == protectionDevice.Id); foreach (var board in boards) { BoardCardInfo boardCardInfo1 = JsonConvert.DeserializeObject(board.ContentJson); BoardCardInfo boardCardInfo2 = JsonConvert.DeserializeObject(board.ContentNewJson); BoardCardHistoryOutput boardCardHistoryOutput1 = new BoardCardHistoryOutput() { RecodeDate = board.RecodeDate, BoardId = board.BoardId, PostUpdate = ObjectMapper.Map(boardCardInfo2), PrioUpdate = ObjectMapper.Map(boardCardInfo1), SeqNo = board.SeqNo, }; historys.Add(boardCardHistoryOutput1); } rst.Flag = true; rst.ResultData = historys; rst.TotalCount = historys.Count; } } catch (Exception ex) { rst.Message = ex.Message; rst.Flag = false; Log4Helper.Error(this.GetType(), "保护装置信息管理服务", ex); } return rst; } public RequestResult GetProtectionDeviceQRcode(Guid id) { RequestResult rst = new RequestResult(); return rst; } string deviceBoardStatesRedisKey = "deviceBoardStates"; /// /// 获取板卡状态 /// /// /// /// [HttpGet] [AbpAllowAnonymous] [ShowApi] public async Task>> GetBoardStateInfo(Guid equipmentId,Guid deviceId) { RequestResult> rst = new RequestResult>(); try { ProtectionDeviceInfo protectionDeviceInfo = null; if (deviceId == default) { protectionDeviceInfo = _protectionDeviceInfoRepository.GetAll().FirstOrDefault(t => t.EquipmentInfoId == equipmentId); } else { protectionDeviceInfo = _protectionDeviceInfoRepository.Get(deviceId); } if (protectionDeviceInfo != null) { var state = await _deviceBoardStatesRedis.HashSetGetAllAsync(deviceBoardStatesRedisKey, protectionDeviceInfo.DeviceAddress.ToString()); rst.ResultData = state.Select(t=>new DeviceRunState { Name = t.Name,State = t.Value}).ToList(); rst.Flag = true; } } catch (Exception ex) { rst.Flag = false; rst.Message = ex.Message; } return rst; } /// /// 获取板卡IO信息 /// /// /// /// [HttpGet] [AbpAllowAnonymous] [ShowApi] public async Task>> GetBoardIOState(Guid equipmentId, Guid deviceId) { RequestResult> rst = new RequestResult>(); try { ProtectionDeviceInfo protectionDeviceInfo = null; if (deviceId == default) { protectionDeviceInfo = _protectionDeviceInfoRepository.GetAll().FirstOrDefault(t => t.EquipmentInfoId == equipmentId); } else { protectionDeviceInfo = _protectionDeviceInfoRepository.Get(deviceId); } if (protectionDeviceInfo != null) { var states =await _deviceIOsRedis.HashSetGetDicAllAsync(deviceIORedisKey); var datas = states.Where(t => t.Key.Contains($"{protectionDeviceInfo.DeviceAddress}_")).GroupBy(t=>t.Key.Split('_')[1]).Select(t => new IOStateList { Name ="B0"+ t.Key, IOStates =t.Select(x=>x.Value).ToList(), }).OrderBy(t=>t.Name); rst.ResultData = datas.ToList(); rst.Flag = true; } } catch (Exception ex) { rst.Flag = false; rst.Message = ex.Message; } return rst; } /// /// 生成板卡信息 /// /// [ShowApi] [AbpAllowAnonymous] [HttpGet] public RequestEasyResult SpwanBoardInfoList() { RequestEasyResult rst = new RequestEasyResult(); try { var boards = _boardCardInfoRepository.GetAll().ToList(); var devices = _protectionDeviceInfoRepository.GetAll(); var manfac = _manufacturerInfoRepository.GetAll().FirstOrDefault(t => t.ManufacturerName.Contains("运达")); foreach (var device in devices) { var boardInfos = boards.Where(t => t.ProtectionDeviceInfoId == device.Id); if (boardInfos.Count() > 0) { continue; } // 插件信息表 var pluginData = new BoardCardInfo[] { new BoardCardInfo{ BoardId = "B01", SeqNo = 1, SerialNumber = "1933CA00300001", BoardType = "CPU插件", BoardTypeId = "J2CA", ProductionDate = new DateTime(2019, 8, 13), Remark = "CPU 主插件" }, new BoardCardInfo{ BoardId = "B02", SeqNo = 2, SerialNumber = "1933JA04100001", BoardType = "交流插件", BoardTypeId = "J2JA", ProductionDate = new DateTime(2019, 8, 13), Remark = "交流插件" }, new BoardCardInfo{ BoardId = "B03", SeqNo = 3, SerialNumber = "2233IA220V400001", BoardType = "开入插件", BoardTypeId = "J2IA", ProductionDate = new DateTime(2022, 8, 13), Remark = "开入插件1" }, new BoardCardInfo{ BoardId = "B04", SeqNo = 4, SerialNumber = "2233IA220V400002", BoardType = "开入插件", BoardTypeId = "J2IA", ProductionDate = new DateTime(2022, 8, 13), Remark = "开入插件2" }, new BoardCardInfo{ BoardId = "B05", SeqNo = 5, SerialNumber = "2233OA0000400001", BoardType = "开出插件", BoardTypeId = "J2OA", ProductionDate = new DateTime(2022, 8, 13), Remark = "开出插件" }, new BoardCardInfo{ BoardId = "B06", SeqNo = 6, SerialNumber = "2233OC220V400001", BoardType = "操作插件", BoardTypeId = "J2OC", ProductionDate = new DateTime(2022, 8, 13), Remark = "操作插件" }, new BoardCardInfo{ BoardId = "B07", SeqNo = 7, SerialNumber = "1933DA00100001", BoardType = "电源插件", BoardTypeId = "J2IA", ProductionDate = new DateTime(2019, 8, 13), Remark = "电源插件" }, new BoardCardInfo{ BoardId = "液晶", SeqNo = 8, SerialNumber = "1933DA00100001", BoardType = "液晶", BoardTypeId = "液晶", ProductionDate = new DateTime(2019, 8, 13), Remark = "液晶" }, }; // 模拟插入插件数据 foreach (var plugin in pluginData) { var boardCardInfo = new BoardCardInfo { SeqNo = plugin.SeqNo, BoardId = plugin.BoardId, BoardType = plugin.BoardType, ManufacturerInfoId = manfac.Id, ProtectionDeviceInfoId = device.Id, IsActive = true, SerialNumber = plugin.SerialNumber, ProductionDate = plugin.ProductionDate, Remark = plugin.Remark, InstallationDate = DateTime.Now.AddMonths(-3) // 默认安装时间 }; if (plugin.SeqNo == 1) { boardCardInfo.HardwareVersion = "1.0.0"; boardCardInfo.InterfaceVersion = "2.0.0"; boardCardInfo.InterfaceChecksum = "8b18"; boardCardInfo.InterfaceDatabaseVersion = "v1.104"; boardCardInfo.ProtectionDatabaseVersion = "v1.104"; boardCardInfo.SystemVersion = "3.10.10-ADI-2013R1"; boardCardInfo.ProtectionVersion = "v5.3.1"; boardCardInfo.ProtectionChecksum = "8b18"; boardCardInfo.BootVersion = "BOOT2024"; boardCardInfo.Iec61850Version = "IEC61850-V2"; boardCardInfo.FpgaVersion = "v4.000"; boardCardInfo.CidChecksum = "CIDCHK2024"; boardCardInfo.CcdChecksum = "CCDCHK2024"; boardCardInfo.VerificationPerson = "贺严玲"; boardCardInfo.VerificationDate = "2024-01-01"; boardCardInfo.VerificationRecords = "全部通过"; } // 模拟插入数据库 _boardCardInfoRepository.Insert(boardCardInfo); } } rst.Flag = true; } catch (Exception ex) { throw; } return rst; } /// /// 更新版本信息 /// /// [ShowApi] [AbpAllowAnonymous] [HttpPost] public RequestEasyResult ModifyBoardInfoList(ProtectionDeviceVersionInfo input) { RequestEasyResult rst = new RequestEasyResult(); try { var boards = _boardCardInfoRepository.GetAll().ToList(); var devices = _protectionDeviceInfoRepository.GetAll().Where(t=>t.DeviceAddress == input.DeviceAddr).ToList(); var protectionDeviceTypes = _protectionDeviceTypeRepository.GetAll().ToList(); if (devices.Count >0) { var device = devices[0]; device.ProtectionDeviceTypeId = protectionDeviceTypes.FirstOrDefault(t=>t.Model == input.DeviceType)?.Id; //device.BaselineBoardVersion = input. var deviceBoards = boards.Where(t => t.ProtectionDeviceInfoId == device.Id); //保护 BoardCardInfo cpuCardInfo = deviceBoards.FirstOrDefault(t => t.BoardId == "B01"); cpuCardInfo.FpgaVersion = input.FpgaVer; cpuCardInfo.InterfaceDatabaseVersion = input.CommcpuCfgVer; cpuCardInfo.InterfaceVersion = input.CommcpuVer; cpuCardInfo.InterfaceChecksum = input.CommcpuCrc; cpuCardInfo.SystemVersion = input.LinuxVer; cpuCardInfo.ProtectionChecksum = input.ProtectCrc; cpuCardInfo.ProtectionDatabaseVersion = input.ProtectCfgVer; cpuCardInfo.ProtectionVersion = input.ProtectVer; //液晶 BoardCardInfo ioyjCardInfo = deviceBoards.FirstOrDefault(t => t.BoardId == "液晶"); ioyjCardInfo.YjVersion = input.YjVer; ioyjCardInfo.YjCrc = input.YjCrc; if (input.IoVersion.Count==5) { BoardCardInfo io1CardInfo = deviceBoards.FirstOrDefault(t => t.BoardId == "B03"); io1CardInfo.IOVersion = input.IoVersion[0].IoVer; io1CardInfo.IOCrc = input.IoVersion[0].IoCrc; BoardCardInfo io2CardInfo = deviceBoards.FirstOrDefault(t => t.BoardId == "B04"); io2CardInfo.IOVersion = input.IoVersion[1].IoVer; io2CardInfo.IOCrc = input.IoVersion[1].IoCrc; BoardCardInfo io3CardInfo = deviceBoards.FirstOrDefault(t => t.BoardId == "B05"); io3CardInfo.IOVersion = input.IoVersion[2].IoVer; io3CardInfo.IOCrc = input.IoVersion[2].IoCrc; BoardCardInfo io4CardInfo = deviceBoards.FirstOrDefault(t => t.BoardId == "B06"); io4CardInfo.IOVersion = input.IoVersion[3].IoVer; io4CardInfo.IOCrc = input.IoVersion[3].IoCrc; BoardCardInfo io5CardInfo = deviceBoards.FirstOrDefault(t => t.BoardId == "B07"); io5CardInfo.IOVersion = input.IoVersion[4].IoVer; io5CardInfo.IOCrc = input.IoVersion[4].IoCrc; } } rst.Flag = true; } catch (Exception ex) { } return rst; } #if DEBUG /// /// 生成板卡信息 /// /// [ShowApi] [AbpAllowAnonymous] public async Task SpwanBoardMancfactoryJDYDList() { RequestEasyResult rst = new RequestEasyResult(); try { var manfac = _manufacturerInfoRepository.GetAll().FirstOrDefault(t => t.ManufacturerName.Contains("运达")); if (manfac!=null) { using (var unitOfWork = _unitOfWorkManager.Begin()) { var boards = _boardCardInfoRepository.GetAll(); foreach (var board in boards) { board.ManufacturerInfoId = manfac.Id; board.BoardTypeId = "JDYD"; } await unitOfWork.CompleteAsync(); } using (var unitOfWork = _unitOfWorkManager.Begin()) { var devices = _protectionDeviceInfoRepository.GetAllIncluding(t => t.EquipmentInfo); foreach (var device in devices) { device.EquipmentInfo.ManufacturerInfoId = manfac.Id; } await unitOfWork.CompleteAsync(); } } } catch (Exception ex) { throw; } return rst; } /// /// 填充出厂编号 /// /// [ShowApi] [AbpAllowAnonymous] [HttpGet] public async Task UpdateProtetionInfoForHistoryTest() { RequestEasyResult rst = new RequestEasyResult(); try { List datas; using (var unitOfWork = _unitOfWorkManager.Begin()) { datas = _boardCardInfoRepository.GetAllIncluding(t=>t.ProtectionDeviceInfo).ToList(); await unitOfWork.CompleteAsync(); } Random random = new Random(); for (int i = 0; i < 3; i++) { foreach (var data in datas) { EditBoardCardQrCodeInput deviceInfo = new EditBoardCardQrCodeInput { ProtectionDeviceName = data.ProtectionDeviceInfo.Name, BoardId = data.BoardId, BoardType = data.BoardType, SerialNumber = "210" + random.Next(100000, 1000000).ToString(), // 出厂编号设为 "210" 开头 + 6 位随机数 ProductionDate = new DateTime(2023, 5, 15), // 示例生产日期 VerificationPerson = "贺严玲", // 示例检验人员 VerificationDate = DateTime.Now.AddDays(-100 + i).ToString("yyyy-MM-dd"), // 检验日期设为当前日期 VerificationRecords = "检验板卡外观及功能,均符合要求" // 示例检验记录 }; using (var unitOfWork = _unitOfWorkManager.Begin()) { ScanDeviceQRCode(deviceInfo); await unitOfWork.CompleteAsync(); } } } } catch (Exception ex) { throw; } return rst; } #endif } }