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.
255 lines
13 KiB
255 lines
13 KiB
using BBWYB.Common.Http;
|
|
using BBWYB.Common.Log;
|
|
using BBWYB.Common.Models;
|
|
using BBWYB.Server.Model;
|
|
using BBWYB.Server.Model.Db;
|
|
using FreeSql;
|
|
using JD.Dto;
|
|
using Newtonsoft.Json;
|
|
using System.Text;
|
|
using Yitter.IdGenerator;
|
|
|
|
namespace BBWYB.Server.Business.JD
|
|
{
|
|
public class JDBusiness : BaseBusiness, IDenpendency
|
|
{
|
|
private RestApiService restApiService;
|
|
private VenderBusiness venderBusiness;
|
|
private DingDingBusiness dingDingBusiness;
|
|
private TaskSchedulerManager taskSchedulerManager;
|
|
public JDBusiness(IFreeSql fsql,
|
|
NLogManager nLogManager,
|
|
IIdGenerator idGenerator,
|
|
RestApiService restApiService,
|
|
VenderBusiness venderBusiness,
|
|
DingDingBusiness dingDingBusiness,
|
|
TaskSchedulerManager taskSchedulerManager) : base(fsql, nLogManager, idGenerator)
|
|
{
|
|
this.restApiService = restApiService;
|
|
this.venderBusiness = venderBusiness;
|
|
this.dingDingBusiness = dingDingBusiness;
|
|
this.taskSchedulerManager = taskSchedulerManager;
|
|
}
|
|
|
|
public ApiResponse<IList<JDInStoreOrderDetail>> GetJDInStoreOrderDetailList(string sourceShopName, IList<string> poOrderNos)
|
|
{
|
|
var shop = venderBusiness.GetShopList(shopName: sourceShopName).FirstOrDefault();
|
|
if (shop == null)
|
|
return new ApiResponse<IList<JDInStoreOrderDetail>>() { Code = 0, Msg = $"未找到店铺{sourceShopName}" };
|
|
|
|
try
|
|
{
|
|
var httpResult = restApiService.SendRequest("https://yunding.qiyue666.com/", "api/PlatformSDK/GetJDInStorePurchaseOrderList", new
|
|
{
|
|
Platform = shop.PlatformId,
|
|
shop.AppKey,
|
|
shop.AppSecret,
|
|
shop.AppToken,
|
|
PoOrderNos = string.Join(',', poOrderNos)
|
|
}, null, HttpMethod.Post);
|
|
|
|
if (httpResult.StatusCode != System.Net.HttpStatusCode.OK)
|
|
return new ApiResponse<IList<JDInStoreOrderDetail>>() { Code = 0, Msg = httpResult.Content };
|
|
|
|
|
|
var response = JsonConvert.DeserializeObject<ApiResponse<IList<JDInStoreOrderDetail>>>(httpResult.Content);
|
|
return response;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ApiResponse<IList<JDInStoreOrderDetail>>() { Code = 0, Msg = ex.Message };
|
|
}
|
|
}
|
|
|
|
public void CheckInStoreOrder()
|
|
{
|
|
List<string> orderIds = null;
|
|
List<IUpdate<Order>> updateOrderList = new List<IUpdate<Order>>();
|
|
List<TimeLimitTask> insertTimeLimitTaskList = new List<TimeLimitTask>();
|
|
try
|
|
{
|
|
var checkTaskList = fsql.Select<InStoreOrderCheckTask>().Where(x => x.IsChecked == false).ToList();
|
|
if (checkTaskList.Count() == 0)
|
|
return;
|
|
|
|
orderIds = checkTaskList.Select(x => x.OrderId).ToList();
|
|
var timelimitTaskList = fsql.Select<TimeLimitTask>().Where(t => orderIds.Contains(t.OrderId) && t.TaskType == Enums.TimeLimitTaskType.待核算任务).ToList();
|
|
|
|
var orderPurchaseInfoList = fsql.Select<OrderPurchaseInfo>().Where(opi => orderIds.Contains(opi.OrderId) && opi.IsEnabled == true).ToList();
|
|
|
|
var waitCheckInStoreOrderList = fsql.Select<InStorePurchaseOrdeRrelationInfo, Order>()
|
|
.InnerJoin((io, o) => io.OrderId == o.Id)
|
|
.Where((io, o) => orderIds.Contains(io.OrderId) && !string.IsNullOrEmpty(o.SourceShopName))
|
|
.ToList((io, o) => new
|
|
{
|
|
io.Id,
|
|
io.PurchaseOrderId,
|
|
io.CreateTime,
|
|
io.InStorePurchaseOrderId,
|
|
io.OrderId,
|
|
io.ShopId,
|
|
io.WaybillNo,
|
|
o.SourceShopName
|
|
});
|
|
var shopIds = checkTaskList.Select(x => x.ShopId.ToString()).ToList();
|
|
var shopList = venderBusiness.GetShopList(shopIds);
|
|
|
|
foreach (var checkTask in checkTaskList)
|
|
{
|
|
var dingdingMsg = new StringBuilder();
|
|
var inStoreOrderRelationList = waitCheckInStoreOrderList.Where(x => x.OrderId == checkTask.OrderId).ToList();
|
|
var currentPurchaseOrderList = orderPurchaseInfoList.Where(opi => opi.OrderId == checkTask.OrderId).ToList();
|
|
var shop = shopList.FirstOrDefault(s => s.ShopId == checkTask.ShopId.ToString());
|
|
if (inStoreOrderRelationList == null || inStoreOrderRelationList.Count() == 0)
|
|
{
|
|
if (currentPurchaseOrderList.Count() > 0)
|
|
{
|
|
foreach (var opi in currentPurchaseOrderList)
|
|
{
|
|
dingdingMsg.AppendLine($"店铺名称:{shop?.ShopName}");
|
|
dingdingMsg.AppendLine($"拳探订单号:{checkTask.OrderSn}");
|
|
dingdingMsg.AppendLine($"采购平台:{opi.PurchasePlatform}");
|
|
dingdingMsg.AppendLine($"采购单号:{opi.PurchaseOrderId}");
|
|
dingdingMsg.AppendLine("缺少入仓采购单号");
|
|
dingdingMsg.AppendLine();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var poOrderNos = inStoreOrderRelationList.Select(i => i.InStorePurchaseOrderId).Distinct().ToList();
|
|
//查询京东
|
|
var inStoreResponse = GetJDInStoreOrderDetailList(inStoreOrderRelationList.FirstOrDefault().SourceShopName, poOrderNos);
|
|
if (!inStoreResponse.Success)
|
|
{
|
|
if (currentPurchaseOrderList.Count() > 0)
|
|
{
|
|
foreach (var opi in currentPurchaseOrderList)
|
|
{
|
|
dingdingMsg.AppendLine($"店铺名称:{shop?.ShopName}");
|
|
dingdingMsg.AppendLine($"拳探订单号:{checkTask.OrderSn}");
|
|
dingdingMsg.AppendLine($"采购平台:{opi.PurchasePlatform}");
|
|
dingdingMsg.AppendLine($"采购单号:{opi.PurchaseOrderId}");
|
|
dingdingMsg.AppendLine($"{inStoreResponse.Msg}");
|
|
dingdingMsg.AppendLine();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
bool validateResult = true;
|
|
foreach (var inStoreOrderRelation in inStoreOrderRelationList)
|
|
{
|
|
var jdInStore = inStoreResponse.Data.FirstOrDefault(x => x.poOrderNo == inStoreOrderRelation.InStorePurchaseOrderId);
|
|
if (jdInStore != null && jdInStore.storageStatus == "2")
|
|
continue;
|
|
|
|
validateResult = false;
|
|
|
|
var opi = currentPurchaseOrderList.FirstOrDefault(opi => opi.PurchaseOrderId == inStoreOrderRelation.PurchaseOrderId);
|
|
|
|
if (jdInStore == null)
|
|
{
|
|
dingdingMsg.AppendLine($"店铺名称:{shop?.ShopName}");
|
|
dingdingMsg.AppendLine($"拳探订单号:{checkTask.OrderSn}");
|
|
dingdingMsg.AppendLine($"采购平台:{opi?.PurchasePlatform}");
|
|
dingdingMsg.AppendLine($"采购单号:{opi?.PurchaseOrderId}");
|
|
dingdingMsg.AppendLine($"入仓采购单号:{inStoreOrderRelation.InStorePurchaseOrderId}");
|
|
dingdingMsg.AppendLine($"未找到该入仓采购单号");
|
|
dingdingMsg.AppendLine();
|
|
continue;
|
|
}
|
|
|
|
var stateText = "";
|
|
switch (jdInStore.storageStatus)
|
|
{
|
|
case "1":
|
|
stateText = "有差异待确认";
|
|
break;
|
|
case "3":
|
|
stateText = "待确认";
|
|
break;
|
|
case "4":
|
|
stateText = "有差异已确认";
|
|
break;
|
|
}
|
|
|
|
dingdingMsg.AppendLine($"店铺名称:{shop?.ShopName}");
|
|
dingdingMsg.AppendLine($"拳探订单号:{checkTask.OrderSn}");
|
|
dingdingMsg.AppendLine($"采购平台:{opi?.PurchasePlatform}");
|
|
dingdingMsg.AppendLine($"采购单号:{opi?.PurchaseOrderId}");
|
|
dingdingMsg.AppendLine($"入仓采购单号:{inStoreOrderRelation.InStorePurchaseOrderId}");
|
|
dingdingMsg.AppendLine($"入仓采购单状态:{jdInStore.storageStatus} {stateText}");
|
|
dingdingMsg.AppendLine();
|
|
}
|
|
|
|
if (validateResult)
|
|
{
|
|
var update = fsql.Update<Order>().Set(o => o.OrderState, Enums.OrderState.待核算)
|
|
.Where(o => o.Id == checkTask.OrderId && o.OrderState == Enums.OrderState.待验收);
|
|
updateOrderList.Add(update);
|
|
|
|
if (!timelimitTaskList.Any(t => t.OrderId == checkTask.OrderId && t.TaskType == Enums.TimeLimitTaskType.待核算任务))
|
|
{
|
|
//创建待核算任务
|
|
var t = new TimeLimitTask()
|
|
{
|
|
CreateTme = DateTime.Now,
|
|
Id = idGenerator.NewLong(),
|
|
OrderId = checkTask.OrderId,
|
|
OrderSn = checkTask.OrderSn,
|
|
ShopId = checkTask.ShopId,
|
|
TaskType = Enums.TimeLimitTaskType.待核算任务,
|
|
ExpirationTime = DateTime.Now.AddDays(1)
|
|
};
|
|
insertTimeLimitTaskList.Add(t);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (dingdingMsg.Length > 0)
|
|
{
|
|
Task.Factory.StartNew(() => SendDingDing(dingdingMsg.ToString()), CancellationToken.None, TaskCreationOptions.None, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
nLogManager.Default().Error(ex, "CheckInStoreOrder");
|
|
}
|
|
finally
|
|
{
|
|
try
|
|
{
|
|
fsql.Transaction(() =>
|
|
{
|
|
if (updateOrderList.Count() > 0)
|
|
{
|
|
foreach (var update in updateOrderList)
|
|
update.ExecuteAffrows();
|
|
}
|
|
|
|
if (orderIds != null && orderIds.Count() > 0)
|
|
fsql.Update<InStoreOrderCheckTask>().Set(x => x.IsChecked, true).Where(x => orderIds.Contains(x.OrderId)).ExecuteAffrows();
|
|
|
|
if (insertTimeLimitTaskList.Count() > 0)
|
|
fsql.Insert(insertTimeLimitTaskList).ExecuteAffrows();
|
|
});
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
|
|
private void SendDingDing(string content)
|
|
{
|
|
try
|
|
{
|
|
dingDingBusiness.SendDingDingBotMessage("SEC5f08a3dd6813e50bf9a3b81350ec12a8086c64b9e29ef858a17f5cc7887906d7",
|
|
"https://oapi.dingtalk.com/robot/send?access_token=7ce472411bb8dde0c3ff503fcca9ead84d39950ee3c4c65c808dbc58981eb929",
|
|
content);
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
}
|
|
|