using Furion;
using Furion.FriendlyException;
using Furion.Logging;
using Furion.RemoteRequest.Extensions;
using Myshipping.Core;
using Myshipping.Core.Service;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Asn1.X500;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application.Helper
{
///
/// 获取场站数据辅助类(调用爬虫新运踪接口)
///
public static class YardDataHelper
{
///
/// 异步调取出口运踪场站详情(按箱明细显示)
///
/// 用户名ID
/// 租户ID
/// 租户名称
/// 提单号
/// 场站ID
/// 是否网站
/// 返回场站详情(按箱明细显示)
public static async Task<(bool isSuccess, string data, string contact)> GetYardData(long userId, long tenantId, string tenantName, string mblno, string yardid, bool isWeb = true)
{
var cacheService = App.GetService();
var webAccService = App.GetService();
var sysConfig = await cacheService.GetAllSysConfig();
var userKey = sysConfig.FirstOrDefault(x => x.Code == "spiderUserKeyBilltrace");
var userSecret = sysConfig.FirstOrDefault(x => x.Code == "spiderUserSecretBilltrace");
var spiderServerUrl = sysConfig.FirstOrDefault(x => x.Code == "spiderServerUrlBillTraceNew");
var needYgtAccountList = cacheService.GetAllDictData().Result.Where(x => x.TypeCode == "YardListNeedYgtAccount").ToList();//需要云港通账号的场站
var needLhtAccountList = cacheService.GetAllDictData().Result.Where(x => x.TypeCode == "YardListNeedLhtAccount").ToList();//需要陆海通账号的场站
var webAcc = "";
var webPwd = "";
if (needYgtAccountList.Count(x => x.Code == yardid) > 0)
{
//从租户参数中获取网站云港通账号密码
var tenantParam = await cacheService.GetAllTenantParam();
var ygtAcc = await webAccService.GetAccountConfig("YunGangTong", userId);
if (ygtAcc == null || string.IsNullOrEmpty(ygtAcc.Account) || string.IsNullOrEmpty(ygtAcc.Password))
{
return (false, "未找到云港通账号、密码配置,请到账号维护模块中添加", "");
}
webAcc = ygtAcc.Account;
webPwd = ygtAcc.Password;
}
if (needLhtAccountList.Count(x => x.Code == yardid) > 0)
{
//从租户参数中获取网站云港通账号密码
var tenantParam = await cacheService.GetAllTenantParam();
var ygtAcc = await webAccService.GetAccountConfig("LuHaiTong", userId);
if (ygtAcc == null || string.IsNullOrEmpty(ygtAcc.Account) || string.IsNullOrEmpty(ygtAcc.Password))
{
return (false, "未找到陆海通账号、密码配置,请到账号维护模块中添加", "");
}
webAcc = ygtAcc.Account;
webPwd = ygtAcc.Password;
}
var yardMappings = await cacheService.GetAllMappingYard();
var ym = yardMappings.FirstOrDefault(x => x.Code.ToLower() == yardid.ToLower() && x.Module == "BillTrace");
if (ym == null)
{
return (false, $"场站代号配置未找到:{yardid}", "");
}
var objSend = new
{
user_key = userKey.Value,
user_secret = userSecret.Value,
customer_id = tenantId.ToString(),
customer_name = tenantName,
web_code = ym.MapCode,
bno = mblno,
req_type = isWeb ? "1" : "0",
web_user = webAcc,
web_psw = webPwd,
};
var urlYard = spiderServerUrl.Value;
if (!urlYard.EndsWith("/"))
{
urlYard += "/";
}
urlYard += "real/query";
var strJson = objSend.ToJsonString();
Log.Information($"查询实时场站数据发送数据,url:{urlYard},json:{strJson}");
string rtn = await urlYard.SetBody(objSend).PostAsStringAsync();
Log.Information($"查询实时场站数据,返回:{rtn}");
var rtnObj = JObject.Parse(rtn);
if (rtnObj.GetIntValue("code") != 200)
{
return (false, $"获取场站数失败:{rtnObj.GetStringValue("msg")}", "");
}
else
{
var rtnData = rtnObj.GetStringValue("data");
if (isWeb)
{
var viewstatehtml = GetDataHtmlList(rtnData, "__VIEWSTATE", "/>");
if (viewstatehtml != "")
{
rtnData = rtnData.Replace(viewstatehtml, "");
}
}
var rtnContact = rtnObj.GetJArrayValue("contact_list")?.ToJsonString() ?? "";
return (true, rtnData, rtnContact);
}
}
#region 异步调取出口运踪场站详情
///
/// 异步调取出口运踪场站详情(按箱明细显示,并对箱型做了本地转换)
///
/// 用户名ID
/// 租户ID
/// 租户名称
/// 提单号
/// 场站ID
/// 是否网站
/// 返回场站详情(按箱明细显示,并对箱型做了本地转换)
public static async Task GetYardDataWithTranslateAsync(long userId, long tenantId, string tenantName, string mblno,
string yardid, bool isWeb = true)
{
(bool isSuccess, string data, string contact) rtn = await GetYardData(userId, tenantId, tenantName, mblno, yardid, isWeb);
if (!rtn.isSuccess)
{
throw Oops.Bah(rtn.data);
}
var _cache = App.GetService();
//场站引入的数据,转换为订舱箱型,且带上箱型代码
var ctnList = await _cache.GetAllCodeCtn();
var mapCtn = await _cache.GetAllMappingCtn();
var jData = JArray.Parse(rtn.data);
foreach (JObject item in jData)
{
var ctnall = item.GetStringValue("CTNALL");
var findMap = mapCtn.FirstOrDefault(x => x.Module == "YardData" && x.MapCode == ctnall);
var findCtn = ctnList.FirstOrDefault(x => x.Name == ctnall);
if (findMap != null)
{
item.Add("CtnCode", findMap.Code);
findCtn = ctnList.First(c => c.Code == findMap.Code);
item["CTNALL"] = findCtn.Name; //名称显示维护的箱型
}
else if (findCtn != null)
{
item.Add("CtnCode", findCtn.Code);
item["CTNALL"] = findCtn.Name; //名称显示维护的箱型
}
else
{
throw Oops.Bah($"未找到箱型{ctnall}的场站引入配置");
}
}
return jData.ToString();
}
#endregion
private static string GetDataHtmlList(string html, string startstr, string endstr)
{
var subhtml = html;
var htmllength = subhtml.Length;
var startindex = subhtml.IndexOf(startstr);
//if (startindex == -1 || startindex == 0) return "";
if (startindex == -1) return "";
subhtml = subhtml.Substring(startindex + startstr.Length, htmllength - startindex - startstr.Length);
var endindex = subhtml.IndexOf(endstr);
if (endindex != -1 && endindex != 0)
subhtml = subhtml.Substring(0, endindex);
return subhtml;
}
}
}