步步为盈
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
5.9 KiB

3 years ago
using BBWY._1688SDK.entity.OrderPreview;
using BBWY.Common.Http;
using BBWY.Common.Models;
3 years ago
using BBWY.Server.Model;
using BBWY.Server.Model.Dto;
using com.alibaba.openapi.client;
using com.alibaba.openapi.client.policy;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json.Linq;
using NLog;
using System.Collections.Generic;
using System.Linq;
3 years ago
namespace BBWY.Server.Business
{
public class _1688Business : PlatformSDKBusiness
{
public override Enums.Platform Platform => Enums.Platform.;
private RestApiService restApiService;
private _1688TradeTypeCompare _1688TradeTypeCompare;
3 years ago
public _1688Business(IMemoryCache memoryCache, ILogger logger, RestApiService restApiService) : base(memoryCache, logger)
3 years ago
{
this.restApiService = restApiService;
_1688TradeTypeCompare = new _1688TradeTypeCompare();
3 years ago
}
private SyncAPIClient GetSyncAPIClient(string appKey, string appSecret)
{
if (!memoryCache.TryGetValue(appKey, out SyncAPIClient syncAPIClient))
{
syncAPIClient = new SyncAPIClient(appKey, appSecret, restApiService);
3 years ago
memoryCache.Set(appKey, syncAPIClient, expirationTimeSpan);
}
return syncAPIClient;
}
public override PreviewOrderResponse PreviewOrder(PreviewOrderReuqest previewOrderReuqest)
{
var client = GetSyncAPIClient(previewOrderReuqest.AppKey, previewOrderReuqest.AppSecret);
RequestPolicy reqPolicy = new RequestPolicy();
reqPolicy.HttpMethod = "POST";
reqPolicy.NeedAuthorization = false;
reqPolicy.RequestSendTimestamp = false;
3 years ago
reqPolicy.UseHttps = false;
reqPolicy.UseSignture = true;
reqPolicy.AccessPrivateApi = false;
Request request = new Request();
APIId apiId = new APIId();
apiId.Name = "alibaba.createOrder.preview";
apiId.NamespaceValue = "com.alibaba.trade";
apiId.Version = 1;
request.ApiId = apiId;
var param = new CreateOrderPreview()
{
addressParam = new AddressParam()
{
fullName = previewOrderReuqest.Consignee.ContactName,
mobile = previewOrderReuqest.Consignee.Mobile,
phone = previewOrderReuqest.Consignee.TelePhone,
3 years ago
postCode = "000000",
address = previewOrderReuqest.Consignee.Address,
provinceText = previewOrderReuqest.Consignee.Province,
cityText = previewOrderReuqest.Consignee.City,
areaText = previewOrderReuqest.Consignee.County,
townText = previewOrderReuqest.Consignee.Town
3 years ago
},
cargoParamList = new List<CargoParam>(),
flow = "general"
};
foreach (var cargo in previewOrderReuqest.CargoParamList)
{
param.cargoParamList.Add(new CargoParam()
{
3 years ago
offerId = long.Parse(cargo.ProductId),
3 years ago
specId = cargo.SpecId,
quantity = cargo.Quantity
});
}
request.RequestEntity = param;
if (!string.IsNullOrEmpty(previewOrderReuqest.AppToken))
request.AccessToken = previewOrderReuqest.AppToken;
var result = client.NewRequest(request, reqPolicy);
if (result.Value<bool>("success") != true)
3 years ago
throw new BusinessException(result.Value<string>("errorMsg")) { Code = 0 };
3 years ago
var orderPreviewResuslt = (JArray)result["orderPreviewResuslt"];
List<JToken> intersectTradeModeList = new List<JToken>();
foreach (var orderPreviewJToken in orderPreviewResuslt)
{
if (orderPreviewJToken["tradeModelList"] == null)
throw new BusinessException("当前交易不可通过API下单,请使用1688网页交易 [交易模式列表为空]");
var tradeModeJArray = ((JArray)orderPreviewJToken["tradeModelList"]).Where(tradeJToken => tradeJToken.Value<bool>("opSupport"));
if (tradeModeJArray.Count() == 0)
throw new BusinessException("当前交易不可通过API下单,请使用1688网页交易 [没有支持开放平台下单的交易模式]");
if (intersectTradeModeList.Count() == 0)
intersectTradeModeList.AddRange(tradeModeJArray);
else
intersectTradeModeList = intersectTradeModeList.Intersect(tradeModeJArray, _1688TradeTypeCompare).ToList();
}
if (intersectTradeModeList.Count() == 0)
throw new BusinessException("当前交易不可通过API下单,请使用1688网页交易 [多个拆单之间没有相同的交易模式]");
3 years ago
return new PreviewOrderResponse()
{
FreightAmount = orderPreviewResuslt.Sum(jt => jt.Value<decimal>("sumCarriage")) / 100M,
ProductAmount = orderPreviewResuslt.Sum(jt => jt.Value<decimal>("sumPaymentNoCarriage")) / 100M,
TotalAmount = orderPreviewResuslt.Sum(jt => jt.Value<decimal>("sumPayment")) / 100M,
OrderTradeType = new OrderTradeTypeResponse()
{
Code = intersectTradeModeList.First().Value<string>("tradeType"),
Name = intersectTradeModeList.First().Value<string>("name"),
}
3 years ago
};
}
}
public class _1688TradeTypeCompare : IEqualityComparer<JToken>
{
public bool Equals(JToken x, JToken y)
{
return x.Value<string>("tradeType").Equals(y.Value<string>("tradeType"));
}
public int GetHashCode(JToken obj)
{
return obj.GetHashCode();
}
}
3 years ago
}