102 lines
3.1 KiB
C#
102 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CPRNIMS.Infrastructure.Dto.Canvass.Result
|
|
{
|
|
public class GroqSupplierResult
|
|
{
|
|
[JsonPropertyName("company_name")]
|
|
public string CompanyName { get; set; } = string.Empty;
|
|
|
|
[JsonPropertyName("country")]
|
|
public string? Country { get; set; }
|
|
|
|
[JsonPropertyName("phone_number")]
|
|
public string? PhoneNumber { get; set; }
|
|
|
|
[JsonPropertyName("contact_email")]
|
|
public string? ContactEmail { get; set; }
|
|
|
|
[JsonPropertyName("website")]
|
|
public string? Website { get; set; }
|
|
|
|
[JsonPropertyName("source")]
|
|
public string? Source { get; set; }
|
|
|
|
[JsonConverter(typeof(FlexibleDecimalConverter))]
|
|
public decimal? EstimatedPriceUsd { get; set; }
|
|
|
|
[JsonPropertyName("item_specifications")]
|
|
public JsonElement? ItemSpecifications { get; set; }
|
|
}
|
|
public class FlexibleDecimalConverter : JsonConverter<decimal?>
|
|
{
|
|
public override decimal? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
switch (reader.TokenType)
|
|
{
|
|
case JsonTokenType.Number:
|
|
return reader.TryGetDecimal(out var num) ? num : null;
|
|
|
|
case JsonTokenType.String:
|
|
var str = reader.GetString()?.Trim();
|
|
if (string.IsNullOrWhiteSpace(str)) return null;
|
|
|
|
// Strip currency symbols and commas e.g. "$1,200.00" → 1200.00
|
|
str = Regex.Replace(str, @"[^\d.]", "");
|
|
return decimal.TryParse(str, NumberStyles.Any, CultureInfo.InvariantCulture, out var parsed)
|
|
? parsed
|
|
: null; // "contact for pricing", "varies", etc. → null
|
|
|
|
case JsonTokenType.Null:
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, decimal? value, JsonSerializerOptions options)
|
|
{
|
|
if (value.HasValue) writer.WriteNumberValue(value.Value);
|
|
else writer.WriteNullValue();
|
|
}
|
|
}
|
|
public class TavilySearchResult
|
|
{
|
|
public List<TavilyResult> Results { get; set; } = new();
|
|
}
|
|
|
|
public class TavilyResult
|
|
{
|
|
public string Title { get; set; } = string.Empty;
|
|
public string Url { get; set; } = string.Empty;
|
|
public string Content { get; set; } = string.Empty;
|
|
public double Score { get; set; }
|
|
}
|
|
public class GroqMatchResult
|
|
{
|
|
public bool Matched { get; set; }
|
|
public int? SupplierId { get; set; }
|
|
}
|
|
public class GroqResponse
|
|
{
|
|
public List<GroqChoice> Choices { get; set; } = new();
|
|
}
|
|
|
|
public class GroqChoice
|
|
{
|
|
public GroqMessage Message { get; set; } = new();
|
|
}
|
|
|
|
public class GroqMessage
|
|
{
|
|
public string Content { get; set; } = string.Empty;
|
|
}
|
|
}
|