using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace Laservall.Solidworks.Common { public class PartTypeItem { [JsonProperty("type_id")] public string TypeId { get; set; } [JsonProperty("parent_type_id")] public string ParentTypeId { get; set; } [JsonProperty("code")] public string Code { get; set; } [JsonProperty("type_name")] public string TypeName { get; set; } [JsonProperty("type_key")] public string TypeKey { get; set; } [JsonProperty("is_child")] public string IsChild { get; set; } public string DisplayText => $"{Code} - {TypeName}"; public override string ToString() => DisplayText; } public static class PLMService { private const string PartTypeSearchUrl = "http://10.0.0.155:9992/api/PLM/PartType/Search"; private static readonly HttpClient _httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(30) }; private static List _cachedPartTypes; private static readonly object _lock = new object(); public static List CachedPartTypes { get { lock (_lock) { return _cachedPartTypes ?? new List(); } } } public static async Task> SearchPartTypesAsync(string code = "", string name = "") { try { var requestBody = new { code = code ?? "", name = name ?? "" }; var json = JsonConvert.SerializeObject(requestBody); var content = new StringContent(json, Encoding.UTF8, "application/json-patch+json"); var response = await _httpClient.PostAsync(PartTypeSearchUrl, content).ConfigureAwait(false); var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false); if (!response.IsSuccessStatusCode) { Debug.WriteLine($"PLM PartType/Search failed: {(int)response.StatusCode} {responseText}"); return new List(); } return JsonConvert.DeserializeObject>(responseText) ?? new List(); } catch (Exception ex) { Debug.WriteLine($"PLM PartType/Search error: {ex.Message}"); return new List(); } } public static async Task LoadAndCachePartTypesAsync() { var items = await SearchPartTypesAsync("", "").ConfigureAwait(false); lock (_lock) { _cachedPartTypes = items; } Debug.WriteLine($"PLM PartTypes loaded: {items.Count} items"); } } }