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.

350 lines
15 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using DS.Module.Core;
using DS.Module.Core.Enums;
using DS.Module.Core.Extensions;
using DS.Module.Core.Helpers;
using DS.WMS.Core.Code.Entity;
using DS.WMS.Core.Map.Entity;
using DS.WMS.Core.Op.Dtos;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Sys.Entity;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NPOI.OpenXmlFormats;
using SqlSugar;
namespace DS.WMS.Core.Op.Method
{
/// <summary>
/// 海运出口场站相关接口
/// </summary>
public partial class SeaExportService
{
/// <summary>
/// 获取场站数据
/// </summary>
/// <param name="id">业务Id</param>
/// <param name="isWeb"></param>
/// <returns></returns>
public async Task<DataResult<string>> GetYardData(string id, bool isWeb = false)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var order = await tenantDb.Queryable<SeaExport>().Where(x => x.Id == long.Parse(id)).FirstAsync();
if (order.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed("海运出口信息不存在!", MultiLanguageConst.SeaExportNotExist));
}
if (string.IsNullOrEmpty(order.Yard) || order.YardId == 0 || order.YardId.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed("请先选择场站!", MultiLanguageConst.SeaExportNotExist));
}
var data = await GetYardDataAndMappingSystem(order, isWeb);
if (!data.Succeeded)
{
return await Task.FromResult(DataResult<string>.Failed(data.Message));
}
return await Task.FromResult(DataResult<string>.Success(JsonConvert.SerializeObject(data.Data)));
}
/// <summary>
/// 获取订舱记录在指定网站的HTML数据
/// </summary>
/// <param name="id">业务Id</param>
/// <param name="webCode">网站编码值为YARD或HLW_CD或YGTYARD获取订舱记录所属场站的网页数据HLW_CD获取互联网+的网页数据YGT获取云港通的网页数据</param>
/// <returns></returns>
public async Task<DataResult<string>> GetWebData(string id, string webCode)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var order = tenantDb.Queryable<SeaExport>().Where(x => x.Id == long.Parse(id)).First();
if (order.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed("海运出口信息不存在!", MultiLanguageConst.SeaExportNotExist));
}
if (string.IsNullOrEmpty(order.Yard) || order.YardId == 0 || order.YardId.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed("请先选择场站!", MultiLanguageConst.SeaExportNotExist));
}
WebCodeEnum codeEnum = webCode switch
{
"YARD" => WebCodeEnum.YARD,
"HLW_CD" => WebCodeEnum.HLW_CD,
"YGT" => WebCodeEnum.YGT,
_ => throw new Exception("webCode参数错误!")
};
var data = await GetWebData(new WebDataReq() {
MBLNO = order.MBLNO,
YardId = order.YardId,
WebCode = codeEnum,
});
if (!data.Succeeded)
{
return await Task.FromResult(DataResult<string>.Failed(data.Message));
}
return await Task.FromResult(DataResult<string>.Success(JsonConvert.SerializeObject(data.Data)));
}
/// <summary>
/// 从爬虫获取场站数据,并映射为系统中的箱型
/// </summary>
/// <returns></returns>
private async Task<DataResult<List<YardCtnImportRes>>> GetYardDataAndMappingSystem(SeaExport order, bool isWeb = false)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
//var yardCode = seaComService.GetClientCode(order.YardId, tenantDb);
var req = new YardDataReq
{
YardId = order.YardId,
MBLNO = order.MBLNO,
IsWeb = isWeb
};
var rtn = await GetYardData(req);
if (!rtn.Succeeded)
{
return await Task.FromResult(DataResult<List<YardCtnImportRes>>.Failed(rtn.Message));
}
//场站引入的数据,转换为订舱箱型,且带上箱型代码
var ctnList = await tenantDb.Queryable<CodeCtn>().Where(x => x.Status == StatusEnum.Enable).ToListAsync();
var mapCtn = await tenantDb.Queryable<MappingCtn>().Where(x => x.Status == StatusEnum.Enable).ToListAsync();
var listRtn = JsonConvert.DeserializeObject<List<YardCtnImportRes>>(rtn.Data);
foreach (var item in listRtn)
{
var ctnall = item.CTNALL;
var findMap = mapCtn.FirstOrDefault(x => x.Module == "YardData" && x.MapCode == ctnall);
var findCtn = ctnList.FirstOrDefault(x => x.CtnName == ctnall);
if (findMap != null)
{
item.CtnCode = findMap.Code;
findCtn = ctnList.First(c => c.EdiCode == findMap.Code);
item.CTNALL = findCtn.CtnName; //名称显示维护的箱型
}
else if (findCtn != null)
{
item.CtnCode = findCtn.EdiCode;
item.CTNALL = findCtn.CtnName; //名称显示维护的箱型
}
else
{
return await Task.FromResult(DataResult<List<YardCtnImportRes>>.Failed($"未找到箱型{ctnall}的场站引入配置"));
}
}
return await Task.FromResult(DataResult<List<YardCtnImportRes>>.Success(listRtn));
}
/// <summary>
/// 异步调取出口运踪场站详情(按箱明细显示)
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public async Task<DataResult<string>> GetYardData(YardDataReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var account = seaComService.GetCodeThirdParty("BillTrace", user.UserId, tenantDb, req.YardId);
//tenantDb.Queryable<CodeThirdParty>().Where(x => x.CustomerId == req.YardId && x.AccountType == "BillTrace").First();
var userKey = await db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "spiderUserKeyBilltrace" && x.TenantId == 1288018625843826688).FirstAsync();
var userSecret = await db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "spiderUserSecretBilltrace" && x.TenantId == 1288018625843826688).FirstAsync();
var spiderServerUrl = await db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "spiderServerUrlBillTraceNew" && x.TenantId == 1288018625843826688).FirstAsync();
var tenant = await db.Queryable<SysTenant>().Filter(null, true).Where(x => x.Id == long.Parse(user.TenantId)).FirstAsync();
if (account.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed("未配置出口运踪场站第三方账号信息!"));
}
var yardMap = await tenantDb.Queryable<MappingYard>().Where(x => x.LinkId == req.YardId && x.Module == "BillTrace").FirstAsync();
if (yardMap.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed($"场站代号配置未找到:{req.YardId}"));
}
var objSend = new
{
user_key = userKey.Value,
user_secret = userSecret.Value,
customer_id = user.TenantId,
customer_name = tenant.Name,
web_code = yardMap.MapCode,
bno = req.MBLNO,
req_type = req.IsWeb ? "1" : "0",
web_user = account.AppKey,
web_psw = account.AppSecret,
};
var urlYard = spiderServerUrl.Value;
if (!urlYard.EndsWith("/"))
{
urlYard += "/";
}
urlYard += "real/query";
var res = await RequestHelper.PostJsonAsyncNoHeaders(urlYard,JsonConvert.SerializeObject(objSend));
var rtnObj = JObject.Parse(res);
if (rtnObj.GetIntValue("code") != 200)
{
return await Task.FromResult(DataResult<string>.Failed("获取场站数据失败:"+rtnObj.GetStringValue("msg")));
}
else
{
var rtnData = rtnObj.GetStringValue("data");
if (req.IsWeb)
{
var viewstatehtml = GetDataHtmlList(rtnData, "__VIEWSTATE", "/>");
if (viewstatehtml != "")
{
rtnData = rtnData.Replace(viewstatehtml, "");
}
}
return await Task.FromResult(DataResult<string>.Success(rtnData));
}
}
/// <summary>
/// 异步调取网站HTML数据
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public async Task<DataResult<string>> GetWebData(WebDataReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
//var account = seaComService.GetCodeThirdParty("BillTrace", user.UserId, tenantDb, req.YardId);
//tenantDb.Queryable<CodeThirdParty>().Where(x => x.CustomerId == req.YardId && x.AccountType == "BillTrace").First();
var userKey = db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "spiderUserKeyBilltrace" && x.TenantId == 1288018625843826688).First().Value;
var userSecret = db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "spiderUserSecretBilltrace" && x.TenantId == 1288018625843826688).First().Value;
var spiderServerUrl = db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "spiderServerUrlBillTraceNew" && x.TenantId == 1288018625843826688).First().Value;
var tenantName = db.Queryable<SysTenant>().Filter(null, true).Where(x => x.Id == long.Parse(user.TenantId)).First().Name;
//if (account.IsNull())
//{
// return await Task.FromResult(DataResult<string>.Failed("未配置出口运踪场站第三方账号信息!"));
//}
var webAcc = "";
var webPwd = "";
var newWebCode = "";
if (req.WebCode == WebCodeEnum.YARD)
{
var yardCode = seaComService.GetClientCode(req.YardId, tenantDb);
var needYgtAccountList = db.Queryable<SysDictData>().InnerJoin<SysDictType>((a, b) => a.TypeId == b.Id)
.Where((a, b) => b.Code == "YardListNeedYgtAccount").OrderBy((a, b) => a.OrderNo)
.Select<SysDictData>().ToList();
var needLhtAccountList = db.Queryable<SysDictData>().InnerJoin<SysDictType>((a, b) => a.TypeId == b.Id)
.Where((a, b) => b.Code == "YardListNeedLhtAccount").OrderBy((a, b) => a.OrderNo)
.Select<SysDictData>().ToList();
if (needYgtAccountList.Count(x => x.Value == yardCode) > 0)
{
var ygtAcc = seaComService.GetCodeThirdParty("YunGangTong", user.UserId, tenantDb);
if (ygtAcc == null || string.IsNullOrEmpty(ygtAcc.AppKey) || string.IsNullOrEmpty(ygtAcc.AppSecret))
{
return await Task.FromResult(DataResult<string>.Failed("未找到云港通账号、密码配置,请到第三方账号中添加!"));
}
webAcc = ygtAcc.AppKey;
webPwd = ygtAcc.AppSecret;
}
else if (needLhtAccountList.Count(x => x.Value == yardCode) > 0)
{
var lhtAcc = seaComService.GetCodeThirdParty("LuHaiTong", user.UserId, tenantDb);
if (lhtAcc == null || string.IsNullOrEmpty(lhtAcc.AppKey) || string.IsNullOrEmpty(lhtAcc.AppSecret))
{
return await Task.FromResult(DataResult<string>.Failed("未找到陆海通账号、密码配置,请到第三方账号中添加!"));
}
webAcc = lhtAcc.AppKey;
webPwd = lhtAcc.AppSecret;
}
else {
var account = seaComService.GetCodeThirdParty("BillTrace", user.UserId, tenantDb);
if (account.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed("未配置出口运踪场站第三方账号信息!"));
}
webAcc = account.AppKey;
webAcc = account.AppSecret;
}
var yardMap = tenantDb.Queryable<MappingYard>().Where(x => x.LinkId == req.YardId && x.Module == "BillTrace").First();
if (yardMap.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed($"场站代号配置未找到:{yardCode}"));
}
newWebCode = yardMap.MapCode;
}
else
{
newWebCode = req.WebCode.ToString();
}
var objSend = new
{
user_key = userKey,
user_secret = userSecret,
customer_id = user.TenantId,
customer_name = tenantName,
web_code = newWebCode,
bno = req.MBLNO,
req_type = "1" ,
web_user = webAcc,
web_psw = webPwd,
};
var urlYard = spiderServerUrl;
if (!urlYard.EndsWith("/"))
{
urlYard += "/";
}
urlYard += "real/query";
var res = RequestHelper.Post(JsonConvert.SerializeObject(objSend), urlYard);
var rtnObj = JObject.Parse(res);
if (rtnObj.GetIntValue("code") != 200)
{
return await Task.FromResult(DataResult<string>.Failed("获取场站数据失败:" + rtnObj.GetStringValue("msg")));
}
else
{
var rtnData = rtnObj.GetStringValue("data");
var viewstatehtml = GetDataHtmlList(rtnData, "__VIEWSTATE", "/>");
if (viewstatehtml != "")
{
rtnData = rtnData.Replace(viewstatehtml, "");
}
return await Task.FromResult(DataResult<string>.Success(rtnData));
}
}
public 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;
}
}
}