124 lines
4.8 KiB
C#
124 lines
4.8 KiB
C#
using Laservall.Solidworks.Server.Dto;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Laservall.Solidworks.Server.Handlers
|
|
{
|
|
internal sealed class MatchMaterialHandler
|
|
{
|
|
private const string PlmApiUrl = "http://10.0.0.155:9991/api/PLM/FindPartsCode";
|
|
private static readonly HttpClient _httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(30) };
|
|
|
|
public async Task HandleMatch(HttpListenerContext context, CancellationToken ct)
|
|
{
|
|
string body;
|
|
using (var reader = new StreamReader(context.Request.InputStream, Encoding.UTF8))
|
|
{
|
|
body = await reader.ReadToEndAsync();
|
|
}
|
|
|
|
var request = JsonConvert.DeserializeObject<MatchMaterialRequest>(body);
|
|
if (request?.Items == null || request.Items.Count == 0)
|
|
{
|
|
await WriteJson(context.Response, new MatchMaterialResponse
|
|
{
|
|
Success = true,
|
|
Results = new List<MatchMaterialResultDto>()
|
|
}, ct);
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
var plmRequestBody = request.Items
|
|
.Select(i => new PlmFindPartsCodeRequest { model = i.DrawingNo })
|
|
.ToList();
|
|
|
|
var plmJson = JsonConvert.SerializeObject(plmRequestBody);
|
|
var httpContent = new StringContent(plmJson, Encoding.UTF8, "application/json-patch+json");
|
|
var httpResponse = await _httpClient.PostAsync(PlmApiUrl, httpContent, ct);
|
|
var responseText = await httpResponse.Content.ReadAsStringAsync();
|
|
|
|
if (!httpResponse.IsSuccessStatusCode)
|
|
{
|
|
await WriteJson(context.Response, new MatchMaterialResponse
|
|
{
|
|
Success = false,
|
|
Results = new List<MatchMaterialResultDto>(),
|
|
Error = $"PLM API 返回 {(int)httpResponse.StatusCode}: {responseText}"
|
|
}, ct);
|
|
return;
|
|
}
|
|
|
|
var plmResults = JsonConvert.DeserializeObject<List<PlmFindPartsCodeResponse>>(responseText)
|
|
?? new List<PlmFindPartsCodeResponse>();
|
|
|
|
var plmLookup = plmResults
|
|
.Where(r => !string.IsNullOrEmpty(r.part_code))
|
|
.ToDictionary(r => r.model ?? "", r => r, StringComparer.OrdinalIgnoreCase);
|
|
|
|
var results = new List<MatchMaterialResultDto>();
|
|
foreach (var item in request.Items)
|
|
{
|
|
PlmFindPartsCodeResponse plmMatch;
|
|
if (plmLookup.TryGetValue(item.DrawingNo ?? "", out plmMatch))
|
|
{
|
|
results.Add(new MatchMaterialResultDto
|
|
{
|
|
DrawingNo = item.DrawingNo,
|
|
DocPath = item.DocPath,
|
|
PartCode = plmMatch.part_code,
|
|
PartName = plmMatch.part_name,
|
|
Matched = true
|
|
});
|
|
}
|
|
else
|
|
{
|
|
results.Add(new MatchMaterialResultDto
|
|
{
|
|
DrawingNo = item.DrawingNo,
|
|
DocPath = item.DocPath,
|
|
PartCode = "",
|
|
PartName = "",
|
|
Matched = false
|
|
});
|
|
}
|
|
}
|
|
|
|
await WriteJson(context.Response, new MatchMaterialResponse
|
|
{
|
|
Success = true,
|
|
Results = results
|
|
}, ct);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await WriteJson(context.Response, new MatchMaterialResponse
|
|
{
|
|
Success = false,
|
|
Results = new List<MatchMaterialResultDto>(),
|
|
Error = $"匹配失败: {ex.Message}"
|
|
}, ct);
|
|
}
|
|
}
|
|
|
|
private static async Task WriteJson(HttpListenerResponse response, object data, CancellationToken ct)
|
|
{
|
|
response.ContentType = "application/json; charset=utf-8";
|
|
string json = JsonConvert.SerializeObject(data);
|
|
byte[] bytes = Encoding.UTF8.GetBytes(json);
|
|
response.ContentLength64 = bytes.Length;
|
|
await response.OutputStream.WriteAsync(bytes, 0, bytes.Length, ct);
|
|
response.Close();
|
|
}
|
|
}
|
|
}
|