Browse Source

1688回调

qianyi
shanji 3 years ago
parent
commit
ac0447f5c2
  1. 10
      BBWY.Server.API/Controllers/PurchaseOrderController.cs
  2. 52
      BBWY.Server.Business/MDSBusiness.cs
  3. 49
      BBWY.Server.Business/Order/OrderBusiness.cs
  4. 93
      BBWY.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs
  5. 3
      BBWY.Server.Business/TaskSchedulerManager.cs
  6. 61
      BBWY.Server.Model/Db/PurchaseScheme/PurchaseAccount.cs
  7. 30
      BBWY.Server.Model/Dto/Response/Vender/ShopResponse.cs

10
BBWY.Server.API/Controllers/PurchaseOrderController.cs

@ -62,5 +62,15 @@ namespace BBWY.Server.API.Controllers
{
purchaseOrderBusiness.FastCreateOrder(createOnlinePurchaseOrderRequest);
}
/// <summary>
/// 1688发货回调
/// </summary>
/// <param name="param"></param>
[HttpPost]
public void DeliveryCallbackFrom1688([FromBody]object param)
{
purchaseOrderBusiness.DeliveryCallbackFrom1688(param);
}
}
}

52
BBWY.Server.Business/MDSBusiness.cs

@ -0,0 +1,52 @@
using BBWY.Common.Http;
using BBWY.Common.Models;
using BBWY.Server.Model;
using BBWY.Server.Model.Dto;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
namespace BBWY.Server.Business
{
public class MDSBusiness : IDenpendency
{
private RestApiService restApiService;
private GlobalConfig globalConfig;
private IDictionary<string, string> mdsApiHeader;
public MDSBusiness(RestApiService restApiService, IOptions<GlobalConfig> options)
{
this.restApiService = restApiService;
this.globalConfig = options.Value;
mdsApiHeader = new Dictionary<string, string>() {
{ "qy","qy"}
};
}
public ShopResponse GetShopInfoByShopId(long shopId)
{
var shopResult = restApiService.SendRequest(globalConfig.MdsApi, "/TaskList/Shop/GetShopsByShopId", $"shopId={shopId}", mdsApiHeader, HttpMethod.Get, enableRandomTimeStamp: true);
if (shopResult.StatusCode != System.Net.HttpStatusCode.OK)
throw new Exception($"SyncOrder 获取店铺信息失败 shopId:{shopId} httpCode:{shopResult.StatusCode} httpContent:{shopResult.Content}");
var shopResponseJToken = JToken.Parse(shopResult.Content);
if (shopResponseJToken.Value<bool>("Success") != true)
throw new Exception($"SyncOrder 获取店铺信息失败 shopId:{shopId} ErrorMsg:{shopResponseJToken.Value<string>("Msg")}");
var shopJToken = shopResponseJToken["Data"].FirstOrDefault();
return new ShopResponse()
{
AppKey = shopJToken.Value<string>("AppKey"),
AppSecret = shopJToken.Value<string>("AppSecret"),
AppToken = shopJToken.Value<string>("AppToken"),
Platform = (Enums.Platform)shopJToken.Value<int>("PlatformId"),
VenderType = shopJToken.Value<string>("ShopType"),
ShopId = shopId,
Name = shopJToken.Value<string>("ShopName")
};
}
}
}

49
BBWY.Server.Business/Order/OrderBusiness.cs

