using Myshipping.Core;
using Myshipping.Core.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application.Helper
{
public static class PlaceToPortHelper
{
///
/// 根据收货地港口英文名解析出起始港对象
///
/// 收货地港口英文名
/// 起始港缓存
/// 起始港缓存映射
/// 起始港对象
public static async Task PlaceReceiptToPortload(string portEnName, List cachePortLoad, Func>> cacheMapPortLoadFunc)
{
if (string.IsNullOrEmpty(portEnName))
{
return null;
}
// 匹配方式1:精准匹配
var portInfo = cachePortLoad.FirstOrDefault(x => x.EnName.Equals(portEnName, StringComparison.OrdinalIgnoreCase));
if (portInfo != null) return portInfo;
// 匹配方式2:起始模糊匹配
portInfo = cachePortLoad.FirstOrDefault(x => x.EnName.StartsWith(portEnName, StringComparison.OrdinalIgnoreCase));
if (portInfo != null) return portInfo;
// 匹配方式3:完整模糊匹配
portInfo = cachePortLoad.FirstOrDefault(x => x.EnName.Contains(portEnName, StringComparison.OrdinalIgnoreCase));
if (portInfo != null) return portInfo;
// 匹配方式4:精准映射匹配
var mapCachePortLoad = await cacheMapPortLoadFunc();
var map = mapCachePortLoad.FirstOrDefault(x => x.Module == CommonConst.RECEIPT_TO_PORTLOAD
&& x.MapName.Equals(portEnName, StringComparison.OrdinalIgnoreCase));
if (map != null)
{
portInfo = cachePortLoad.FirstOrDefault(x => x.Code == map.Code);
if (portInfo != null) return portInfo;
}
return null;
}
///
/// 根据交货地港口英文名解析出目的港对象
///
/// 交货地港口英文名
/// 目的港缓存
/// 目的港缓存映射
/// 目的港对象
public static async Task PlaceDeliveryToPort(string portEnName, List cachePort, Func>> cacheMapPortFunc)
{
if (string.IsNullOrEmpty(portEnName))
{
return null;
}
// 匹配方式1:精准匹配
var portInfo = cachePort.FirstOrDefault(x => x.EnName.Equals(portEnName, StringComparison.OrdinalIgnoreCase));
if (portInfo != null) return portInfo;
// 匹配方式2:起始模糊匹配
portInfo = cachePort.FirstOrDefault(x => x.EnName.StartsWith(portEnName, StringComparison.OrdinalIgnoreCase));
if (portInfo != null) return portInfo;
// 匹配方式3:完整模糊匹配
portInfo = cachePort.FirstOrDefault(x => x.EnName.Contains(portEnName, StringComparison.OrdinalIgnoreCase));
if (portInfo != null) return portInfo;
// 匹配方式4:精准映射匹配
var mapCachePort = await cacheMapPortFunc();
var map = mapCachePort.FirstOrDefault(x => x.Module == CommonConst.DELIVERY_TO_PORT
&& x.MapName.Equals(portEnName, StringComparison.OrdinalIgnoreCase));
if (map != null)
{
portInfo = cachePort.FirstOrDefault(x => x.Code == map.Code);
if (portInfo != null) return portInfo;
}
return null;
}
}
}