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.
1562 lines
88 KiB
1562 lines
88 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.Text;
|
|
using Yitter.IdGenerator;
|
|
using BBWYB.Server.Business.Extensions;
|
|
|
|
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 PurchaseSchemeBusiness purchaseSchemeBusiness;
|
|
private ExpressCompanyNameConverter expressCompanyNameConverter;
|
|
private RestApiService restApiService;
|
|
private IList<Enums.OrderState> cantPurchaseOrderStateList;
|
|
private KuaiDi100Manager kuaiDi100Manager;
|
|
public PurchaseOrderBusiness(IFreeSql fsql,
|
|
NLogManager nLogManager,
|
|
IIdGenerator idGenerator,
|
|
PP_PlatformClientFactory ppPlatformClientFactory,
|
|
TaskSchedulerManager taskSchedulerManager,
|
|
FreeSqlMultiDBManager fsqlManager,
|
|
OrderBusiness orderBusiness,
|
|
VenderBusiness venderBusiness,
|
|
PurchaseSchemeBusiness purchaseSchemeBusiness,
|
|
ExpressCompanyNameConverter expressCompanyNameConverter,
|
|
RestApiService restApiService,
|
|
KuaiDi100Manager kuaiDi100Manager) : 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;
|
|
this.purchaseSchemeBusiness = purchaseSchemeBusiness;
|
|
cantPurchaseOrderStateList = new List<Enums.OrderState>()
|
|
{
|
|
Enums.OrderState.已取消,
|
|
Enums.OrderState.已完成,
|
|
Enums.OrderState.待付款,
|
|
Enums.OrderState.待完结
|
|
};
|
|
this.kuaiDi100Manager = kuaiDi100Manager;
|
|
}
|
|
|
|
/// <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;
|
|
|
|
var previewAmountGroupByPurchaserList = new List<PreviewAmountGroupByPurchaser>();
|
|
|
|
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;
|
|
|
|
previewAmountGroupByPurchaserList.Add(new PreviewAmountGroupByPurchaser()
|
|
{
|
|
FreightAmount = response.FreightAmount,
|
|
ProductAmount = response.ProductAmount,
|
|
TotalAmount = response.TotalAmount,
|
|
PurchasePlatform = cargoParamGroup.PurchasePlatform,
|
|
PurchaserId = cargoParamGroup.PurchaserId,
|
|
PurchaserName = cargoParamGroup.PurchaserName
|
|
});
|
|
}
|
|
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,
|
|
PreviewAmountGroupByPurchaserList = previewAmountGroupByPurchaserList
|
|
};
|
|
}
|
|
|
|
/// <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 (cantPurchaseOrderStateList.Contains(dbOrder.OrderState.Value))
|
|
throw new BusinessException($"当前订单状态:{dbOrder.OrderState},不允许采购");
|
|
|
|
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 isRepurchase = fsql.Select<OrderCost>(dbOrder.Id).Any();
|
|
var orderSkus = fsql.Select<OrderSku>().Where(osku => osku.Price != 0 && osku.OrderId == request.OrderId).ToList();
|
|
var orderPurchaseInfoList = fsql.Select<OrderPurchaseInfo>().Where(opi => opi.OrderId == request.OrderId && opi.IsEnabled == true).ToList();
|
|
|
|
var extJArray = JsonConvert.DeserializeObject<JArray>(request.Extensions);
|
|
|
|
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 totalPurchaseProductAmount = 0M;
|
|
var totalPurchaseFreight = 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 = !string.IsNullOrEmpty(cargoParamGroup.Remark) ? cargoParamGroup.Remark : request.Remark,
|
|
OrderProductParamList = orderProductParamList
|
|
});
|
|
|
|
var purchaseOrderSimpleInfo = client.QueryOrderDetail(new PP_QueryOrderDetailRequest()
|
|
{
|
|
AppKey = purchaseAccount.AppKey,
|
|
AppSecret = purchaseAccount.AppSecret,
|
|
AppToken = purchaseAccount.AppToken,
|
|
OrderId = createOrderResponse.OrderId
|
|
});
|
|
|
|
totalPurchaseProductAmount += purchaseOrderSimpleInfo.ProductAmount;
|
|
totalPurchaseFreight += purchaseOrderSimpleInfo.FreightAmount;
|
|
|
|
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
|
|
});
|
|
|
|
}
|
|
|
|
//采购单总件数,由于1688运费只存在订单层,sku层没有运费,所以需要以订单总件数为基础等比计算采购运费
|
|
var purchaseSkuTotalQuantity = purchaseOrderSimpleInfo.ItemList.Sum(x => x.Quantity);
|
|
var belongSkuGroups = cargoParamGroup.CargoParamList.GroupBy(p => p.BelongSkuId);
|
|
foreach (var belongSkuGroup in belongSkuGroups)
|
|
{
|
|
var belongSkuId = belongSkuGroup.Key;
|
|
var currentOrderSkuCargoParamList = cargoParamGroup.CargoParamList.Where(p => p.BelongSkuId == belongSkuId); //找当前skuId的采购skuId
|
|
|
|
var currentSkuAmount = 0M; //采购成本
|
|
var currentSkuTotalPurchaseQuantity = currentOrderSkuCargoParamList.Sum(x => x.Quantity); //当前skuId的采购数量总和
|
|
var currentPurchaseFreight = purchaseOrderSimpleInfo.FreightAmount *
|
|
(1.0M * currentSkuTotalPurchaseQuantity / purchaseSkuTotalQuantity); //采购运费
|
|
|
|
foreach (var currentOrderSkuCargo in currentOrderSkuCargoParamList)
|
|
{
|
|
var currentPurchaseSkuProductAmount = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.SkuId)
|
|
.Sum(p => p.ProductAmount);
|
|
var currentPurchaseSkuTotalQuantity = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.SkuId)
|
|
.Sum(p => p.Quantity);
|
|
|
|
currentSkuAmount += currentPurchaseSkuProductAmount * (1.0M * currentOrderSkuCargo.Quantity / currentPurchaseSkuTotalQuantity);
|
|
//currentPurchaseFreight += purchaseOrderSimpleInfo.FreightAmount * (1.0M * currentOrderSkuCargo.Quantity / purchaseSkuTotalQuantity);
|
|
}
|
|
|
|
#region 成本明细
|
|
var orderSku = orderSkus.FirstOrDefault(osku => osku.SkuId == belongSkuId);
|
|
var orderCostDetail = new OrderCostDetail()
|
|
{
|
|
Id = idGenerator.NewLong(),
|
|
CreateTime = DateTime.Now,
|
|
DeductionQuantity = orderSku.ItemTotal.Value,
|
|
OrderId = request.OrderId,
|
|
ProductId = orderSku.ProductId,
|
|
PurchaseOrderId = purchaseOrderSimpleInfo.OrderId,
|
|
SkuId = belongSkuId,
|
|
IsEnabled = true
|
|
};
|
|
orderCostDetail.CalculationOrderCostDetailCostAndProfit(orderSku.Price.Value * orderSku.ItemTotal.Value,
|
|
orderSku.BuyerPayFreight ?? 0M,
|
|
orderSku.InPackAmount ?? 0M,
|
|
currentSkuAmount,
|
|
currentPurchaseFreight,
|
|
0M,
|
|
0M);
|
|
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.线上采购,
|
|
OrderState = Enums.PurchaseOrderState.待发货,
|
|
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,
|
|
Remark = !string.IsNullOrEmpty(cargoParamGroup.Remark) ? cargoParamGroup.Remark : request.Remark
|
|
};
|
|
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,
|
|
IsManualEdited = false
|
|
};
|
|
orderCost.CalculationOrderCostCostAndProfit(dbOrder.OrderTotalPrice.Value,
|
|
totalPurchaseProductAmount,
|
|
totalPurchaseFreight,
|
|
0M,
|
|
0M);
|
|
|
|
if (!isRepurchase)
|
|
{
|
|
insertOrderCost = fsql.Insert(orderCost);
|
|
}
|
|
else
|
|
{
|
|
updateOrderCost = fsql.Update<OrderCost>().SetSource(orderCost).IgnoreColumns(a => new { a.CreateTime });
|
|
}
|
|
#endregion
|
|
|
|
#region 订单状态
|
|
dbOrder.CalculationOrderState(fsql, orderSkus, orderPurchaseInfoList.Union(insertOrderPurchaseInfos).ToList());
|
|
#endregion
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
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).Set(o => o.OrderState, dbOrder.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,
|
|
PurchaseOrderId = 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)
|
|
{
|
|
var dbOrder = fsql.Select<Order>(request.OrderId).ToOne();
|
|
if (dbOrder == null)
|
|
throw new BusinessException("订单不存在");
|
|
|
|
if (cantPurchaseOrderStateList.Contains(dbOrder.OrderState.Value))
|
|
throw new BusinessException($"当前订单状态:{dbOrder.OrderState},不允许关联");
|
|
|
|
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 dbvalidPurchaseOrderList = dbPurchaseOrderList.Where(opi => opi.IsEnabled).ToList();
|
|
|
|
if (request.AssociationPurchaseOrderList.Any(x => dbInvalidPurchaseOrderIdList.Contains(x.PurchaseOrderId)))
|
|
throw new BusinessException("关联采购单时不能包含历史采购单");
|
|
|
|
#region 读取采购单中的采购账号/采购方案
|
|
IList<string> purchaseAccountIdList = request.AssociationPurchaseOrderList.Select(x => string.IsNullOrEmpty(x.PurchaseAccountId) ?
|
|
x.PurchaseAccountName :
|
|
x.PurchaseAccountId).Distinct().ToList();
|
|
IList<Purchaseaccount> dbPurchaseAccountList = null;
|
|
if (purchaseAccountIdList.Count() > 0)
|
|
{
|
|
dbPurchaseAccountList = fsqlManager.MDSfsql.Select<Purchaseaccount>().Where(pa => purchaseAccountIdList.Contains(pa.Id) ||
|
|
purchaseAccountIdList.Contains(pa.AccountName)).ToList();
|
|
}
|
|
#endregion
|
|
|
|
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();
|
|
|
|
|
|
|
|
List<OrderPurchaseInfo> insertOrderPurchaseInfoList = new List<OrderPurchaseInfo>();
|
|
List<OrderPurchaseSkuInfo> insertOrderPurchaseSkuInfoList = new List<OrderPurchaseSkuInfo>();
|
|
List<OrderPurchaseRelationInfo> insertOrderPurchaseRelationInfoList = new List<OrderPurchaseRelationInfo>();
|
|
|
|
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.PurchaseOrderId))
|
|
continue;
|
|
|
|
var totalQuantity = purchaseOrder.AssocationOrderCostDetailList.Sum(x => x.PurchaseQuantity);
|
|
|
|
var dbPurchaserOrder = dbPurchaseOrderList.FirstOrDefault(x => x.PurchaseOrderId == purchaseOrder.PurchaseOrderId);
|
|
if (dbPurchaserOrder == null)
|
|
{
|
|
if (purchaseOrder.PurchasePlatform == Enums.Platform.阿里巴巴)
|
|
{
|
|
#region 补齐采购SKU
|
|
var purchaseAccount = dbPurchaseAccountList.FirstOrDefault(pa => pa.Id == purchaseOrder.PurchaseAccountId ||
|
|
pa.AccountName == purchaseOrder.PurchaseAccountName);
|
|
if (purchaseAccount == null)
|
|
throw new BusinessException($"采购单{purchaseOrder.PurchaseOrderId}缺少有效的采购账号");
|
|
|
|
var client = ppPlatformClientFactory.GetClient((AdapterEnums.PlatformType)purchaseOrder.PurchasePlatform);
|
|
var purchaseOrderSimpleInfo = client.QueryOrderDetail(new PP_QueryOrderDetailRequest()
|
|
{
|
|
AppKey = purchaseAccount.AppKey,
|
|
AppSecret = purchaseAccount.AppSecret,
|
|
AppToken = purchaseAccount.AppToken,
|
|
OrderId = purchaseOrder.PurchaseOrderId
|
|
});
|
|
|
|
#region 处理采购商Id
|
|
var purchaserId = purchaseOrderSimpleInfo.PurchaserId;
|
|
if (!string.IsNullOrEmpty(purchaserId))
|
|
throw new BusinessException($"采购单{purchaseOrder.PurchaseOrderId}缺少采购商Id");
|
|
purchaserId = purchaserId.Replace("b2b-", string.Empty);
|
|
var purchaserId2 = purchaserId.Substring(0, purchaserId.Length - 5);
|
|
var purchaserIds = new List<string>() { purchaserId, purchaserId2 };
|
|
var dbPurchaser = fsql.Select<Purchaser>().Where(p => p.Platform == Enums.Platform.阿里巴巴 && purchaserIds.Contains(p.Id)).ToOne();
|
|
if (dbPurchaser == null)
|
|
throw new BusinessException($"采购单{purchaseOrder.PurchaseOrderId}缺少有效采购商");
|
|
purchaserId = dbPurchaser.Id;
|
|
#endregion
|
|
|
|
#region 匹配采购方案
|
|
var skuIds = purchaseOrder.AssocationOrderCostDetailList.Select(ocd => ocd.SkuId).ToList();
|
|
var purchaseSchemeSkuList = fsql.Select<Purchaser, PurchaseScheme, PurchaseSchemeProductSku>()
|
|
.InnerJoin((p, ps, pss) => p.Id == ps.PurchaserId)
|
|
.InnerJoin((p, ps, pss) => ps.Id == pss.SkuPurchaseSchemeId)
|
|
.Where((p, ps, pss) => p.Id == purchaserId)
|
|
.Where((p, ps, pss) => ps.PurchasePlatform == Enums.Platform.阿里巴巴)
|
|
.Where((p, ps, pss) => skuIds.Contains(pss.SkuId))
|
|
.ToList((p, ps, pss) => new
|
|
{
|
|
pss.Id,
|
|
pss.SkuId,
|
|
pss.ProductId,
|
|
pss.PurchaseProductId,
|
|
pss.PurchaseSkuId,
|
|
pss.PurchaseSkuSpecId,
|
|
pss.SkuPurchaseSchemeId
|
|
});
|
|
if (purchaseSchemeSkuList.Count() == 0)
|
|
throw new BusinessException($"采购单{purchaseOrder.PurchaseOrderId} 采购商Id{purchaserId} 未匹配到采购方案");
|
|
#endregion
|
|
|
|
|
|
foreach (var purchaseOrderSku in purchaseOrderSimpleInfo.ItemList)
|
|
{
|
|
var orderPurchaseSkuInfo = new OrderPurchaseSkuInfo()
|
|
{
|
|
Id = purchaseOrderSku.OrderSkuId,
|
|
CreateTime = DateTime.Now,
|
|
OrderId = request.OrderId,
|
|
PurchaseOrderId = purchaseOrder.PurchaseOrderId,
|
|
PurchaseProductId = purchaseOrderSku.ProductId,
|
|
PurchaseSkuId = purchaseOrderSku.SkuId,
|
|
ShopId = request.ShopId
|
|
};
|
|
insertOrderPurchaseSkuInfoList.Add(orderPurchaseSkuInfo);
|
|
}
|
|
#endregion
|
|
|
|
#region 补齐采购关系
|
|
foreach (var assOrderCostDetail in purchaseOrder.AssocationOrderCostDetailList)
|
|
{
|
|
var currentPurchaseSchemeSkuList = purchaseSchemeSkuList.Where(x => x.SkuId == assOrderCostDetail.SkuId).ToList();
|
|
if (currentPurchaseSchemeSkuList == null || currentPurchaseSchemeSkuList.Count() == 0)
|
|
throw new BusinessException($"采购单{purchaseOrder.PurchaseOrderId} 采购商Id {purchaserId} Sku{assOrderCostDetail.SkuId}缺少采购方案");
|
|
|
|
var dbOrderSku = dbOrderSkuList.FirstOrDefault(osku => osku.SkuId == assOrderCostDetail.SkuId);
|
|
foreach (var currentPurchaseSchemeSku in currentPurchaseSchemeSkuList)
|
|
{
|
|
var purchaseSkuFromApi = purchaseOrderSimpleInfo.ItemList.FirstOrDefault(x => x.SkuId == currentPurchaseSchemeSku.PurchaseSkuId);
|
|
if (purchaseSkuFromApi == null)
|
|
{
|
|
throw new BusinessException($"Sku{assOrderCostDetail.SkuId}使用了采购方案{currentPurchaseSchemeSku.SkuPurchaseSchemeId}下的采购Sku{currentPurchaseSchemeSku.PurchaseSkuId},而且在采购单{purchaseOrder.PurchaseOrderId}的商品明细中没有找到该采购sku");
|
|
}
|
|
var orderPurchaseRelationInfo = new OrderPurchaseRelationInfo()
|
|
{
|
|
Id = idGenerator.NewLong(),
|
|
BelongSkuId = assOrderCostDetail.SkuId,
|
|
CreateTime = DateTime.Now,
|
|
OrderId = assOrderCostDetail.OrderId,
|
|
PurchaseOrderId = purchaseOrder.PurchaseOrderId,
|
|
PurchaseSkuId = currentPurchaseSchemeSku.PurchaseSkuId,
|
|
PurchaseProductId = currentPurchaseSchemeSku.PurchaseProductId,
|
|
PurchaseSpecId = currentPurchaseSchemeSku.PurchaseSkuSpecId,
|
|
SchemeId = currentPurchaseSchemeSku.SkuPurchaseSchemeId,
|
|
Quantity = assOrderCostDetail.PurchaseQuantity,
|
|
SourceSkuId = dbOrderSku.BelongSkuId
|
|
};
|
|
insertOrderPurchaseRelationInfoList.Add(orderPurchaseRelationInfo);
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
#region 订单成本明细
|
|
foreach (var assOrderCostDetail in purchaseOrder.AssocationOrderCostDetailList)
|
|
{
|
|
|
|
var dbOrderSku = dbOrderSkuList.FirstOrDefault(osku => osku.SkuId == assOrderCostDetail.SkuId);
|
|
var purchaseFreight = purchaseOrder.PurchaseFreight * (1.0M * assOrderCostDetail.PurchaseQuantity / totalQuantity);
|
|
var orderCostDetail = new OrderCostDetail()
|
|
{
|
|
Id = idGenerator.NewLong(),
|
|
CreateTime = DateTime.Now,
|
|
DeductionQuantity = assOrderCostDetail.PurchaseQuantity,
|
|
IsEnabled = true,
|
|
OrderId = request.OrderId,
|
|
ProductId = dbOrderSku.ProductId,
|
|
SkuId = assOrderCostDetail.SkuId,
|
|
PurchaseOrderId = purchaseOrder.PurchaseOrderId
|
|
};
|
|
orderCostDetail.CalculationOrderCostDetailCostAndProfit(dbOrderSku.Price.Value * dbOrderSku.ItemTotal.Value,
|
|
dbOrderSku.BuyerPayFreight ?? 0M,
|
|
dbOrderSku.InPackAmount ?? 0M,
|
|
assOrderCostDetail.SkuAmount,
|
|
purchaseFreight,
|
|
0M,
|
|
0M);
|
|
insertOrderCostDetailList.Add(orderCostDetail);
|
|
}
|
|
#endregion
|
|
|
|
#region 采购单
|
|
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.关联外部单,
|
|
OrderState = Enums.PurchaseOrderState.待发货,
|
|
PurchaseOrderId = purchaseOrder.PurchaseOrderId,
|
|
PurchasePlatform = purchaseOrder.PurchasePlatform,
|
|
PurchaserId = purchaseOrder.PurchaserId,
|
|
PurchaserName = purchaseOrder.PurchaserName,
|
|
ShopId = request.ShopId
|
|
};
|
|
insertOrderPurchaseInfoList.Add(dbPurchaserOrder);
|
|
#endregion
|
|
}
|
|
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 dbOrderSku = dbOrderSkuList.FirstOrDefault(osku => osku.SkuId == assOrderCostDetail.SkuId);
|
|
|
|
var orderCostDetail = dbOrderCostDetailList.FirstOrDefault(ocd => ocd.SkuId == assOrderCostDetail.SkuId &&
|
|
ocd.PurchaseOrderId == purchaseOrder.PurchaseOrderId);
|
|
|
|
var purchaseFreight = purchaseOrder.PurchaseFreight * (1.0M * assOrderCostDetail.PurchaseQuantity / totalQuantity);
|
|
|
|
|
|
if (orderCostDetail == null)
|
|
{
|
|
orderCostDetail = new OrderCostDetail()
|
|
{
|
|
Id = idGenerator.NewLong(),
|
|
CreateTime = DateTime.Now,
|
|
DeductionQuantity = assOrderCostDetail.PurchaseQuantity,
|
|
IsEnabled = true,
|
|
OrderId = request.OrderId,
|
|
ProductId = dbOrderSku.ProductId,
|
|
SkuId = assOrderCostDetail.SkuId,
|
|
PurchaseOrderId = purchaseOrder.PurchaseOrderId
|
|
};
|
|
}
|
|
orderCostDetail.DeductionQuantity = assOrderCostDetail.PurchaseQuantity;
|
|
orderCostDetail.CalculationOrderCostDetailCostAndProfit(dbOrderSku.Price.Value * dbOrderSku.ItemTotal.Value,
|
|
dbOrderSku.BuyerPayFreight ?? 0M,
|
|
dbOrderSku.InPackAmount ?? 0M,
|
|
assOrderCostDetail.SkuAmount,
|
|
purchaseFreight,
|
|
orderCostDetail.OutPackAmount ?? 0M,
|
|
orderCostDetail.DeliveryExpressFreight ?? 0M);
|
|
|
|
if (orderCostDetail == null)
|
|
insertOrderCostDetailList.Add(orderCostDetail);
|
|
else
|
|
updateOrderCostDetailList.Add(fsql.Update<OrderCostDetail>().SetSource(orderCostDetail));
|
|
}
|
|
}
|
|
}
|
|
|
|
if (dbOrderCost == null)
|
|
{
|
|
dbOrderCost = new OrderCost()
|
|
{
|
|
OrderId = request.OrderId,
|
|
IsManualEdited = false,
|
|
CreateTime = DateTime.Now,
|
|
};
|
|
}
|
|
|
|
var totalPurchaseProductAmount = request.AssociationPurchaseOrderList.Sum(x => x.PurchaseAmount);
|
|
var totalPurchaseFreight = request.AssociationPurchaseOrderList.Sum(x => x.PurchaseFreight);
|
|
var totalOutPackAmount = dbOrderCostDetailList.Sum(ocd => ocd.OutPackAmount);
|
|
dbOrderCost.CalculationOrderCostCostAndProfit(dbOrder.OrderTotalPrice.Value,
|
|
totalPurchaseProductAmount,
|
|
totalPurchaseFreight,
|
|
totalOutPackAmount ?? 0M,
|
|
0M);
|
|
|
|
if (dbOrderCost == null)
|
|
insertOrderCost = fsql.Insert(dbOrderCost);
|
|
else
|
|
updateOrderCost = fsql.Update<OrderCost>().SetSource(dbOrderCost);
|
|
|
|
#region 订单状态
|
|
dbOrder.CalculationOrderState(fsql, dbOrderSkuList, dbvalidPurchaseOrderList.Union(insertOrderPurchaseInfoList).ToList());
|
|
#endregion
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
if (insertOrderPurchaseInfoList.Count() > 0)
|
|
fsql.Insert(insertOrderPurchaseInfoList).ExecuteAffrows();
|
|
if (insertOrderPurchaseSkuInfoList.Count() > 0)
|
|
fsql.Insert(insertOrderPurchaseSkuInfoList).ExecuteAffrows();
|
|
if (insertOrderPurchaseRelationInfoList.Count() > 0)
|
|
fsql.Insert(insertOrderPurchaseRelationInfoList).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();
|
|
|
|
fsql.Update<Order>(dbOrder.Id).Set(o => o.OrderState, dbOrder.OrderState)
|
|
.Set(o => o.IsPurchased, true)
|
|
.ExecuteAffrows();
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置历史采购单
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
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)
|
|
.Set(opi => opi.OrderState, Enums.PurchaseOrderState.已取消)
|
|
.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();
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 手动发货
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
public void ManualDelivery(ManualDeliveryRequest request)
|
|
{
|
|
#region 准备数据库更新对象
|
|
List<PurchaseExpressOrder> insertPurchaseExpressOrderList = new List<PurchaseExpressOrder>();
|
|
IList<IUpdate<OrderPurchaseSkuInfo>> updateOrderPurchaseSkuList = new List<IUpdate<OrderPurchaseSkuInfo>>();
|
|
IUpdate<OrderPurchaseInfo> updateOrderPurchase = null;
|
|
IUpdate<Order> updateOrder = null;
|
|
#endregion
|
|
|
|
#region 查询该笔采购单的快递单信息
|
|
var purchaseExpressOrderList = fsql.Select<PurchaseExpressOrder>().Where(x => x.PurchaseOrderId == request.PurchaseOrderId).ToList();
|
|
bool isExists = purchaseExpressOrderList.Any(exo => exo.WaybillNo == request.WaybillNo);
|
|
#endregion
|
|
|
|
#region 查询当前采购单的订单信息
|
|
var dbOrder = fsql.Select<OrderPurchaseInfo, Order>().InnerJoin((opi, o) => opi.OrderId == o.Id)
|
|
.Where((opi, o) => opi.PurchaseOrderId == request.PurchaseOrderId && opi.IsEnabled == true)
|
|
.ToOne((opi, o) => new Order
|
|
{
|
|
Id = o.Id,
|
|
OrderState = o.OrderState,
|
|
ShopId = o.ShopId
|
|
});
|
|
if (dbOrder == null)
|
|
throw new BusinessException("未查询到采购单的订单信息");
|
|
#endregion
|
|
|
|
#region 查询订单的全部采购单信息
|
|
var orderPurchaseInfoList = fsql.Select<OrderPurchaseInfo>().Where(opi => opi.OrderId == dbOrder.Id && opi.IsEnabled == true).ToList();
|
|
var orderPurchaseInfo = orderPurchaseInfoList.FirstOrDefault(opi => opi.PurchaseOrderId == request.PurchaseOrderId);
|
|
if (orderPurchaseInfo == null)
|
|
throw new BusinessException("未查询到采购单信息");
|
|
#endregion
|
|
|
|
#region 查询采购关联信息
|
|
var orderPurchaseRelationList = fsql.Select<OrderPurchaseRelationInfo>().Where(opri => opri.PurchaseOrderId == request.PurchaseOrderId &&
|
|
opri.BelongSkuId == request.SkuId).ToList();
|
|
if (orderPurchaseRelationList.Count() == 0)
|
|
throw new BusinessException("未查询到采购单的关联信息");
|
|
#endregion
|
|
|
|
#region 查询该笔采购单的采购sku信息并更新快递单号
|
|
var orderPurchaseSkuList = fsql.Select<OrderPurchaseSkuInfo>().Where(x => x.PurchaseOrderId == request.PurchaseOrderId).ToList();
|
|
if (orderPurchaseSkuList.Count() == 0)
|
|
throw new BusinessException("未查询到采购单sku信息");
|
|
|
|
foreach (var relation in orderPurchaseRelationList)
|
|
{
|
|
var orderPurchaseSku = orderPurchaseSkuList.FirstOrDefault(posku => posku.PurchaseSkuId == relation.PurchaseSkuId);
|
|
if (orderPurchaseSku == null)
|
|
throw new BusinessException("未查询到采购单sku信息");
|
|
if (orderPurchaseSku.WaybillNo != request.WaybillNo)
|
|
{
|
|
orderPurchaseSku.WaybillNo = request.WaybillNo;
|
|
updateOrderPurchaseSkuList.Add(fsql.Update<OrderPurchaseSkuInfo>(orderPurchaseSku.Id).Set(ps => ps.WaybillNo, request.WaybillNo));
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 订阅快递100
|
|
if (!isExists)
|
|
kuaiDi100Manager.SubscribeKuaiDi100(request.WaybillNo, string.Empty, request.ExpressId, "http://bbwyb.qiyue666.com/api/purchaseorder/kuaidi100publish");
|
|
#endregion
|
|
|
|
#region 创建快递单
|
|
if (!isExists)
|
|
{
|
|
var purchaseExpressOrder = new PurchaseExpressOrder()
|
|
{
|
|
OrderId = dbOrder.Id,
|
|
CreateTime = DateTime.Now,
|
|
PurchaseOrderId = request.PurchaseOrderId,
|
|
ShopId = dbOrder.ShopId ?? 0,
|
|
TargetExpressId = request.ExpressId,
|
|
TargetExpressName = request.ExpressName,
|
|
WaybillNo = request.WaybillNo,
|
|
ExpressState = kuaiDi100Manager.GetExpressState(1) //快递100发货状态值 默认揽收
|
|
};
|
|
insertPurchaseExpressOrderList.Add(purchaseExpressOrder);
|
|
}
|
|
#endregion
|
|
|
|
#region 计算采购单状态
|
|
orderPurchaseInfo.CalculationOrderState(fsql, orderPurchaseSkuList, purchaseExpressOrderList.Union(insertPurchaseExpressOrderList).ToList());
|
|
updateOrderPurchase = fsql.Update<OrderPurchaseInfo>(orderPurchaseInfo.Id)
|
|
.Set(opi => opi.OrderState, orderPurchaseInfo.OrderState);
|
|
#endregion
|
|
|
|
#region 计算订单状态
|
|
dbOrder.CalculationOrderState(fsql, null, orderPurchaseInfoList);
|
|
updateOrder = fsql.Update<Order>(dbOrder.Id)
|
|
.Set(o => o.OrderState, dbOrder.OrderState);
|
|
#endregion
|
|
|
|
#region 通知C端订单状态
|
|
|
|
#endregion
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
if (insertPurchaseExpressOrderList.Count() > 0)
|
|
fsql.Insert(insertPurchaseExpressOrderList).ExecuteAffrows();
|
|
if (updateOrderPurchaseSkuList.Count() > 0)
|
|
{
|
|
foreach (var update in updateOrderPurchaseSkuList)
|
|
update.ExecuteAffrows();
|
|
}
|
|
updateOrderPurchase?.ExecuteAffrows();
|
|
updateOrder?.ExecuteAffrows();
|
|
});
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 签收采购单
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
public void SignPurchaseOrder(SignPurchaseOrderRequest request)
|
|
{
|
|
var dbOrder = fsql.Select<Order>(request.OrderId).ToOne();
|
|
if (dbOrder == null)
|
|
throw new BusinessException("无效订单号");
|
|
if (dbOrder.OrderState == Enums.OrderState.已取消)
|
|
throw new BusinessException("订单已取消");
|
|
if (dbOrder.OrderState == Enums.OrderState.已完成)
|
|
throw new BusinessException("订单已完成无需签收");
|
|
|
|
var dbOrderPurchaseInfoList = fsql.Select<OrderPurchaseInfo>().Where(opi => opi.OrderId == request.OrderId && opi.IsEnabled == true).ToList();
|
|
|
|
var dbOrderPurchaseInfo = dbOrderPurchaseInfoList.FirstOrDefault(opi => opi.PurchaseOrderId == request.PurchaseOrderId);
|
|
if (dbOrderPurchaseInfo == null)
|
|
throw new BusinessException("无效采购单号");
|
|
if (!dbOrderPurchaseInfo.IsEnabled)
|
|
throw new BusinessException("采购单已失效");
|
|
if (dbOrderPurchaseInfo.OrderState == Enums.PurchaseOrderState.已签收 ||
|
|
dbOrderPurchaseInfo.IsSign == true)
|
|
throw new BusinessException("采购单已签收");
|
|
|
|
dbOrderPurchaseInfo.IsSign = true;
|
|
dbOrderPurchaseInfo.OrderState = Enums.PurchaseOrderState.已签收;
|
|
|
|
dbOrder.CalculationOrderState(fsql, null, dbOrderPurchaseInfoList);
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
fsql.Update<OrderPurchaseInfo>().SetSource(dbOrderPurchaseInfo).ExecuteAffrows();
|
|
fsql.Update<Order>(dbOrder.Id)
|
|
.Set(o => o.OrderState, dbOrder.OrderState)
|
|
.Set(o => o.IsWaitPack, 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;
|
|
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(() => DeliveryCallbackFrom1688(purchaseOrderId), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 1688发货回调
|
|
/// </summary>
|
|
/// <param name="purchaseOrderId">采购单</param>
|
|
private void DeliveryCallbackFrom1688(string purchaseOrderId)
|
|
{
|
|
string orderId = string.Empty;
|
|
long? shopId = null;
|
|
string currentProgress = string.Empty;
|
|
string wayBillNoResponseInfo = string.Empty;
|
|
try
|
|
{
|
|
List<PurchaseExpressOrder> insertPurchaseExpressOrderList = new List<PurchaseExpressOrder>();
|
|
IList<IUpdate<OrderPurchaseSkuInfo>> updateOrderPurchaseSkuList = new List<IUpdate<OrderPurchaseSkuInfo>>();
|
|
IUpdate<OrderPurchaseInfo> updateOrderPurchase = null;
|
|
IUpdate<Order> updateOrder = null;
|
|
|
|
#region 查询当前采购单的订单信息
|
|
currentProgress = "查询当前采购单的订单信息";
|
|
var dbOrder = fsql.Select<OrderPurchaseInfo, Order>().InnerJoin((opi, o) => opi.OrderId == o.Id)
|
|
.Where((opi, o) => opi.PurchaseOrderId == purchaseOrderId && opi.IsEnabled == true)
|
|
.ToOne((opi, o) => new Order
|
|
{
|
|
Id = o.Id,
|
|
OrderState = o.OrderState,
|
|
ShopId = o.ShopId
|
|
});
|
|
if (dbOrder == null)
|
|
throw new Exception("未查询到采购单的订单信息");
|
|
orderId = dbOrder.Id;
|
|
shopId = dbOrder.ShopId;
|
|
#endregion
|
|
|
|
#region 查询订单的全部采购单信息
|
|
currentProgress = "查询订单的全部采购单信息";
|
|
var orderPurchaseInfoList = fsql.Select<OrderPurchaseInfo>().Where(opi => opi.OrderId == orderId && opi.IsEnabled == true).ToList();
|
|
var orderPurchaseInfo = orderPurchaseInfoList.FirstOrDefault(opi => opi.PurchaseOrderId == purchaseOrderId);
|
|
#endregion
|
|
|
|
#region 查询该笔采购单的sku信息
|
|
currentProgress = "查询该笔采购单的sku信息";
|
|
var orderPurchaseSkuList = fsql.Select<OrderPurchaseSkuInfo>().Where(x => x.PurchaseOrderId == purchaseOrderId).ToList();
|
|
#endregion
|
|
|
|
#region 查询该笔采购单的快递单信息
|
|
currentProgress = "查询该笔采购单的快递单信息";
|
|
var purchaseExpressOrderList = fsql.Select<PurchaseExpressOrder>().Where(x => x.PurchaseOrderId == purchaseOrderId).ToList();
|
|
#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 = "获取采购单的物流信息";
|
|
var client = ppPlatformClientFactory.GetClient(AdapterEnums.PlatformType.阿里巴巴);
|
|
var ppQueryOrderLogisticsRequest = new PP_QueryOrderLogisticsRequest()
|
|
{
|
|
AppKey = purchaseAccount.AppKey,
|
|
AppSecret = purchaseAccount.AppSecret,
|
|
AppToken = purchaseAccount.AppToken,
|
|
OrderId = purchaseOrderId,
|
|
Platform = AdapterEnums.PlatformType.阿里巴巴
|
|
};
|
|
var logisticsList = client.QueryOrderLogistics(ppQueryOrderLogisticsRequest);
|
|
wayBillNoResponseInfo = JsonConvert.SerializeObject(new { purchaseOrderId, logisticsList });
|
|
#endregion
|
|
|
|
|
|
#region 找出新发货的快递单
|
|
foreach (var logisticsInfo in logisticsList)
|
|
{
|
|
if (purchaseExpressOrderList.Any(po => po.WaybillNo == logisticsInfo.WayBillNo))
|
|
continue;
|
|
|
|
#region 订阅快递100
|
|
currentProgress = "订阅快递100";
|
|
kuaiDi100Manager.SubscribeKuaiDi100(logisticsInfo.WayBillNo, logisticsInfo.ExpressName, string.Empty, "http://bbwyb.qiyue666.com/api/purchaseorder/kuaidi100publish");
|
|
#endregion
|
|
|
|
#region 创建快递单
|
|
var purchaseExpressOrder = new PurchaseExpressOrder()
|
|
{
|
|
OrderId = orderId,
|
|
CreateTime = DateTime.Now,
|
|
PurchaseOrderId = purchaseOrderId,
|
|
ShopId = shopId ?? 0,
|
|
SourceExpressId = logisticsInfo.ExpressId,
|
|
SourceExpressName = logisticsInfo.ExpressName,
|
|
WaybillNo = logisticsInfo.WayBillNo,
|
|
ExpressState = kuaiDi100Manager.GetExpressState(1) //快递100发货状态值 默认揽收
|
|
};
|
|
insertPurchaseExpressOrderList.Add(purchaseExpressOrder);
|
|
#endregion
|
|
|
|
#region 更新采购sku的快递单号
|
|
foreach (var orderEntryId in logisticsInfo.OrderEntryIds)
|
|
{
|
|
var purchaseSku = orderPurchaseSkuList.FirstOrDefault(x => x.Id == orderEntryId);
|
|
if (purchaseSku == null)
|
|
continue;
|
|
purchaseSku.WaybillNo = logisticsInfo.WayBillNo;
|
|
updateOrderPurchaseSkuList.Add(fsql.Update<OrderPurchaseSkuInfo>(orderEntryId).Set(ps => ps.WaybillNo, logisticsInfo.WayBillNo));
|
|
}
|
|
#endregion
|
|
}
|
|
#endregion
|
|
|
|
#region 计算采购单状态
|
|
orderPurchaseInfo.CalculationOrderState(fsql, orderPurchaseSkuList, purchaseExpressOrderList.Union(insertPurchaseExpressOrderList).ToList());
|
|
updateOrderPurchase = fsql.Update<OrderPurchaseInfo>(orderPurchaseInfo.Id)
|
|
.Set(opi => opi.OrderState, orderPurchaseInfo.OrderState);
|
|
#endregion
|
|
|
|
#region 计算订单状态
|
|
dbOrder.CalculationOrderState(fsql, null, orderPurchaseInfoList);
|
|
updateOrder = fsql.Update<Order>(dbOrder.Id)
|
|
.Set(o => o.OrderState, dbOrder.OrderState);
|
|
#endregion
|
|
|
|
#region 通知C端订单状态
|
|
|
|
#endregion
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
if (insertPurchaseExpressOrderList.Count() > 0)
|
|
fsql.Insert(insertPurchaseExpressOrderList).ExecuteAffrows();
|
|
if (updateOrderPurchaseSkuList.Count() > 0)
|
|
{
|
|
foreach (var update in updateOrderPurchaseSkuList)
|
|
update.ExecuteAffrows();
|
|
}
|
|
updateOrderPurchase?.ExecuteAffrows();
|
|
updateOrder?.ExecuteAffrows();
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
nLogManager.Default().Error(ex, $"DeliveryCallback 回调平台1688,订单号{orderId},采购单号{purchaseOrderId},执行进度[{currentProgress}],采购单物流信息:{wayBillNoResponseInfo}");
|
|
}
|
|
}
|
|
|
|
/// <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 && o.IsEnabled == true).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
|
|
//}
|
|
//#endregion
|
|
|
|
|
|
#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
|
|
|
|
|
|
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>>();
|
|
IList<IUpdate<OrderPurchaseInfo>> updateOrderPurchaseInfoList = new List<IUpdate<OrderPurchaseInfo>>();
|
|
|
|
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 dbOrderPurchaseInfoList = fsql.Select<OrderPurchaseInfo>().Where(opi => purchaseOrderIds.Contains(opi.PurchaseOrderId) && opi.IsEnabled == true).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();
|
|
if (isSignAll)
|
|
{
|
|
var dbOrderPurchaseInfo = dbOrderPurchaseInfoList.FirstOrDefault(x => x.PurchaseOrderId == group.Key && x.IsEnabled == true);
|
|
if (dbOrderPurchaseInfo != null)
|
|
{
|
|
dbOrderPurchaseInfo.OrderState = Enums.PurchaseOrderState.已签收;
|
|
updateOrderPurchaseInfoList.Add(fsql.Update<OrderPurchaseInfo>().SetSource(dbOrderPurchaseInfo));
|
|
}
|
|
}
|
|
|
|
#region 通知齐库
|
|
var relationList = dbOrderPurchaseRelationInfoList.Where(x => x.PurchaseOrderId == group.Key).ToList();
|
|
foreach (var relation in relationList)
|
|
{
|
|
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
|
|
}
|
|
}
|
|
|
|
if (updateOrderPurchaseSkuInfoList.Count() > 0 || updateOrderPurchaseInfoList.Count() > 0)
|
|
{
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var update in updateOrderPurchaseSkuInfoList)
|
|
update.ExecuteAffrows();
|
|
foreach (var update in updateOrderPurchaseInfoList)
|
|
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}-未查询到采购单关联明细,手动关联的采购单不支持改价");
|
|
|
|
var dbOrderSkus = fsql.Select<OrderSku>().Where(osku => osku.OrderId == orderPurchaseInfo.OrderId).ToList();
|
|
|
|
List<IUpdate<OrderCostDetail>> updateOrderCostDetailList = new List<IUpdate<OrderCostDetail>>();
|
|
IUpdate<OrderCost> updateOrderCost = null;
|
|
|
|
var client = ppPlatformClientFactory.GetClient(AdapterEnums.PlatformType.阿里巴巴);
|
|
|
|
var totalPurchaseProductAmount = 0M;
|
|
var totalPurchaseFreight = 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
|
|
});
|
|
|
|
totalPurchaseProductAmount += purchaseOrderSimpleInfo.ProductAmount;
|
|
totalPurchaseFreight += purchaseOrderSimpleInfo.FreightAmount;
|
|
|
|
var purchaseSkuTotalQuantity = purchaseOrderSimpleInfo.ItemList.Sum(x => x.Quantity);
|
|
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 currentOrderSkuCargoParamList = belongSkuGroup.ToList(); //找当前skuId的采购skuId
|
|
var currentOrderSkuProductAmount = 0M; //采购成本
|
|
var currentSkuTotalPurchaseQuantity = currentOrderSkuCargoParamList.Sum(x => x.Quantity); //当前skuId的采购数量总和
|
|
var currentPurchaseFreight = purchaseOrderSimpleInfo.FreightAmount *
|
|
(1.0M * currentSkuTotalPurchaseQuantity / purchaseSkuTotalQuantity); //采购运费
|
|
|
|
foreach (var currentOrderSkuCargo in currentOrderSkuCargoParamList)
|
|
{
|
|
var currentPurchaseSkuProductAmount = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.PurchaseSkuId)
|
|
.Sum(p => p.ProductAmount);
|
|
var currentPurchaseSkuTotalQuantity = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.PurchaseSkuId)
|
|
.Sum(p => p.Quantity);
|
|
currentOrderSkuProductAmount += currentPurchaseSkuProductAmount * (1.0M * currentOrderSkuCargo.Quantity.Value / currentPurchaseSkuTotalQuantity);
|
|
}
|
|
|
|
var dbOrderSkuDetail = dbOrderCostDetails.FirstOrDefault(ocd => ocd.SkuId == belongSkuId);
|
|
var orderSku = dbOrderSkus.FirstOrDefault(osku => osku.SkuId == belongSkuId);
|
|
//dbOrderSkuDetail.SkuAmount = currentOrderSkuProductAmount;
|
|
//dbOrderSkuDetail.PurchaseFreight = currentOrderSkuFreightAmount;
|
|
//dbOrderSkuDetail.TotalCost = currentOrderSkuProductAmount + currentOrderSkuFreightAmount;
|
|
|
|
dbOrderSkuDetail.CalculationOrderCostDetailCostAndProfit(orderSku.Price.Value * orderSku.ItemTotal.Value,
|
|
orderSku.BuyerPayFreight ?? 0M,
|
|
orderSku.InPackAmount ?? 0M,
|
|
currentOrderSkuProductAmount,
|
|
currentPurchaseFreight ?? 0M,
|
|
dbOrderSkuDetail.OutPackAmount ?? 0M,
|
|
dbOrderSkuDetail.DeliveryExpressFreight ?? 0M);
|
|
updateOrderCostDetailList.Add(fsql.Update<OrderCostDetail>().SetSource(dbOrderSkuDetail));
|
|
}
|
|
|
|
//dbOrderCost.TotalCost = dbOrderCost.PurchaseAmount = totalPurchaseAmount;
|
|
//dbOrderCost.Profit = dbOrder.OrderTotalPrice -
|
|
// dbOrderCost.PurchaseAmount -
|
|
// dbOrderCost.DeliveryExpressFreight;
|
|
|
|
|
|
dbOrderCost.CalculationOrderCostCostAndProfit(dbOrder.OrderTotalPrice ?? 0M,
|
|
totalPurchaseProductAmount,
|
|
totalPurchaseFreight,
|
|
dbOrderCostDetails.Sum(ocd => ocd.OutPackAmount ?? 0M),
|
|
dbOrderCostDetails.Sum(ocd => ocd.DeliveryExpressFreight ?? 0M));
|
|
updateOrderCost = fsql.Update<OrderCost>().SetSource(dbOrderCost);
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var update in updateOrderCostDetailList)
|
|
update.ExecuteAffrows();
|
|
updateOrderCost?.ExecuteAffrows();
|
|
});
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
nLogManager.Default().Error(ex, $"OrderPriceModificationCallback 回调平台{callbackPlatform},采购单号{purchaseOrderId},执行进度[{currentProgress}]");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|