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.

243 lines
12 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Extensions;
using DS.Module.Core.Helpers;
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;
namespace DS.WMS.Core.Op.Method
{
/// <summary>
/// 海运出口运踪相关接口
/// </summary>
public partial class SeaExportService
{
#region 运踪相关
/// <summary>
/// 发起运踪订阅或者更新提单号
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public async Task<DataResult<string>> SendOrUpdateTrace(List<BillTraceSendReq> req)
{
if (req.Count == 0)
{
return await Task.FromResult(DataResult<string>.Failed("未传入正确参数!"));
}
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
//var userKey = db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "seae_billtraceurl_userKey" && x.TenantId == 1288018625843826688).First().Value;
//var userPwd = db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "seae_billtraceurl_userPwd" && x.TenantId == 1288018625843826688).First().Value;
//var userId = db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "seae_billtraceurl_userId" && x.TenantId == 1288018625843826688).First().Value;
var reqUrl = db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "request_seae_billtraceurl" && x.TenantId == 1288018625843826688).First().Value;
var resUrl = db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "response_seae_billtraceurl" && x.TenantId == 1288018625843826688).First().Value;
var account = seaComService.GetCodeThirdParty("BillTraceSubcribe", user.UserId, tenantDb);
if (account.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed("未配置运踪订阅第三方账号信息!"));
}
if (account.AppSecret.IsNull()|| account.AppKey.IsNull()|| account.RegistContractName.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed("请检查运踪订阅第三方账号关键参数!"));
}
BillTraceMain billdto = new BillTraceMain();
List<BillTraceReq> billTraceList = new List<BillTraceReq>();
foreach (var item in req)
{
var yardCode = seaComService.GetClientCode(item.YardId, tenantDb);
var carrierCode = seaComService.GetClientCode(item.CarrierId, tenantDb);
if (!string.IsNullOrWhiteSpace(item.CarrierId.ToString()))
{
var et = await tenantDb.Queryable<BookingStatus>().Where(x => x.StatusCode == "status_mudigang" && x.BusinessId == Convert.ToInt64(item.BusinessId)).FirstAsync();
if (et != null)
{
return await Task.FromResult(DataResult<string>.Failed("\"提单号\" + item.MBLNO + \"已订阅!"));
}
}
if (!string.IsNullOrWhiteSpace(item.YardId.ToString()))
{
var et = await tenantDb.Queryable<BookingStatus>().Where(x => x.StatusCode == "status_qiyungang" && x.BusinessId == Convert.ToInt64(item.BusinessId)).FirstAsync();
if (et != null)
{
return await Task.FromResult(DataResult<string>.Failed("\"提单号\" + item.MBLNO + \"已订阅!"));
}
}
if (item.isBook && (string.IsNullOrWhiteSpace(carrierCode) || string.IsNullOrWhiteSpace(item.Carrier) || string.IsNullOrWhiteSpace(item.MBLNO)))
{
return await Task.FromResult(DataResult<string>.Failed("订阅目的港船公司或提单号不能为空!"));
}
if (!item.isBook && (string.IsNullOrWhiteSpace(item.Yard) || string.IsNullOrWhiteSpace(yardCode) || string.IsNullOrWhiteSpace(item.MBLNO)))
{
return await Task.FromResult(DataResult<string>.Failed("订阅港前数据提单号或场站不能为空!"));
}
var yardMap = tenantDb.Queryable<MappingYard>().Where(x => x.Code == yardCode && x.Module == "BillTrace").First();
if (yardMap.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed($"场站代号配置未找到:{yardCode}"));
}
//var carrierMap = tenantDb.Queryable<MappingYard>().Where(x => x.Code == yardCode && x.Module == "BillTrace").First();
//if (yardMap.IsNull())
//{
// return await Task.FromResult(DataResult<string>.Failed($"船公司代号配置未找到:{yardCode}"));
//}
var carrierName =string.Empty;
if (string.IsNullOrEmpty(carrierCode))
{
carrierName = "";
}
else
{
carrierName = seaComService.GetCarrierMapper(item.CarrierId, tenantDb).MapName;
}
billTraceList.Add(new BillTraceReq
{
BusinessId = item.BusinessId,
MBLNO = item.MBLNO,
YARD = yardMap.MapName,
YardCode = yardMap.MapCode == "" ? null : yardMap.MapCode,
CARRIER = carrierName,
CARRIERID = carrierCode == "" ? null : carrierCode,
isBook = item.isBook
});
var status = string.Empty;
var IsBookingYZ = String.Empty;
if (!string.IsNullOrWhiteSpace(yardCode) && !string.IsNullOrWhiteSpace(carrierCode))
{
status = "订阅起运港,目的港";
IsBookingYZ = "3";
}
else if (string.IsNullOrWhiteSpace(yardCode) && !string.IsNullOrWhiteSpace(carrierCode))
{
status = "订阅目的港";
IsBookingYZ = "2";
}
else if (!string.IsNullOrWhiteSpace(yardCode) && string.IsNullOrWhiteSpace(carrierCode))
{
status = "订阅起运港";
IsBookingYZ = "1";
}
else
{
IsBookingYZ = "0";
}
//TODO 添加booking日志
//更新运踪标识
var info = tenantDb.Queryable<SeaExport>().First(x => x.Id == Convert.ToInt64(item.BusinessId));
info.IsBookingYZ = IsBookingYZ;
await tenantDb.Updateable(info).ExecuteCommandAsync();
}
billdto.Children = billTraceList;
billdto.Key = account.AppKey;
billdto.PWD = account.AppSecret;
billdto.url = resUrl;
billdto.Gid = account.RegistContractName;
billdto.PushBackKey = user.TenantId;//根据回传的租户Id回调
var json = billdto.ToJsonString();
var postData =new { msg = json };
if (!reqUrl.EndsWith("/"))
{
reqUrl += "/";
}
reqUrl += "BillTrace/SubcribeOrUpdateBilltrace";
var result = RequestHelper.Post(postData.ToJsonString(), reqUrl);
var res = JsonConvert.DeserializeObject<BillTraceRes>(result);
if (!res.Success)
{
return await Task.FromResult(DataResult<string>.Failed($"发起运踪订阅或者更新提单号失败:{res.Message}"));
}
else
{
return await Task.FromResult(DataResult<string>.Success(res.Data.ToJsonString()));
}
}
/// <summary>
/// 运踪退订
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public async Task<DataResult<string>> UnsubscribeBillTrace(List<BillTraceUnsubscribeList> req)
{
if (req.Count == 0)
{
return await Task.FromResult(DataResult<string>.Failed("未传入正确参数!"));
}
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
//var userKey = db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "seae_billtraceurl_userKey" && x.TenantId == 1288018625843826688).First().Value;
//var userPwd = db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "seae_billtraceurl_userPwd" && x.TenantId == 1288018625843826688).First().Value;
//var userId = db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "seae_billtraceurl_userId" && x.TenantId == 1288018625843826688).First().Value;
var reqUrl = db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "request_seae_billtraceurl" && x.TenantId == 1288018625843826688).First().Value;
var resUrl = db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "response_seae_billtraceurl" && x.TenantId == 1288018625843826688).First().Value;
var account = seaComService.GetCodeThirdParty("BillTraceSubcribe", user.UserId, tenantDb);
if (account.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed("未配置运踪订阅第三方账号信息!"));
}
if (account.AppSecret.IsNull() || account.AppKey.IsNull() || account.RegistContractName.IsNull())
{
return await Task.FromResult(DataResult<string>.Failed("请检查运踪订阅第三方账号关键参数!"));
}
BillTraceUnsubscribeDto reqDto = new BillTraceUnsubscribeDto()
{
Children = new List<BillTraceUnsubscribeList>(),
Key = account.AppKey,
PWD = account.AppSecret,
Gid = account.RegistContractName,
PushBackKey= user.TenantId,
};
foreach (var item in req)
{
if (string.IsNullOrEmpty(item.BusinessId))
{
return await Task.FromResult(DataResult<string>.Failed("订舱记录主键不能为空!!"));
}
reqDto.Children.Add(new BillTraceUnsubscribeList(item.BusinessId, item.MBLNO));
//TODO 添加booking日志
//更新运踪标识
var info = tenantDb.Queryable<SeaExport>().First(x => x.Id == Convert.ToInt64(item.BusinessId));
info.IsBookingYZ = "0";
await tenantDb.Updateable(info).ExecuteCommandAsync();
}
var json = reqDto.ToJsonString();
if (!reqUrl.EndsWith("/"))
{
reqUrl += "/";
}
reqUrl += "BillTrace/UnsubscribeBillTrace";
var postData = new { msg = json };
var result = RequestHelper.Post(postData.ToJsonString(), reqUrl);
var res = JsonConvert.DeserializeObject<BillTraceUnsubscribeRes>(result);
if (!res.Success)
{
return await Task.FromResult(DataResult<string>.Failed($"运踪退订失败:{res.Message}"));
}
else
{
return await Task.FromResult(DataResult<string>.Success(res.Data));
}
}
#endregion
}
}