public ClientResult GetClient(GetClientBy getClientBy, int?clientID, string clientCode, string ipAddress, bool withDetail, bool activeClient) { try { BSL.ClientService cs = new BSL.ClientService(); var client = cs.GetClient(getClientBy, clientID, clientCode, ipAddress, activeClient); var sr = new ClientResult { IsSuccess = true, Client = client, }; if (client != null && withDetail) { using (var db = new MyDbContext()) { db.Configuration.ProxyCreationEnabled = false; db.Configuration.LazyLoadingEnabled = false; RmsMonitoringProfile monitorngProfile = new RmsMonitoringProfile(); List <RmsMonitoringProfileDevice> monitoringProfileDevices = new List <RmsMonitoringProfileDevice>(); List <RmsDevice> devices = new List <RmsDevice>(); sr.MonitoringProfile = monitorngProfile; sr.ListMonitoringProfileDevices = monitoringProfileDevices; var _client = db.RmsClients.Where(c => c.ClientId == client.ClientId && (!activeClient || (c.Enable == true && c.EffectiveDate <= DateTime.Today && (c.ExpiredDate == null || c.ExpiredDate >= DateTime.Today)))) .Include( i => i.RmsClientMonitorings.Select(cm => cm.RmsMonitoringProfile) .Select(mp => mp.RmsMonitoringProfileDevices.Select(mpd => mpd.RmsDevice))).FirstOrDefault(); if (_client != null) { var rmsClientMonitoring = _client.RmsClientMonitorings.Where(cm => cm.EffectiveDate <= DateTime.Today) .OrderByDescending(od => od.EffectiveDate) .FirstOrDefault(); if (rmsClientMonitoring != null) { monitorngProfile = rmsClientMonitoring.RmsMonitoringProfile; sr.MonitoringProfile = monitorngProfile; monitoringProfileDevices = new List <RmsMonitoringProfileDevice>(monitorngProfile.RmsMonitoringProfileDevices); sr.ListMonitoringProfileDevices = monitoringProfileDevices; foreach (var mpd in monitoringProfileDevices) { if (devices.All(d => d.DeviceId != mpd.RmsDevice.DeviceId)) { devices.Add(mpd.RmsDevice); } } sr.ListDevices = new List <RmsDevice>(devices); } } sr.ListDeviceType = db.RmsDeviceTypes.ToList(); } } return(sr); } catch (Exception ex) { new RMSWebException(this, "0500", "GetClient failed. " + ex.Message, ex, true); var sr = new ClientResult { IsSuccess = false, ErrorMessage = ex.Message }; return(sr); } }
public RmsClient GetClient(GetClientBy getClientBy, int?clientID, string clientCode, string ipAddress, bool activeClient) { try { using (var db = new MyDbContext()) { db.Configuration.ProxyCreationEnabled = false; db.Configuration.LazyLoadingEnabled = false; RmsClient client = new RmsClient(); if (getClientBy == GetClientBy.ClientID) { var ret = db.RmsClients.Where(c => c.ClientId == clientID && (!activeClient || (c.Enable == true && c.EffectiveDate <= DateTime.Today && (c.ExpiredDate == null || c.ExpiredDate >= DateTime.Today)))); var lClients = new List <RmsClient>(ret.ToList()); if (lClients.Count == 0) { throw new Exception("Client not found by ClientID: " + clientID + ". Please check Monitoring Database"); } client = lClients[0]; } else if (getClientBy == GetClientBy.ClientCode) { var ret = db.RmsClients.Where(c => c.ClientCode == clientCode && (!activeClient || (c.Enable == true && c.EffectiveDate <= DateTime.Today && (c.ExpiredDate == null || c.ExpiredDate >= DateTime.Today)))); var lClients = new List <RmsClient>(ret.ToList()); if (lClients.Count > 1) { throw new Exception("Found more thand one client by ClientCode: " + clientCode + ". Please check Monitoring Database"); } if (lClients.Count == 0) { throw new Exception("Client not found by ClientCode: " + clientCode + ". Please check Monitoring Database"); } client = lClients[0]; } else if (getClientBy == GetClientBy.IPAddress) { var ret = db.RmsClients.Where(c => c.IpAddress == ipAddress && c.ClientTypeId == 1 && (!activeClient || (c.Enable == true && c.EffectiveDate <= DateTime.Today && (c.ExpiredDate == null || c.ExpiredDate >= DateTime.Today)))); var lClients = new List <RmsClient>(ret.ToList()); if (lClients.Count > 1) { throw new Exception("Found more thand one client by IPAddress: " + ipAddress + ". Please check Monitoring Database"); } if (lClients.Count == 0) { throw new Exception("Client not found by IPAddress: " + ipAddress + ". Please check Monitoring Database"); } client = lClients[0]; } return(client); } } catch (Exception ex) { throw new RMSWebException(this, "0500", "GetClient failed. " + ex.Message, ex, false); } }