/// <summary> /// Client want's to know occurred errors. /// </summary> /// <param name="request"></param> /// <returns></returns> public GXErrorsResponse Post(GXErrorsRequest request) { lock (Db) { if (request.System) { List <string> Filter = new List <string>(); string query = "SELECT * FROM " + GuruxAMI.Server.AppHost.GetTableName <GXAmiSystemError>(Db); if (request.UserIDs != null && request.UserIDs.Length != 0) { bool first = true; query += "WHERE UserID IN ("; foreach (long it in request.UserIDs) { if (!first) { query += ", "; } query += it.ToString(); first = false; } query += ")"; } else if (request.UserGroupIDs != null && request.UserGroupIDs.Length != 0) { string query2 = string.Format("SELECT DISTINCT {0}.ID FROM {0} INNER JOIN {1} ON {0}.ID = {1}.UserID WHERE UserGroupID IN (", GuruxAMI.Server.AppHost.GetTableName <GXAmiUser>(Db), GuruxAMI.Server.AppHost.GetTableName <GXAmiUserGroupUser>(Db)); bool first = true; foreach (ulong it in request.UserGroupIDs) { if (!first) { query2 += ", "; } query2 += it.ToString(); first = false; } first = true; List <long> userIDs = Db.Select <long>(query2); first = true; query += "WHERE UserID IN ("; foreach (long it in userIDs) { if (!first) { query += ", "; } query += it.ToString(); first = false; } query += ")"; } if (request.Descending) { query += string.Format(" ORDER BY {0}.ID DESC ", GuruxAMI.Server.AppHost.GetTableName <GXAmiSystemError>(Db)); } List <GXAmiSystemError> errors = Db.Select <GXAmiSystemError>(query); //Get errors by range. if (request.Index != 0 || request.Count != 0) { if (request.Count == 0 || request.Index + request.Count > errors.Count) { request.Count = errors.Count - request.Index; } errors.RemoveRange(0, request.Index); var limitUsers = errors.Take(request.Count); errors = limitUsers.ToList(); } return(new GXErrorsResponse(errors.ToArray())); } else { List <string> Filter = new List <string>(); string query = "SELECT * FROM " + GuruxAMI.Server.AppHost.GetTableName <GXAmiDeviceError>(Db); if (request.DeviceIDs != null && request.DeviceIDs.Length != 0) { bool first = true; string str = "TargetDeviceID IN ("; foreach (long it in request.DeviceIDs) { if (!first) { str += ", "; } str += it.ToString(); first = false; } str += ")"; Filter.Add(str); } if (request.DeviceGroupIDs != null && request.DeviceGroupIDs.Length != 0) { //query += "DeviceGroup = " + request.DeviceGroupIDs; bool first = true; string str = "TargetDeviceID IN ("; foreach (long it in request.DeviceIDs) { if (!first) { str += ", "; } str += it.ToString(); first = false; } str += ")"; Filter.Add(str); } if (Filter.Count != 0) { query += "WHERE "; query += string.Join(" AND ", Filter.ToArray()); } if (request.Descending) { query += string.Format(" ORDER BY {0}.ID DESC ", GuruxAMI.Server.AppHost.GetTableName <GXAmiDeviceError>(Db)); } List <GXAmiDeviceError> errors = Db.Select <GXAmiDeviceError>(query); //Get errors by range. if (request.Index != 0 || request.Count != 0) { if (request.Count == 0 || request.Index + request.Count > errors.Count) { request.Count = errors.Count - request.Index; } errors.RemoveRange(0, request.Index); var limitUsers = errors.Take(request.Count); errors = limitUsers.ToList(); } return(new GXErrorsResponse(errors.ToArray())); } } }
/// <summary> /// Client want's to know occurred errors. /// </summary> /// <param name="request"></param> /// <returns></returns> public GXErrorsResponse Post(GXErrorsRequest request) { lock (Db) { if (request.System) { List<string> Filter = new List<string>(); string query = "SELECT * FROM " + GuruxAMI.Server.AppHost.GetTableName<GXAmiSystemError>(Db); if (request.UserIDs != null && request.UserIDs.Length != 0) { bool first = true; query += "WHERE UserID IN ("; foreach (long it in request.UserIDs) { if (!first) { query += ", "; } query += it.ToString(); first = false; } query += ")"; } else if (request.UserGroupIDs != null && request.UserGroupIDs.Length != 0) { string query2 = string.Format("SELECT DISTINCT {0}.ID FROM {0} INNER JOIN {1} ON {0}.ID = {1}.UserID WHERE UserGroupID IN (", GuruxAMI.Server.AppHost.GetTableName<GXAmiUser>(Db), GuruxAMI.Server.AppHost.GetTableName<GXAmiUserGroupUser>(Db)); bool first = true; foreach (ulong it in request.UserGroupIDs) { if (!first) { query2 += ", "; } query2 += it.ToString(); first = false; } first = true; List<long> userIDs = Db.Select<long>(query2); first = true; query += "WHERE UserID IN ("; foreach (long it in userIDs) { if (!first) { query += ", "; } query += it.ToString(); first = false; } query += ")"; } if (request.Descending) { query += string.Format(" ORDER BY {0}.ID DESC ", GuruxAMI.Server.AppHost.GetTableName<GXAmiSystemError>(Db)); } List<GXAmiSystemError> errors = Db.Select<GXAmiSystemError>(query); //Get errors by range. if (request.Index != 0 || request.Count != 0) { if (request.Count == 0 || request.Index + request.Count > errors.Count) { request.Count = errors.Count - request.Index; } errors.RemoveRange(0, request.Index); var limitUsers = errors.Take(request.Count); errors = limitUsers.ToList(); } return new GXErrorsResponse(errors.ToArray()); } else { List<string> Filter = new List<string>(); string query = "SELECT * FROM " + GuruxAMI.Server.AppHost.GetTableName<GXAmiDeviceError>(Db); if (request.DeviceIDs != null && request.DeviceIDs.Length != 0) { bool first = true; string str = "TargetDeviceID IN ("; foreach (long it in request.DeviceIDs) { if (!first) { str += ", "; } str += it.ToString(); first = false; } str += ")"; Filter.Add(str); } if (request.DeviceGroupIDs != null && request.DeviceGroupIDs.Length != 0) { //query += "DeviceGroup = " + request.DeviceGroupIDs; bool first = true; string str = "TargetDeviceID IN ("; foreach (long it in request.DeviceIDs) { if (!first) { str += ", "; } str += it.ToString(); first = false; } str += ")"; Filter.Add(str); } if (Filter.Count != 0) { query += "WHERE "; query += string.Join(" AND ", Filter.ToArray()); } if (request.Descending) { query += string.Format(" ORDER BY {0}.ID DESC ", GuruxAMI.Server.AppHost.GetTableName<GXAmiDeviceError>(Db)); } List<GXAmiDeviceError> errors = Db.Select<GXAmiDeviceError>(query); //Get errors by range. if (request.Index != 0 || request.Count != 0) { if (request.Count == 0 || request.Index + request.Count > errors.Count) { request.Count = errors.Count - request.Index; } errors.RemoveRange(0, request.Index); var limitUsers = errors.Take(request.Count); errors = limitUsers.ToList(); } return new GXErrorsResponse(errors.ToArray()); } } }