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.
BookingHeChuan/Myshipping.Application/Service/BookingOrder/BookingMSKSPOTAPIService.cs

303 lines
12 KiB
C#

using Microsoft.Extensions.Logging;
using Myshipping.Application.Entity;
using Myshipping.Core.Service;
using Myshipping.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Furion.FriendlyException;
using Furion.JsonSerialization;
using Furion.RemoteRequest.Extensions;
using Furion;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
namespace Myshipping.Application.Service.BookingOrder
{
/// <summary>
/// 马士基 SPOT
/// </summary>
[ApiDescriptionSettings("Application", Name = "BookingMSKSPOTAPI", Order = 10)]
public class BookingMSKSPOTAPIService: IBookingMSKSPOTAPIService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<BookingDeliveryRecord> _bookingDeliveryRecordRep;
private readonly SqlSugarRepository<BookingDeliveryRecordCtn> _bookingDeliveryRecordCtnRep;
private readonly ISysCacheService _cache;
private readonly IDjyWebsiteAccountConfigService _webAccountConfig;
private readonly ILogger<BookingMSKSPOTAPIService> _logger;
private readonly ISysDataUserMenu _sysDataUserMenuService;
const string CONST_MSK_API_COMMODITY_URL = "MSKApiCommodity";
const string CONST_MSK_API_BOOKING_URL = "MSKApiBooking";
const string CONST_MSK_SPOT_API_QUERY_SCHEDULE_RATE_URL = "MSKSPOTScheduleRate";
const string CONST_MSK_SPOT_API_LOCATION_URL = "MSKSPOTApilocation";
public BookingMSKSPOTAPIService(ILogger<BookingMSKSPOTAPIService> logger, ISysCacheService cache,
IDjyWebsiteAccountConfigService webAccountConfig, SqlSugarRepository<BookingDeliveryRecord> bookingDeliveryRecordRep,
ISysDataUserMenu sysDataUserMenuService,
SqlSugarRepository<BookingDeliveryRecordCtn> bookingDeliveryRecordCtnRep)
{
_logger = logger;
_cache = cache;
_webAccountConfig = webAccountConfig;
_bookingDeliveryRecordRep = bookingDeliveryRecordRep;
_bookingDeliveryRecordCtnRep = bookingDeliveryRecordCtnRep;
_sysDataUserMenuService = sysDataUserMenuService;
}
#region 检索始发地、目的港口信息
/// <summary>
/// 检索始发地、目的港口信息
/// </summary>
/// <param name="model">请求详情</param>
/// <returns>返回检索结果</returns>
[HttpPost("/BookingMSKSPOTAPI/SearchLocations")]
public async Task<List<MSKAPISPOTSearchLocationResultDataDto>> SearchLocations(QuerySPOTLocationsDto model)
{
List<MSKAPISPOTSearchLocationResultDataDto> list = new List<MSKAPISPOTSearchLocationResultDataDto>();
/*
MSKApiCommodity
*/
try
{
if (string.IsNullOrWhiteSpace(model.cityName))
throw Oops.Oh("港口或城市名称不能为空");
if (string.IsNullOrWhiteSpace(model.carrierCode))
throw Oops.Oh("服务船公司不能为空");
if (string.IsNullOrWhiteSpace(model.carrierId))
throw Oops.Oh("船公司代码不能为空");
if (model.cityName.Length < 3)
throw Oops.Oh("港口或城市名称至少输入3个以上字符");
string queryUrl = string.Empty;
if (model.carrierId.Equals("MSK", StringComparison.OrdinalIgnoreCase))
{
queryUrl = _cache.GetAllDictData().GetAwaiter().GetResult()
.FirstOrDefault(x => x.TypeCode == "url_set" && x.Code == CONST_MSK_SPOT_API_LOCATION_URL)?.Value;
}
else
{
throw Oops.Oh($"当前船公司 {model.carrierId} 未配置相应的请求接口");
}
if (string.IsNullOrWhiteSpace(queryUrl))
throw Oops.Oh("未配置商品请求接口地址,请联系管理员");
var webAccountConfig = _webAccountConfig
.GetAccountConfig("MSKSpotApi", UserManager.UserId).GetAwaiter().GetResult();
if (webAccountConfig == null)
throw Oops.Oh("未配检索始发地、目的港口,请先配置个人账户 类型-MSKApi");
MSKAPISPOTSearchLocationDto queryInfo = new MSKAPISPOTSearchLocationDto
{
userKey = App.Configuration["MSKAPIDjyUserKey"],
userSecret = App.Configuration["MSKAPIDjyUserSecret"],
operatingEnvironment = App.Configuration["MSKAPIOPEnvironment"],
spot_api_key = webAccountConfig.Account,
brandScac = model.carrierCode,
originPrefix = model.cityName
};
MSKAPISPOTSearchLocationResultDto resultInfo = null;
var rlt = await queryUrl.SetBody(queryInfo)
.PostAsStringAsync();
if (!string.IsNullOrWhiteSpace(rlt))
{
try
{
resultInfo = JSON.Deserialize<MSKAPISPOTSearchLocationResultDto>(rlt);
}
catch (Exception ex)
{
_logger.LogInformation($"请求MSK API检索始发地、目的港口异常原因{ex.Message}");
throw Oops.Bah($"请求MSK API检索始发地、目的港口异常原因{ex.Message}");
}
}
if (resultInfo != null && resultInfo.code == 200
&& resultInfo.data != null && resultInfo.data.Count > 0)
{
list = resultInfo.data;
}
}
catch (Exception ex)
{
_logger.LogError($"检索始发地、目的港口异常req={JSON.Serialize(model)} 原因:{ex.Message}");
throw Oops.Bah($"检索始发地、目的港口失败,{ex.Message}");
}
return list;
}
#endregion
#region 检索海运船期详情
/// <summary>
/// 检索海运船期详情
/// </summary>
/// <param name="model">请求船期详情</param>
/// <returns>返回船期结果</returns>
[HttpPost("/BookingMSKSPOTAPI/SearchShipSailingSchedule")]
public async Task<List<MSKAPISPOTSearchScheduleRateResultDataDto>> SearchShipSailingSchedule(QueryMSKSPOTShipSailingScheduleDto model)
{
List<MSKAPISPOTSearchScheduleRateResultDataDto> list = new List<MSKAPISPOTSearchScheduleRateResultDataDto>();
/*
MSKApiSailingSchedulePoint2Point
*/
try
{
if (string.IsNullOrWhiteSpace(model.originGeoId))
throw Oops.Oh($"起运地不能为空");
if (string.IsNullOrWhiteSpace(model.destinationGeoId))
throw Oops.Oh($"目的地不能为空");
if (string.IsNullOrWhiteSpace(model.brandScac))
throw Oops.Oh($"服务船公司不能为空");
if (string.IsNullOrWhiteSpace(model.carrierId))
throw Oops.Oh("船公司代码不能为空");
if (string.IsNullOrWhiteSpace(model.departureDate))
throw Oops.Oh($"预计离港日期不能为空");
DateTime etd = DateTime.MinValue;
if (!DateTime.TryParse(model.departureDate, out etd))
throw Oops.Oh($"预计离港日期格式错误");
if (string.IsNullOrWhiteSpace(model.containerType))
throw Oops.Oh($"箱型不能为空");
string queryUrl = string.Empty;
if (model.carrierId.Equals("MSK", StringComparison.OrdinalIgnoreCase))
{
queryUrl = _cache.GetAllDictData().GetAwaiter().GetResult()
.FirstOrDefault(x => x.TypeCode == "url_set" && x.Code == CONST_MSK_SPOT_API_QUERY_SCHEDULE_RATE_URL)?.Value;
}
else
{
throw Oops.Oh($"当前船公司 {model.carrierId} 未配置相应的请求接口");
}
if (string.IsNullOrWhiteSpace(queryUrl))
throw Oops.Oh("未配置查询船期请求接口地址,请联系管理员");
var webAccountConfig = _webAccountConfig
.GetAccountConfig("MSKSpotApi", UserManager.UserId).GetAwaiter().GetResult();
if (webAccountConfig == null)
throw Oops.Oh("未配置个人账户,请先配置个人账户 类型-MSKApi");
MSKAPISPOTSearchScheduleRateDto queryInfo = new MSKAPISPOTSearchScheduleRateDto
{
userKey = App.Configuration["MSKAPIDjyUserKey"],
userSecret = App.Configuration["MSKAPIDjyUserSecret"],
operatingEnvironment = App.Configuration["MSKAPIOPEnvironment"],
spot_api_key = webAccountConfig.Account,
originServiceMode = model.originServiceMode,
destinationServiceMode = model.destinationServiceMode,
originGeoId = model.originGeoId,
destinationGeoId = model.destinationGeoId,
brandScac = model.brandScac,
departureDate = etd.ToString("yyyy-MM-dd"),
};
//有时候船期需要带上箱型检索
if (!string.IsNullOrWhiteSpace(model.containerType))
{
//这里需要翻译一下箱型
var ctnCodeMappingList = _cache.GetAllMappingCtn().GetAwaiter().GetResult().ToList();
if (ctnCodeMappingList.Count > 0)
ctnCodeMappingList = ctnCodeMappingList.Where(x => x.CarrierCode == "MSK" && x.Module == "BOOK_MSK_SPOT_API").ToList();
var ctnMapping = ctnCodeMappingList.FirstOrDefault(t => t.Code.Equals(model.containerType));
if (ctnMapping == null)
{
queryInfo.containerType = model.containerType;
}
else
{
queryInfo.containerType = ctnMapping.MapCode;
}
}
if (model.containerCount.HasValue)
{
queryInfo.containerCount = model.containerCount.Value;
}
if (model.containerWeight.HasValue)
{
queryInfo.containerWeight = model.containerWeight.Value;
}
MSKAPISPOTSearchScheduleRateResultDto resultInfo = null;
var jsonBody = Newtonsoft.Json.JsonConvert.SerializeObject(queryInfo);
var rlt = await queryUrl.SetBody(jsonBody)
.PostAsStringAsync();
_logger.LogInformation($"请求MSK SPOT API查询船期请求{jsonBody}");
if (!string.IsNullOrWhiteSpace(rlt))
{
try
{
_logger.LogInformation($"请求MSK SPOT API查询船期结果{rlt}");
resultInfo = JSON.Deserialize<MSKAPISPOTSearchScheduleRateResultDto>(rlt);
}
catch (Exception ex)
{
_logger.LogInformation($"请求MSK SPOT API查询船期异常原因{ex.Message}");
throw Oops.Bah($"请求MSK API查询船期异常原因{ex.Message}");
}
}
if (resultInfo != null && resultInfo.code == 200
&& resultInfo.data != null && resultInfo.data.Count > 0)
{
list = resultInfo.data;
}
else
{
throw Oops.Bah($"检索海运船期详情失败,{resultInfo.msg}");
}
}
catch (Exception ex)
{
_logger.LogError($"检索海运船期详情异常req={JSON.Serialize(model)} 原因:{ex.Message}");
throw Oops.Bah($"检索海运船期详情失败,{ex.Message}");
}
return list;
}
#endregion
}
}