@ -6,7 +6,6 @@ using BBWY.Server.Model;
using BBWY.Server.Model.Db;
using BBWY.Server.Model.Dto;
using FreeSql;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@ -26,10 +25,10 @@ namespace BBWY.Server.Business
private IFreeSql fsql;
private IDictionary<Enums.Platform, Action<JArray, long, string, string, string, string>> syncOrderMethodDic;
private IIdGenerator idGenerator;
private IDictionary<string, string> mdsApiHeader;
private TaskSchedulerManager taskSchedulerManager;
private MDSBusiness mdsBusiness;
public OrderBusiness(RestApiService restApiService, IConfiguration configuration, ILogger logger, IFreeSql fsql, IIdGenerator idGenerator, IOptions<GlobalConfig> options, TaskSchedulerManager taskSchedulerManager) : base(restApiService, options)
public OrderBusiness(RestApiService restApiService, ILogger logger, IFreeSql fsql, IIdGenerator idGenerator, IOptions<GlobalConfig> options, TaskSchedulerManager taskSchedulerManager, MDSBusiness mdsBusiness) : base(restApiService, options)
{
this.logger = logger;
this.fsql = fsql;
@ -39,9 +38,7 @@ namespace BBWY.Server.Business
{
{ Enums.Platform., SyncJDOrder }
};
mdsApiHeader = new Dictionary<string, string>() {
{ "qy","qy"}
};
this.mdsBusiness = mdsBusiness;
}
public OrderListResponse GetOrderList(SearchOrderRequest searchOrderRequest)
@ -666,44 +663,24 @@ namespace BBWY.Server.Business
/// <exception cref="Exception"></exception>
public void SyncOrder(long shopId, string orderId, DateTime? startTime = null, DateTime? endTime = null)
{
#region 获取店铺信息;
var shopResult = restApiService.SendRequest(globalConfig.MdsApi, "/TaskList/Shop/GetShopsByShopId", $"shopId={shopId}", mdsApiHeader, HttpMethod.Get, enableRandomTimeStamp: true);
if (shopResult.StatusCode != System.Net.HttpStatusCode.OK)
throw new Exception($"SyncOrder 获取店铺信息失败 shopId:{shopId} httpCode:{shopResult.StatusCode} httpContent:{shopResult.Content}");
var shopResponseJToken = JToken.Parse(shopResult.Content);
if (shopResponseJToken.Value<bool>("Success") != true)
throw new Exception($"SyncOrder 获取店铺信息失败 shopId:{shopId} ErrorMsg:{shopResponseJToken.Value<string>("Msg")}");
#endregion
var shopJToken = shopResponseJToken["Data"].FirstOrDefault();
var shop = mdsBusiness.GetShopInfoByShopId(shopId);
try
{
var appKey = shopJToken.Value<string>("AppKey");
var appSecret = shopJToken.Value<string>("AppSecret");
var appToken = shopJToken.Value<string>("AppToken");
var platformId = shopJToken.Value<int?>("PlatformId");
var shopType = shopJToken.Value<string>("ShopType");
if (string.IsNullOrEmpty(appKey) || string.IsNullOrEmpty(appSecret) || string.IsNullOrEmpty(appToken) || platformId == null)
throw new Exception("缺少店铺必要信息");
var platform = (Enums.Platform)platformId;
if (!syncOrderMethodDic.ContainsKey(platform))
if (!syncOrderMethodDic.ContainsKey(shop.Platform))
throw new Exception("不支持的平台");
var relayAPIHost = GetPlatformRelayAPIHost((Enums.Platform)platformId);
var relayAPIHost = GetPlatformRelayAPIHost(shop.Platform);
var orderListApiResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetOrderList", new SearchPlatformOrderRequest()
{
StartDate = startTime ?? DateTime.Now.AddHours(-3),
EndDate = endTime ?? DateTime.Now,
AppKey = appKey,
AppSecret = appSecret,
AppToken = appToken,
AppKey = shop.AppKey,
AppSecret = shop.AppSecret,
AppToken = shop.AppToken,
PageIndex = 1,
PageSize = 100,
Platform = platform,
JDColType = string.IsNullOrEmpty(shopType) ? "0" : shopType,
Platform = shop.Platform,
JDColType = string.IsNullOrEmpty(shop.VenderType) ? "0" : shop.VenderType,
SaveResponseLog = true,
OrderId = orderId
}, null, HttpMethod.Post);
@ -717,11 +694,11 @@ namespace BBWY.Server.Business
if (orderListResponse.Data == null || orderListResponse.Data.Count == 0)
return;
syncOrderMethodDic[platform](orderListResponse.Data, shopId, relayAPIHost, appKey, appSecret, appToken);
syncOrderMethodDic[shop.Platform](orderListResponse.Data, shopId, relayAPIHost, shop.AppKey, shop.AppSecret, shop.AppToken);
}
catch (Exception ex)
{
var shopData = JsonConvert.SerializeObject(shopJToken);
var shopData = JsonConvert.SerializeObject(shop);
logger.Error(ex, $"SyncOrder ShopData:{shopData}");
}
}

