/// <summary> /// 检查设备添加或更新时的错误原因 /// </summary> /// <param name="device">设备</param> /// <param name="channels">通道集合</param> /// <returns>数据校验结果</returns> private ModelStateDictionary CheckError(DensityDevice device, List <DensityChannel> channels) { ModelStateDictionary modelState = new ModelStateDictionary(); if (_context.Devices.Count(d => d.Ip == device.Ip) > 0) { modelState.AddModelError("Ip", "设备IP重复"); } if (channels != null) { List <int> indexes = channels.Select(c => c.ChannelIndex).Distinct().ToList(); if (indexes.Count < channels.Count) { modelState.AddModelError("ChannelIndex", "通道序号重复"); } if (channels.Any(c => c.ChannelIndex <= 0)) { modelState.AddModelError("ChannelIndex", "通道序号应该大于0"); } foreach (DensityChannel newChannel in channels) { ModelStateDictionary channelModelState = ChannelsManager.CheckUpdateError(_context, newChannel); if (channelModelState.IsValid) { if (_context.Device_Channels.Count(dc => dc.ChannelId == newChannel.ChannelId && dc.DeviceId != device.DeviceId) != 0) { modelState.AddModelError("ChannelId", $"通道 {newChannel.ChannelId} 已经关联在其他设备"); } } else { foreach (var(key, value) in channelModelState) { foreach (var error in value.Errors) { modelState.AddModelError(key, error.ErrorMessage); } } } } } return(modelState); }
/// <summary> /// 更新设备下的通道集合 /// </summary> /// <param name="device">设备</param> /// <param name="channels">通道集合</param> private void UpdateChannels(DensityDevice device, List <DensityChannel> channels) { device.DensityDevice_DensityChannels = new List <DensityDevice_DensityChannel>(); if (channels != null) { foreach (var channel in channels) { DensityDevice_DensityChannel relation = new DensityDevice_DensityChannel { DeviceId = device.DeviceId, ChannelId = channel.ChannelId }; device.DensityDevice_DensityChannels.Add(relation); if (!ChannelsManager.UpdateChannel(_context, channel)) { ChannelsManager.AddChannel(_context, channel); } } } }