|
|
@ -899,6 +899,169 @@ namespace BBWY.Server.Business |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 关联外部订单
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="relationPurchaseOrderRequestV2"></param>
|
|
|
|
public void RelationPurchaseOrderV2(RelationPurchaseOrderRequestV2 relationPurchaseOrderRequestV2) |
|
|
|
{ |
|
|
|
var dbOrder = fsql.Select<Order>(relationPurchaseOrderRequestV2.OrderDropShippingList[0].OrderId).ToOne(); |
|
|
|
if (dbOrder == null) |
|
|
|
throw new BusinessException($"订单号{relationPurchaseOrderRequestV2.OrderDropShippingList[0].OrderId}不存在"); |
|
|
|
|
|
|
|
if (relationPurchaseOrderRequestV2.PlatformCommissionRatio == 0M) |
|
|
|
relationPurchaseOrderRequestV2.PlatformCommissionRatio = 0.05M; |
|
|
|
|
|
|
|
List<OrderDropShipping> insertOrderDropShippingList = new List<OrderDropShipping>(); |
|
|
|
List<OrderDropShipping> updateOrderDropShippingList = new List<OrderDropShipping>(); |
|
|
|
IInsert<OrderCost> insertOrderCost = null; |
|
|
|
IUpdate<OrderCost> updateOrderCost = null; |
|
|
|
IDelete<PurchaseOrder> deletePurchaseOrder = null; |
|
|
|
IDelete<OrderCostDetail> deleteOrderCostDetail = null; |
|
|
|
List<IUpdate<OrderSku>> updateOrderSkuList = new List<IUpdate<OrderSku>>(); |
|
|
|
List<PurchaseOrder> insertPurchaseOrderList = new List<PurchaseOrder>(); |
|
|
|
List<OrderCostDetail> insertOrderCostDetailList = new List<OrderCostDetail>(); |
|
|
|
|
|
|
|
var oldPourchaseIdList = fsql.Select<OrderCostDetail>().Where(ocd => ocd.OrderId == dbOrder.Id) |
|
|
|
.ToList(ocd => ocd.PurchaseOrderPKId); |
|
|
|
|
|
|
|
deletePurchaseOrder = fsql.Delete<PurchaseOrder>().Where(po => oldPourchaseIdList.Contains(po.Id)); |
|
|
|
deleteOrderCostDetail = fsql.Delete<OrderCostDetail>().Where(ocd => ocd.OrderId == dbOrder.Id); |
|
|
|
|
|
|
|
foreach (var orderDropShipping in relationPurchaseOrderRequestV2.OrderDropShippingList) |
|
|
|
{ |
|
|
|
#region 代发信息表
|
|
|
|
if (orderDropShipping.Id == 0) |
|
|
|
{ |
|
|
|
orderDropShipping.Id = idGenerator.NewLong(); |
|
|
|
orderDropShipping.CreateTime = DateTime.Now; |
|
|
|
orderDropShipping.ShopId = dbOrder.ShopId; |
|
|
|
insertOrderDropShippingList.Add(orderDropShipping); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
updateOrderDropShippingList.Add(orderDropShipping); |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
foreach (var relationOrderSku in orderDropShipping.RelationPurchaseOrderSkuList) |
|
|
|
{ |
|
|
|
#region OrderSku
|
|
|
|
var updateOrderSku = fsql.Update<OrderSku>(relationOrderSku.Id).Set(osku => osku.OrderDropShippingId, orderDropShipping.Id); |
|
|
|
updateOrderSkuList.Add(updateOrderSku); |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 采购单
|
|
|
|
var purchaseOrder = new PurchaseOrder() |
|
|
|
{ |
|
|
|
Id = idGenerator.NewLong(), |
|
|
|
CreateTime = DateTime.Now, |
|
|
|
ProductId = relationOrderSku.ProductId, |
|
|
|
PurchaseMethod = Enums.PurchaseMethod.线下采购, |
|
|
|
StorageType = Enums.StorageType.代发, |
|
|
|
PurchaseOrderId = orderDropShipping.PurchaseOrderId, |
|
|
|
PurchasePlatform = orderDropShipping.PurchasePlatform, |
|
|
|
PurchaseQuantity = relationOrderSku.Quantity, |
|
|
|
RemainingQuantity = 0, |
|
|
|
ShopId = dbOrder.ShopId, |
|
|
|
SkuId = relationOrderSku.SkuId, |
|
|
|
SingleConsumableAmount = 0, |
|
|
|
SingleFirstFreight = 0, |
|
|
|
SingleFreight = orderDropShipping.PurchaseFreight / orderDropShipping.RelationPurchaseOrderSkuList.Count() / relationOrderSku.Quantity, |
|
|
|
SingleOperationAmount = 0, |
|
|
|
SingleStorageAmount = 0, |
|
|
|
SingleSkuAmount = relationOrderSku.SingleSkuAmount, |
|
|
|
SingleDeliveryFreight = 0 |
|
|
|
}; |
|
|
|
insertPurchaseOrderList.Add(purchaseOrder); |
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 明细
|
|
|
|
insertOrderCostDetailList.Add(new OrderCostDetail() |
|
|
|
{ |
|
|
|
Id = idGenerator.NewLong(), |
|
|
|
ConsumableAmount = 0, |
|
|
|
CreateTime = DateTime.Now, |
|
|
|
DeductionQuantity = relationOrderSku.Quantity, |
|
|
|
DeliveryExpressFreight = 0, |
|
|
|
FirstFreight = 0, |
|
|
|
OperationAmount = 0, |
|
|
|
OrderId = dbOrder.Id, |
|
|
|
ProductId = relationOrderSku.ProductId, |
|
|
|
PurchaseFreight = orderDropShipping.PurchaseFreight / orderDropShipping.RelationPurchaseOrderSkuList.Count(), |
|
|
|
SkuAmount = relationOrderSku.SingleSkuAmount * relationOrderSku.Quantity, |
|
|
|
SkuId = relationOrderSku.SkuId, |
|
|
|
StorageAmount = 0, |
|
|
|
TotalCost = relationOrderSku.SingleSkuAmount * relationOrderSku.Quantity + orderDropShipping.PurchaseFreight / orderDropShipping.RelationPurchaseOrderSkuList.Count(), |
|
|
|
UnitCost = purchaseOrder.UnitCost, |
|
|
|
PurchaseOrderPKId = purchaseOrder.Id |
|
|
|
}); |
|
|
|
#endregion
|
|
|
|
} |
|
|
|
|
|
|
|
#region 订单成本
|
|
|
|
var totalDeliveryFreight = relationPurchaseOrderRequestV2.OrderDropShippingList.Sum(ods => ods.DeliveryFreight); |
|
|
|
var totalPurchaseAmount = relationPurchaseOrderRequestV2.OrderDropShippingList.Sum(ods => ods.PurchaseAmount); |
|
|
|
var orderCost = fsql.Select<OrderCost>(dbOrder.Id).ToOne(); |
|
|
|
if (orderCost != null) |
|
|
|
{ |
|
|
|
orderCost.PlatformCommissionRatio = relationPurchaseOrderRequestV2.PlatformCommissionRatio; |
|
|
|
orderCost.PlatformCommissionAmount = dbOrder.OrderSellerPrice * relationPurchaseOrderRequestV2.PlatformCommissionRatio; |
|
|
|
orderCost.DeliveryExpressFreight = totalDeliveryFreight; |
|
|
|
orderCost.PurchaseAmount = totalPurchaseAmount; |
|
|
|
orderCost.Profit = dbOrder.OrderSellerPrice + |
|
|
|
dbOrder.FreightPrice - |
|
|
|
orderCost.PurchaseAmount - |
|
|
|
orderCost.DeliveryExpressFreight - |
|
|
|
orderCost.PlatformCommissionAmount; |
|
|
|
updateOrderCost = fsql.Update<OrderCost>().SetSource(orderCost).IgnoreColumns(oc => new |
|
|
|
{ |
|
|
|
oc.CreateTime, |
|
|
|
oc.SDCommissionAmount, |
|
|
|
oc.PlatformCommissionAmount, |
|
|
|
oc.PlatformCommissionRatio |
|
|
|
}); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
var preferentialAmount = fsql.Select<OrderCoupon>().Where(oc => oc.OrderId == dbOrder.Id) |
|
|
|
.ToAggregate(g => g.Sum(g.Key.CouponPrice)); |
|
|
|
orderCost = new OrderCost() |
|
|
|
{ |
|
|
|
OrderId = dbOrder.Id, |
|
|
|
CreateTime = DateTime.Now, |
|
|
|
DeliveryExpressFreight = totalDeliveryFreight, |
|
|
|
PlatformCommissionRatio = relationPurchaseOrderRequestV2.PlatformCommissionRatio, |
|
|
|
SDCommissionAmount = 0, |
|
|
|
PurchaseAmount = totalPurchaseAmount, |
|
|
|
PlatformCommissionAmount = dbOrder.OrderSellerPrice * relationPurchaseOrderRequestV2.PlatformCommissionRatio, |
|
|
|
PreferentialAmount = preferentialAmount, |
|
|
|
IsManualEdited = true |
|
|
|
}; |
|
|
|
|
|
|
|
orderCost.Profit = dbOrder.OrderSellerPrice + |
|
|
|
dbOrder.FreightPrice - |
|
|
|
orderCost.PurchaseAmount - |
|
|
|
orderCost.DeliveryExpressFreight - |
|
|
|
orderCost.PlatformCommissionAmount; |
|
|
|
insertOrderCost = fsql.Insert(orderCost); |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
} |
|
|
|
|
|
|
|
fsql.Transaction(() => |
|
|
|
{ |
|
|
|
deletePurchaseOrder.ExecuteAffrows(); |
|
|
|
deleteOrderCostDetail.ExecuteAffrows(); |
|
|
|
foreach (var updateOrderSku in updateOrderSkuList) |
|
|
|
updateOrderSku.ExecuteAffrows(); |
|
|
|
fsql.Insert(insertPurchaseOrderList).ExecuteAffrows(); |
|
|
|
fsql.Insert(insertOrderCostDetailList).ExecuteAffrows(); |
|
|
|
insertOrderCost?.ExecuteAffrows(); |
|
|
|
updateOrderCost?.ExecuteAffrows(); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 出库
|
|
|
|
/// </summary>
|
|
|
|