You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
4.3 KiB
C#

using ForwarderQuery.DB;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Web;
namespace ForwarderQuery.Helper
{
public static class QueryHelper
{
public static bool Query(string mblno, string vessel, string voyno, out string strResult)
{
CangdanData cangdanData = new CangdanData();
mblno = mblno.ToUpper();
var dt = DateTime.Now.AddDays(-20);
List<OpSeaeEdi> listData = null;
var mblnoPrefix = mblno.Length > 4 ? mblno.Substring(0, 4) : mblno;
var allLetter = Regex.IsMatch(mblnoPrefix, "^[A-Z]{4}$");
var allNumber = Regex.IsMatch(mblnoPrefix, "^[0-9]{4}$");
if (allLetter) //全部字母
{
listData = cangdanData.Edis.AsNoTracking().Where(x => x. > dt && x. == vessel && x. == voyno && x..StartsWith(mblnoPrefix)).ToList();
}
else if (allNumber) //全部数字
{
var specialArr = new string[] { "14", "15", "79" };
if (specialArr.Contains(mblnoPrefix.Substring(0, 2))) //14、15、19开头的数字
{
var numPrefix = mblnoPrefix.Substring(0, 2);
listData = cangdanData.Edis.AsNoTracking().Where(x => x. > dt && x. == vessel && x. == voyno && x..StartsWith(numPrefix)).ToList();
}
else
{
listData = cangdanData.Edis
.AsNoTracking()
.Where(x => x. > dt && x. == vessel && x. == voyno)
.ToList()
.Where(x => Regex.IsMatch(x..Substring(0, 2), "^[0-9]{2}$") && !specialArr.Contains(x..Substring(0, 2)))
.ToList();
}
}
else
{
listData = cangdanData.Edis.AsNoTracking().Where(x => x. > dt && x. == vessel && x. == voyno).ToList();
listData = listData.Where(x =>
{
var r = true;
for (int idx = 0; idx < 4; idx++)
{
r &= MatchByChar(mblnoPrefix[idx], x.[idx]);
}
return r;
}).ToList();
}
if (listData.Count > 0)
{
//计算数量
var groupCount = listData.GroupBy(x => x.).Select(x => new { x.Key, Count = x.Count() }).ToList();
var maxCount = groupCount.Max(x => x.Count);
var result = groupCount.Where(x => x.Count == maxCount).Select(x => x.Key).ToList();
strResult = string.Join(" 或 ", result);
return true;
}
strResult = string.Empty;
return false;
}
private static bool MatchByChar(char c1, char c2)
{
var s1 = c1.ToString().ToUpper();
var s2 = c2.ToString().ToUpper();
if (Regex.IsMatch(s1, "^[0-9]{1}$")) //第一位是数字
{
if (!Regex.IsMatch(s2, "^[0-9]{1}$")) //非数字
{
return false;
}
}
else if (s1 != s2)
{
return false;
}
return true;
}
public static string QueryCarrier(string mblno)
{
try
{
WebClient webClient = new WebClient();
webClient.Encoding = System.Text.Encoding.UTF8;
var strJson = webClient.DownloadString(ConfigurationManager.AppSettings["CarrierQueryUrl"] + mblno);
var jobj = JObject.Parse(strJson);
if (jobj.GetValue("status").ToString() == "1")
{
var jobjData = jobj.GetValue("data") as JObject;
return $"{jobjData.GetValue("carrierCode")}-{jobjData.GetValue("carrierName")}";
}
}
catch { }
return string.Empty;
}
}
}