|
|
|
using BBWYB.Common.Log;
|
|
|
|
using BBWYB.Common.Models;
|
|
|
|
using BBWYB.Server.Model;
|
|
|
|
using BBWYB.Server.Model.Db;
|
|
|
|
using BBWYB.Server.Model.Db.MDS;
|
|
|
|
using BBWYB.Server.Model.Dto;
|
|
|
|
using FreeSql;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using SDKAdapter;
|
|
|
|
using SDKAdapter.PurchasePlatform.Client;
|
|
|
|
using SDKAdapter.PurchasePlatform.Models;
|
|
|
|
using Yitter.IdGenerator;
|
|
|
|
|
|
|
|
namespace BBWYB.Server.Business
|
|
|
|
{
|
|
|
|
public class PurchaseOrderBusiness : BaseBusiness, IDenpendency
|
|
|
|
{
|
|
|
|
private PP_PlatformClientFactory ppPlatformClientFactory;
|
|
|
|
private TaskSchedulerManager taskSchedulerManager;
|
|
|
|
private FreeSqlMultiDBManager fsqlManager;
|
|
|
|
private OrderBusiness orderBusiness;
|
|
|
|
private VenderBusiness venderBusiness;
|
|
|
|
private ExpressCompanyNameConverter expressCompanyNameConverter;
|
|
|
|
|
|
|
|
public PurchaseOrderBusiness(IFreeSql fsql,
|
|
|
|
NLogManager nLogManager,
|
|
|
|
IIdGenerator idGenerator,
|
|
|
|
PP_PlatformClientFactory ppPlatformClientFactory,
|
|
|
|
TaskSchedulerManager taskSchedulerManager,
|
|
|
|
FreeSqlMultiDBManager fsqlManager,
|
|
|
|
OrderBusiness orderBusiness,
|
|
|
|
VenderBusiness venderBusiness,
|
|
|
|
ExpressCompanyNameConverter expressCompanyNameConverter) : base(fsql, nLogManager, idGenerator)
|
|
|
|
{
|
|
|
|
this.ppPlatformClientFactory = ppPlatformClientFactory;
|
|
|
|
this.taskSchedulerManager = taskSchedulerManager;
|
|
|
|
this.fsqlManager = fsqlManager;
|
|
|
|
this.orderBusiness = orderBusiness;
|
|
|
|
this.venderBusiness = venderBusiness;
|
|
|
|
this.expressCompanyNameConverter = expressCompanyNameConverter;
|
|
|
|
}
|
|
|
|
|
|
|
|
public PreviewOrderResponse PreviewPurchaseOrder(PreviewOrderRequest request)
|
|
|
|
{
|
|
|
|
nLogManager.Default().Info($"PreviewPurchaseOrder {JsonConvert.SerializeObject(request)}");
|
|
|
|
var response = ppPlatformClientFactory.GetClient((AdapterEnums.PlatformType)request.Platform)
|
|
|
|
.PreviewOrder(new PP_PreviewOrderRequest()
|
|
|
|
{
|
|
|
|
AppKey = request.AppKey,
|
|
|
|
AppSecret = request.AppSecret,
|
|
|
|
AppToken = request.AppToken,
|
|
|
|
Consignee = new PP_ConsigneeRequest()
|
|
|
|
{
|
|
|
|
Address = request.Consignee.Address,
|
|
|
|
City = request.Consignee.City,
|
|
|
|
ContactName = request.Consignee.ContactName,
|
|
|
|
County = request.Consignee.County,
|
|
|
|
Mobile = request.Consignee.Mobile,
|
|
|
|
Province = request.Consignee.Province,
|
|
|
|
TelePhone = request.Consignee.TelePhone,
|
|
|
|
Town = request.Consignee.Town
|
|
|
|
},
|
|
|
|
Platform = (AdapterEnums.PlatformType)request.Platform,
|
|
|
|
PurchaseMode = (AdapterEnums.PurchaseMode)request.PurchaseOrderMode,
|
|
|
|
OrderProductParamList = request.CargoParamList.Select(p => new PP_OrderProductParamRequest()
|
|
|
|
{
|
|
|
|
ProductId = p.ProductId,
|
|
|
|
Quantity = p.Quantity,
|
|
|
|
SkuId = p.SkuId,
|
|
|
|
SpecId = p.SpecId
|
|
|
|
}).ToList()
|
|
|
|
});
|
|
|
|
return new PreviewOrderResponse()
|
|
|
|
{
|
|
|
|
Extensions = response.Extensions,
|
|
|
|
FreightAmount = response.FreightAmount,
|
|
|
|
ProductAmount = response.ProductAmount,
|
|
|
|
TotalAmount = response.TotalAmount
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void CreatePurchaseOrder(CreateOrderRequest request)
|
|
|
|
{
|
|
|
|
nLogManager.Default().Info($"CreatePurchaseOrder\r\n{JsonConvert.SerializeObject(request)}");
|
|
|
|
|
|
|
|
var dbOrder = fsql.Select<Order>(request.OrderId).ToOne();
|
|
|
|
if (dbOrder == null)
|
|
|
|
throw new BusinessException("订单不存在");
|
|
|
|
if (dbOrder.OrderState != Enums.OrderState.等待采购 && dbOrder.OrderState != Enums.OrderState.待出库)
|
|
|
|
throw new BusinessException("只能为等待采购或待出库的订单进行采购");
|
|
|
|
|
|
|
|
var deleteOrderCostDetail = fsql.Delete<OrderCostDetail>().Where(ocd => ocd.OrderId == dbOrder.Id);
|
|
|
|
var isRepurchase = fsql.Select<OrderCost>(dbOrder.Id).Any();
|
|
|
|
|
|
|
|
#region 合并重复的采购sku
|
|
|
|
var repeatPurchaseSkuGroups = request.CargoParamList.GroupBy(p => p.SkuId).ToList();
|
|
|
|
foreach (var group in repeatPurchaseSkuGroups)
|
|
|
|
{
|
|
|
|
if (group.Count() > 1)
|
|
|
|
{
|
|
|
|
var repeatSkus = group.ToList();
|
|
|
|
foreach (var repeatSku in repeatSkus)
|
|
|
|
request.CargoParamList.Remove(repeatSku);
|
|
|
|
request.CargoParamList.Add(new CargoParamRequest()
|
|
|
|
{
|
|
|
|
BelongSkuId = repeatSkus[0].BelongSkuId,
|
|
|
|
ProductId = repeatSkus[0].ProductId,
|
|
|
|
SkuId = repeatSkus[0].SkuId,
|
|
|
|
SpecId = repeatSkus[0].SpecId,
|
|
|
|
Quantity = repeatSkus.Sum(s => s.Quantity)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
var orderSkus = fsql.Select<OrderSku>().Where(osku => osku.Price != 0 && osku.OrderId == request.OrderId).ToList();
|
|
|
|
var orderSkuIds = orderSkus.Select(osku => osku.Id).ToList();
|
|
|
|
|
|
|
|
var client = ppPlatformClientFactory.GetClient((AdapterEnums.PlatformType)request.Platform);
|
|
|
|
|
|
|
|
var createOrderResponse = client.CreateOrder(new PP_CreateOrderRequest()
|
|
|
|
{
|
|
|
|
AppKey = request.AppKey,
|
|
|
|
AppSecret = request.AppSecret,
|
|
|
|
AppToken = request.AppToken,
|
|
|
|
Consignee = new PP_ConsigneeRequest()
|
|
|
|
{
|
|
|
|
Address = request.Consignee.Address,
|
|
|
|
City = request.Consignee.City,
|
|
|
|
ContactName = request.Consignee.ContactName,
|
|
|
|
County = request.Consignee.County,
|
|
|
|
Mobile = request.Consignee.Mobile,
|
|
|
|
Province = request.Consignee.Province,
|
|
|
|
TelePhone = request.Consignee.TelePhone,
|
|
|
|
Town = request.Consignee.Town
|
|
|
|
},
|
|
|
|
Platform = (AdapterEnums.PlatformType)request.Platform,
|
|
|
|
PurchaseMode = (AdapterEnums.PurchaseMode)request.PurchaseOrderMode,
|
|
|
|
Extensions = request.Extensions,
|
|
|
|
Remark = request.Remark,
|
|
|
|
OrderProductParamList = request.CargoParamList.Select(p => new PP_OrderProductParamRequest()
|
|
|
|
{
|
|
|
|
ProductId = p.ProductId,
|
|
|
|
Quantity = p.Quantity,
|
|
|
|
SkuId = p.SkuId,
|
|
|
|
SpecId = p.SpecId
|
|
|
|
}).ToList()
|
|
|
|
});
|
|
|
|
|
|
|
|
var purchaseOrderSimpleInfo = client.QueryOrderDetail(new PP_QueryOrderDetailRequest()
|
|
|
|
{
|
|
|
|
AppKey = request.AppKey,
|
|
|
|
AppSecret = request.AppSecret,
|
|
|
|
AppToken = request.AppToken,
|
|
|
|
OrderId = createOrderResponse.OrderId
|
|
|
|
});
|
|
|
|
|
|
|
|
nLogManager.Default().Info($"QueryOrderDetail\r\nShopOrderId {request.OrderId}\r\n purchaseOrderSimpleInfo\r\n{JsonConvert.SerializeObject(purchaseOrderSimpleInfo)}");
|
|
|
|
|
|
|
|
|
|
|
|
List<OrderCostDetail> insertOrderCostDetails = new List<OrderCostDetail>();
|
|
|
|
IInsert<OrderCost> insertOrderCost = null;
|
|
|
|
IUpdate<OrderCost> updateOrderCost = null;
|
|
|
|
IInsert<OrderPurchaseInfo> insertOrderPurchaseInfo = null;
|
|
|
|
IUpdate<OrderPurchaseInfo> updateOrderPurchaseInfo = null;
|
|
|
|
|
|
|
|
foreach (var orderSku in orderSkus)
|
|
|
|
{
|
|
|
|
#region 计算当前sku的采购成本和采购运费
|
|
|
|
var currentOrderSkuProductAmount = 0M; //采购成本
|
|
|
|
var currentOrderSkuCargoParamList = request.CargoParamList.Where(p => p.BelongSkuId == orderSku.SkuId); //找当前skuId的采购skuId
|
|
|
|
currentOrderSkuProductAmount = purchaseOrderSimpleInfo.ItemList.Where(p => currentOrderSkuCargoParamList.Any(p1 => p1.SkuId == p.SkuId))?.Sum(p => p.ProductAmount) ?? 0M;
|
|
|
|
|
|
|
|
var currentOrderSkuFreightAmount = purchaseOrderSimpleInfo.FreightAmount / orderSkus.Count(); //采购运费(按sku数均分)
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 成本明细
|
|
|
|
var orderCostDetail = new OrderCostDetail()
|
|
|
|
{
|
|
|
|
Id = idGenerator.NewLong(),
|
|
|
|
ConsumableAmount = 0,
|
|
|
|
CreateTime = DateTime.Now,
|
|
|
|
DeductionQuantity = orderSku.ItemTotal.Value,
|
|
|
|
DeliveryExpressFreight = 0,
|
|
|
|
FirstFreight = 0,
|
|
|
|
//OperationAmount = 0,
|
|
|
|
InStorageAmount = 0,
|
|
|
|
OutStorageAmount = 0,
|
|
|
|
OrderId = request.OrderId,
|
|
|
|
ProductId = orderSku.ProductId,
|
|
|
|
PurchaseFreight = currentOrderSkuFreightAmount,
|
|
|
|
//PurchaseOrderPKId = purchaseOrder.Id,
|
|
|
|
PurchaseOrderId = purchaseOrderSimpleInfo.OrderId,
|
|
|
|
SkuAmount = currentOrderSkuProductAmount,
|
|
|
|
SkuId = orderSku.SkuId,
|
|
|
|
StorageAmount = 0
|
|
|
|
//UnitCost = purchaseOrder.UnitCost,
|
|
|
|
//TotalCost = currentOrderSkuProductAmount + currentOrderSkuFreightAmount//purchaseOrder.UnitCost * orderSku.ItemTotal.Value
|
|
|
|
};
|
|
|
|
//orderCostDetail.SkuGrossProfit = orderSku.Price.Value * orderCostDetail.DeductionQuantity - avgPreferential - (orderCostDetail.TotalCost + orderCostDetail.DeliveryExpressFreight) - orderSku.Price.Value * orderCostDetail.DeductionQuantity * createOnlinePurchaseOrderRequest.PlatformCommissionRatio;
|
|
|
|
insertOrderCostDetails.Add(orderCostDetail);
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
|
|
|
|
#region 订单成本
|
|
|
|
var orderCost = new OrderCost()
|
|
|
|
{
|
|
|
|
OrderId = request.OrderId,
|
|
|
|
CreateTime = DateTime.Now,
|
|
|
|
DeliveryExpressFreight = 0,
|
|
|
|
IsManualEdited = false,
|
|
|
|
PlatformCommissionRatio = 0,
|
|
|
|
PreferentialAmount = 0,
|
|
|
|
PurchaseAmount = purchaseOrderSimpleInfo.TotalAmount
|
|
|
|
};
|
|
|
|
//orderCost.PlatformCommissionAmount = dbOrder.OrderSellerPrice * orderCost.PlatformCommissionRatio;
|
|
|
|
orderCost.Profit = dbOrder.OrderSellerPrice +
|
|
|
|
dbOrder.FreightPrice -
|
|
|
|
orderCost.PurchaseAmount -
|
|
|
|
orderCost.DeliveryExpressFreight; // -orderCost.PlatformCommissionAmount
|
|
|
|
if (!isRepurchase)
|
|
|
|
{
|
|
|
|
insertOrderCost = fsql.Insert(orderCost);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
updateOrderCost = fsql.Update<OrderCost>().SetSource(orderCost).IgnoreColumns(a => new { a.CreateTime });
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 订单采购信息
|
|
|
|
var orderPurchaserInfo = new OrderPurchaseInfo()
|
|
|
|
{
|
|
|
|
OrderId = request.OrderId,
|
|
|
|
CreateTime = DateTime.Now,
|
|
|
|
PurchaseAccountId = request.PurchaseAccountId,
|
|
|
|
PurchaseAccountName = request.PurchaseAccountName,
|
|
|
|
PurchaseMethod = Enums.PurchaseMethod.线上采购,
|
|
|
|
PurchaseOrderId = createOrderResponse.OrderId,
|
|
|
|
PurchasePlatform = request.Platform,
|
|
|
|
PurchaserName = request.PurchaserName,
|
|
|
|
ShopId = request.ShopId
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!isRepurchase)
|
|
|
|
{
|
|
|
|
insertOrderPurchaseInfo = fsql.Insert(orderPurchaserInfo);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
updateOrderPurchaseInfo = fsql.Update<OrderPurchaseInfo>().SetSource(orderPurchaserInfo).IgnoreColumns(a => new { a.CreateTime });
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
fsql.Transaction(() =>
|
|
|
|
{
|
|
|
|
deleteOrderCostDetail.ExecuteAffrows();
|
|
|
|
fsql.Insert(insertOrderCostDetails).ExecuteAffrows();
|
|
|
|
updateOrderCost?.ExecuteAffrows();
|
|
|
|
insertOrderCost?.ExecuteAffrows();
|
|
|
|
insertOrderPurchaseInfo?.ExecuteAffrows();
|
|
|
|
updateOrderPurchaseInfo?.ExecuteAffrows();
|
|
|
|
fsql.Update<Order>(request.OrderId).SetIf(dbOrder.OrderState == Enums.OrderState.等待采购, o => o.OrderState, Model.Enums.OrderState.待出库)
|
|
|
|
.Set(o => o.IsPurchased, true)
|
|
|
|
.ExecuteAffrows();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#region 1688CallBack
|
|
|
|
public void CallbackFrom1688(string jsonStr)
|
|
|
|
{
|
|
|
|
nLogManager.Default().Info(jsonStr);
|
|
|
|
var jObject = JObject.Parse(jsonStr);
|
|
|
|
var type = jObject.Value<string>("type").ToUpper();
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case "ORDER_BUYER_VIEW_PART_PART_SENDGOODS": //部分发货
|
|
|
|
case "ORDER_BUYER_VIEW_ANNOUNCE_SENDGOODS": //发货
|
|
|
|
DeliveryCallbackFrom1688(jObject);
|
|
|
|
break;
|
|
|
|
case "ORDER_BUYER_VIEW_ORDER_PRICE_MODIFY":
|
|
|
|
OrderPriceModificationCallbackFrom1688(jObject); //订单改价
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 1688发货回调
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="jObject"></param>
|
|
|
|
private void DeliveryCallbackFrom1688(JObject jObject)
|
|
|
|
{
|
|
|
|
var purchaseOrderId = jObject["data"].Value<string>("orderId");
|
|
|
|
Task.Factory.StartNew(() => DeliveryCallback(purchaseOrderId, null, Enums.Platform.阿里巴巴), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 1688订单改价回调
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="jObject"></param>
|
|
|
|
private void OrderPriceModificationCallbackFrom1688(JObject jObject)
|
|
|
|
{
|
|
|
|
//var purchaseOrderId = jObject["data"].Value<string>("orderId");
|
|
|
|
//Task.Factory.StartNew(() => OrderPriceModificationCallback(purchaseOrderId, Enums.Platform.阿里巴巴), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 采购平台发货回调
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="purchaseOrderId"></param>
|
|
|
|
/// <param name="wayBillNoResponse"></param>
|
|
|
|
/// <param name="callbackPlatform"></param>
|
|
|
|
private void DeliveryCallback(string purchaseOrderId, PP_QueryOrderLogisticsResponse wayBillNoResponse, Enums.Platform callbackPlatform)
|
|
|
|
{
|
|
|
|
string currentProgress = string.Empty;
|
|
|
|
string wayBillNoResponseInfo = string.Empty;
|
|
|
|
string expressCompanyListInfo = string.Empty;
|
|
|
|
string expressCompanyInfo = string.Empty;
|
|
|
|
string orderId = string.Empty;
|
|
|
|
long? shopId = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
#region 查询代发信息
|
|
|
|
currentProgress = "查询代发信息";
|
|
|
|
var orderPurchaseInfo = fsql.Select<OrderPurchaseInfo>().Where(o => o.PurchaseOrderId == purchaseOrderId).ToOne();
|
|
|
|
if (orderPurchaseInfo == null)
|
|
|
|
throw new Exception("未查询到代发信息");
|
|
|
|
orderId = orderPurchaseInfo.OrderId;
|
|
|
|
shopId = orderPurchaseInfo.ShopId;
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 查询采购账号的归属店铺
|
|
|
|
currentProgress = "查询采购账号归属店铺";
|
|
|
|
var shop = venderBusiness.GetShopList(shopId: shopId)?.FirstOrDefault();
|
|
|
|
if (shop == null)
|
|
|
|
throw new Exception("未查询到店铺信息");
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 查询采购账号
|
|
|
|
currentProgress = "查询采购账号";
|
|
|
|
var purchaseAccount = fsqlManager.MDSfsql.Select<Purchaseaccount>().Where(pa => pa.Id == orderPurchaseInfo.PurchaseAccountId).ToOne();
|
|
|
|
if (purchaseAccount == null)
|
|
|
|
throw new Exception($"未查询到采购账号{orderPurchaseInfo.PurchaseAccountId}");
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 获取采购单的物流信息
|
|
|
|
currentProgress = "获取采购单的物流信息";
|
|
|
|
if (wayBillNoResponse == null)
|
|
|
|
{
|
|
|
|
var client = ppPlatformClientFactory.GetClient((AdapterEnums.PlatformType)callbackPlatform);
|
|
|
|
var ppQueryOrderLogisticsRequest = new PP_QueryOrderLogisticsRequest()
|
|
|
|
{
|
|
|
|
AppKey = purchaseAccount.AppKey,
|
|
|
|
AppSecret = purchaseAccount.AppSecret,
|
|
|
|
AppToken = purchaseAccount.AppToken,
|
|
|
|
OrderId = purchaseOrderId,
|
|
|
|
Platform = (AdapterEnums.PlatformType)callbackPlatform
|
|
|
|
};
|
|
|
|
wayBillNoResponse = client.QueryOrderLogistics(ppQueryOrderLogisticsRequest);
|
|
|
|
wayBillNoResponseInfo = JsonConvert.SerializeObject(new { Request = ppQueryOrderLogisticsRequest, Result = wayBillNoResponse });
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 获取目标平台的物流公司列表
|
|
|
|
currentProgress = "获取店铺平台物流公司列表";
|
|
|
|
var expressCompanyList = venderBusiness.GetExpressCompanyList(new PlatformRequest()
|
|
|
|
{
|
|
|
|
AppKey = shop.AppKey,
|
|
|
|
AppSecret = shop.AppSecret,
|
|
|
|
AppToken = shop.AppToken,
|
|
|
|
Platform = shop.PlatformId
|
|
|
|
});
|
|
|
|
if (expressCompanyList != null)
|
|
|
|
expressCompanyListInfo = JsonConvert.SerializeObject(expressCompanyList);
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 物流公司翻译
|
|
|
|
currentProgress = "物流公司翻译";
|
|
|
|
//logisticsCompanyId = ConvertLogisticsCompanyId(wayBillNoResponse.LogisticsCompanyName, logisticsCompanyList, shop.Platform);
|
|
|
|
var convertExpressCompany = expressCompanyNameConverter.Converter(wayBillNoResponse.ExpressName,
|
|
|
|
(AdapterEnums.PlatformType)callbackPlatform,
|
|
|
|
(AdapterEnums.PlatformType)shop.PlatformId,
|
|
|
|
expressCompanyList);
|
|
|
|
expressCompanyInfo = JsonConvert.SerializeObject(convertExpressCompany);
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 店铺平台订单出库
|
|
|
|
currentProgress = "店铺平台订单出库";
|
|
|
|
//outStockRequest = new OutStockRequest()
|
|
|
|
//{
|
|
|
|
// AppKey = shop.AppKey,
|
|
|
|
// AppSecret = shop.AppSecret,
|
|
|
|
// AppToken = shop.AppToken,
|
|
|
|
// OrderId = orderPurchaseInfo.OrderId,
|
|
|
|
// Platform = shop.PlatformId,
|
|
|
|
// WayBillNo = wayBillNoResponse.WayBillNo,
|
|
|
|
// LogisticsId = logisticsCompanyId, //物流公司Id
|
|
|
|
// SaveResponseLog = true
|
|
|
|
//};
|
|
|
|
//orderBusiness.OutStock(outStockRequest);
|
|
|
|
orderBusiness.OutStock(new OutStockRequest()
|
|
|
|
{
|
|
|
|
AppKey = shop.AppKey,
|
|
|
|
AppSecret = shop.AppSecret,
|
|
|
|
AppToken = shop.AppToken,
|
|
|
|
OrderId = orderId,
|
|
|
|
ExpressId = convertExpressCompany.ExpressId, //物流公司Id
|
|
|
|
ExpressName = convertExpressCompany.ExpressName, //物流公司名称
|
|
|
|
Platform = shop.PlatformId,
|
|
|
|
WayBillNo = wayBillNoResponse.WayBillNo
|
|
|
|
});
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
nLogManager.Default().Info($"DeliveryCallback 回调平台{callbackPlatform},订单号{orderId},采购单号{purchaseOrderId},执行进度[{currentProgress}],采购单物流信息:{wayBillNoResponseInfo},店铺平台物流公司列表:{expressCompanyListInfo},翻译后的物流公司:{expressCompanyInfo}");
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
nLogManager.Default().Error(ex, $"DeliveryCallback 回调平台{callbackPlatform},订单号{orderId},采购单号{purchaseOrderId},执行进度[{currentProgress}],采购单物流信息:{wayBillNoResponseInfo},店铺平台物流公司列表:{expressCompanyListInfo},翻译后的物流公司:{expressCompanyInfo}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///// <summary>
|
|
|
|
///// 采购平台改价回调
|
|
|
|
///// </summary>
|
|
|
|
///// <param name="purchaseOrderId"></param>
|
|
|
|
///// <param name="callbackPlatform"></param>
|
|
|
|
//private void OrderPriceModificationCallback(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 查询订单Sku
|
|
|
|
// currentProgress = "查询订单Sku";
|
|
|
|
// var orderSkuList = fsql.Select<OrderSku>().Where(osku => osku.Price != 0 && osku.OrderId == orderDropshipping.OrderId).ToList();
|
|
|
|
// if (orderSkuList == null || orderSkuList.Count() == 0)
|
|
|
|
// throw new BusinessException("订单Sku不存在");
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// #region 查询采购单
|
|
|
|
// currentProgress = "查询采购单";
|
|
|
|
// var purchaseOrderList = fsql.Select<PurchaseOrder>().Where(po => po.PurchaseOrderId == purchaseOrderId).ToList();
|
|
|
|
// if (purchaseOrderList == null || purchaseOrderList.Count() == 0)
|
|
|
|
// throw new BusinessException("采购单不存在");
|
|
|
|
// if (orderSkuList.Count() > 1 && purchaseOrderList.Any(p => p.PurchaseMethod == Enums.PurchaseMethod.线下采购))
|
|
|
|
// throw new Exception("多sku订单关联采购单不支持改价");
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// #region 查询成本
|
|
|
|
// currentProgress = "查询成本";
|
|
|
|
// var orderCost = fsql.Select<OrderCost>(orderDropshipping.OrderId).ToOne();
|
|
|
|
// if (orderCost == null)
|
|
|
|
// throw new BusinessException("订单成本不存在");
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// #region 查询成本明细
|
|
|
|
// currentProgress = "查询成本明细";
|
|
|
|
// var orderCostDetailList = fsql.Select<OrderCostDetail>().Where(ocd => ocd.OrderId == orderDropshipping.OrderId).ToList();
|
|
|
|
// if (orderCostDetailList == null || orderCostDetailList.Count() == 0)
|
|
|
|
// throw new BusinessException("订单成本明细不存在");
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// #region 查询采购账号
|
|
|
|
// currentProgress = "查询采购账号";
|
|
|
|
// var purchaseAccount = fsql.Select<PurchaseAccount>().WhereIf(orderDropshipping.PurchaseAccountId != 0, pa => pa.Id == orderDropshipping.PurchaseAccountId)
|
|
|
|
// .WhereIf(orderDropshipping.PurchaseAccountId == 0, pa => pa.AccountName == orderDropshipping.BuyerAccount)
|
|
|
|
// .Where(pa => pa.PurchasePlatformId == callbackPlatform).ToOne();
|
|
|
|
// if (purchaseAccount == null)
|
|
|
|
// throw new Exception($"未查询到采购账号{orderDropshipping.BuyerAccount}");
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// #region 查询改价后的订单金额
|
|
|
|
// currentProgress = "查询改价后的订单金额";
|
|
|
|
// var purchaseOrderSimpleInfo = platformSDKBusinessList.FirstOrDefault(p => p.Platform == callbackPlatform).GetOrderSimpleInfo(new GetOrderInfoRequest()
|
|
|
|
// {
|
|
|
|
// AppKey = purchaseAccount.AppKey,
|
|
|
|
// AppSecret = purchaseAccount.AppSecret,
|
|
|
|
// AppToken = purchaseAccount.AppToken,
|
|
|
|
// OrderId = purchaseOrderId,
|
|
|
|
// Platform = callbackPlatform
|
|
|
|
// });
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// #region 查询采购单明细
|
|
|
|
// currentProgress = "查询采购单明细";
|
|
|
|
// var purchaseOrderDetails = fsql.Select<PurchaseOrderDetail>().Where(p => p.OrderId == orderDropshipping.OrderId);
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// #region 查询订单
|
|
|
|
// currentProgress = "查询订单";
|
|
|
|
// var dbOrder = fsql.Select<Order>(orderDropshipping.OrderId).ToOne();
|
|
|
|
// if (dbOrder == null)
|
|
|
|
// throw new BusinessException("订单不存在");
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// IList<IUpdate<PurchaseOrder>> updatePurchaseOrders = new List<IUpdate<PurchaseOrder>>();
|
|
|
|
// IList<IUpdate<OrderCostDetail>> updateOrderCostDetails = new List<IUpdate<OrderCostDetail>>();
|
|
|
|
|
|
|
|
// foreach (var orderSku in orderSkuList)
|
|
|
|
// {
|
|
|
|
// var currentOrderSkuProductAmount = 0M; //采购成本
|
|
|
|
// if (orderSkuList.Count() != 1)
|
|
|
|
// {
|
|
|
|
// var currentOrderSkuPurchaseOrderDetails = purchaseOrderDetails.Where(p => p.SkuId == orderSku.SkuId); //找当前skuId的采购skuId
|
|
|
|
// currentOrderSkuProductAmount = purchaseOrderSimpleInfo.ItemList.Where(p => currentOrderSkuPurchaseOrderDetails.Any(p1 => p1.PurchaseSkuId == p.SkuId))
|
|
|
|
// ?.Sum(p => p.ProductAmount) ?? 0M;
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// currentOrderSkuProductAmount = purchaseOrderSimpleInfo.ProductAmount;
|
|
|
|
// }
|
|
|
|
// var currentOrderSkuFreightAmount = purchaseOrderSimpleInfo.FreightAmount / orderSkuList.Count(); //采购运费(按sku数均分)
|
|
|
|
|
|
|
|
// var purchaseOrder = purchaseOrderList.FirstOrDefault(po => po.SkuId == orderSku.SkuId);
|
|
|
|
// var orderCostDetail = orderCostDetailList.FirstOrDefault(oc => oc.PurchaseOrderPKId == purchaseOrder.Id);
|
|
|
|
|
|
|
|
// purchaseOrder.SingleSkuAmount = currentOrderSkuProductAmount / orderSku.ItemTotal.Value;
|
|
|
|
// purchaseOrder.SingleFreight = currentOrderSkuFreightAmount / orderSku.ItemTotal.Value;
|
|
|
|
|
|
|
|
// orderCostDetail.SkuAmount = currentOrderSkuProductAmount;
|
|
|
|
// orderCostDetail.PurchaseFreight = currentOrderSkuFreightAmount;
|
|
|
|
// //orderCostDetail.UnitCost = purchaseOrder.UnitCost;
|
|
|
|
// //orderCostDetail.TotalCost = currentOrderSkuProductAmount + currentOrderSkuFreightAmount;
|
|
|
|
|
|
|
|
// updatePurchaseOrders.Add(fsql.Update<PurchaseOrder>().SetSource(purchaseOrder));
|
|
|
|
// updateOrderCostDetails.Add(fsql.Update<OrderCostDetail>().SetSource(orderCostDetail));
|
|
|
|
|
|
|
|
|
|
|
|
// }
|
|
|
|
// orderCost.PurchaseAmount = purchaseOrderSimpleInfo.TotalAmount;
|
|
|
|
// orderCost.Profit = dbOrder.OrderSellerPrice +
|
|
|
|
// dbOrder.FreightPrice -
|
|
|
|
// orderCost.PurchaseAmount -
|
|
|
|
// orderCost.DeliveryExpressFreight -
|
|
|
|
// orderCost.PlatformCommissionAmount;
|
|
|
|
|
|
|
|
// orderDropshipping.PurchaseAmount = purchaseOrderSimpleInfo.TotalAmount;
|
|
|
|
// orderDropshipping.SkuAmount = purchaseOrderSimpleInfo.ProductAmount;
|
|
|
|
// orderDropshipping.PurchaseFreight = purchaseOrderSimpleInfo.FreightAmount;
|
|
|
|
// fsql.Transaction(() =>
|
|
|
|
// {
|
|
|
|
// foreach (var update in updatePurchaseOrders)
|
|
|
|
// update.ExecuteAffrows();
|
|
|
|
// foreach (var update in updateOrderCostDetails)
|
|
|
|
// update.ExecuteAffrows();
|
|
|
|
// fsql.Update<OrderCost>().SetSource(orderCost).ExecuteAffrows();
|
|
|
|
// fsql.Update<OrderDropShipping>().SetSource(orderDropshipping).ExecuteAffrows();
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// catch (Exception ex)
|
|
|
|
// {
|
|
|
|
// nLogManager.Default().Error(ex, $"OrderPriceModificationCallback 回调平台{callbackPlatform},采购单号{purchaseOrderId},执行进度[{currentProgress}]");
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
}
|