93
BBWY.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs

@ -1,11 +1,17 @@
using BBWY.Common.Extensions;
using BBWY.Common.Models;
using BBWY.Server.Model;
using BBWY.Server.Model.Db;
using BBWY.Server.Model.Dto;
using FreeSql;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Yitter.IdGenerator;
namespace BBWY.Server.Business
@ -13,9 +19,17 @@ namespace BBWY.Server.Business
public class PurchaseOrderBusiness : BaseBusiness, IDenpendency
{
private IEnumerable<PlatformSDKBusiness> platformSDKBusinessList;
public PurchaseOrderBusiness(IFreeSql fsql, NLog.ILogger logger, IIdGenerator idGenerator, IEnumerable<PlatformSDKBusiness> platformSDKBusinessList) : base(fsql, logger, idGenerator)
private TaskSchedulerManager taskSchedulerManager;
private OrderBusiness orderBusiness;
private MDSBusiness mdsBusiness;
public PurchaseOrderBusiness(IFreeSql fsql, NLog.ILogger logger, IIdGenerator idGenerator, IEnumerable<PlatformSDKBusiness> platformSDKBusinessList, TaskSchedulerManager taskSchedulerManager, OrderBusiness orderBusiness, MDSBusiness mdsBusiness) : base(fsql, logger, idGenerator)
{
this.platformSDKBusinessList = platformSDKBusinessList;
this.taskSchedulerManager = taskSchedulerManager;
this.orderBusiness = orderBusiness;
this.mdsBusiness = mdsBusiness;
}
public void AddPurchaseOrder(AddPurchaseOrderRequest addPurchaseOrderRequest)
@ -187,5 +201,82 @@ namespace BBWY.Server.Business
.ExecuteAffrows();
});
}
public void DeliveryCallbackFrom1688(object param)
{
var orderJObject = JObject.Parse(param.ToString());
var purchaseOrderId = orderJObject.Value<string>("orderId");
Task.Factory.StartNew(() => DeliveryCallback(purchaseOrderId, Enums.Platform.), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
}
public void DeliveryCallbackFromPDD(object param)
{
var orderJObject = JObject.Parse(param.ToString());
var purchaseOrderId = orderJObject.Value<string>("orderId");
Task.Factory.StartNew(() => DeliveryCallback(purchaseOrderId, Enums.Platform.), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
}
/// <summary>
/// 采购平台回调核心流程
/// </summary>
/// <param name="purchaseOrderId"></param>
/// <param name="callbackPlatform"></param>
private void DeliveryCallback(string purchaseOrderId, Enums.Platform callbackPlatform)
{
string currentProgress = string.Empty;
try
{
#region 查询采购单
currentProgress = "查询采购单";
var orderDropshipping = fsql.Select<OrderDropShipping>().Where(o => o.PurchaseOrderId == purchaseOrderId).ToOne();
if (orderDropshipping == null)
throw new Exception("未查询到采购单号");
#endregion
#region 查询采购账号
currentProgress = "查询采购账号";
var purchaseAccount = fsql.Select<PurchaseAccount>().Where(pa => pa.AccountName == orderDropshipping.BuyerAccount &&
pa.PurchasePlatformId == callbackPlatform).ToOne();
if (purchaseAccount == null)
throw new Exception($"未查询到采购账号{orderDropshipping.BuyerAccount}");
#endregion
#region 获取采购单的物流信息
currentProgress = "获取采购单的物流信息";
#endregion
#region 查询采购账号的归属店铺
currentProgress = "查询采购账号归属店铺";
var shop = mdsBusiness.GetShopInfoByShopId(purchaseAccount.ShopId.Value);
#endregion
#region 获取目标平台的物流公司列表
currentProgress = "获取目标平台物流公司列表";
#endregion
#region 物流公司翻译
currentProgress = "将采购单的物流公司翻译为店铺平台的物流公司";
#endregion
#region 店铺平台订单出库
currentProgress = "店铺平台订单出库";
orderBusiness.OutStock(new OutStockRequest()
{
AppKey = shop.AppKey,
AppSecret = shop.AppSecret,
AppToken = shop.AppToken,
OrderId = orderDropshipping.OrderId,
Platform = shop.Platform,
WayBillNo = "",
LogisticsId = ""
});
#endregion
}
catch (Exception ex)
{
logger.Error(ex, $"回调平台{callbackPlatform},采购单号{purchaseOrderId},执行进度[{currentProgress}]");
}
}
}
}

