29 changed files with 825 additions and 713 deletions
@ -1,44 +1,42 @@ |
|||
using BBWY.Common.Http; |
|||
using BBWY.Common.Models; |
|||
using BBWY.Server.Model; |
|||
using BBWY.Common.Models; |
|||
using BBWY.Server.Model.Dto; |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.Options; |
|||
using Newtonsoft.Json; |
|||
using System.Collections.Generic; |
|||
using System.Net.Http; |
|||
using System.Linq; |
|||
|
|||
namespace BBWY.Server.Business |
|||
{ |
|||
public class ProductBusiness : BasePlatformRelayBusiness, IDenpendency |
|||
public class ProductBusiness : IDenpendency |
|||
{ |
|||
public ProductBusiness(RestApiService restApiService, IConfiguration configuration, IOptions<GlobalConfig> options, YunDingBusiness yunDingBusiness) : base(restApiService, options, yunDingBusiness) |
|||
private IEnumerable<PlatformSDKBusiness> platformSDKBusinessList; |
|||
public ProductBusiness(IEnumerable<PlatformSDKBusiness> platformSDKBusinessList) |
|||
{ |
|||
|
|||
this.platformSDKBusinessList = platformSDKBusinessList; |
|||
} |
|||
|
|||
public ProductListResponse GetProductList(SearchProductRequest searchProductRequest) |
|||
{ |
|||
var relayAPIHost = GetPlatformRelayAPIHost(searchProductRequest.Platform); |
|||
var sendResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetProductList", searchProductRequest, GetYunDingRequestHeader(), HttpMethod.Post); |
|||
if (sendResult.StatusCode != System.Net.HttpStatusCode.OK) |
|||
throw new BusinessException(sendResult.Content) { Code = (int)sendResult.StatusCode }; |
|||
var response = JsonConvert.DeserializeObject<ApiResponse<ProductListResponse>>(sendResult.Content); |
|||
if (!response.Success) |
|||
throw new BusinessException(response.Msg) { Code = response.Code }; |
|||
return response.Data; |
|||
//var relayAPIHost = GetPlatformRelayAPIHost(searchProductRequest.Platform);
|
|||
//var sendResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetProductList", searchProductRequest, GetYunDingRequestHeader(), HttpMethod.Post);
|
|||
//if (sendResult.StatusCode != System.Net.HttpStatusCode.OK)
|
|||
// throw new BusinessException(sendResult.Content) { Code = (int)sendResult.StatusCode };
|
|||
//var response = JsonConvert.DeserializeObject<ApiResponse<ProductListResponse>>(sendResult.Content);
|
|||
//if (!response.Success)
|
|||
// throw new BusinessException(response.Msg) { Code = response.Code };
|
|||
//return response.Data;
|
|||
return platformSDKBusinessList.FirstOrDefault(p => p.Platform == searchProductRequest.Platform).GetProductList(searchProductRequest); |
|||
} |
|||
|
|||
public IList<ProductSkuResponse> GetProductSkuList(SearchProductSkuRequest searchProductSkuRequest) |
|||
{ |
|||
var relayAPIHost = GetPlatformRelayAPIHost(searchProductSkuRequest.Platform); |
|||
var sendResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetProductSkuList", searchProductSkuRequest, GetYunDingRequestHeader(), HttpMethod.Post); |
|||
if (sendResult.StatusCode != System.Net.HttpStatusCode.OK) |
|||
throw new BusinessException(sendResult.Content) { Code = (int)sendResult.StatusCode }; |
|||
var response = JsonConvert.DeserializeObject<ApiResponse<IList<ProductSkuResponse>>>(sendResult.Content); |
|||
if (!response.Success) |
|||
throw new BusinessException(response.Msg) { Code = response.Code }; |
|||
return response.Data; |
|||
//var relayAPIHost = GetPlatformRelayAPIHost(searchProductSkuRequest.Platform);
|
|||
//var sendResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetProductSkuList", searchProductSkuRequest, GetYunDingRequestHeader(), HttpMethod.Post);
|
|||
//if (sendResult.StatusCode != System.Net.HttpStatusCode.OK)
|
|||
// throw new BusinessException(sendResult.Content) { Code = (int)sendResult.StatusCode };
|
|||
//var response = JsonConvert.DeserializeObject<ApiResponse<IList<ProductSkuResponse>>>(sendResult.Content);
|
|||
//if (!response.Success)
|
|||
// throw new BusinessException(response.Msg) { Code = response.Code };
|
|||
//return response.Data;
|
|||
return platformSDKBusinessList.FirstOrDefault(p => p.Platform == searchProductSkuRequest.Platform).GetProductSkuList(searchProductSkuRequest); |
|||
} |
|||
} |
|||
} |
|||
|
@ -1,244 +1,233 @@ |
|||
using BBWY.Common.Extensions; |
|||
using BBWY.Common.Http; |
|||
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 NLog; |
|||
using System; |
|||
using BBWY.Common.Models; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Net.Http; |
|||
using System.Threading.Tasks; |
|||
using Yitter.IdGenerator; |
|||
|
|||
namespace BBWY.Server.Business |
|||
{ |
|||
public class AfterSaleOrderSyncBusiness : BaseSyncBusiness, IDenpendency |
|||
{ |
|||
private IDictionary<Enums.Platform, Action<JArray, ShopResponse>> syncAfterSaleOrderMethodDic; |
|||
|
|||
private IDictionary<int, Enums.ServiceResult> processResultDic; |
|||
|
|||
|
|||
public AfterSaleOrderSyncBusiness(RestApiService restApiService, |
|||
IOptions<GlobalConfig> options, |
|||
NLogManager nLogManager, |
|||
IFreeSql fsql, |
|||
IIdGenerator idGenerator, |
|||
TaskSchedulerManager taskSchedulerManager, |
|||
VenderBusiness venderBusiness, YunDingBusiness yunDingBusiness) : base(restApiService, |
|||
options, |
|||
nLogManager, |
|||
fsql, |
|||
idGenerator, |
|||
taskSchedulerManager, |
|||
venderBusiness, |
|||
yunDingBusiness) |
|||
//private IDictionary<Enums.Platform, Action<JArray, ShopResponse>> syncAfterSaleOrderMethodDic;
|
|||
|
|||
//private IDictionary<int, Enums.ServiceResult> processResultDic;
|
|||
|
|||
|
|||
//public AfterSaleOrderSyncBusiness(RestApiService restApiService,
|
|||
// IOptions<GlobalConfig> options,
|
|||
// NLogManager nLogManager,
|
|||
// IFreeSql fsql,
|
|||
// IIdGenerator idGenerator,
|
|||
// TaskSchedulerManager taskSchedulerManager,
|
|||
// VenderBusiness venderBusiness, YunDingBusiness yunDingBusiness) : base(restApiService,
|
|||
// options,
|
|||
// nLogManager,
|
|||
// fsql,
|
|||
// idGenerator,
|
|||
// taskSchedulerManager,
|
|||
// venderBusiness,
|
|||
// yunDingBusiness)
|
|||
//{
|
|||
// syncAfterSaleOrderMethodDic = new Dictionary<Enums.Platform, Action<JArray, ShopResponse>>()
|
|||
// {
|
|||
// { Enums.Platform.京东, SyncJDAfterOrder }
|
|||
// };
|
|||
|
|||
// processResultDic = new Dictionary<int, Enums.ServiceResult>()
|
|||
// {
|
|||
// { 23,Enums.ServiceResult.换新},
|
|||
// { 40,Enums.ServiceResult.退货},
|
|||
// { 60,Enums.ServiceResult.原返},
|
|||
// { 90,Enums.ServiceResult.线下换新},
|
|||
// { 110,Enums.ServiceResult.商品补发}
|
|||
// };
|
|||
//}
|
|||
|
|||
//private void SyncJDAfterOrder(JArray afterOrderTokenJArray, ShopResponse shop)
|
|||
//{
|
|||
// var shopId = long.Parse(shop.ShopId);
|
|||
// var validAfterOrderJArray = afterOrderTokenJArray.Where(j => (j.Value<int>("serviceStatus") == 10010 || j.Value<int>("serviceStatus") == 10009) && //完成
|
|||
// processResultDic.ContainsKey(j.Value<int>("processResult")));
|
|||
|
|||
|
|||
// var orderIds = validAfterOrderJArray.Select(j => j["orderId"].ToString()).Distinct().ToList();
|
|||
// var orderSkuIds = validAfterOrderJArray.Select(j => j["skuId"].ToString()).Distinct().ToList();
|
|||
|
|||
// var updateOrders = fsql.Select<Order>().Where(o => orderIds.Contains(o.Id) && o.IsAfterSaleOrder == false).ToList();
|
|||
// var dbOrderSkus = fsql.Select<OrderSku>().Where(osku => orderIds.Contains(osku.OrderId) &&
|
|||
// orderSkuIds.Contains(osku.SkuId) &&
|
|||
// osku.Price != 0).ToList();
|
|||
// var updateOrderSkus = dbOrderSkus.Where(osku => osku.IsRefund == false).ToList();
|
|||
|
|||
// var afterOrders = fsql.Select<AfterSaleOrder>().Where(aso => orderIds.Contains(aso.OrderId)).ToList();
|
|||
|
|||
// List<AfterSaleOrder> insertAfterSaleOrders = new List<AfterSaleOrder>();
|
|||
// List<IUpdate<AfterSaleOrder>> updateAfterSaleOrders = new List<IUpdate<AfterSaleOrder>>();
|
|||
|
|||
// foreach (var afterOrderJToken in validAfterOrderJArray)
|
|||
// {
|
|||
// var orderId = afterOrderJToken.Value<string>("orderId");
|
|||
// var skuId = afterOrderJToken.Value<string>("skuId");
|
|||
// var serviceId = afterOrderJToken.Value<string>("serviceId");
|
|||
// var processResult = processResultDic[afterOrderJToken.Value<int>("processResult")];
|
|||
|
|||
// var afterOrder = afterOrders.FirstOrDefault(aso => aso.ServiceId == serviceId);
|
|||
// if (afterOrder == null)
|
|||
// {
|
|||
// var dbOrderSku = dbOrderSkus.FirstOrDefault(osku => osku.OrderId == orderId && osku.SkuId == skuId);
|
|||
// if (dbOrderSku == null)
|
|||
// continue;
|
|||
|
|||
// decimal refundAmount = 0M;
|
|||
// try
|
|||
// {
|
|||
// if (processResult == Enums.ServiceResult.退货)
|
|||
// refundAmount = SyncJDRefundOrder(shop, orderId, serviceId);
|
|||
// }
|
|||
// catch (Exception ex)
|
|||
// {
|
|||
// nLogManager.Default().Error(ex, $"SyncAfterOrder-SyncRefound ServiceId:{serviceId} OrderId:{orderId} ShopData:{JsonConvert.SerializeObject(shop)}");
|
|||
// }
|
|||
|
|||
|
|||
// insertAfterSaleOrders.Add(new AfterSaleOrder()
|
|||
// {
|
|||
// Id = idGenerator.NewLong(),
|
|||
// CreateTime = DateTime.Now,
|
|||
// OrderId = dbOrderSku.OrderId,
|
|||
// ProductId = dbOrderSku.ProductId,
|
|||
// RefundAmount = refundAmount,
|
|||
// SkuId = skuId,
|
|||
// ShopId = shopId,
|
|||
// ServiceResult = processResult,
|
|||
// ServiceId = serviceId,
|
|||
// ApplyTime = afterOrderJToken.Value<long>("applyTime").StampToDateTime()
|
|||
// });
|
|||
// }
|
|||
// else
|
|||
// {
|
|||
// if (afterOrder.ServiceResult == null ||
|
|||
// (afterOrder.ServiceResult != processResult &&
|
|||
// afterOrder.ServiceResult != Enums.ServiceResult.维修 &&
|
|||
// afterOrder.ServiceResult != Enums.ServiceResult.仅退款 &&
|
|||
// afterOrder.ServiceResult != Enums.ServiceResult.SD退货))
|
|||
// {
|
|||
// var update = fsql.Update<AfterSaleOrder>(afterOrder.Id).Set(aso => aso.ServiceResult, processResult);
|
|||
// updateAfterSaleOrders.Add(update);
|
|||
// }
|
|||
// }
|
|||
// }
|
|||
|
|||
// fsql.Transaction(() =>
|
|||
// {
|
|||
// if (updateOrders.Count() > 0)
|
|||
// foreach (var updateOrder in updateOrders)
|
|||
// fsql.Update<Order>(updateOrder.Id).Set(o => o.IsAfterSaleOrder, true).ExecuteAffrows();
|
|||
|
|||
// if (updateOrderSkus.Count() > 0)
|
|||
// foreach (var updateOrderSku in updateOrderSkus)
|
|||
// fsql.Update<OrderSku>(updateOrderSku.Id).Set(osku => osku.IsRefund, true).ExecuteAffrows();
|
|||
|
|||
// if (insertAfterSaleOrders.Count() > 0)
|
|||
// fsql.Insert(insertAfterSaleOrders).ExecuteAffrows();
|
|||
// if (updateAfterSaleOrders.Count() > 0)
|
|||
// foreach (var update in updateAfterSaleOrders)
|
|||
// update.ExecuteAffrows();
|
|||
// });
|
|||
//}
|
|||
|
|||
//private void SyncAfterOrder(ShopResponse shop, string orderId, DateTime? startTime = null, DateTime? endTime = null, bool isAuto = false)
|
|||
//{
|
|||
// try
|
|||
// {
|
|||
// //logger.Info($"售后订单同步 {shop.ShopName} isAuto {isAuto}");
|
|||
// if (!syncAfterSaleOrderMethodDic.ContainsKey(shop.PlatformId))
|
|||
// throw new Exception("不支持的平台");
|
|||
// var shopId = long.Parse(shop.ShopId);
|
|||
// var relayAPIHost = GetPlatformRelayAPIHost(shop.PlatformId);
|
|||
// var afterOrderListApiResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetAfterOrderList", new SyncAfterOrderRequest()
|
|||
// {
|
|||
// StartDate = startTime ?? DateTime.Now.Date.AddDays(-3),
|
|||
// EndDate = endTime ?? DateTime.Now,
|
|||
// AppKey = shop.AppKey,
|
|||
// AppSecret = shop.AppSecret,
|
|||
// AppToken = shop.AppToken,
|
|||
// PageIndex = 1,
|
|||
// PageSize = 50,
|
|||
// Platform = shop.PlatformId,
|
|||
// SaveResponseLog = true,
|
|||
// OrderId = orderId,
|
|||
// VenderId = shop.VenderId
|
|||
// }, GetYunDingRequestHeader(), HttpMethod.Post);
|
|||
// if (afterOrderListApiResult.StatusCode != System.Net.HttpStatusCode.OK)
|
|||
// throw new Exception($"获取退款订单失败 {afterOrderListApiResult.Content}");
|
|||
|
|||
// var afterOrderListResponse = JsonConvert.DeserializeObject<ApiResponse<JArray>>(afterOrderListApiResult.Content);
|
|||
// if (!afterOrderListResponse.Success)
|
|||
// throw new Exception($"获取退款订单失败 {afterOrderListApiResult.Content}");
|
|||
|
|||
// if (afterOrderListResponse.Data == null || afterOrderListResponse.Data.Count == 0)
|
|||
// return;
|
|||
|
|||
// syncAfterSaleOrderMethodDic[shop.PlatformId](afterOrderListResponse.Data, shop);
|
|||
// }
|
|||
// catch (Exception ex)
|
|||
// {
|
|||
// var shopData = JsonConvert.SerializeObject(shop);
|
|||
// nLogManager.Default().Error(ex, $"SyncAfterOrder ShopData:{shopData}");
|
|||
// }
|
|||
//}
|
|||
|
|||
//public void SyncAllShopAfterOrder()
|
|||
//{
|
|||
// var shopList = venderBusiness.GetShopList(platform: Enums.Platform.京东);
|
|||
// //SyncAfterOrder(shopList.FirstOrDefault(s => s.ShopName == "布莱特玩具专营店"), string.Empty, isAuto: true);
|
|||
// foreach (var shop in shopList)
|
|||
// {
|
|||
// Task.Factory.StartNew(() => SyncAfterOrder(shop, string.Empty, isAuto: true),
|
|||
// System.Threading.CancellationToken.None,
|
|||
// TaskCreationOptions.LongRunning,
|
|||
// taskSchedulerManager.SyncAfterOrderTaskScheduler);
|
|||
// }
|
|||
//}
|
|||
|
|||
///// <summary>
|
|||
///// 获取退款单
|
|||
///// </summary>
|
|||
///// <param name="shop"></param>
|
|||
///// <param name="orderId"></param>
|
|||
///// <param name="serviceId"></param>
|
|||
///// <returns></returns>
|
|||
///// <exception cref="Exception"></exception>
|
|||
//private decimal SyncJDRefundOrder(ShopResponse shop, string orderId, string serviceId)
|
|||
//{
|
|||
// var relayAPIHost = GetPlatformRelayAPIHost(shop.PlatformId);
|
|||
// var refundOrderListApiResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetRefundList", new SearchRefundPlatformOrderRequest()
|
|||
// {
|
|||
// AppKey = shop.AppKey,
|
|||
// AppSecret = shop.AppSecret,
|
|||
// AppToken = shop.AppToken,
|
|||
// PageIndex = 1,
|
|||
// PageSize = 50,
|
|||
// Platform = shop.PlatformId,
|
|||
// SaveResponseLog = false,
|
|||
// OrderId = orderId
|
|||
// }, GetYunDingRequestHeader(), HttpMethod.Post);
|
|||
// if (refundOrderListApiResult.StatusCode != System.Net.HttpStatusCode.OK)
|
|||
// throw new Exception($"获取退款订单失败 {refundOrderListApiResult.Content}");
|
|||
|
|||
// var refundOrderListResponse = JsonConvert.DeserializeObject<ApiResponse<JArray>>(refundOrderListApiResult.Content);
|
|||
// if (!refundOrderListResponse.Success)
|
|||
// throw new Exception($"获取退款订单失败 {refundOrderListApiResult.Content}");
|
|||
|
|||
// if (refundOrderListResponse.Data == null || refundOrderListResponse.Data.Count == 0)
|
|||
// return 0M;
|
|||
|
|||
// var jtoken = refundOrderListResponse.Data.FirstOrDefault(j => j["sameOrderServiceBill"]["serviceId"].ToString() == serviceId &&
|
|||
// j.Value<int>("status") == 13 &&
|
|||
// j.Value<decimal>("refoundAmount") != 0M);
|
|||
// return jtoken == null ? 0M : jtoken.Value<decimal>("refoundAmount");
|
|||
//}
|
|||
public AfterSaleOrderSyncBusiness(NLogManager nLogManager, IFreeSql fsql, IIdGenerator idGenerator, TaskSchedulerManager taskSchedulerManager, IEnumerable<PlatformSDKBusiness> platformSDKBusinessList, VenderBusiness venderBusiness) : base(nLogManager, fsql, idGenerator, taskSchedulerManager, platformSDKBusinessList, venderBusiness) |
|||
{ |
|||
syncAfterSaleOrderMethodDic = new Dictionary<Enums.Platform, Action<JArray, ShopResponse>>() |
|||
{ |
|||
{ Enums.Platform.京东, SyncJDAfterOrder } |
|||
}; |
|||
|
|||
processResultDic = new Dictionary<int, Enums.ServiceResult>() |
|||
{ |
|||
{ 23,Enums.ServiceResult.换新}, |
|||
{ 40,Enums.ServiceResult.退货}, |
|||
{ 60,Enums.ServiceResult.原返}, |
|||
{ 90,Enums.ServiceResult.线下换新}, |
|||
{ 110,Enums.ServiceResult.商品补发} |
|||
}; |
|||
} |
|||
|
|||
private void SyncJDAfterOrder(JArray afterOrderTokenJArray, ShopResponse shop) |
|||
{ |
|||
var shopId = long.Parse(shop.ShopId); |
|||
var validAfterOrderJArray = afterOrderTokenJArray.Where(j => (j.Value<int>("serviceStatus") == 10010 || j.Value<int>("serviceStatus") == 10009) && //完成
|
|||
processResultDic.ContainsKey(j.Value<int>("processResult"))); |
|||
|
|||
|
|||
var orderIds = validAfterOrderJArray.Select(j => j["orderId"].ToString()).Distinct().ToList(); |
|||
var orderSkuIds = validAfterOrderJArray.Select(j => j["skuId"].ToString()).Distinct().ToList(); |
|||
|
|||
var updateOrders = fsql.Select<Order>().Where(o => orderIds.Contains(o.Id) && o.IsAfterSaleOrder == false).ToList(); |
|||
var dbOrderSkus = fsql.Select<OrderSku>().Where(osku => orderIds.Contains(osku.OrderId) && |
|||
orderSkuIds.Contains(osku.SkuId) && |
|||
osku.Price != 0).ToList(); |
|||
var updateOrderSkus = dbOrderSkus.Where(osku => osku.IsRefund == false).ToList(); |
|||
|
|||
var afterOrders = fsql.Select<AfterSaleOrder>().Where(aso => orderIds.Contains(aso.OrderId)).ToList(); |
|||
|
|||
List<AfterSaleOrder> insertAfterSaleOrders = new List<AfterSaleOrder>(); |
|||
List<IUpdate<AfterSaleOrder>> updateAfterSaleOrders = new List<IUpdate<AfterSaleOrder>>(); |
|||
|
|||
foreach (var afterOrderJToken in validAfterOrderJArray) |
|||
{ |
|||
var orderId = afterOrderJToken.Value<string>("orderId"); |
|||
var skuId = afterOrderJToken.Value<string>("skuId"); |
|||
var serviceId = afterOrderJToken.Value<string>("serviceId"); |
|||
var processResult = processResultDic[afterOrderJToken.Value<int>("processResult")]; |
|||
|
|||
var afterOrder = afterOrders.FirstOrDefault(aso => aso.ServiceId == serviceId); |
|||
if (afterOrder == null) |
|||
{ |
|||
var dbOrderSku = dbOrderSkus.FirstOrDefault(osku => osku.OrderId == orderId && osku.SkuId == skuId); |
|||
if (dbOrderSku == null) |
|||
continue; |
|||
|
|||
decimal refundAmount = 0M; |
|||
try |
|||
{ |
|||
if (processResult == Enums.ServiceResult.退货) |
|||
refundAmount = SyncJDRefundOrder(shop, orderId, serviceId); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
nLogManager.Default().Error(ex, $"SyncAfterOrder-SyncRefound ServiceId:{serviceId} OrderId:{orderId} ShopData:{JsonConvert.SerializeObject(shop)}"); |
|||
} |
|||
|
|||
|
|||
insertAfterSaleOrders.Add(new AfterSaleOrder() |
|||
{ |
|||
Id = idGenerator.NewLong(), |
|||
CreateTime = DateTime.Now, |
|||
OrderId = dbOrderSku.OrderId, |
|||
ProductId = dbOrderSku.ProductId, |
|||
RefundAmount = refundAmount, |
|||
SkuId = skuId, |
|||
ShopId = shopId, |
|||
ServiceResult = processResult, |
|||
ServiceId = serviceId, |
|||
ApplyTime = afterOrderJToken.Value<long>("applyTime").StampToDateTime() |
|||
}); |
|||
} |
|||
else |
|||
{ |
|||
if (afterOrder.ServiceResult == null || |
|||
(afterOrder.ServiceResult != processResult && |
|||
afterOrder.ServiceResult != Enums.ServiceResult.维修 && |
|||
afterOrder.ServiceResult != Enums.ServiceResult.仅退款 && |
|||
afterOrder.ServiceResult != Enums.ServiceResult.SD退货)) |
|||
{ |
|||
var update = fsql.Update<AfterSaleOrder>(afterOrder.Id).Set(aso => aso.ServiceResult, processResult); |
|||
updateAfterSaleOrders.Add(update); |
|||
} |
|||
} |
|||
} |
|||
|
|||
fsql.Transaction(() => |
|||
{ |
|||
if (updateOrders.Count() > 0) |
|||
foreach (var updateOrder in updateOrders) |
|||
fsql.Update<Order>(updateOrder.Id).Set(o => o.IsAfterSaleOrder, true).ExecuteAffrows(); |
|||
|
|||
if (updateOrderSkus.Count() > 0) |
|||
foreach (var updateOrderSku in updateOrderSkus) |
|||
fsql.Update<OrderSku>(updateOrderSku.Id).Set(osku => osku.IsRefund, true).ExecuteAffrows(); |
|||
|
|||
if (insertAfterSaleOrders.Count() > 0) |
|||
fsql.Insert(insertAfterSaleOrders).ExecuteAffrows(); |
|||
if (updateAfterSaleOrders.Count() > 0) |
|||
foreach (var update in updateAfterSaleOrders) |
|||
update.ExecuteAffrows(); |
|||
}); |
|||
} |
|||
|
|||
private void SyncAfterOrder(ShopResponse shop, string orderId, DateTime? startTime = null, DateTime? endTime = null, bool isAuto = false) |
|||
{ |
|||
try |
|||
{ |
|||
//logger.Info($"售后订单同步 {shop.ShopName} isAuto {isAuto}");
|
|||
if (!syncAfterSaleOrderMethodDic.ContainsKey(shop.PlatformId)) |
|||
throw new Exception("不支持的平台"); |
|||
var shopId = long.Parse(shop.ShopId); |
|||
var relayAPIHost = GetPlatformRelayAPIHost(shop.PlatformId); |
|||
var afterOrderListApiResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetAfterOrderList", new SyncAfterOrderRequest() |
|||
{ |
|||
StartDate = startTime ?? DateTime.Now.Date.AddDays(-3), |
|||
EndDate = endTime ?? DateTime.Now, |
|||
AppKey = shop.AppKey, |
|||
AppSecret = shop.AppSecret, |
|||
AppToken = shop.AppToken, |
|||
PageIndex = 1, |
|||
PageSize = 50, |
|||
Platform = shop.PlatformId, |
|||
SaveResponseLog = true, |
|||
OrderId = orderId, |
|||
VenderId = shop.VenderId |
|||
}, GetYunDingRequestHeader(), HttpMethod.Post); |
|||
if (afterOrderListApiResult.StatusCode != System.Net.HttpStatusCode.OK) |
|||
throw new Exception($"获取退款订单失败 {afterOrderListApiResult.Content}"); |
|||
|
|||
var afterOrderListResponse = JsonConvert.DeserializeObject<ApiResponse<JArray>>(afterOrderListApiResult.Content); |
|||
if (!afterOrderListResponse.Success) |
|||
throw new Exception($"获取退款订单失败 {afterOrderListApiResult.Content}"); |
|||
|
|||
if (afterOrderListResponse.Data == null || afterOrderListResponse.Data.Count == 0) |
|||
return; |
|||
|
|||
syncAfterSaleOrderMethodDic[shop.PlatformId](afterOrderListResponse.Data, shop); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
var shopData = JsonConvert.SerializeObject(shop); |
|||
nLogManager.Default().Error(ex, $"SyncAfterOrder ShopData:{shopData}"); |
|||
} |
|||
} |
|||
|
|||
public void SyncAllShopAfterOrder() |
|||
{ |
|||
var shopList = venderBusiness.GetShopList(platform: Enums.Platform.京东); |
|||
//SyncAfterOrder(shopList.FirstOrDefault(s => s.ShopName == "布莱特玩具专营店"), string.Empty, isAuto: true);
|
|||
foreach (var shop in shopList) |
|||
{ |
|||
Task.Factory.StartNew(() => SyncAfterOrder(shop, string.Empty, isAuto: true), |
|||
System.Threading.CancellationToken.None, |
|||
TaskCreationOptions.LongRunning, |
|||
taskSchedulerManager.SyncAfterOrderTaskScheduler); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 获取退款单
|
|||
/// </summary>
|
|||
/// <param name="shop"></param>
|
|||
/// <param name="orderId"></param>
|
|||
/// <param name="serviceId"></param>
|
|||
/// <returns></returns>
|
|||
/// <exception cref="Exception"></exception>
|
|||
private decimal SyncJDRefundOrder(ShopResponse shop, string orderId, string serviceId) |
|||
{ |
|||
var relayAPIHost = GetPlatformRelayAPIHost(shop.PlatformId); |
|||
var refundOrderListApiResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetRefundList", new SearchRefundPlatformOrderRequest() |
|||
{ |
|||
AppKey = shop.AppKey, |
|||
AppSecret = shop.AppSecret, |
|||
AppToken = shop.AppToken, |
|||
PageIndex = 1, |
|||
PageSize = 50, |
|||
Platform = shop.PlatformId, |
|||
SaveResponseLog = false, |
|||
OrderId = orderId |
|||
}, GetYunDingRequestHeader(), HttpMethod.Post); |
|||
if (refundOrderListApiResult.StatusCode != System.Net.HttpStatusCode.OK) |
|||
throw new Exception($"获取退款订单失败 {refundOrderListApiResult.Content}"); |
|||
|
|||
var refundOrderListResponse = JsonConvert.DeserializeObject<ApiResponse<JArray>>(refundOrderListApiResult.Content); |
|||
if (!refundOrderListResponse.Success) |
|||
throw new Exception($"获取退款订单失败 {refundOrderListApiResult.Content}"); |
|||
|
|||
if (refundOrderListResponse.Data == null || refundOrderListResponse.Data.Count == 0) |
|||
return 0M; |
|||
|
|||
var jtoken = refundOrderListResponse.Data.FirstOrDefault(j => j["sameOrderServiceBill"]["serviceId"].ToString() == serviceId && |
|||
j.Value<int>("status") == 13 && |
|||
j.Value<decimal>("refoundAmount") != 0M); |
|||
return jtoken == null ? 0M : jtoken.Value<decimal>("refoundAmount"); |
|||
} |
|||
} |
|||
} |
|||
|
@ -1,34 +1,41 @@ |
|||
using BBWY.Common.Http; |
|||
using BBWY.Common.Models; |
|||
using BBWY.Common.Models; |
|||
using BBWY.Server.Model; |
|||
using Microsoft.Extensions.Options; |
|||
using NLog; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Yitter.IdGenerator; |
|||
|
|||
namespace BBWY.Server.Business |
|||
{ |
|||
public class BaseSyncBusiness : BasePlatformRelayBusiness, IDenpendency |
|||
public class BaseSyncBusiness : IDenpendency |
|||
{ |
|||
protected NLogManager nLogManager; |
|||
protected IFreeSql fsql; |
|||
protected IIdGenerator idGenerator; |
|||
protected TaskSchedulerManager taskSchedulerManager; |
|||
protected IEnumerable<PlatformSDKBusiness> platformSDKBusinessList; |
|||
protected VenderBusiness venderBusiness; |
|||
|
|||
public BaseSyncBusiness(RestApiService restApiService, |
|||
IOptions<GlobalConfig> options, |
|||
NLogManager nLogManager, |
|||
public BaseSyncBusiness(NLogManager nLogManager, |
|||
IFreeSql fsql, |
|||
IIdGenerator idGenerator, |
|||
TaskSchedulerManager taskSchedulerManager, |
|||
VenderBusiness venderBusiness, |
|||
YunDingBusiness yunDingBusiness) : base(restApiService, options, yunDingBusiness) |
|||
IEnumerable<PlatformSDKBusiness> platformSDKBusinessList, |
|||
VenderBusiness venderBusiness) |
|||
{ |
|||
this.nLogManager = nLogManager; |
|||
this.fsql = fsql; |
|||
this.idGenerator = idGenerator; |
|||
this.venderBusiness = venderBusiness; |
|||
this.taskSchedulerManager = taskSchedulerManager; |
|||
this.platformSDKBusinessList = platformSDKBusinessList; |
|||
this.venderBusiness = venderBusiness; |
|||
} |
|||
|
|||
public PlatformSDKBusiness GetPlatformSDKBusiness(Enums.Platform platofrm) |
|||
{ |
|||
var pb = platformSDKBusinessList.FirstOrDefault(p => p.Platform == platofrm); |
|||
if (pb == null) |
|||
throw new BusinessException($"不支持平台{platofrm}"); |
|||
return pb; |
|||
} |
|||
} |
|||
} |
|||
|
Loading…
Reference in new issue