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.
2154 lines
123 KiB
2154 lines
123 KiB
using BBWYB.Common.Http;
|
|
using BBWYB.Common.Log;
|
|
using BBWYB.Common.Models;
|
|
using BBWYB.Server.Business.Extensions;
|
|
using BBWYB.Server.Model;
|
|
using BBWYB.Server.Model.Db;
|
|
using BBWYB.Server.Model.Db.Mds;
|
|
using BBWYB.Server.Model.Db.MDS;
|
|
using BBWYB.Server.Model.Dto;
|
|
using FreeSql;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using SDKAdapter;
|
|
using SDKAdapter.PurchasePlatform.Client;
|
|
using SDKAdapter.PurchasePlatform.Models;
|
|
using System.Text;
|
|
using Yitter.IdGenerator;
|
|
|
|
namespace BBWYB.Server.Business
|
|
{
|
|
public class PurchaseOrderBusiness : BaseBusiness, IDenpendency
|
|
{
|
|
private Lazy<PP_PlatformClientFactory> pplatformClientFactoryLazy;
|
|
private Lazy<TaskSchedulerManager> taskSchedulerManagerLazy;
|
|
private Lazy<FreeSqlMultiDBManager> fsqlManagerLazy;
|
|
private Lazy<ExpressCompanyNameConverter> expressCompanyNameConverterLazy;
|
|
private Lazy<KuaiDi100Manager> kuaiDi100ManagerLazy;
|
|
private Lazy<DingDingBusiness> dingDingBusinessLazy;
|
|
private Lazy<QiKuManager> qiKuManagerLazy;
|
|
private Lazy<RestApiService> restApiServiceLazy;
|
|
|
|
private PP_PlatformClientFactory ppPlatformClientFactory => pplatformClientFactoryLazy.Value;
|
|
private TaskSchedulerManager taskSchedulerManager => taskSchedulerManagerLazy.Value;
|
|
private FreeSqlMultiDBManager fsqlManager => fsqlManagerLazy.Value;
|
|
private ExpressCompanyNameConverter expressCompanyNameConverter => expressCompanyNameConverterLazy.Value;
|
|
private KuaiDi100Manager kuaiDi100Manager => kuaiDi100ManagerLazy.Value;
|
|
private DingDingBusiness dingDingBusiness => dingDingBusinessLazy.Value;
|
|
private QiKuManager qiKuManager => qiKuManagerLazy.Value;
|
|
private RestApiService restApiService => restApiServiceLazy.Value;
|
|
|
|
private IList<Enums.OrderState> cantPurchaseOrderStateList;
|
|
|
|
public PurchaseOrderBusiness(IFreeSql fsql,
|
|
NLogManager nLogManager,
|
|
IIdGenerator idGenerator,
|
|
IServiceProvider serviceProvider) : base(fsql, nLogManager, idGenerator)
|
|
{
|
|
pplatformClientFactoryLazy = new Lazy<PP_PlatformClientFactory>(() => serviceProvider.GetService<PP_PlatformClientFactory>());
|
|
taskSchedulerManagerLazy = new Lazy<TaskSchedulerManager>(() => serviceProvider.GetService<TaskSchedulerManager>());
|
|
fsqlManagerLazy = new Lazy<FreeSqlMultiDBManager>(() => serviceProvider.GetService<FreeSqlMultiDBManager>());
|
|
expressCompanyNameConverterLazy = new Lazy<ExpressCompanyNameConverter>(() => serviceProvider.GetService<ExpressCompanyNameConverter>());
|
|
kuaiDi100ManagerLazy = new Lazy<KuaiDi100Manager>(() => serviceProvider.GetService<KuaiDi100Manager>());
|
|
dingDingBusinessLazy = new Lazy<DingDingBusiness>(() => serviceProvider.GetService<DingDingBusiness>());
|
|
qiKuManagerLazy = new Lazy<QiKuManager>(() => serviceProvider.GetService<QiKuManager>());
|
|
restApiServiceLazy = new Lazy<RestApiService>(() => serviceProvider.GetService<RestApiService>());
|
|
cantPurchaseOrderStateList = new List<Enums.OrderState>()
|
|
{
|
|
Enums.OrderState.已取消,
|
|
Enums.OrderState.已完成,
|
|
Enums.OrderState.待付款,
|
|
Enums.OrderState.待完结
|
|
};
|
|
}
|
|
|
|
/// <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("缺少报价参数");
|
|
|
|
#region 验证同一个批次中,一个订单sku不能同时拥有多个采购方案
|
|
IDictionary<string, long> schemeValidationDictionary = new Dictionary<string, long>();
|
|
foreach (var purchaseGroup in request.CargoParamGroupList)
|
|
{
|
|
foreach (var cargo in purchaseGroup.CargoParamList)
|
|
{
|
|
if (!schemeValidationDictionary.ContainsKey(cargo.BelongSkuId))
|
|
schemeValidationDictionary.TryAdd(cargo.BelongSkuId, cargo.SchemeId);
|
|
|
|
if (schemeValidationDictionary.TryGetValue(cargo.BelongSkuId, out long schemeId))
|
|
{
|
|
if (cargo.SchemeId != schemeId)
|
|
{
|
|
throw new BusinessException($"订单sku{cargo.BelongSkuId}只允许使用一个采购方案进行报价");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
schemeValidationDictionary.Clear();
|
|
#endregion
|
|
|
|
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("缺少下单商品参数");
|
|
|
|
#region 验证同一个批次中,一个订单sku不能同时拥有多个采购方案
|
|
IDictionary<string, long> schemeValidationDictionary = new Dictionary<string, long>();
|
|
foreach (var purchaseGroup in request.CargoParamGroupList)
|
|
{
|
|
foreach (var cargo in purchaseGroup.CargoParamList)
|
|
{
|
|
if (!schemeValidationDictionary.ContainsKey(cargo.BelongSkuId))
|
|
schemeValidationDictionary.TryAdd(cargo.BelongSkuId, cargo.SchemeId);
|
|
|
|
if (schemeValidationDictionary.TryGetValue(cargo.BelongSkuId, out long schemeId))
|
|
{
|
|
if (cargo.SchemeId != schemeId)
|
|
{
|
|
throw new BusinessException($"订单sku{cargo.BelongSkuId}只允许使用一个采购方案进行下单");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
schemeValidationDictionary.Clear();
|
|
#endregion
|
|
|
|
|
|
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
|
|
|
|
#region 通知C端状态
|
|
Task.Factory.StartNew(() => SendPurchaseOrderStateToC(dbOrder.Id, dbOrder.OrderState.Value), CancellationToken.None, TaskCreationOptions.None, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
|
|
#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)
|
|
{
|
|
nLogManager.Default().Info($"AssociatePurchaseOrder {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.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 数据验证
|
|
var validation_groups_pid = request.AssociationPurchaseOrderList.GroupBy(po => po.PurchaseOrderId);
|
|
if (validation_groups_pid.Any(x => x.Count() > 1))
|
|
throw new BusinessException("不允许包含重复的采购单");
|
|
|
|
foreach (var apo in request.AssociationPurchaseOrderList)
|
|
{
|
|
if (Math.Abs(apo.PurchaseAmount - apo.AssocationOrderCostDetailList.Sum(aocd => aocd.SkuAmount)) > 1)
|
|
throw new BusinessException($"采购单{apo.PurchaseOrderId}的采购货款与明细采购货款总和误差不能超过1");
|
|
}
|
|
#endregion
|
|
|
|
#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,
|
|
ShopId = request.ShopId
|
|
};
|
|
}
|
|
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));
|
|
}
|
|
}
|
|
}
|
|
|
|
var dbOrderCostIsEmpty = dbOrderCost == null;
|
|
if (dbOrderCostIsEmpty)
|
|
{
|
|
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 (dbOrderCostIsEmpty)
|
|
insertOrderCost = fsql.Insert(dbOrderCost);
|
|
else
|
|
updateOrderCost = fsql.Update<OrderCost>().SetSource(dbOrderCost);
|
|
|
|
#region 订单状态
|
|
dbOrder.CalculationOrderState(fsql, dbOrderSkuList, dbvalidPurchaseOrderList.Union(insertOrderPurchaseInfoList).ToList());
|
|
#endregion
|
|
|
|
#region 通知C端状态
|
|
Task.Factory.StartNew(() => SendPurchaseOrderStateToC(dbOrder.Id, dbOrder.OrderState.Value), CancellationToken.None, TaskCreationOptions.None, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
|
|
#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>
|
|
/// 预览关联订单信息(不支持关联预览的平台会返回null)
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
public AssociationPurchaseOrderResponse PreviewAssocationPurchaseOrder(AssocationPurchaseOrderPreviewRequest request)
|
|
{
|
|
if (request.PurchasePlatform != Enums.Platform.阿里巴巴)
|
|
return null;
|
|
|
|
var client = ppPlatformClientFactory.GetClient((AdapterEnums.PlatformType)request.PurchasePlatform);
|
|
var purchaseOrderSimpleInfo = client.QueryOrderDetail(new PP_QueryOrderDetailRequest()
|
|
{
|
|
AppKey = request.PurchaseAccount.AppKey,
|
|
AppSecret = request.PurchaseAccount.AppSecret,
|
|
AppToken = request.PurchaseAccount.AppToken,
|
|
OrderId = request.PurchaseOrderId
|
|
});
|
|
|
|
#region 处理采购商Id
|
|
var purchaserId = purchaseOrderSimpleInfo.PurchaserId;
|
|
if (string.IsNullOrEmpty(purchaserId))
|
|
throw new BusinessException($"采购单{request.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($"采购单{request.PurchaseOrderId}缺少有效采购商");
|
|
purchaserId = dbPurchaser.Id;
|
|
#endregion
|
|
|
|
#region 查询订单Sku
|
|
var dbOrderSkuList = fsql.Select<OrderSku>().Where(osku => osku.OrderId == request.OrderId).ToList();
|
|
if (dbOrderSkuList.Count() == 0)
|
|
throw new BusinessException("缺少订单sku信息");
|
|
#endregion
|
|
|
|
#region 查询订单sku所有采购方案
|
|
var skuIds = dbOrderSkuList.Select(osku => osku.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($"采购单{request.PurchaseOrderId} 采购商Id{purchaserId} 未匹配到采购方案");
|
|
#endregion
|
|
|
|
#region 匹配采购单所使用到的采购方案
|
|
var assocationOrderCostDetailList = new List<AssocationOrderCostDetailResponse>();
|
|
var purchaseSchemeGroups = purchaseSchemeSkuList.GroupBy(s => s.SkuPurchaseSchemeId);
|
|
var totalPurchaseCount = purchaseOrderSimpleInfo.ItemList.Sum(x => x.Quantity);
|
|
foreach (var schemeGroup in purchaseSchemeGroups)
|
|
{
|
|
var schemePurchaseSkuList = schemeGroup.ToList();
|
|
if (schemePurchaseSkuList.Any(psku => purchaseOrderSimpleInfo.ItemList.Count(x => x.SkuId == psku.PurchaseSkuId) == 0))
|
|
continue;
|
|
|
|
var orderPurchaseSkuOfCurrentSchemeList = purchaseOrderSimpleInfo.ItemList.Where(psku => schemePurchaseSkuList.Count(x => x.PurchaseSkuId == psku.SkuId) > 0);
|
|
|
|
|
|
var skuId = schemePurchaseSkuList.FirstOrDefault().SkuId;
|
|
var orderSku = dbOrderSkuList.FirstOrDefault(osku => osku.SkuId == skuId);
|
|
var assocationOrderCostDetail = new AssocationOrderCostDetailResponse()
|
|
{
|
|
Logo = orderSku.Logo,
|
|
Title = orderSku.Title,
|
|
SkuId = skuId,
|
|
OrderId = orderSku.OrderId,
|
|
SkuAmount = orderPurchaseSkuOfCurrentSchemeList.Sum(psku => psku.ProductAmount),
|
|
PurchaseQuantity = orderSku.ItemTotal.Value,
|
|
PurchaseFreight = purchaseOrderSimpleInfo.FreightAmount * (orderPurchaseSkuOfCurrentSchemeList.Sum(x => x.Quantity) / totalPurchaseCount)
|
|
};
|
|
assocationOrderCostDetail.PurchasePrice = assocationOrderCostDetail.SkuAmount / assocationOrderCostDetail.PurchaseFreight;
|
|
assocationOrderCostDetailList.Add(assocationOrderCostDetail);
|
|
}
|
|
#endregion
|
|
|
|
if (assocationOrderCostDetailList.Count() == 0)
|
|
throw new BusinessException("没有匹配到合适的采购方案");
|
|
|
|
return new AssociationPurchaseOrderResponse(assocationOrderCostDetailList)
|
|
{
|
|
PurchaserId = dbPurchaser.Id,
|
|
PurchaserName = dbPurchaser.Name,
|
|
PurchaseAccountId = request.PurchaseAccount.Id,
|
|
PurchaseAccountName = request.PurchaseAccount.AccountName,
|
|
PurchaseAmount = purchaseOrderSimpleInfo.ProductAmount,
|
|
PurchaseFreight = purchaseOrderSimpleInfo.FreightAmount,
|
|
PurchaseMethod = Enums.PurchaseMethod.关联外部单,
|
|
PurchaseOrderId = request.PurchaseOrderId,
|
|
PurchasePlatform = request.PurchasePlatform,
|
|
IsEnabled = true
|
|
};
|
|
}
|
|
|
|
/// <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.Delete<OrderPurchaseSkuInfo>().Where(opsi => opsi.PurchaseOrderId == request.PurchaseOrderId).ExecuteAffrows();
|
|
fsql.Delete<OrderPurchaseRelationInfo>().Where(opri => opri.PurchaseOrderId == request.PurchaseOrderId).ExecuteAffrows();
|
|
fsql.Delete<PurchaseExpressOrder>().Where(peo => peo.PurchaseOrderId == request.PurchaseOrderId).ExecuteAffrows();
|
|
|
|
fsql.Update<OrderCost>().SetSource(dbOrderCost).ExecuteAffrows();
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改采购快递单
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
public void EditPurchaseExpressOrder(EditPurchaseExpressOrderRequest request)
|
|
{
|
|
var dbOrder = fsql.Select<Order>(request.OrderId).ToOne(o => new { o.Id, o.ShopId, o.OrderSn });
|
|
if (dbOrder == null)
|
|
throw new BusinessException($"订单号{request.OrderId}不存在");
|
|
|
|
var shopIds = dbOrder.ShopId.ToString();
|
|
var shop = fsqlManager.MDSfsql.Select<Shops>().Where(s => s.ShopId == shopIds).ToOne();
|
|
|
|
var oldPeo = fsql.Select<PurchaseExpressOrder>(request.OldWaybillNo).ToOne();
|
|
if (oldPeo == null)
|
|
throw new BusinessException($"旧快递单号{request.OldWaybillNo}不存在");
|
|
if (oldPeo.OrderId != request.OrderId || oldPeo.PurchaseOrderId != request.PurchaseOrderId)
|
|
throw new BusinessException("快递单号不属于当前操作提交的订单或采购单");
|
|
var newPeo = fsql.Select<PurchaseExpressOrder>(request.NewWaybillNo).ToOne();
|
|
if (newPeo != null)
|
|
throw new BusinessException($"新快递单号{request.NewWaybillNo}已存在");
|
|
|
|
newPeo = new PurchaseExpressOrder()
|
|
{
|
|
WaybillNo = request.NewWaybillNo,
|
|
TargetExpressId = request.NewExpressId,
|
|
TargetExpressName = request.NewExpressName,
|
|
CreateTime = DateTime.Now,
|
|
OrderId = request.OrderId,
|
|
PurchaseOrderId = request.PurchaseOrderId,
|
|
ShopId = oldPeo.ShopId,
|
|
ExpressState = kuaiDi100Manager.GetExpressState(1) //快递100发货状态值 默认揽收
|
|
};
|
|
try
|
|
{
|
|
kuaiDi100Manager.SubscribeKuaiDi100(request.NewWaybillNo, request.NewExpressId, "http://bbwyb.qiyue666.com/api/purchaseorder/kuaidi100publish");
|
|
newPeo.IsSubscribeKD100 = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
nLogManager.Default().Error(ex, $"EditPurchaseExpressOrder,Request:{JsonConvert.SerializeObject(request)}");
|
|
|
|
#region 订阅失败发送钉钉通知
|
|
var dingdingMsg = new StringBuilder();
|
|
dingdingMsg.AppendLine($"错误:{ex.Message}");
|
|
dingdingMsg.AppendLine($"采购订单号:{request.PurchaseOrderId}");
|
|
dingdingMsg.AppendLine($"拳探订单号:{request.OrderId}");
|
|
dingdingMsg.AppendLine($"拳探订单Sn:{dbOrder.OrderSn}");
|
|
dingdingMsg.AppendLine($"源物流公司:无");
|
|
dingdingMsg.AppendLine($"目标物流公司:{request.NewExpressName} {request.NewExpressId}");
|
|
dingdingMsg.AppendLine($"快递单号:{request.NewWaybillNo}");
|
|
dingdingMsg.AppendLine("触发环节:修改物流单号");
|
|
dingdingMsg.Append($"店铺名:{shop.ShopName}");
|
|
Task.Factory.StartNew(() => SendDingDingOnKD100SubscribeFail(dingdingMsg.ToString()), CancellationToken.None, TaskCreationOptions.None, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
|
|
#endregion
|
|
}
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
fsql.Delete<PurchaseExpressOrder>(request.OldWaybillNo).ExecuteAffrows();
|
|
fsql.Update<OrderPurchaseSkuInfo>().Set(psku => psku.WaybillNo, request.NewWaybillNo)
|
|
.Where(psku => psku.PurchaseOrderId == request.PurchaseOrderId)
|
|
.Where(psku => psku.WaybillNo == request.OldWaybillNo)
|
|
.ExecuteAffrows();
|
|
fsql.Insert(newPeo).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,
|
|
OrderSn = o.OrderSn,
|
|
OrderState = o.OrderState,
|
|
ShopId = o.ShopId
|
|
});
|
|
if (dbOrder == null)
|
|
throw new BusinessException("未查询到采购单的订单信息");
|
|
#endregion
|
|
|
|
#region 店铺信息
|
|
var shop = fsqlManager.MDSfsql.Select<Shops>().Where(s => s.ShopId == dbOrder.ShopId.ToString()).ToOne();
|
|
if (shop == 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
|
|
|
|
IList<OrderPurchaseSkuInfo> orderPurchaseSkuList = null;
|
|
if (orderPurchaseInfo.PurchasePlatform == Enums.Platform.阿里巴巴)
|
|
{
|
|
#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信息并更新快递单号
|
|
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
|
|
bool isSubscribeKD100 = false;
|
|
if (!isExists)
|
|
{
|
|
#region 订阅快递100
|
|
try
|
|
{
|
|
kuaiDi100Manager.SubscribeKuaiDi100(request.WaybillNo, request.ExpressId, "http://bbwyb.qiyue666.com/api/purchaseorder/kuaidi100publish");
|
|
isSubscribeKD100 = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
nLogManager.Default().Error(ex, $"ManualDelivery,Request:{JsonConvert.SerializeObject(request)}");
|
|
|
|
#region 订阅失败发送钉钉通知
|
|
var dingdingMsg = new StringBuilder();
|
|
dingdingMsg.AppendLine($"错误:{ex.Message}");
|
|
dingdingMsg.AppendLine($"采购订单号:{request.PurchaseOrderId}");
|
|
dingdingMsg.AppendLine($"拳探订单号:{dbOrder.Id}");
|
|
dingdingMsg.AppendLine($"拳探订单Sn:{dbOrder.OrderSn}");
|
|
dingdingMsg.AppendLine($"源物流公司:无");
|
|
dingdingMsg.AppendLine($"目标物流公司:{request.ExpressName} {request.ExpressId}");
|
|
dingdingMsg.AppendLine($"快递单号:{request.WaybillNo}");
|
|
dingdingMsg.AppendLine("触发环节:手动发货");
|
|
dingdingMsg.Append($"店铺名:{shop.ShopName}");
|
|
Task.Factory.StartNew(() => SendDingDingOnKD100SubscribeFail(dingdingMsg.ToString()), CancellationToken.None, TaskCreationOptions.None, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
|
|
#endregion
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
#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发货状态值 默认揽收
|
|
IsSubscribeKD100 = isSubscribeKD100,
|
|
ExpressContent = "手动发货",
|
|
ExpressChangedTime = DateTime.Now
|
|
};
|
|
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端状态
|
|
Task.Factory.StartNew(() => SendPurchaseOrderStateToC(dbOrder.Id, dbOrder.OrderState.Value), CancellationToken.None, TaskCreationOptions.None, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
|
|
#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 ManualSign(ManualSignRequest request)
|
|
{
|
|
IList<IUpdate<PurchaseExpressOrder>> updatePurchaseExpressOrderList = new List<IUpdate<PurchaseExpressOrder>>();
|
|
IUpdate<OrderPurchaseInfo> updateOrderPurchase = null;
|
|
IUpdate<Order> updateOrder = null;
|
|
|
|
#region 查询该笔采购单的快递单信息
|
|
var purchaseExpressOrderList = fsql.Select<PurchaseExpressOrder>().Where(x => x.PurchaseOrderId == request.PurchaseOrderId).ToList();
|
|
#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
|
|
|
|
IList<OrderPurchaseSkuInfo> orderPurchaseSkuList = null;
|
|
IList<OrderPurchaseRelationInfo> orderPurchaseRelationList = null;
|
|
if (orderPurchaseInfo.PurchasePlatform == Enums.Platform.阿里巴巴)
|
|
{
|
|
#region 查询采购关联信息
|
|
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信息
|
|
orderPurchaseSkuList = fsql.Select<OrderPurchaseSkuInfo>().Where(x => x.PurchaseOrderId == request.PurchaseOrderId).ToList();
|
|
if (orderPurchaseSkuList.Count() == 0)
|
|
throw new BusinessException("未查询到采购单sku信息");
|
|
#endregion
|
|
|
|
#region 更新采购sku的快递单状态为签收
|
|
foreach (var relation in orderPurchaseRelationList)
|
|
{
|
|
var orderPurchaseSku = orderPurchaseSkuList.FirstOrDefault(posku => posku.PurchaseSkuId == relation.PurchaseSkuId);
|
|
if (orderPurchaseSku == null)
|
|
throw new BusinessException("未查询到采购单sku信息");
|
|
if (!string.IsNullOrEmpty(orderPurchaseSku.WaybillNo))
|
|
{
|
|
var purchaseExpressOrder = purchaseExpressOrderList.FirstOrDefault(exo => exo.WaybillNo == orderPurchaseSku.WaybillNo);
|
|
if (purchaseExpressOrder == null)
|
|
throw new BusinessException("未查询到采购sku的快递信息");
|
|
var expressState = kuaiDi100Manager.GetExpressState(3);
|
|
if (purchaseExpressOrder.ExpressState != expressState)
|
|
{
|
|
purchaseExpressOrder.ExpressState = expressState;
|
|
updatePurchaseExpressOrderList.Add(fsql.Update<PurchaseExpressOrder>(purchaseExpressOrder.WaybillNo)
|
|
.Set(exo => exo.ExpressState, purchaseExpressOrder.ExpressState)
|
|
.Set(exo => exo.ExpressChangedTime, DateTime.Now)
|
|
.Set(exo => exo.ExpressContent, "手动签收"));
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
else
|
|
{
|
|
foreach (var purchaseExpressOrder in purchaseExpressOrderList)
|
|
{
|
|
var expressState = kuaiDi100Manager.GetExpressState(3);
|
|
if (purchaseExpressOrder.ExpressState != expressState)
|
|
{
|
|
purchaseExpressOrder.ExpressState = expressState;
|
|
updatePurchaseExpressOrderList.Add(fsql.Update<PurchaseExpressOrder>(purchaseExpressOrder.WaybillNo)
|
|
.Set(exo => exo.ExpressState, purchaseExpressOrder.ExpressState)
|
|
.Set(exo => exo.ExpressChangedTime, DateTime.Now)
|
|
.Set(exo => exo.ExpressContent, "手动签收"));
|
|
}
|
|
}
|
|
}
|
|
|
|
#region 计算采购单状态
|
|
orderPurchaseInfo.CalculationOrderState(fsql, orderPurchaseSkuList, purchaseExpressOrderList);
|
|
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 通知齐库打包落仓情况
|
|
Task.Factory.StartNew(() => qiKuManager.PublishQiKu(orderPurchaseInfo, orderPurchaseRelationList, orderPurchaseSkuList, purchaseExpressOrderList),
|
|
CancellationToken.None,
|
|
TaskCreationOptions.LongRunning,
|
|
taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
|
|
#endregion
|
|
|
|
#region 通知C端状态
|
|
Task.Factory.StartNew(() => SendPurchaseOrderStateToC(dbOrder.Id, dbOrder.OrderState.Value), CancellationToken.None, TaskCreationOptions.None, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
|
|
#endregion
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
if (updatePurchaseExpressOrderList.Count() > 0)
|
|
{
|
|
foreach (var update in updatePurchaseExpressOrderList)
|
|
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,
|
|
OrderSn = o.OrderSn,
|
|
OrderState = o.OrderState,
|
|
ShopId = o.ShopId
|
|
});
|
|
if (dbOrder == null)
|
|
throw new Exception("未查询到采购单的订单信息");
|
|
orderId = dbOrder.Id;
|
|
shopId = dbOrder.ShopId;
|
|
#endregion
|
|
|
|
#region 店铺信息
|
|
var shop = fsqlManager.MDSfsql.Select<Shops>().Where(s => s.ShopId == dbOrder.ShopId.ToString()).ToOne();
|
|
if (shop == null)
|
|
throw new BusinessException("未查询到采购单所属店铺");
|
|
#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 (string.IsNullOrEmpty(logisticsInfo.WayBillNo) ||
|
|
purchaseExpressOrderList.Any(po => po.WaybillNo == logisticsInfo.WayBillNo))
|
|
continue;
|
|
|
|
#region 订阅快递100
|
|
currentProgress = "订阅快递100";
|
|
LogisticsCompanyRelationship kuaidi100Company = null;
|
|
bool isSubscribeKD100 = false;
|
|
try
|
|
{
|
|
kuaidi100Company = expressCompanyNameConverter.ConverterToKuaiDi100Company(logisticsInfo.ExpressName);
|
|
if (kuaidi100Company == null)
|
|
throw new Exception($"无翻译结果");
|
|
kuaiDi100Manager.SubscribeKuaiDi100(logisticsInfo.WayBillNo, kuaidi100Company.TargetCode, "http://bbwyb.qiyue666.com/api/purchaseorder/kuaidi100publish");
|
|
isSubscribeKD100 = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (!ex.Message.Contains("重复订阅"))
|
|
{
|
|
nLogManager.Default().Error(ex, $"DeliveryCallback 回调平台1688,订单号{orderId},采购单号{purchaseOrderId},执行进度[{currentProgress}],采购单物流信息:{wayBillNoResponseInfo}");
|
|
|
|
#region 订阅失败发送钉钉通知
|
|
var dingdingMsg = new StringBuilder();
|
|
dingdingMsg.AppendLine($"错误:{ex.Message}");
|
|
dingdingMsg.AppendLine($"采购订单号:{purchaseOrderId}");
|
|
dingdingMsg.AppendLine($"拳探订单号:{orderId}");
|
|
dingdingMsg.AppendLine($"拳探订单Sn:{dbOrder.OrderSn}");
|
|
dingdingMsg.AppendLine($"源物流公司:{logisticsInfo.ExpressName}");
|
|
dingdingMsg.AppendLine($"目标物流公司:{kuaidi100Company?.TargetName} {kuaidi100Company?.TargetCode}");
|
|
dingdingMsg.AppendLine($"快递单号:{logisticsInfo.WayBillNo}");
|
|
dingdingMsg.AppendLine("触发环节:1688发货回调");
|
|
dingdingMsg.Append($"店铺名:{shop.ShopName}");
|
|
Task.Factory.StartNew(() => SendDingDingOnKD100SubscribeFail(dingdingMsg.ToString()), CancellationToken.None, TaskCreationOptions.None, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
|
|
#endregion
|
|
}
|
|
}
|
|
#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,
|
|
TargetExpressId = kuaidi100Company?.TargetCode,
|
|
TargetExpressName = kuaidi100Company?.TargetName,
|
|
ExpressState = kuaiDi100Manager.GetExpressState(1), //快递100发货状态值 默认揽收
|
|
IsSubscribeKD100 = isSubscribeKD100
|
|
};
|
|
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端订单状态
|
|
Task.Factory.StartNew(() => SendPurchaseOrderStateToC(dbOrder.Id, dbOrder.OrderState.Value), CancellationToken.None, TaskCreationOptions.None, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
|
|
#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}]");
|
|
}
|
|
}
|
|
|
|
public void KuaiDi100Publish(string param)
|
|
{
|
|
Task.Factory.StartNew(() => KuaiDi100PublishCore(param), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
|
|
}
|
|
|
|
private void KuaiDi100PublishCore(string param)
|
|
{
|
|
nLogManager.GetLogger("快递100").Info($"KuaiDi100Publish {param}");
|
|
JObject jobject = JObject.Parse(param);
|
|
var waybillNo = jobject["lastResult"].Value<string>("nu");
|
|
try
|
|
{
|
|
var state = jobject["lastResult"].Value<int>("state");
|
|
var convertState = kuaiDi100Manager.GetExpressState(state);
|
|
if (convertState == "Unknow")
|
|
return;
|
|
|
|
var dataArray = jobject["lastResult"]["data"].Children().Select(d => new
|
|
{
|
|
context = d.Value<string>("context"),
|
|
ftime = d.Value<DateTime>("ftime"),
|
|
statusCode = d.Value<int>("statusCode")
|
|
}).OrderByDescending(d => d.ftime).ToList();
|
|
|
|
var lastData = dataArray.FirstOrDefault();
|
|
|
|
IUpdate<PurchaseExpressOrder> updatePurchaseExpressOrder = null;
|
|
IUpdate<OrderPurchaseInfo> updateOrderPurchase = null;
|
|
IUpdate<Order> updateOrder = null;
|
|
|
|
#region 查询该笔快递单
|
|
var tpeo = fsql.Select<PurchaseExpressOrder>(waybillNo).ToOne();
|
|
if (tpeo == null)
|
|
throw new Exception("未查询到快递单");
|
|
#endregion
|
|
|
|
#region 查询采购单
|
|
var orderPurchaseInfoList = fsql.Select<OrderPurchaseInfo>().Where(opi => opi.OrderId == tpeo.OrderId && opi.IsEnabled == true).ToList();
|
|
var orderPurchaseInfo = orderPurchaseInfoList.FirstOrDefault(opi => opi.PurchaseOrderId == tpeo.PurchaseOrderId);
|
|
if (orderPurchaseInfo == null)
|
|
throw new Exception("未查询到采购单");
|
|
#endregion
|
|
|
|
#region 查询订单/采购sku/快递单/采购关系
|
|
var dbOrder = fsql.Select<Order>(tpeo.OrderId).ToOne(o => new Order
|
|
{
|
|
Id = o.Id,
|
|
ShopId = o.ShopId,
|
|
OrderState = o.OrderState
|
|
});
|
|
if (dbOrder == null)
|
|
throw new Exception("未查询到订单");
|
|
|
|
IList<OrderPurchaseSkuInfo> orderPurchaseSkuList = null;
|
|
IList<OrderPurchaseRelationInfo> orderPurchaseRelationList = null;
|
|
if (orderPurchaseInfo.PurchasePlatform == Enums.Platform.阿里巴巴)
|
|
{
|
|
orderPurchaseSkuList = fsql.Select<OrderPurchaseSkuInfo>().Where(x => x.PurchaseOrderId == tpeo.PurchaseOrderId).ToList();
|
|
if (orderPurchaseSkuList.Count() == 0)
|
|
throw new BusinessException("未查询到采购单sku信息");
|
|
|
|
orderPurchaseRelationList = fsql.Select<OrderPurchaseRelationInfo>().Where(opri => opri.PurchaseOrderId == tpeo.PurchaseOrderId).ToList();
|
|
if (orderPurchaseRelationList.Count() == 0)
|
|
throw new BusinessException("未查询到采购单的关联信息");
|
|
}
|
|
|
|
var purchaseExpressOrderList = fsql.Select<PurchaseExpressOrder>().Where(x => x.PurchaseOrderId == tpeo.PurchaseOrderId).ToList();
|
|
var purchaseExpressOrder = purchaseExpressOrderList.FirstOrDefault(exo => exo.WaybillNo == waybillNo);
|
|
purchaseExpressOrder.ExpressState = convertState;
|
|
purchaseExpressOrder.ExpressChangedTime = lastData.ftime;
|
|
purchaseExpressOrder.ExpressContent = lastData.context;
|
|
updatePurchaseExpressOrder = fsql.Update<PurchaseExpressOrder>().SetSource(purchaseExpressOrder);
|
|
|
|
#endregion
|
|
|
|
#region 计算采购单状态
|
|
orderPurchaseInfo.CalculationOrderState(fsql, orderPurchaseSkuList, purchaseExpressOrderList);
|
|
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 通知齐库打包落仓情况
|
|
Task.Factory.StartNew(() => qiKuManager.PublishQiKu(orderPurchaseInfo, orderPurchaseRelationList, orderPurchaseSkuList, purchaseExpressOrderList),
|
|
CancellationToken.None,
|
|
TaskCreationOptions.LongRunning,
|
|
taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
|
|
#endregion
|
|
|
|
#region 通知C端状态
|
|
Task.Factory.StartNew(() => SendPurchaseOrderStateToC(dbOrder.Id, dbOrder.OrderState.Value), CancellationToken.None, TaskCreationOptions.None, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler);
|
|
#endregion
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
updatePurchaseExpressOrder?.ExecuteAffrows();
|
|
updateOrderPurchase?.ExecuteAffrows();
|
|
updateOrder?.ExecuteAffrows();
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
nLogManager.GetLogger("快递100").Error(ex, waybillNo);
|
|
}
|
|
}
|
|
|
|
private void SendDingDingOnKD100SubscribeFail(string content)
|
|
{
|
|
try
|
|
{
|
|
dingDingBusiness.SendDingDingBotMessage("SEC5f08a3dd6813e50bf9a3b81350ec12a8086c64b9e29ef858a17f5cc7887906d7",
|
|
"https://oapi.dingtalk.com/robot/send?access_token=7ce472411bb8dde0c3ff503fcca9ead84d39950ee3c4c65c808dbc58981eb929",
|
|
content);
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
private void SendPurchaseOrderStateToC(string orderId, Enums.OrderState orderState)
|
|
{
|
|
try
|
|
{
|
|
restApiService.SendRequest("https://bbwy.qiyue666.com", "api/BatchPurchase/UpdatePurchaseOrderState", new
|
|
{
|
|
OrderId = orderId,
|
|
PurchaseOrderState = orderState
|
|
}, null, HttpMethod.Post);
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|