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.

266 lines
13 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Data;
using DS.Module.Core.Extensions;
using DS.Module.Core.Helpers;
using DS.Module.DjyServiceStatus;
using DS.WMS.Core.Code.Entity;
using DS.WMS.Core.Info.Entity;
using DS.WMS.Core.Map.Entity;
using DS.WMS.Core.Op.Dtos;
using DS.WMS.Core.Op.EDI;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Sys.Entity;
using Mapster;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
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<List<SubmitTelexResultDto>>> SubmitTelex(IdModel req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var orderList = await tenantDb.Queryable<SeaExport>().Where(x => req.Ids.Contains(x.Id)).ToListAsync();
if (orderList.Count == 0)
{
return await Task.FromResult(DataResult<List<SubmitTelexResultDto>>.Failed($"电放失败,未找到相应订舱"));
}
var url = db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "TelexServiceUrl" && x.TenantId == 1288018625843826688).First()?.Value;
var key = db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "TelexServiceKey" && x.TenantId == 1288018625843826688).First()?.Value;
var secret = db.Queryable<SysConfig>().Filter(null, true).Where(x => x.Code == "TelexServiceSecret" && x.TenantId == 1288018625843826688).First()?.Value;
if (string.IsNullOrWhiteSpace(url))
{
return await Task.FromResult(DataResult<List<SubmitTelexResultDto>>.Failed($"电放失败,大简云电放服务接口地址未配置,请联系管理员"));
}
if (string.IsNullOrWhiteSpace(key))
{
return await Task.FromResult(DataResult<List<SubmitTelexResultDto>>.Failed($"电放失败大简云电放服务接口Key未配置请联系管理员"));
}
if (string.IsNullOrWhiteSpace(secret))
{
return await Task.FromResult(DataResult<List<SubmitTelexResultDto>>.Failed($"电放失败大简云电放服务接口Secret未配置请联系管理员"));
}
var webAccount = tenantDb.Queryable<CodeThirdParty>().Where(x => x.AccountType == "MSKWeb" && x.CreateBy == long.Parse(user.UserId)).First();
if (webAccount == null)
{
return await Task.FromResult(DataResult<List<SubmitTelexResultDto>>.Failed($"电放失败未配置网站账户类型MSKWeb"));
}
var mappingCarrier = tenantDb.Queryable<MappingCarrier>().Where(v => v.Status == StatusEnum.Enable).ToList();
var mappingIssueType = tenantDb.Queryable<MappingIssueType>().Where(v => v.Status == StatusEnum.Enable).ToList();
List<SubmitTelexResultDto> result = new();
List<(long Id, string MBLNO, string CarrierId)> pushList = new();
foreach (var order in orderList)
{
try
{
var carrierCode = seaComService.GetCarrierCode(order.YardId, tenantDb);
if (string.IsNullOrWhiteSpace(order.MBLNO))
{
result.Add(new SubmitTelexResultDto(order.MBLNO, false, "提单号为空"));
continue;
}
if (string.IsNullOrWhiteSpace(order.IssueType))
{
result.Add(new SubmitTelexResultDto(order.MBLNO, false, "签单方式为空"));
continue;
}
if (string.IsNullOrWhiteSpace(order.Carrier))
{
result.Add(new SubmitTelexResultDto(order.MBLNO, false, "船公司为空"));
continue;
}
// 判断船公司是否符合条件
string carrierId = mappingCarrier.FirstOrDefault(x => x.Module == "TelexCheck" && x.LinkId == order.CarrierId)?.MapCode;
carrierId = (carrierId ?? carrierCode).ToUpper();
if (carrierId != "MSK")
{
result.Add(new SubmitTelexResultDto(order.MBLNO, false, $"[{order.Carrier}]船公司此功能待开发"));
continue;
}
// 判断签单方式是否符合条件
string issueType = mappingIssueType.FirstOrDefault(x => x.Module == "TelexCheck" && x.Code == order.IssueType)?.MapCode;
issueType = issueType ?? order.IssueType;
if (!issueType.Equals("telex", StringComparison.OrdinalIgnoreCase))
{
result.Add(new SubmitTelexResultDto(order.MBLNO, false, $"签单方式非电放"));
continue;
}
pushList.Add((order.Id, order.MBLNO, carrierId));
}
catch (Exception ex)
{
_logger.Error("请求电放条件判断的过程中发生未知异常mblno={mblno}, ex={ex}", order.MBLNO, ex);
result.Add(new SubmitTelexResultDto(order.MBLNO, false, $"提交错误:{ex.Message}"));
continue;
}
}
if (pushList.Count > 0)
{
return await Task.FromResult(DataResult<List<SubmitTelexResultDto>>.FailedData(result));
//return result;
}
if (req.Ids.Length > 1)
{
var usr = db.Queryable<SysUser>().Filter(null, true).First(u => u.Id == long.Parse(user.UserId));
url += "/v1/tasks";
var mblnos = string.Concat(',', pushList.Select(x => x.MBLNO));
try
{
// 提交电放
var data = pushList.Select(x => new
{
web_code = x.CarrierId,
web_user = webAccount.AppKey,
web_psw = webAccount.AppSecret,
bno = x.MBLNO,
email = usr.Email
});
var body = new
{
user_key = key,
user_secret = secret,
data = data
};
_logger.Info("请求电放提交接口开始mblnos={mblnos}, url={url}, body={body}", mblnos, url, body);
//var rtn = await url.OnClientCreating(client =>
//{
// client.Timeout = TimeSpan.FromMinutes(3); // 设置超时时间 3分钟
//}).SetBody(body).PostAsStringAsync();
var rtn = RequestHelper.Post(JsonConvert.SerializeObject(body), url, 180000);
_logger.Info("请求电放提交接口结束rtn={rtn}", rtn);
var jsonRtn = JObject.Parse(rtn);
if (jsonRtn.GetIntValue("code") == 200)
{
_logger.Info("请求电放提交接口成功mblnos={mblnos}", mblnos);
foreach (var item in pushList)
{
// 推送 电放已安排 状态
var pushModel = new EmbedServiceProjectStatusDto
{
businessId = item.Id.ToString(),
SourceType = 0,
StatusCodes = new List<EmbedServiceProjectStatusDetailDto> {
new EmbedServiceProjectStatusDetailDto { StatusCode = "DFYAP" } }
};
//var saveStatusRlt = await bookingValueAddedService.SaveServiceStatus(pushModel);
var saveStatusRlt = await _djyServiceStatusService.SaveServiceStatus(pushModel);
_logger.Info("请求电放提交接口成功后推送 电放已安排 状态mblno={mblno}JSON={json} ,结果={rlt}",
item.MBLNO, JsonConvert.SerializeObject(pushModel), JsonConvert.SerializeObject(saveStatusRlt));
result.Add(new SubmitTelexResultDto(item.MBLNO, true, null));
}
return await Task.FromResult(DataResult<List<SubmitTelexResultDto>>.Success(result));
}
else
{
var msg = jsonRtn.GetStringValue("msg")?.Replace("爬虫", "");
_logger.Info("请求电放提交接口失败mblnos={mblnos}, 电放服务返回:{msg}", mblnos, msg);
result.AddRange(pushList.Select(x => new SubmitTelexResultDto(x.MBLNO, false, $"电放服务返回:{msg}")));
}
}
catch (Exception ex)
{
_logger.Error("请求电放服务的过程中发生未知异常mblnos={mblnos}, ex={ex}", mblnos, ex);
result.AddRange(pushList.Select(x => new SubmitTelexResultDto(x.MBLNO, false, $"错误:{ex.Message}")));
}
}
else
{
url += "/v1/submit";
var order = pushList[0];
try
{
// 提交电放
var body = new
{
user_key = key,
user_secret = secret,
web_user = webAccount.AppKey,
web_psw = webAccount.AppSecret,
web_code = order.CarrierId,
bno = order.MBLNO
};
_logger.Info("请求电放提交接口开始mblno={mblno}, url={url}, body={body}", order.MBLNO, url, body);
//var rtn = await url.SetBody(body).PostAsStringAsync();
var rtn = RequestHelper.Post(JsonConvert.SerializeObject(body), url, 180000);
_logger.Info("请求电放提交接口结束mblno={mblno}, rtn={rtn}", order.MBLNO, rtn);
var jsonRtn = JObject.Parse(rtn);
if (jsonRtn.GetIntValue("code") == 200)
{
_logger.Info("请求电放提交接口成功mblno={mblno}", order.MBLNO);
// 推送 电放已安排 状态
var pushModel = new EmbedServiceProjectStatusDto
{
businessId = order.Id.ToString(),
SourceType = 0,
StatusCodes = new List<EmbedServiceProjectStatusDetailDto> {
new EmbedServiceProjectStatusDetailDto { StatusCode = "DFYAP" } }
};
var saveStatusRlt = await _djyServiceStatusService.SaveServiceStatus(pushModel);
//if (saveStatusRlt.success)
//{
// seaComService.SetBookingGoodsStatus(bookingOrder.Id, true, "手动更新货物状态");
//}
_logger.Info("请求电放提交接口成功后推送 电放已安排 状态JSON={json} ,结果={rlt}", JsonConvert.SerializeObject(pushModel), JsonConvert.SerializeObject(saveStatusRlt));
result.Add(new SubmitTelexResultDto(order.MBLNO, true, null));
return await Task.FromResult(DataResult<List<SubmitTelexResultDto>>.Success(result));
}
else
{
var msg = jsonRtn.GetStringValue("msg")?.Replace("爬虫", "");
_logger.Info("请求电放提交接口失败mblno={mblno}, 电放服务返回:{msg}", order.MBLNO, msg);
result.Add(new SubmitTelexResultDto(order.MBLNO, false, $"电放服务返回:{msg}"));
}
}
catch (Exception ex)
{
_logger.Error("请求电放服务的过程中发生未知异常mblno={mblno}, ex={ex}", order.MBLNO, ex);
result.Add(new SubmitTelexResultDto(order.MBLNO, false, $"错误:{ex.Message}"));
}
}
if (result.Count > 0)
{
return await Task.FromResult(DataResult<List<SubmitTelexResultDto>>.FailedData(result));
}
else
{
return await Task.FromResult(DataResult<List<SubmitTelexResultDto>>.SuccessMsg("电放成功!"));
}
}
#endregion
}
}