步步为盈
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.

262 lines
13 KiB

2 years ago
using BBWY.Common.Http;
using BBWY.Common.Models;
using BBWY.Server.Model;
using BBWY.Server.Model.Dto;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
2 years ago
using System.Net.Http;
using System.Threading.Tasks;
using Yitter.IdGenerator;
using System.Linq;
using BBWY.Server.Model.Db;
2 years ago
using FreeSql;
2 years ago
namespace BBWY.Server.Business.Sync
{
public class JDServiceOrderSyncBusiness : BaseSyncBusiness, IDenpendency
{
2 years ago
private IList<LogisticsCompanyRelationship> jd_kd100_logisticsCompanyDictionary;
2 years ago
public JDServiceOrderSyncBusiness(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)
{
2 years ago
jd_kd100_logisticsCompanyDictionary = new List<LogisticsCompanyRelationship>()
{
new LogisticsCompanyRelationship(){ SourceName="<JD>",TargetName="<Kuaii100>",SecondTargetName="<kuaidi100 2>" }
};
2 years ago
}
2 years ago
public void SyncServiceOrder()
2 years ago
{
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(() => SyncServiceOrder(shop, string.Empty, isAuto: true),
System.Threading.CancellationToken.None,
TaskCreationOptions.LongRunning,
2 years ago
taskSchedulerManager.SyncServiceOrderTaskScheduler);
2 years ago
}
}
2 years ago
public void SyncServiceOrder(long shopId, string serviceId)
{
var shopList = venderBusiness.GetShopList(shopId);
Task.Factory.StartNew(() => SyncServiceOrder(shopList[0], serviceId, isAuto: true),
System.Threading.CancellationToken.None,
TaskCreationOptions.LongRunning,
taskSchedulerManager.SyncServiceOrderTaskScheduler);
}
2 years ago
private void SyncServiceOrder(ShopResponse shop, long shopId, IList<JToken> jtokenList, string relayAPIHost, string appKey, string appSecret, string token)
2 years ago
{
2 years ago
var serviceIdList = jtokenList.Select(j => j.Value<string>("serviceId")).ToList();
var dbServiceOrderList = fsql.Select<ServiceOrder>().Where(s => s.ShopId == shop.ShopId && serviceIdList.Contains(s.ServiceId)).ToList();
var dbServiceIdList = dbServiceOrderList.Select(s => s.ServiceId).ToList();
var exceptServiceIdList = serviceIdList.Except(dbServiceIdList);
2 years ago
2 years ago
var apiOrderIdList = jtokenList.Where(j => exceptServiceIdList.Contains(j.Value<string>("serviceId"))).Select(j => j.Value<string>("orderId")).ToList();
var dbOrderSkuList = fsql.Select<OrderSku, Order>().InnerJoin((osku, o) => osku.OrderId == o.Id)
.Where((osku, o) => osku.ShopId == shopId && apiOrderIdList.Contains(osku.OrderId))
.ToList((osku, o) => new
{
Id = osku.Id,
ProductId = osku.ProductId,
SkuId = osku.SkuId,
OrderId = osku.OrderId,
ItemTotal = osku.ItemTotal,
StorageType = o.StorageType
});
2 years ago
List<ServiceOrder> insertServiceOrderList = new List<ServiceOrder>();
List<IUpdate<ServiceOrder>> updateServiceOrderList = new List<IUpdate<ServiceOrder>>();
2 years ago
foreach (var serviceOrderJToken in jtokenList)
{
var serviceId = serviceOrderJToken.Value<string>("serviceId");
var status = serviceOrderJToken.Value<int>("sserviceOrderJToken");
2 years ago
var statusUpdateTime = serviceOrderJToken.Value<DateTime>("updateTime");
2 years ago
var dbServiceOrder = dbServiceOrderList.FirstOrDefault(s => s.ServiceId == serviceId);
if (dbServiceOrder == null)
{
var serviceOrderId = serviceOrderJToken.Value<string>("orderId");
var skuId = serviceOrderJToken.Value<string>("skuId");
var dbOsku = dbOrderSkuList.FirstOrDefault(osku => osku.OrderId == serviceOrderId && osku.SkuId == skuId);
if (dbOsku == null)
continue;
var isNeedSubscribeKuaiDi100 = false;
#region 待收货服务单,检查是否需要订阅快递100
if (status == 10005)
{
if (dbOsku.StorageType == Enums.StorageType.)
isNeedSubscribeKuaiDi100 = true;
else
{
try
{
var serviceOrderDetailResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetServiceOrderDetail", new QueryServiceOrderDetailRequest()
{
AppKey = appKey,
AppSecret = appSecret,
AppToken = token,
OrderId = serviceOrderId,
ServiceId = serviceId,
Platform = Enums.Platform.,
VenderId = shop.VenderId
}, GetYunDingRequestHeader(), HttpMethod.Post);
if (serviceOrderDetailResult.StatusCode != System.Net.HttpStatusCode.OK)
throw new Exception($"获取服务单详情失败 {serviceOrderDetailResult.Content}");
var serviceOrderDetailResponse = JsonConvert.DeserializeObject<ApiResponse<JToken>>(serviceOrderDetailResult.Content);
if (!serviceOrderDetailResponse.Success)
throw new Exception($"获取服务单详情失败 {serviceOrderDetailResponse.Msg}");
//检查退货城市是否为泉州
isNeedSubscribeKuaiDi100 = serviceOrderDetailResponse.Data["returnWareAddress"].Value<int>("cityCode") == 2812;
}
catch { }
}
}
#endregion
dbServiceOrder = new ServiceOrder()
{
Id = idGenerator.NewLong(),
ServiceId = serviceId,
OrderId = serviceOrderId,
ShopId = shop.ShopId,
ProductId = dbOsku.ProductId,
SkuItemCount = dbOsku.ItemTotal,
Status = status,
CreateTime = DateTime.Now,
SkuId = skuId,
2 years ago
StatusUpdateTime = statusUpdateTime,
2 years ago
IsSubscribeKuaiDi100 = false,
IsNeedSubscribeKuaiDi100 = isNeedSubscribeKuaiDi100,
WaybillNo = string.Empty,
ExpressName = string.Empty,
ApplyTime = serviceOrderJToken.Value<DateTime>("applyTime")
};
2 years ago
insertServiceOrderList.Add(dbServiceOrder);
}
else if (dbServiceOrder.Status != status)
{
var update = fsql.Update<ServiceOrder>(dbServiceOrder.Id).Set(s => s.Status, status)
.Set(s => s.StatusUpdateTime, statusUpdateTime);
updateServiceOrderList.Add(update);
2 years ago
}
}
2 years ago
fsql.Transaction(() =>
{
if (insertServiceOrderList.Count() > 0)
fsql.Insert(insertServiceOrderList).ExecuteAffrows();
if (updateServiceOrderList.Count() > 0)
foreach (var update in updateServiceOrderList)
update.ExecuteAffrows();
});
2 years ago
}
2 years ago
private void SyncServiceOrder(ShopResponse shop, string serviceId, DateTime? startTime = null, DateTime? endTime = null, bool isAuto = false)
2 years ago
{
/*
:
: 10001
: 10002
: 10012
: 10005
: 10011
: 10004
: 10009
: 10010
:
: 10007
:7060
:7023
线:7090
: 13000
*/
2 years ago
var loggerName = $"新服务单同步-{shop.ShopName}";
2 years ago
try
{
var shopId = long.Parse(shop.ShopId);
var relayAPIHost = GetPlatformRelayAPIHost(shop.PlatformId);
var serviceStatusList = new List<int>() { 10005, 10011, 10010 };
2 years ago
var request = new QueryServiceOrderRequest()
2 years ago
{
UpdateTimeBegin = startTime ?? DateTime.Now.Date.AddHours(-1),
2 years ago
UpdateTimeEnd = endTime ?? DateTime.Now,
AppKey = shop.AppKey,
AppSecret = shop.AppSecret,
AppToken = shop.AppToken,
PageIndex = 1,
PageSize = 50,
Platform = shop.PlatformId,
SaveResponseLog = true,
2 years ago
ServiceId = serviceId,
VenderId = shop.VenderId
};
2 years ago
List<JToken> jtokenList = new List<JToken>();
foreach (var serviceStatus in serviceStatusList)
{
2 years ago
if (string.IsNullOrEmpty(serviceId))
request.ServiceStatus = serviceStatus;
var serviceOrderListApiResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetServiceOrderList", request, GetYunDingRequestHeader(), HttpMethod.Post);
if (serviceOrderListApiResult.StatusCode != System.Net.HttpStatusCode.OK)
throw new Exception($"获取服务单失败 {serviceOrderListApiResult.Content}");
var serviceOrderListResponse = JsonConvert.DeserializeObject<ApiResponse<JArray>>(serviceOrderListApiResult.Content);
if (!serviceOrderListResponse.Success)
throw new Exception($"获取服务单失败 {serviceOrderListResponse.Msg}");
jtokenList.AddRange(serviceOrderListResponse.Data);
2 years ago
if (!string.IsNullOrEmpty(serviceId))
break;
}
2 years ago
2 years ago
SyncServiceOrder(shop, shopId, jtokenList, relayAPIHost, request.AppKey, request.AppSecret, request.AppToken);
2 years ago
}
catch (Exception ex)
{
2 years ago
nLogManager.GetLogger(loggerName).Error(ex, $"SyncServiceOrder ShopName:{shop.ShopName} ShopId:{shop.ShopId}");
2 years ago
}
}
2 years ago
public void SubscribeKuaiDi100()
{
var shopList = venderBusiness.GetShopList(platform: Enums.Platform.);
foreach (var shop in shopList)
{
Task.Factory.StartNew(() => SyncServiceOrder(shop, string.Empty, isAuto: true),
System.Threading.CancellationToken.None,
TaskCreationOptions.LongRunning,
taskSchedulerManager.SyncServiceOrderTaskScheduler);
}
}
private void SubscribeKuaiDi100(ShopResponse shop)
{
var serviceOrderList = fsql.Select<ServiceOrder>().Where(s => s.ShopId == shop.Id &&
s.IsNeedSubscribeKuaiDi100 == true &&
s.IsSubscribeKuaiDi100 == false)
.Page(1, 50)
.ToList();
foreach (var serviceOrder in serviceOrderList)
{
if (string.IsNullOrEmpty(serviceOrder.WaybillNo) || string.IsNullOrEmpty(serviceOrder.ExpressName))
{
#region 查询服务单运单详情
#endregion
}
}
}
2 years ago
}
}