|
|
@ -15,6 +15,7 @@ using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Net.Http; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Yitter.IdGenerator; |
|
|
|
|
|
|
|
namespace BBWY.Server.Business |
|
|
@ -26,12 +27,14 @@ namespace BBWY.Server.Business |
|
|
|
private IDictionary<Enums.Platform, Action<JArray, long, string, string, string, string>> syncOrderMethodDic; |
|
|
|
private IIdGenerator idGenerator; |
|
|
|
private IDictionary<string, string> mdsApiHeader; |
|
|
|
private TaskSchedulerManager taskSchedulerManager; |
|
|
|
|
|
|
|
public OrderBusiness(RestApiService restApiService, IConfiguration configuration, ILogger logger, IFreeSql fsql, IIdGenerator idGenerator, IOptions<GlobalConfig> options) : base(restApiService, options) |
|
|
|
public OrderBusiness(RestApiService restApiService, IConfiguration configuration, ILogger logger, IFreeSql fsql, IIdGenerator idGenerator, IOptions<GlobalConfig> options, TaskSchedulerManager taskSchedulerManager) : base(restApiService, options) |
|
|
|
{ |
|
|
|
this.logger = logger; |
|
|
|
this.fsql = fsql; |
|
|
|
this.idGenerator = idGenerator; |
|
|
|
this.taskSchedulerManager = taskSchedulerManager; |
|
|
|
syncOrderMethodDic = new Dictionary<Enums.Platform, Action<JArray, long, string, string, string, string>>() |
|
|
|
{ |
|
|
|
{ Enums.Platform.京东, SyncJDOrder } |
|
|
@ -45,6 +48,8 @@ namespace BBWY.Server.Business |
|
|
|
{ |
|
|
|
if (searchOrderRequest.OrderState == Enums.OrderState.已取消) |
|
|
|
searchOrderRequest.ExcludeCanceled = false; |
|
|
|
if (searchOrderRequest.EndDate != null) |
|
|
|
searchOrderRequest.EndDate = searchOrderRequest.EndDate.Value.Date.AddDays(1).AddSeconds(-1); |
|
|
|
|
|
|
|
var select = fsql.Select<Order, OrderConsignee, OrderCost, OrderDropShipping>().LeftJoin((o, ocs, oct, ods) => o.Id == ocs.OrderId) |
|
|
|
.LeftJoin((o, ocs, oct, ods) => o.Id == oct.OrderId) |
|
|
@ -651,7 +656,15 @@ namespace BBWY.Server.Business |
|
|
|
.ExecuteAffrows(); |
|
|
|
} |
|
|
|
|
|
|
|
public void SyncOrder(long shopId, string orderId) |
|
|
|
/// <summary>
|
|
|
|
/// 同步订单
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="shopId"></param>
|
|
|
|
/// <param name="orderId"></param>
|
|
|
|
/// <param name="startTime">默认3小时前</param>
|
|
|
|
/// <param name="endTime">默认当前时间</param>
|
|
|
|
/// <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); |
|
|
@ -682,8 +695,8 @@ namespace BBWY.Server.Business |
|
|
|
var relayAPIHost = GetPlatformRelayAPIHost((Enums.Platform)platformId); |
|
|
|
var orderListApiResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetOrderList", new SearchPlatformOrderRequest() |
|
|
|
{ |
|
|
|
StartDate = DateTime.Now.AddHours(-3), |
|
|
|
EndDate = DateTime.Now, |
|
|
|
StartDate = startTime ?? DateTime.Now.AddHours(-3), |
|
|
|
EndDate = endTime ?? DateTime.Now, |
|
|
|
AppKey = appKey, |
|
|
|
AppSecret = appSecret, |
|
|
|
AppToken = appToken, |
|
|
@ -713,6 +726,62 @@ namespace BBWY.Server.Business |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public void SyncOrderByDate(SyncOrderByDateRequest syncOrderByDateRequest) |
|
|
|
{ |
|
|
|
if (fsql.Select<OrderSyncTask>().Where(ost => ost.ShopId == syncOrderByDateRequest.ShopId && ost.State == Enums.OrderSyncState.Running).Any()) |
|
|
|
throw new BusinessException("存在未结束的同步任务,请稍后同步"); |
|
|
|
syncOrderByDateRequest.EndTime = syncOrderByDateRequest.EndTime.Date.AddDays(1).AddSeconds(-1); |
|
|
|
if ((syncOrderByDateRequest.EndTime - syncOrderByDateRequest.StartTime).Days > 7) |
|
|
|
throw new BusinessException("同步任务时差最长7天"); |
|
|
|
|
|
|
|
var orderSyncTask = new OrderSyncTask() |
|
|
|
{ |
|
|
|
Id = idGenerator.NewLong(), |
|
|
|
ShopId = syncOrderByDateRequest.ShopId, |
|
|
|
State = Enums.OrderSyncState.Running, |
|
|
|
SyncStartTime = syncOrderByDateRequest.StartTime, |
|
|
|
SyncEndTime = syncOrderByDateRequest.EndTime |
|
|
|
}; |
|
|
|
fsql.Insert(orderSyncTask).ExecuteAffrows(); |
|
|
|
Task.Factory.StartNew(() => |
|
|
|
{ |
|
|
|
var currentStartTime = syncOrderByDateRequest.StartTime; |
|
|
|
var currentEndTime = currentStartTime.AddHours(3); |
|
|
|
var keeploop = true; |
|
|
|
while (keeploop) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
SyncOrder(syncOrderByDateRequest.ShopId, String.Empty, currentStartTime, currentEndTime); |
|
|
|
} |
|
|
|
catch (Exception ex) { } |
|
|
|
finally |
|
|
|
{ |
|
|
|
var lessHour = (syncOrderByDateRequest.EndTime - currentEndTime).TotalHours; |
|
|
|
if (lessHour > 3) |
|
|
|
{ |
|
|
|
currentStartTime = currentStartTime.AddHours(3); |
|
|
|
currentEndTime = currentEndTime.AddHours(3); |
|
|
|
} |
|
|
|
else if (lessHour > 0 && lessHour < 3) |
|
|
|
{ |
|
|
|
currentStartTime = currentEndTime; |
|
|
|
currentEndTime = syncOrderByDateRequest.EndTime; |
|
|
|
} |
|
|
|
else if (lessHour <= 0) |
|
|
|
{ |
|
|
|
keeploop = false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
try |
|
|
|
{ |
|
|
|
fsql.Update<OrderSyncTask>(orderSyncTask.Id).Set(ost => ost.State, Enums.OrderSyncState.End).ExecuteAffrows(); |
|
|
|
} |
|
|
|
catch (Exception ex) { } |
|
|
|
}, System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.SyncOrderTaskScheduler); |
|
|
|
} |
|
|
|
|
|
|
|
private void SyncJDOrder(JArray orderTokenJArray, long shopId, string relayAPIHost, string appKey, string appSecret, string appToken) |
|
|
|
{ |
|
|
|
var orderTokenList = orderTokenJArray.Where(o => o.Value<decimal>("orderTotalPrice") != 0); |
|
|
@ -738,7 +807,6 @@ namespace BBWY.Server.Business |
|
|
|
dbOrderCostDetailList = fsql.Select<OrderCostDetail>().Where(ocd => interfaceCanceledOrderIdList.Contains(ocd.OrderId)).ToList(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var orderSkuIds = new List<string>(); |
|
|
|
foreach (var orderJToken in orderTokenList) |
|
|
|
{ |
|
|
|