3
BBWY.Server.Business/TaskSchedulerManager.cs

@ -6,9 +6,12 @@ namespace BBWY.Server.Business
{
public LimitedConcurrencyLevelTaskScheduler SyncOrderTaskScheduler { get; private set; }
public LimitedConcurrencyLevelTaskScheduler PurchaseOrderCallbackTaskScheduler { get; private set; }
public TaskSchedulerManager()
{
SyncOrderTaskScheduler = new LimitedConcurrencyLevelTaskScheduler(10);
PurchaseOrderCallbackTaskScheduler = new LimitedConcurrencyLevelTaskScheduler(10);
}
}
}

61
BBWY.Server.Model/Db/PurchaseScheme/PurchaseAccount.cs

@ -0,0 +1,61 @@
using FreeSql.DataAnnotations;
using System;
namespace BBWY.Server.Model.Db
{
/// <summary>
/// 采购账号表
/// </summary>
[Table(Name = "purchaseaccount", DisableSyncStructure = true)]
public partial class PurchaseAccount
{
/// <summary>
/// 主键
/// </summary>
[Column(StringLength = 50, IsPrimary = true, IsNullable = false)]
public string Id { get; set; }
public string AccountName { get; set; }
public string AppKey { get; set; }
public string AppSecret { get; set; }
public string AppToken { get; set; }
/// <summary>
/// 创建时间
/// </summary>
[Column(DbType = "datetime")]
public DateTime CreateTime { get; set; }
/// <summary>
/// 创建人Id
/// </summary>
[Column(StringLength = 50)]
public string CreatorId { get; set; }
/// <summary>
/// 否已删除
/// </summary>
[Column(DbType = "tinyint(4)")]
public sbyte Deleted { get; set; }
[Column(DbType = "int(1)", MapType = typeof(int?))]
public Enums.Platform? PurchasePlatformId { get; set; }
/// <summary>
/// 采购账号归属店铺ID
/// </summary>
[Column(DbType = "bigint(1)")]
public long? ShopId { get; set; }
}
}

30
BBWY.Server.Model/Dto/Response/Vender/ShopResponse.cs

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Text;
using static BBWY.Server.Model.Enums;
namespace BBWY.Server.Model.Dto
{
public class ShopResponse
{
public long ShopId { get; set; }
public string Name { get; set; }
/// <summary>
/// 商家类型
/// </summary>
public string VenderType { get; set; }
/// <summary>
/// 店铺平台
/// </summary>
public Platform Platform { get; set; }
public string AppKey { get; set; }
public string AppSecret { get; set; }
public string AppToken { get; set; }
}
}
Loading…
Cancel
Save