mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 03:43:11 +00:00
Add zip-to-zip distance calculation for vendor dispatch
- ZipCodeDistance helper with Haversine formula on 33K US zip code centroids - Loaded as singleton from wwwroot/zipcodes.csv on startup - Vendor Dropdown endpoint accepts siteZip, returns address and distanceMiles - Vendors sorted by distance when siteZip provided - Include locationZip in GetWorkorderById response
This commit is contained in:
parent
ee69a1863a
commit
521bb0fba2
5 changed files with 33240 additions and 7 deletions
|
|
@ -13,10 +13,12 @@ namespace Api.SeaHavenIndustries.Controllers
|
|||
public class VendorController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _db;
|
||||
private readonly Helper.ZipCodeDistance _zipDistance;
|
||||
|
||||
public VendorController(ApplicationDbContext db)
|
||||
public VendorController(ApplicationDbContext db, Helper.ZipCodeDistance zipDistance)
|
||||
{
|
||||
_db = db;
|
||||
_zipDistance = zipDistance;
|
||||
}
|
||||
|
||||
[HttpGet("GetVendorList")]
|
||||
|
|
@ -155,22 +157,51 @@ namespace Api.SeaHavenIndustries.Controllers
|
|||
}
|
||||
|
||||
[HttpGet("Dropdown")]
|
||||
public async Task<IActionResult> GetDropdown([FromQuery] string? trade = null)
|
||||
public async Task<IActionResult> GetDropdown([FromQuery] string? trade = null, [FromQuery] string? siteZip = null)
|
||||
{
|
||||
var query = _db.Vendors.Where(v => v.IsActive);
|
||||
|
||||
var vendors = await query.OrderBy(v => v.CompanyName)
|
||||
.Select(v => new { v.Id, v.CompanyName, v.TradeSpecialties })
|
||||
.Select(v => new
|
||||
{
|
||||
v.Id,
|
||||
v.CompanyName,
|
||||
v.TradeSpecialties,
|
||||
v.Address,
|
||||
v.City,
|
||||
v.State,
|
||||
v.Zip
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
var result = vendors.Select(v =>
|
||||
{
|
||||
var distance = _zipDistance.GetDistanceMiles(siteZip, v.Zip);
|
||||
var addr = new[] { v.Address, v.City, v.State, v.Zip }
|
||||
.Where(s => !string.IsNullOrWhiteSpace(s));
|
||||
return new
|
||||
{
|
||||
v.Id,
|
||||
v.CompanyName,
|
||||
v.TradeSpecialties,
|
||||
address = string.Join(", ", addr),
|
||||
distanceMiles = distance.HasValue ? Math.Round(distance.Value, 1) : (double?)null
|
||||
};
|
||||
}).ToList();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(trade))
|
||||
{
|
||||
var tradeMatched = vendors.Where(v => (v.TradeSpecialties ?? "").Contains(trade)).ToList();
|
||||
var rest = vendors.Where(v => !(v.TradeSpecialties ?? "").Contains(trade)).ToList();
|
||||
vendors = tradeMatched.Concat(rest).ToList();
|
||||
var tradeMatched = result.Where(v => (v.TradeSpecialties ?? "").Contains(trade)).ToList();
|
||||
var rest = result.Where(v => !(v.TradeSpecialties ?? "").Contains(trade)).ToList();
|
||||
result = tradeMatched.Concat(rest).ToList();
|
||||
}
|
||||
|
||||
return Ok(vendors);
|
||||
if (!string.IsNullOrWhiteSpace(siteZip))
|
||||
{
|
||||
result = result.OrderBy(v => v.distanceMiles ?? 99999).ToList();
|
||||
}
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -610,6 +610,7 @@ namespace Api.SeaHavenIndustries.Controllers
|
|||
assignee = s.AssignToUser != null ? s.AssignToUser.FirstName : "",
|
||||
location = s.Locations != null ? s.Locations.Name : "",
|
||||
locationId = s.LocationId,
|
||||
locationZip = s.Locations != null ? s.Locations.ZipCode : "",
|
||||
priority = s.Priority,
|
||||
s.Problem,
|
||||
s.Trade,
|
||||
|
|
|
|||
48
Api.SeaHavenIndustries/Helper/ZipCodeDistance.cs
Normal file
48
Api.SeaHavenIndustries/Helper/ZipCodeDistance.cs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
namespace Api.SeaHavenIndustries.Helper
|
||||
{
|
||||
public class ZipCodeDistance
|
||||
{
|
||||
private readonly Dictionary<string, (double lat, double lng)> _coords = new();
|
||||
private bool _loaded;
|
||||
|
||||
public void Load(string csvPath)
|
||||
{
|
||||
if (_loaded) return;
|
||||
foreach (var line in File.ReadLines(csvPath).Skip(1))
|
||||
{
|
||||
var parts = line.Split(',');
|
||||
if (parts.Length >= 3
|
||||
&& double.TryParse(parts[1].Trim(), out var lat)
|
||||
&& double.TryParse(parts[2].Trim(), out var lng))
|
||||
{
|
||||
_coords[parts[0].Trim().PadLeft(5, '0')] = (lat, lng);
|
||||
}
|
||||
}
|
||||
_loaded = true;
|
||||
}
|
||||
|
||||
public double? GetDistanceMiles(string? zip1, string? zip2)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(zip1) || string.IsNullOrWhiteSpace(zip2)) return null;
|
||||
var z1 = zip1.Trim().PadLeft(5, '0');
|
||||
var z2 = zip2.Trim().PadLeft(5, '0');
|
||||
if (z1.Length > 5) z1 = z1.Substring(0, 5);
|
||||
if (z2.Length > 5) z2 = z2.Substring(0, 5);
|
||||
if (!_coords.TryGetValue(z1, out var c1) || !_coords.TryGetValue(z2, out var c2)) return null;
|
||||
return Haversine(c1.lat, c1.lng, c2.lat, c2.lng);
|
||||
}
|
||||
|
||||
private static double Haversine(double lat1, double lon1, double lat2, double lon2)
|
||||
{
|
||||
const double R = 3958.8;
|
||||
var dLat = ToRad(lat2 - lat1);
|
||||
var dLon = ToRad(lon2 - lon1);
|
||||
var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
|
||||
Math.Cos(ToRad(lat1)) * Math.Cos(ToRad(lat2)) *
|
||||
Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
|
||||
return R * 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
|
||||
}
|
||||
|
||||
private static double ToRad(double deg) => deg * Math.PI / 180.0;
|
||||
}
|
||||
}
|
||||
|
|
@ -32,6 +32,14 @@ builder.Services.AddResponseCompression(opts =>
|
|||
builder.Services.AddCors(option =>
|
||||
option.AddDefaultPolicy(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()));
|
||||
builder.Services.AddScoped<SendMessage>();
|
||||
builder.Services.AddSingleton<Api.SeaHavenIndustries.Helper.ZipCodeDistance>(sp =>
|
||||
{
|
||||
var zipService = new Api.SeaHavenIndustries.Helper.ZipCodeDistance();
|
||||
var env = sp.GetRequiredService<IWebHostEnvironment>();
|
||||
var csvPath = Path.Combine(env.WebRootPath, "zipcodes.csv");
|
||||
if (File.Exists(csvPath)) zipService.Load(csvPath);
|
||||
return zipService;
|
||||
});
|
||||
builder.Services.AddAuthentication(options =>
|
||||
{
|
||||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
|
|
|
|||
33145
Api.SeaHavenIndustries/wwwroot/zipcodes.csv
Normal file
33145
Api.SeaHavenIndustries/wwwroot/zipcodes.csv
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue