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.
1037 lines
56 KiB
1037 lines
56 KiB
using BBWYB.Common.Http;
|
|
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.OperationPlatform.Models;
|
|
using SDKAdapter.PurchasePlatform.Client;
|
|
using SDKAdapter.PurchasePlatform.Models;
|
|
using System.Net;
|
|
using System.Text;
|
|
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;
|
|
private RestApiService restApiService;
|
|
|
|
public PurchaseOrderBusiness(IFreeSql fsql,
|
|
NLogManager nLogManager,
|
|
IIdGenerator idGenerator,
|
|
PP_PlatformClientFactory ppPlatformClientFactory,
|
|
TaskSchedulerManager taskSchedulerManager,
|
|
FreeSqlMultiDBManager fsqlManager,
|
|
OrderBusiness orderBusiness,
|
|
VenderBusiness venderBusiness,
|
|
ExpressCompanyNameConverter expressCompanyNameConverter,
|
|
RestApiService restApiService) : base(fsql, nLogManager, idGenerator)
|
|
{
|
|
this.ppPlatformClientFactory = ppPlatformClientFactory;
|
|
this.taskSchedulerManager = taskSchedulerManager;
|
|
this.fsqlManager = fsqlManager;
|
|
this.orderBusiness = orderBusiness;
|
|
this.venderBusiness = venderBusiness;
|
|
this.expressCompanyNameConverter = expressCompanyNameConverter;
|
|
this.restApiService = restApiService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 合并相同的采购Sku
|
|
/// </summary>
|
|
/// <param name="cargoParamList"></param>
|
|
/// <returns></returns>
|
|
private IList<PP_OrderProductParamRequest> CombineRepeatPurchaseSku(IList<CargoParamRequest> cargoParamList)
|
|
{
|
|
IList<PP_OrderProductParamRequest> orderProductParamList = new List<PP_OrderProductParamRequest>();
|
|
var samePurchaseSkuGroups = cargoParamList.GroupBy(p => p.SkuId).ToList();
|
|
foreach (var samePurchaseSkuGroup in samePurchaseSkuGroups)
|
|
{
|
|
var first = samePurchaseSkuGroup.First();
|
|
orderProductParamList.Add(new PP_OrderProductParamRequest()
|
|
{
|
|
ProductId = first.ProductId,
|
|
SkuId = first.SkuId,
|
|
SpecId = first.SpecId,
|
|
Quantity = samePurchaseSkuGroup.Sum(p => p.Quantity)
|
|
});
|
|
}
|
|
return orderProductParamList;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 预览采购单
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="BusinessException"></exception>
|
|
public PreviewOrderResponse PreviewPurchaseOrder(PreviewOrderRequest request)
|
|
{
|
|
nLogManager.Default().Info($"PreviewPurchaseOrder {JsonConvert.SerializeObject(request)}");
|
|
if (request.Consignee == null ||
|
|
string.IsNullOrEmpty(request.Consignee.Address) ||
|
|
string.IsNullOrEmpty(request.Consignee.Mobile) ||
|
|
string.IsNullOrEmpty(request.Consignee.ContactName))
|
|
throw new BusinessException("缺少收货人信息");
|
|
if (request.PurchaseAccountList == null || request.PurchaseAccountList.Count() == 0)
|
|
throw new BusinessException("缺少采购账号");
|
|
if (request.CargoParamGroupList == null || request.CargoParamGroupList.Count() == 0)
|
|
throw new BusinessException("缺少报价参数");
|
|
|
|
var extJArray = new List<object>();
|
|
var errorBuilder = new StringBuilder();
|
|
var freightAmount = 0M;
|
|
var productAmount = 0M;
|
|
var totalAmount = 0M;
|
|
|
|
foreach (var cargoParamGroup in request.CargoParamGroupList)
|
|
{
|
|
var purchaseAccount = request.PurchaseAccountList.FirstOrDefault(pa => pa.PurchasePlatformId == cargoParamGroup.PurchasePlatform);
|
|
if (purchaseAccount == null)
|
|
throw new BusinessException($"缺少{cargoParamGroup.PurchasePlatform}采购平台账号,请在店铺配置中设置");
|
|
|
|
var orderProductParamList = CombineRepeatPurchaseSku(cargoParamGroup.CargoParamList);
|
|
try
|
|
{
|
|
var response = ppPlatformClientFactory.GetClient((AdapterEnums.PlatformType)cargoParamGroup.PurchasePlatform)
|
|
.PreviewOrder(new PP_PreviewOrderRequest()
|
|
{
|
|
AppKey = purchaseAccount.AppKey,
|
|
AppSecret = purchaseAccount.AppSecret,
|
|
AppToken = purchaseAccount.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)cargoParamGroup.PurchasePlatform,
|
|
PurchaseMode = (AdapterEnums.PurchaseMode)request.PurchaseOrderMode,
|
|
OrderProductParamList = orderProductParamList
|
|
});
|
|
|
|
if (cargoParamGroup.PurchasePlatform == Enums.Platform.阿里巴巴)
|
|
extJArray.Add(new { cargoParamGroup.PurchaserId, cargoParamGroup.PurchasePlatform, OrderTradeTypeCode = response.Extensions });
|
|
|
|
freightAmount += response.FreightAmount;
|
|
productAmount += response.ProductAmount;
|
|
totalAmount += response.TotalAmount;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
errorBuilder.AppendLine($"采购商:{cargoParamGroup.PurchaserName}");
|
|
errorBuilder.AppendLine(ex.Message);
|
|
throw new BusinessException(errorBuilder.ToString());
|
|
}
|
|
}
|
|
|
|
return new PreviewOrderResponse()
|
|
{
|
|
Extensions = JsonConvert.SerializeObject(extJArray),
|
|
FreightAmount = freightAmount,
|
|
ProductAmount = productAmount,
|
|
TotalAmount = totalAmount
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建采购单
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <exception cref="BusinessException"></exception>
|
|
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("只能为等待采购或待出库的订单进行采购");
|
|
|
|
if (request.Consignee == null ||
|
|
string.IsNullOrEmpty(request.Consignee.Address) ||
|
|
string.IsNullOrEmpty(request.Consignee.Mobile) ||
|
|
string.IsNullOrEmpty(request.Consignee.ContactName))
|
|
throw new BusinessException("缺少收货人信息");
|
|
if (request.PurchaseAccountList == null || request.PurchaseAccountList.Count() == 0)
|
|
throw new BusinessException("缺少采购账号");
|
|
if (request.CargoParamGroupList == null || request.CargoParamGroupList.Count() == 0)
|
|
throw new BusinessException("缺少下单商品参数");
|
|
|
|
//var deleteOrderCostDetail = fsql.Delete<OrderCostDetail>().Where(ocd => ocd.OrderId == dbOrder.Id);
|
|
var isRepurchase = fsql.Select<OrderCost>(dbOrder.Id).Any();
|
|
var orderSkus = fsql.Select<OrderSku>().Where(osku => osku.Price != 0 && osku.OrderId == request.OrderId).ToList();
|
|
|
|
|
|
var extJArray = JsonConvert.DeserializeObject<JArray>(request.Extensions);
|
|
//IList<PP_QueryOrderDetailResponse> purchaseOrderSimpleInfoList = new List<PP_QueryOrderDetailResponse>();
|
|
|
|
List<OrderCostDetail> insertOrderCostDetails = new List<OrderCostDetail>();
|
|
List<OrderPurchaseInfo> insertOrderPurchaseInfos = new List<OrderPurchaseInfo>();
|
|
List<OrderPurchaseSkuInfo> insertOrderPurchaseSkuInfos = new List<OrderPurchaseSkuInfo>();
|
|
List<long> updatePurchaseTimeSchemeIdList = new List<long>();
|
|
List<OrderPurchaseRelationInfo> insertOrderPurchaseRelationInfoList = new List<OrderPurchaseRelationInfo>();
|
|
|
|
#region 待更新
|
|
IList<long> updateOrderCostDetailIdList = fsql.Select<OrderCostDetail>()
|
|
.Where(ocd => ocd.OrderId == request.OrderId && ocd.IsEnabled == true)
|
|
.ToList(ocd => ocd.Id);
|
|
IList<long> updatePurchaseOrderIdList = fsql.Select<OrderPurchaseInfo>()
|
|
.Where(opi => opi.OrderId == request.OrderId && opi.IsEnabled == true)
|
|
.ToList(opi => opi.Id);
|
|
#endregion
|
|
|
|
IInsert<OrderCost> insertOrderCost = null;
|
|
IUpdate<OrderCost> updateOrderCost = null;
|
|
var totalPurchaseAmount = 0M;
|
|
|
|
foreach (var cargoParamGroup in request.CargoParamGroupList)
|
|
{
|
|
var purchaseAccount = request.PurchaseAccountList.FirstOrDefault(pa => pa.PurchasePlatformId == cargoParamGroup.PurchasePlatform);
|
|
if (purchaseAccount == null)
|
|
throw new BusinessException($"缺少{cargoParamGroup.PurchasePlatform}采购平台账号,请在店铺配置中设置");
|
|
|
|
var client = ppPlatformClientFactory.GetClient((AdapterEnums.PlatformType)cargoParamGroup.PurchasePlatform);
|
|
var extJson = extJArray.FirstOrDefault(j => j.Value<string>("PurchaserId") == cargoParamGroup.PurchaserId);
|
|
|
|
updatePurchaseTimeSchemeIdList.AddRange(cargoParamGroup.CargoParamList.Select(p => p.SchemeId).Distinct());
|
|
|
|
var orderProductParamList = CombineRepeatPurchaseSku(cargoParamGroup.CargoParamList);
|
|
|
|
var createOrderResponse = client.CreateOrder(new PP_CreateOrderRequest()
|
|
{
|
|
AppKey = purchaseAccount.AppKey,
|
|
AppSecret = purchaseAccount.AppSecret,
|
|
AppToken = purchaseAccount.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)purchaseAccount.PurchasePlatformId,
|
|
PurchaseMode = (AdapterEnums.PurchaseMode)request.PurchaseOrderMode,
|
|
Extensions = extJson.Value<string>("OrderTradeTypeCode"),
|
|
Remark = request.Remark,
|
|
OrderProductParamList = orderProductParamList
|
|
});
|
|
|
|
|
|
|
|
var purchaseOrderSimpleInfo = client.QueryOrderDetail(new PP_QueryOrderDetailRequest()
|
|
{
|
|
AppKey = purchaseAccount.AppKey,
|
|
AppSecret = purchaseAccount.AppSecret,
|
|
AppToken = purchaseAccount.AppToken,
|
|
OrderId = createOrderResponse.OrderId
|
|
});
|
|
|
|
foreach (var cargoParam in cargoParamGroup.CargoParamList)
|
|
{
|
|
var orderSku = orderSkus.FirstOrDefault(osku => osku.SkuId == cargoParam.BelongSkuId);
|
|
insertOrderPurchaseRelationInfoList.Add(new OrderPurchaseRelationInfo()
|
|
{
|
|
Id = idGenerator.NewLong(),
|
|
BelongSkuId = cargoParam.BelongSkuId,
|
|
CreateTime = DateTime.Now,
|
|
OrderId = request.OrderId,
|
|
PurchaseOrderId = purchaseOrderSimpleInfo.OrderId,
|
|
PurchaseProductId = cargoParam.ProductId,
|
|
PurchaseSkuId = cargoParam.SkuId,
|
|
PurchaseSpecId = cargoParam.SpecId,
|
|
Quantity = cargoParam.Quantity,
|
|
SchemeId = cargoParam.SchemeId,
|
|
SourceSkuId = orderSku?.BelongSkuId
|
|
});
|
|
|
|
}
|
|
|
|
totalPurchaseAmount += purchaseOrderSimpleInfo.TotalAmount;
|
|
|
|
var belongSkuGroups = cargoParamGroup.CargoParamList.GroupBy(p => p.BelongSkuId);
|
|
foreach (var belongSkuGroup in belongSkuGroups)
|
|
{
|
|
var belongSkuId = belongSkuGroup.Key;
|
|
var currentOrderSkuProductAmount = 0M; //采购成本
|
|
var currentOrderSkuCargoParamList = cargoParamGroup.CargoParamList.Where(p => p.BelongSkuId == belongSkuId); //找当前skuId的采购skuId
|
|
|
|
foreach (var currentOrderSkuCargo in currentOrderSkuCargoParamList)
|
|
{
|
|
var purchaseSkuProductAmount = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.SkuId)
|
|
.Sum(p => p.ProductAmount);
|
|
var purchaseSkuTotalQuantity = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.SkuId)
|
|
.Sum(p => p.Quantity);
|
|
currentOrderSkuProductAmount += purchaseSkuProductAmount * (1.0M * currentOrderSkuCargo.Quantity / purchaseSkuTotalQuantity);
|
|
}
|
|
|
|
var currentOrderSkuFreightAmount = purchaseOrderSimpleInfo.FreightAmount / belongSkuGroups.Count(); //采购运费(按sku数均分)
|
|
|
|
#region 成本明细
|
|
var orderSku = orderSkus.FirstOrDefault(osku => osku.SkuId == belongSkuId);
|
|
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,
|
|
PurchaseOrderId = purchaseOrderSimpleInfo.OrderId,
|
|
SkuAmount = currentOrderSkuProductAmount,
|
|
TotalCost = currentOrderSkuProductAmount + currentOrderSkuFreightAmount,
|
|
SkuId = belongSkuId,
|
|
StorageAmount = 0,
|
|
IsEnabled = true
|
|
};
|
|
insertOrderCostDetails.Add(orderCostDetail);
|
|
#endregion
|
|
}
|
|
|
|
#region 采购订单信息
|
|
var orderPurchaserInfo = new OrderPurchaseInfo()
|
|
{
|
|
Id = idGenerator.NewLong(),
|
|
OrderId = request.OrderId,
|
|
CreateTime = DateTime.Now,
|
|
PurchaseAccountId = purchaseAccount.Id,
|
|
PurchaseAccountName = purchaseAccount.AccountName,
|
|
PurchaseMethod = Enums.PurchaseMethod.线上采购,
|
|
PurchaseOrderId = createOrderResponse.OrderId,
|
|
PurchasePlatform = cargoParamGroup.PurchasePlatform,
|
|
PurchaserId = cargoParamGroup.PurchaserId,
|
|
PurchaserName = cargoParamGroup.PurchaserName,
|
|
ShopId = request.ShopId,
|
|
BelongSkuIds = string.Join(",", belongSkuGroups.Select(bsg => bsg.Key)),
|
|
IsEnabled = true
|
|
};
|
|
insertOrderPurchaseInfos.Add(orderPurchaserInfo);
|
|
#endregion
|
|
|
|
#region 采购订单Sku信息
|
|
foreach (var purchaseOrderSku in purchaseOrderSimpleInfo.ItemList)
|
|
{
|
|
var orderPurchaseSkuInfo = new OrderPurchaseSkuInfo()
|
|
{
|
|
Id = purchaseOrderSku.OrderSkuId,
|
|
CreateTime = DateTime.Now,
|
|
OrderId = request.OrderId,
|
|
PurchaseOrderId = purchaseOrderSimpleInfo.OrderId,
|
|
PurchaseProductId = purchaseOrderSku.ProductId,
|
|
PurchaseSkuId = purchaseOrderSku.SkuId,
|
|
ShopId = request.ShopId
|
|
};
|
|
insertOrderPurchaseSkuInfos.Add(orderPurchaseSkuInfo);
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
#region 订单成本
|
|
var orderCost = new OrderCost()
|
|
{
|
|
OrderId = request.OrderId,
|
|
CreateTime = DateTime.Now,
|
|
DeliveryExpressFreight = 0,
|
|
IsManualEdited = false,
|
|
PlatformCommissionRatio = 0,
|
|
PreferentialAmount = 0,
|
|
PurchaseAmount = totalPurchaseAmount,
|
|
TotalCost = totalPurchaseAmount
|
|
};
|
|
//orderCost.PlatformCommissionAmount = dbOrder.OrderSellerPrice * orderCost.PlatformCommissionRatio;
|
|
orderCost.Profit = dbOrder.OrderTotalPrice -
|
|
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
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
//deleteOrderCostDetail.ExecuteAffrows();
|
|
if (updatePurchaseOrderIdList.Count() > 0)
|
|
fsql.Update<OrderPurchaseInfo>(updatePurchaseOrderIdList).Set(opi => opi.IsEnabled, false).ExecuteAffrows();
|
|
if (updateOrderCostDetailIdList.Count() > 0)
|
|
fsql.Update<OrderCostDetail>(updateOrderCostDetailIdList).Set(ocd => ocd.IsEnabled, false).ExecuteAffrows();
|
|
|
|
fsql.Insert(insertOrderCostDetails).ExecuteAffrows();
|
|
fsql.Insert(insertOrderPurchaseInfos).ExecuteAffrows();
|
|
fsql.Insert(insertOrderPurchaseSkuInfos).ExecuteAffrows();
|
|
fsql.Insert(insertOrderPurchaseRelationInfoList).ExecuteAffrows();
|
|
updateOrderCost?.ExecuteAffrows();
|
|
insertOrderCost?.ExecuteAffrows();
|
|
if (updatePurchaseTimeSchemeIdList.Count() > 0)
|
|
fsql.Update<PurchaseScheme>(updatePurchaseTimeSchemeIdList).Set(p => p.LastPurchaseTime, DateTime.Now).ExecuteAffrows();
|
|
fsql.Update<Order>(request.OrderId).SetIf(dbOrder.OrderState == Enums.OrderState.等待采购, o => o.OrderState, Model.Enums.OrderState.待出库)
|
|
.SetIf(!string.IsNullOrEmpty(request.Remark), o => o.PurchaseRemark, request.Remark)
|
|
.Set(o => o.IsPurchased, true)
|
|
.Set(o => o.PackConfigState, Enums.PackConfigState.待配置)
|
|
.ExecuteAffrows();
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取关联订单列表
|
|
/// </summary>
|
|
/// <param name="orderId"></param>
|
|
/// <returns></returns>
|
|
public IList<AssociationPurchaseOrderResponse> GetAssociationPurchaseOrderList(string orderId)
|
|
{
|
|
IList<AssociationPurchaseOrderResponse> list = new List<AssociationPurchaseOrderResponse>();
|
|
var purchaseOrderList = fsql.Select<OrderPurchaseInfo>().Where(opi => opi.OrderId == orderId).ToList();
|
|
var orderSkuList = fsql.Select<OrderSku>().Where(osku => osku.OrderId == orderId).ToList();
|
|
var orderCostDetailList = fsql.Select<OrderCostDetail>().Where(ocd => ocd.OrderId == orderId).ToList();
|
|
|
|
foreach (var purchaseOrder in purchaseOrderList)
|
|
{
|
|
var associationPurchaseOrder = new AssociationPurchaseOrderResponse()
|
|
{
|
|
PurchaseAccountId = purchaseOrder.PurchaseAccountId,
|
|
PurchaseAccountName = purchaseOrder.PurchaseAccountName,
|
|
PurchasePlatform = purchaseOrder.PurchasePlatform.Value,
|
|
PurchaserId = purchaseOrder.PurchaserId,
|
|
PurchaserName = purchaseOrder.PurchaserName,
|
|
PurchaserOrderId = purchaseOrder.PurchaseOrderId,
|
|
IsEnabled = purchaseOrder.IsEnabled,
|
|
PurchaseMethod = purchaseOrder.PurchaseMethod.Value
|
|
};
|
|
|
|
var currentOrderCostDetailList = orderCostDetailList.Where(ocd => ocd.PurchaseOrderId == purchaseOrder.PurchaseOrderId).ToList();
|
|
|
|
foreach (var orderCostDetail in currentOrderCostDetailList)
|
|
{
|
|
var orderSku = orderSkuList.FirstOrDefault(osku => osku.SkuId == orderCostDetail.SkuId);
|
|
var assocationOrderCostDetail = new AssocationOrderCostDetailResponse()
|
|
{
|
|
Id = orderCostDetail.Id,
|
|
Logo = orderSku.Logo,
|
|
Title = orderSku.Title,
|
|
OrderId = orderSku.OrderId,
|
|
SkuId = orderSku.SkuId,
|
|
PurchaseQuantity = orderCostDetail.DeductionQuantity ?? 0,
|
|
PurchaseFreight = orderCostDetail.PurchaseFreight ?? 0M,
|
|
SkuAmount = orderCostDetail.SkuAmount ?? 0M,
|
|
};
|
|
if (assocationOrderCostDetail.PurchaseQuantity != 0)
|
|
assocationOrderCostDetail.PurchasePrice = assocationOrderCostDetail.SkuAmount / assocationOrderCostDetail.PurchaseQuantity;
|
|
|
|
associationPurchaseOrder.AssocationOrderCostDetailList.Add(assocationOrderCostDetail);
|
|
}
|
|
associationPurchaseOrder.PurchaseAmount = associationPurchaseOrder.AssocationOrderCostDetailList.Sum(x => x.SkuAmount);
|
|
associationPurchaseOrder.PurchaseFreight = associationPurchaseOrder.AssocationOrderCostDetailList.Sum(x => x.PurchaseFreight);
|
|
list.Add(associationPurchaseOrder);
|
|
}
|
|
return list;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关联订单
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
public void AssociatePurchaseOrder(AssociationOrderRequest request)
|
|
{
|
|
if (request.AssociationPurchaseOrderList == null || request.AssociationPurchaseOrderList.Count() == 0)
|
|
throw new BusinessException("缺少采购单信息");
|
|
|
|
var dbPurchaseOrderList = fsql.Select<OrderPurchaseInfo>().Where(opi => opi.OrderId == request.OrderId).ToList();
|
|
var dbInvalidPurchaseOrderIdList = dbPurchaseOrderList.Where(opi => !opi.IsEnabled).Select(opi => opi.PurchaseOrderId).ToList();
|
|
|
|
var dbOrderSkuList = fsql.Select<OrderSku>().Where(osku => osku.OrderId == request.OrderId).ToList();
|
|
var dbOrderCostDetailList = fsql.Select<OrderCostDetail>().Where(ocd => ocd.OrderId == request.OrderId && ocd.IsEnabled == true).ToList();
|
|
var dbOrderCost = fsql.Select<OrderCost>(request.OrderId).ToOne();
|
|
var dbOrder = fsql.Select<Order>(request.OrderId).ToOne();
|
|
|
|
List<OrderPurchaseInfo> insertOrderPurchaseInfoList = new List<OrderPurchaseInfo>();
|
|
IList<IUpdate<OrderPurchaseInfo>> updateOrderPurchaseInfoList = new List<IUpdate<OrderPurchaseInfo>>();
|
|
List<OrderCostDetail> insertOrderCostDetailList = new List<OrderCostDetail>();
|
|
IList<IUpdate<OrderCostDetail>> updateOrderCostDetailList = new List<IUpdate<OrderCostDetail>>();
|
|
IInsert<OrderCost> insertOrderCost = null;
|
|
IUpdate<OrderCost> updateOrderCost = null;
|
|
|
|
foreach (var purchaseOrder in request.AssociationPurchaseOrderList)
|
|
{
|
|
if (dbInvalidPurchaseOrderIdList.Contains(purchaseOrder.PurchaserOrderId))
|
|
continue;
|
|
|
|
var avgFreight = purchaseOrder.PurchaseFreight / purchaseOrder.AssocationOrderCostDetailList.Count();
|
|
var dbPurchaserOrder = dbPurchaseOrderList.FirstOrDefault(x => x.PurchaseOrderId == purchaseOrder.PurchaserOrderId);
|
|
if (dbPurchaserOrder == null)
|
|
{
|
|
dbPurchaserOrder = new OrderPurchaseInfo()
|
|
{
|
|
Id = idGenerator.NewLong(),
|
|
OrderId = request.OrderId,
|
|
PurchaseAccountId = purchaseOrder.PurchaseAccountId,
|
|
PurchaseAccountName = purchaseOrder.PurchaseAccountName,
|
|
IsEnabled = true,
|
|
BelongSkuIds = string.Join(",", purchaseOrder.AssocationOrderCostDetailList.Select(x => x.SkuId)),
|
|
CreateTime = DateTime.Now,
|
|
PurchaseMethod = Enums.PurchaseMethod.关联外部单,
|
|
PurchaseOrderId = purchaseOrder.PurchaserOrderId,
|
|
PurchasePlatform = purchaseOrder.PurchasePlatform,
|
|
PurchaserId = purchaseOrder.PurchaserId,
|
|
PurchaserName = purchaseOrder.PurchaserName,
|
|
ShopId = request.ShopId
|
|
};
|
|
insertOrderPurchaseInfoList.Add(dbPurchaserOrder);
|
|
|
|
foreach (var assOrderCostDetail in purchaseOrder.AssocationOrderCostDetailList)
|
|
{
|
|
var dbOrderSku = dbOrderSkuList.FirstOrDefault(osku => osku.SkuId == assOrderCostDetail.SkuId);
|
|
var orderCostDetail = new OrderCostDetail()
|
|
{
|
|
Id = idGenerator.NewLong(),
|
|
CreateTime = DateTime.Now,
|
|
DeductionQuantity = assOrderCostDetail.PurchaseQuantity,
|
|
IsEnabled = true,
|
|
OrderId = request.OrderId,
|
|
ProductId = dbOrderSku.ProductId,
|
|
SkuId = assOrderCostDetail.SkuId,
|
|
PurchaseFreight = avgFreight,
|
|
TotalCost = assOrderCostDetail.SkuAmount + avgFreight,
|
|
PurchaseOrderId = purchaseOrder.PurchaserOrderId
|
|
};
|
|
insertOrderCostDetailList.Add(orderCostDetail);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbPurchaserOrder.PurchaseAccountId = purchaseOrder.PurchaseAccountId;
|
|
dbPurchaserOrder.PurchaseAccountName = purchaseOrder.PurchaseAccountName;
|
|
//dbPurchaserOrder.BelongSkuIds = string.Join(",", purchaseOrder.AssocationOrderCostDetailList.Select(x => x.SkuId));
|
|
dbPurchaserOrder.PurchasePlatform = purchaseOrder.PurchasePlatform;
|
|
dbPurchaserOrder.PurchaserId = purchaseOrder.PurchaserId;
|
|
dbPurchaserOrder.PurchaserName = purchaseOrder.PurchaserName;
|
|
updateOrderPurchaseInfoList.Add(fsql.Update<OrderPurchaseInfo>().SetSource(dbPurchaserOrder));
|
|
|
|
foreach (var assOrderCostDetail in purchaseOrder.AssocationOrderCostDetailList)
|
|
{
|
|
var dbOrderCostDetail = dbOrderCostDetailList.FirstOrDefault(ocd => ocd.SkuId == assOrderCostDetail.SkuId);
|
|
dbOrderCostDetail.SkuAmount = assOrderCostDetail.SkuAmount;
|
|
dbOrderCostDetail.DeductionQuantity = assOrderCostDetail.PurchaseQuantity;
|
|
dbOrderCostDetail.PurchaseFreight = avgFreight;
|
|
dbOrderCostDetail.TotalCost = assOrderCostDetail.SkuAmount + avgFreight;
|
|
updateOrderCostDetailList.Add(fsql.Update<OrderCostDetail>().SetSource(dbOrderCostDetail));
|
|
}
|
|
}
|
|
}
|
|
|
|
var totalPurchaseAmount = request.AssociationPurchaseOrderList.Where(x => !dbInvalidPurchaseOrderIdList.Contains(x.PurchaserOrderId))
|
|
.Sum(p => p.PurchaseAmount + p.PurchaseFreight);
|
|
var profit = dbOrder.OrderTotalPrice - totalPurchaseAmount - (dbOrderCost?.DeliveryExpressFreight ?? 0); // -orderCost.PlatformCommissionAmount
|
|
|
|
if (dbOrderCost == null)
|
|
{
|
|
dbOrderCost = new OrderCost()
|
|
{
|
|
OrderId = request.OrderId,
|
|
DeliveryExpressFreight = 0,
|
|
IsManualEdited = false,
|
|
PlatformCommissionAmount = 0,
|
|
PlatformCommissionRatio = 0,
|
|
PreferentialAmount = 0,
|
|
CreateTime = DateTime.Now,
|
|
PurchaseAmount = totalPurchaseAmount,
|
|
TotalCost = totalPurchaseAmount,
|
|
Profit = profit
|
|
};
|
|
insertOrderCost = fsql.Insert(dbOrderCost);
|
|
}
|
|
else
|
|
{
|
|
dbOrderCost.PurchaseAmount = totalPurchaseAmount;
|
|
dbOrderCost.Profit = profit;
|
|
updateOrderCost = fsql.Update<OrderCost>(request.OrderId).Set(oc => oc.PurchaseAmount, totalPurchaseAmount)
|
|
.Set(oc => oc.TotalCost, totalPurchaseAmount)
|
|
.Set(oc => oc.Profit, profit);
|
|
}
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
if (insertOrderPurchaseInfoList.Count() > 0)
|
|
fsql.Insert(insertOrderPurchaseInfoList).ExecuteAffrows();
|
|
if (updateOrderPurchaseInfoList.Count() > 0)
|
|
{
|
|
foreach (var update in updateOrderPurchaseInfoList)
|
|
update.ExecuteAffrows();
|
|
}
|
|
if (insertOrderCostDetailList.Count() > 0)
|
|
fsql.Insert(insertOrderCostDetailList).ExecuteAffrows();
|
|
if (updateOrderCostDetailList.Count() > 0)
|
|
{
|
|
foreach (var update in updateOrderCostDetailList)
|
|
update.ExecuteAffrows();
|
|
}
|
|
insertOrderCost?.ExecuteAffrows();
|
|
updateOrderCost?.ExecuteAffrows();
|
|
if (dbOrder.OrderState == Enums.OrderState.待付款)
|
|
fsql.Update<Order>(dbOrder.Id).Set(o => o.OrderState, Enums.OrderState.待出库).ExecuteAffrows();
|
|
});
|
|
}
|
|
|
|
public void SetHistoryPurchaseOrder(SetHistoryPurchaseOrderRequest request)
|
|
{
|
|
var dbOrderCostDetailList = fsql.Select<OrderCostDetail>().Where(ocd => ocd.OrderId == request.OrderId).ToList();
|
|
var dbOrderCost = fsql.Select<OrderCost>(request.OrderId).ToOne();
|
|
var dbOrder = fsql.Select<Order>(request.OrderId).ToOne();
|
|
|
|
try
|
|
{
|
|
dbOrderCost.PurchaseAmount = dbOrderCostDetailList.Where(ocd => ocd.PurchaseOrderId != request.PurchaseOrderId).Sum(ocd => ocd.TotalCost);
|
|
}
|
|
catch
|
|
{
|
|
dbOrderCost.PurchaseAmount = 0;
|
|
}
|
|
dbOrderCost.Profit = dbOrder.OrderTotalPrice -
|
|
dbOrderCost.PurchaseAmount -
|
|
dbOrderCost.DeliveryExpressFreight; // -orderCost.PlatformCommissionAmount
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
fsql.Update<OrderPurchaseInfo>()
|
|
.Set(opi => opi.IsEnabled, false)
|
|
.Where(opi => opi.PurchaseOrderId == request.PurchaseOrderId)
|
|
.ExecuteAffrows();
|
|
|
|
fsql.Update<OrderCostDetail>()
|
|
.Set(ocd => ocd.IsEnabled, false)
|
|
.Where(ocd => ocd.PurchaseOrderId == request.PurchaseOrderId)
|
|
.ExecuteAffrows();
|
|
|
|
fsql.Update<OrderCost>().SetSource(dbOrderCost).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;
|
|
case "LOGISTICS_BUYER_VIEW_TRACE":
|
|
LogisticsUpdateCallbackFrom1688(jObject);//1688物流信息变更
|
|
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 = "物流公司翻译";
|
|
|
|
OP_QueryExpressCompanyResponse convertExpressCompany = null;
|
|
|
|
try
|
|
{
|
|
convertExpressCompany = expressCompanyNameConverter.Converter(wayBillNoResponse.ExpressName,
|
|
(AdapterEnums.PlatformType)callbackPlatform,
|
|
(AdapterEnums.PlatformType)shop.PlatformId,
|
|
expressCompanyList);
|
|
if (convertExpressCompany != null)
|
|
expressCompanyInfo = JsonConvert.SerializeObject(convertExpressCompany);
|
|
}
|
|
catch
|
|
{
|
|
throw;
|
|
}
|
|
finally
|
|
{
|
|
#region 店铺平台订单出库
|
|
currentProgress = "店铺平台订单出库";
|
|
orderBusiness.OutStock(new OutStockRequest()
|
|
{
|
|
AppKey = shop.AppKey,
|
|
AppSecret = shop.AppSecret,
|
|
AppToken = shop.AppToken,
|
|
OrderId = orderId,
|
|
TargetExpressId = convertExpressCompany?.ExpressId ?? string.Empty, //物流公司Id
|
|
TargetExpressName = convertExpressCompany?.ExpressName ?? string.Empty, //物流公司名称
|
|
SourceExpressId = wayBillNoResponse.ExpressId,
|
|
SourceExpressName = wayBillNoResponse.ExpressName,
|
|
PurchaseOrderId = purchaseOrderId,
|
|
Platform = shop.PlatformId,
|
|
WayBillNo = wayBillNoResponse.WayBillNo
|
|
});
|
|
#endregion
|
|
|
|
#region 通知C端出库
|
|
//通知C端
|
|
try
|
|
{
|
|
restApiService.SendRequest("https://bbwy.qiyue666.com",
|
|
"/Api/PurchaseOrder/QuanTanSendGoodsCallback",
|
|
new
|
|
{
|
|
OrderId = orderPurchaseInfo.OrderId,
|
|
ExpressId = wayBillNoResponse.ExpressId,
|
|
ExpressName = wayBillNoResponse.ExpressName,
|
|
wayBillNoResponse.WayBillNo
|
|
},
|
|
null,
|
|
HttpMethod.Post);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
#endregion
|
|
}
|
|
#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>
|
|
/// 1688物流信息变更回调
|
|
/// </summary>
|
|
/// <param name="jObject"></param>
|
|
private void LogisticsUpdateCallbackFrom1688(JObject jObject)
|
|
{
|
|
Task.Factory.StartNew(() =>
|
|
{
|
|
IList<IUpdate<OrderPurchaseSkuInfo>> updateOrderPurchaseSkuInfoList = new List<IUpdate<OrderPurchaseSkuInfo>>();
|
|
|
|
var statusChanged = jObject["data"]["OrderLogisticsTracingModel"].Value<string>("statusChanged").ToUpper();
|
|
var orderLogsItems = jObject["data"]["OrderLogisticsTracingModel"]["orderLogsItems"].Children();
|
|
|
|
var purchaseOrderIds = orderLogsItems.Select(x => x.Value<string>("orderId")).Distinct().ToList();
|
|
var orderEntryIds = orderLogsItems.Select(x => x.Value<string>("orderEntryId")).Distinct().ToList();
|
|
|
|
var dbOrderPurchaseSkuInfoList = fsql.Select<OrderPurchaseSkuInfo>()
|
|
.WhereIf(purchaseOrderIds.Count() > 1, ops => purchaseOrderIds.Contains(ops.PurchaseOrderId))
|
|
.WhereIf(purchaseOrderIds.Count() == 1, ops => ops.PurchaseOrderId == purchaseOrderIds[0])
|
|
.ToList();
|
|
|
|
var dbOrderPurchaseRelationInfoList = fsql.Select<OrderPurchaseRelationInfo>()
|
|
.WhereIf(purchaseOrderIds.Count() > 1, opr => purchaseOrderIds.Contains(opr.PurchaseOrderId))
|
|
.WhereIf(purchaseOrderIds.Count() == 1, opr => opr.PurchaseOrderId == purchaseOrderIds[0])
|
|
.ToList();
|
|
|
|
foreach (var orderEntryId in orderEntryIds)
|
|
{
|
|
var dbOrderPurchaseSkuInfo = dbOrderPurchaseSkuInfoList.FirstOrDefault(x => x.Id == orderEntryId);
|
|
if (dbOrderPurchaseSkuInfo.ExpressState != statusChanged)
|
|
{
|
|
var update = fsql.Update<OrderPurchaseSkuInfo>(orderEntryId).Set(x => x.ExpressState, statusChanged)
|
|
.Set(x => x.ExpressChangeTime, DateTime.Now);
|
|
updateOrderPurchaseSkuInfoList.Add(update);
|
|
dbOrderPurchaseSkuInfo.ExpressState = statusChanged;
|
|
}
|
|
}
|
|
|
|
if (statusChanged == "SIGN")
|
|
{
|
|
var groupsByPoIds = dbOrderPurchaseSkuInfoList.GroupBy(x => x.PurchaseOrderId);
|
|
foreach (var group in groupsByPoIds)
|
|
{
|
|
var isSignAll = group.Count() == group.Where(x => x.ExpressState == "SIGN").Count();
|
|
|
|
#region 通知齐库
|
|
var relationList = dbOrderPurchaseRelationInfoList.Where(x => x.PurchaseOrderId == group.Key).ToList();
|
|
foreach (var relation in relationList)
|
|
{
|
|
//通知C端
|
|
try
|
|
{
|
|
restApiService.SendRequest("http://qiku.qiyue666.com",
|
|
"/Api/PackPurchaseTask/UpdateAvailabilityState",
|
|
new
|
|
{
|
|
availability = isSignAll ? 0 : 1,
|
|
orderId = relation.OrderId,
|
|
skuId = relation.SourceSkuId
|
|
},
|
|
null,
|
|
HttpMethod.Post);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
var groupsByOrderIds = dbOrderPurchaseSkuInfoList.GroupBy(x => x.OrderId);
|
|
foreach (var group in groupsByOrderIds)
|
|
{
|
|
var isSignAll = group.Count() == group.Where(x => x.ExpressState == "SIGN").Count();
|
|
|
|
if (isSignAll)
|
|
{
|
|
//通知C端
|
|
try
|
|
{
|
|
restApiService.SendRequest("https://bbwy.qiyue666.com",
|
|
"/Api/PurchaseOrder/SignPurchaseOrder",
|
|
new { orderId = group.Key },
|
|
null,
|
|
HttpMethod.Post);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (updateOrderPurchaseSkuInfoList.Count() > 0)
|
|
{
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var update in updateOrderPurchaseSkuInfoList)
|
|
update.ExecuteAffrows();
|
|
});
|
|
}
|
|
|
|
|
|
}, CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 采购平台改价回调
|
|
/// </summary>
|
|
/// <param name="purchaseOrderId"></param>
|
|
/// <param name="callbackPlatform"></param>
|
|
private void OrderPriceModificationCallback(string purchaseOrderId, Enums.Platform callbackPlatform)
|
|
{
|
|
string currentProgress = string.Empty;
|
|
|
|
try
|
|
{
|
|
var orderPurchaseInfo = fsql.Select<OrderPurchaseInfo>().Where(opi => opi.PurchaseOrderId == purchaseOrderId).ToOne();
|
|
if (orderPurchaseInfo == null)
|
|
throw new Exception($"采购单{purchaseOrderId}-未查询到采购单");
|
|
|
|
var purchaseAccount = fsqlManager.MDSfsql.Select<Purchaseaccount>().Where(pa => pa.Id == orderPurchaseInfo.PurchaseAccountId).ToOne();
|
|
if (purchaseAccount == null)
|
|
throw new Exception($"采购单{purchaseOrderId}-未查询到采购账号{orderPurchaseInfo.PurchaseAccountId}");
|
|
|
|
var dbOrder = fsql.Select<Order>(orderPurchaseInfo.OrderId).ToOne();
|
|
var dbOrderCost = fsql.Select<OrderCost>(orderPurchaseInfo.OrderId).ToOne();
|
|
if (dbOrderCost == null)
|
|
throw new Exception($"采购单{purchaseOrderId}-未查询到订单{orderPurchaseInfo.OrderId}的成本");
|
|
|
|
var dbOrderCostDetails = fsql.Select<OrderCostDetail>().Where(ocd => ocd.OrderId == dbOrder.Id && ocd.IsEnabled == true).ToList();
|
|
if (dbOrderCostDetails.Count() == 0)
|
|
throw new Exception($"采购单{purchaseOrderId}-未查询到订单{orderPurchaseInfo.OrderId}的明细成本");
|
|
|
|
|
|
var dbOrderPurchaseInfoList = fsql.Select<OrderPurchaseInfo>().Where(opi => opi.OrderId == orderPurchaseInfo.OrderId && opi.IsEnabled == true).ToList();
|
|
if (dbOrderPurchaseInfoList.Count() == 0)
|
|
throw new Exception($"采购单{purchaseOrderId}-未查询到订单{orderPurchaseInfo.OrderId}的采购单集合");
|
|
|
|
var dbPurchaseOrderIdList = dbOrderPurchaseInfoList.Select(x => x.PurchaseOrderId).ToList();
|
|
var dbOrderPurchaseRelationInfos = fsql.Select<OrderPurchaseRelationInfo>().Where(x => dbPurchaseOrderIdList.Contains(x.PurchaseOrderId)).ToList();
|
|
if (dbOrderPurchaseRelationInfos.Count() == 0)
|
|
throw new Exception($"采购单{purchaseOrderId}-未查询到采购单关联明细,手动关联的采购单不支持改价");
|
|
|
|
List<IUpdate<OrderCostDetail>> updateOrderCostDetailList = new List<IUpdate<OrderCostDetail>>();
|
|
IUpdate<OrderCost> updateOrderCost = null;
|
|
|
|
var client = ppPlatformClientFactory.GetClient(AdapterEnums.PlatformType.阿里巴巴);
|
|
|
|
var totalPurchaseAmount = 0M;
|
|
foreach (var opi in dbOrderPurchaseInfoList)
|
|
{
|
|
var purchaseOrderSimpleInfo = client.QueryOrderDetail(new PP_QueryOrderDetailRequest()
|
|
{
|
|
AppKey = purchaseAccount.AppKey,
|
|
AppSecret = purchaseAccount.AppSecret,
|
|
AppToken = purchaseAccount.AppToken,
|
|
OrderId = opi.PurchaseOrderId
|
|
});
|
|
totalPurchaseAmount += purchaseOrderSimpleInfo.TotalAmount;
|
|
|
|
var currentPurchaseOrderRelationInfos = dbOrderPurchaseRelationInfos.Where(x => x.PurchaseOrderId == opi.PurchaseOrderId).ToList();
|
|
|
|
var belongSkuGroups = currentPurchaseOrderRelationInfos.GroupBy(p => p.BelongSkuId);
|
|
foreach (var belongSkuGroup in belongSkuGroups)
|
|
{
|
|
var belongSkuId = belongSkuGroup.Key;
|
|
var currentOrderSkuProductAmount = 0M; //采购成本
|
|
var currentOrderSkuCargoParamList = belongSkuGroup.ToList(); //找当前skuId的采购skuId
|
|
|
|
foreach (var currentOrderSkuCargo in currentOrderSkuCargoParamList)
|
|
{
|
|
var purchaseSkuProductAmount = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.PurchaseSkuId)
|
|
.Sum(p => p.ProductAmount);
|
|
var purchaseSkuTotalQuantity = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.PurchaseSkuId)
|
|
.Sum(p => p.Quantity);
|
|
currentOrderSkuProductAmount += purchaseSkuProductAmount * (1.0M * currentOrderSkuCargo.Quantity.Value / purchaseSkuTotalQuantity);
|
|
}
|
|
|
|
var currentOrderSkuFreightAmount = purchaseOrderSimpleInfo.FreightAmount / belongSkuGroups.Count(); //采购运费(按sku数均分)
|
|
|
|
var dbOrderSkuDetail = dbOrderCostDetails.FirstOrDefault(ocd => ocd.SkuId == belongSkuId);
|
|
dbOrderSkuDetail.SkuAmount = currentOrderSkuProductAmount;
|
|
dbOrderSkuDetail.PurchaseFreight = currentOrderSkuFreightAmount;
|
|
dbOrderSkuDetail.TotalCost = currentOrderSkuProductAmount + currentOrderSkuFreightAmount;
|
|
updateOrderCostDetailList.Add(fsql.Update<OrderCostDetail>().SetSource(dbOrderSkuDetail));
|
|
}
|
|
|
|
dbOrderCost.PurchaseAmount = totalPurchaseAmount;
|
|
dbOrderCost.Profit = dbOrder.OrderTotalPrice -
|
|
dbOrderCost.PurchaseAmount -
|
|
dbOrderCost.DeliveryExpressFreight; // -orderCost.PlatformCommissionAmount
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var update in updateOrderCostDetailList)
|
|
update.ExecuteAffrows();
|
|
updateOrderCost?.ExecuteAffrows();
|
|
|
|
//fsql.Update<OrderCost>(dbOrderCost.OrderId).Set(oc => oc.PurchaseAmount, dbOrderCost.PurchaseAmount)
|
|
// .Set(oc => oc.Profit, dbOrderCost.Profit)
|
|
// .ExecuteAffrows();
|
|
});
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
nLogManager.Default().Error(ex, $"OrderPriceModificationCallback 回调平台{callbackPlatform},采购单号{purchaseOrderId},执行进度[{currentProgress}]");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|