|
|
@ -431,6 +431,270 @@ namespace BBWY.Server.Business |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 创建订单
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request"></param>
|
|
|
|
public BatchCreareOrderResponse BatchCreateOrderV2(BatchPurchaseCreateOrderRequestV2 request) |
|
|
|
{ |
|
|
|
/* |
|
|
|
下单日志 |
|
|
|
*/ |
|
|
|
var loggerName = $"批量采购-{request.ShopName}"; |
|
|
|
nLogManager.GetLogger(loggerName).Info(JsonConvert.SerializeObject(request)); |
|
|
|
|
|
|
|
if (request.CargoParamGroupList == null || request.CargoParamGroupList.Count() == 0 || |
|
|
|
request.CargoParamGroupList.Any(g => g.CargoParamList == null || g.CargoParamList.Count() == 0 || string.IsNullOrEmpty(g.PurchaserId))) |
|
|
|
throw new BusinessException("缺少商品参数"); |
|
|
|
if (request.Consignee == null || |
|
|
|
string.IsNullOrEmpty(request.Consignee.Address) || |
|
|
|
string.IsNullOrEmpty(request.Consignee.Mobile) || |
|
|
|
string.IsNullOrEmpty(request.Consignee.ContactName)) |
|
|
|
throw new BusinessException("缺少收货人信息"); |
|
|
|
if (request.PurchaseAccountList == null || request.PurchaseAccountList.Count() == 0) |
|
|
|
throw new BusinessException("缺少采购账号"); |
|
|
|
|
|
|
|
var shop = freeSqlMultiDBManager.MDSfsql.Select<Shops>().Where(s => s.ShopId == request.ShopId.ToString()).ToOne(); |
|
|
|
if (shop == null) |
|
|
|
throw new BusinessException("无效的店铺Id"); |
|
|
|
var successSkuIdList = new List<string>(); |
|
|
|
var failSkuList = new List<BatchCreareOrderFailDetail>(); |
|
|
|
var qikuPackSkuConfigRequestList = new List<object>(); |
|
|
|
|
|
|
|
var extJArray = JsonConvert.DeserializeObject<JArray>(request.Extensions); |
|
|
|
|
|
|
|
foreach (var purchaseGroup in request.CargoParamGroupList) |
|
|
|
{ |
|
|
|
var belongSkuGroups = purchaseGroup.CargoParamList.GroupBy(p => p.BelongSkuId); |
|
|
|
|
|
|
|
var belongSkuBasicInfoList = productBusiness.GetProductSkuList(new SearchProductSkuRequest() |
|
|
|
{ |
|
|
|
AppKey = shop.AppKey, |
|
|
|
AppSecret = shop.AppSecret, |
|
|
|
AppToken = shop.AppToken, |
|
|
|
Platform = (Enums.Platform)shop.PlatformId, |
|
|
|
Sku = string.Join(",", belongSkuGroups.Select(x => x.Key)), |
|
|
|
}); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
var purchaseAccount = request.PurchaseAccountList.FirstOrDefault(pa => pa.PurchasePlatformId == purchaseGroup.PurchasePlatform); |
|
|
|
var platformSDKBusiness = platformSDKBusinessList.FirstOrDefault(p => p.Platform == purchaseGroup.PurchasePlatform); |
|
|
|
string tradeMode = "", cardId = ""; |
|
|
|
|
|
|
|
var extJson = extJArray.FirstOrDefault(j => j.Value<string>("PurchaserId") == purchaseGroup.PurchaserId); |
|
|
|
if (purchaseGroup.PurchasePlatform == Enums.Platform.拳探) |
|
|
|
cardId = extJson.Value<string>("CardId"); |
|
|
|
else if (purchaseGroup.PurchasePlatform == Enums.Platform.阿里巴巴) |
|
|
|
tradeMode = extJson.Value<string>("OrderTradeTypeCode"); |
|
|
|
|
|
|
|
|
|
|
|
#region 处理JD SKU和拳探SKU的对应关系
|
|
|
|
var belongSkus_mappingList = new List<JObject>(); |
|
|
|
if (purchaseGroup.PurchasePlatform == Enums.Platform.拳探) |
|
|
|
{ |
|
|
|
foreach (var belongSkuGroup in belongSkuGroups) |
|
|
|
{ |
|
|
|
var firstProductParam = belongSkuGroup.FirstOrDefault(); |
|
|
|
if (!belongSkus_mappingList.Any(j => j.Value<string>("BelongSkuId") == firstProductParam.BelongSkuId)) |
|
|
|
{ |
|
|
|
belongSkus_mappingList.Add(JObject.FromObject(new { firstProductParam.BelongSkuId, firstProductParam.SkuId })); |
|
|
|
} |
|
|
|
} |
|
|
|
if (belongSkus_mappingList.Count() == 0) |
|
|
|
throw new BusinessException("缺少来源SKU信息"); |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
|
|
|
|
var createOrderResponse = platformSDKBusinessList.FirstOrDefault(p => p.Platform == purchaseGroup.PurchasePlatform) |
|
|
|
.FastCreateOrder(new CreateOnlinePurchaseOrderRequest() |
|
|
|
{ |
|
|
|
AppKey = purchaseAccount.AppKey, |
|
|
|
AppSecret = purchaseAccount.AppSecret, |
|
|
|
AppToken = purchaseAccount.AppToken, |
|
|
|
Platform = purchaseGroup.PurchasePlatform, |
|
|
|
Consignee = request.Consignee, |
|
|
|
PurchaseOrderMode = request.PurchaseOrderMode, |
|
|
|
Remark = purchaseGroup.Remark, |
|
|
|
SourceShopName = request.ShopName, |
|
|
|
SourceSku = belongSkus_mappingList, |
|
|
|
CargoParamList = purchaseGroup.CargoParamList.Select(p => new CargoParamRequest() |
|
|
|
{ |
|
|
|
ProductId = p.ProductId, |
|
|
|
SkuId = p.SkuId, |
|
|
|
Quantity = p.Quantity, |
|
|
|
SpecId = p.SpecId |
|
|
|
}).ToList(), |
|
|
|
TradeMode = tradeMode, |
|
|
|
Extensions = cardId, |
|
|
|
AutoPay = request.AutoPay, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
var purchaseOrderSimpleInfo = platformSDKBusinessList.FirstOrDefault(p => p.Platform == purchaseGroup.PurchasePlatform).GetOrderSimpleInfo(new GetOrderInfoRequest() |
|
|
|
{ |
|
|
|
AppKey = purchaseAccount.AppKey, |
|
|
|
AppSecret = purchaseAccount.AppSecret, |
|
|
|
AppToken = purchaseAccount.AppToken, |
|
|
|
OrderId = createOrderResponse.PurchaseOrderId, |
|
|
|
Platform = purchaseGroup.PurchasePlatform |
|
|
|
}); |
|
|
|
|
|
|
|
List<long> updatePurchaseTimeSchemeIdList = purchaseGroup.CargoParamList.Select(p => p.BelongSchemeId).Distinct().ToList(); |
|
|
|
List<PurchaseOrderSku> insertPurchaseOrderSkuList = new List<PurchaseOrderSku>(); |
|
|
|
|
|
|
|
List<object> skuPackConfigList = null; |
|
|
|
if (purchaseGroup.PurchasePlatform == Enums.Platform.拳探) |
|
|
|
skuPackConfigList = new List<object>(); |
|
|
|
|
|
|
|
foreach (var belongSkuGroup in belongSkuGroups) |
|
|
|
{ |
|
|
|
var firstProductParam = belongSkuGroup.FirstOrDefault(); |
|
|
|
var currentOrderSkuProductAmount = 0M; //采购成本
|
|
|
|
currentOrderSkuProductAmount = purchaseOrderSimpleInfo.ItemList.Where(p => belongSkuGroup.Any(p1 => p1.SkuId == p.SkuId)) |
|
|
|
?.Sum(p => p.ProductAmount) ?? 0M; |
|
|
|
|
|
|
|
var currentOrderSkuFreightAmount = purchaseOrderSimpleInfo.FreightAmount / belongSkuGroups.Count(); //采购运费(按sku数均分)
|
|
|
|
|
|
|
|
var belongSkuBasicInfo = belongSkuBasicInfoList.FirstOrDefault(x => x.Id == belongSkuGroup.Key); |
|
|
|
|
|
|
|
var purchaseOrderSku = new PurchaseOrderSku() |
|
|
|
{ |
|
|
|
Id = idGenerator.NewLong(), |
|
|
|
ShopId = request.ShopId, |
|
|
|
PurchaseOrderId = createOrderResponse.PurchaseOrderId, |
|
|
|
ProductId = belongSkuBasicInfo.ProductId, |
|
|
|
SkuId = firstProductParam.BelongSkuId, |
|
|
|
Price = belongSkuBasicInfo.Price, |
|
|
|
SkuTitle = belongSkuBasicInfo.Title, |
|
|
|
Logo = belongSkuBasicInfo.Logo, |
|
|
|
Quantity = firstProductParam.BelongQuantity, |
|
|
|
PurchaseSchemeId = firstProductParam.BelongSchemeId, |
|
|
|
PurchaseSkuIds = string.Join(",", belongSkuGroup.Select(p => p.SkuId).ToList()), |
|
|
|
PurchaseAmount = currentOrderSkuProductAmount + currentOrderSkuFreightAmount, |
|
|
|
ProductAmount = currentOrderSkuProductAmount, |
|
|
|
PurchaseFreight = currentOrderSkuFreightAmount, |
|
|
|
CreateTime = DateTime.Now |
|
|
|
}; |
|
|
|
insertPurchaseOrderSkuList.Add(purchaseOrderSku); |
|
|
|
|
|
|
|
|
|
|
|
if (purchaseGroup.PurchasePlatform == Enums.Platform.拳探) |
|
|
|
{ |
|
|
|
var skuPackConfig = request.PackSkuConfigList?.FirstOrDefault(s => s.SkuId == firstProductParam.BelongSkuId); |
|
|
|
|
|
|
|
if (skuPackConfig != null) |
|
|
|
{ |
|
|
|
|
|
|
|
skuPackConfigList.Add(new |
|
|
|
{ |
|
|
|
skuId = firstProductParam.BelongSkuId, |
|
|
|
skuCount = skuPackConfig.PurchaseCount, |
|
|
|
markMessage = skuPackConfig.RemarkMessage, |
|
|
|
wareHourses = skuPackConfig.PackSkuSplitConfigList.Select(x => new |
|
|
|
{ |
|
|
|
wareId = x.IsJST ? "qiyuejushuitan" : x.Store.Id, |
|
|
|
wareName = x.IsJST ? "齐越聚水潭" : x.Store.Name, |
|
|
|
count = x.PackCount, |
|
|
|
wareType = x.IsJST ? 3 : GetQiKuWareType(x.Store.Type) |
|
|
|
}) |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var purchaseOrderV2 = new PurchaseOrderV2() |
|
|
|
{ |
|
|
|
Id = createOrderResponse.PurchaseOrderId, |
|
|
|
ShopId = request.ShopId, |
|
|
|
OrderState = createOrderResponse.IsPay ? Enums.PurchaseOrderState.等待采购 : Enums.PurchaseOrderState.待付款, |
|
|
|
PurchasePlatform = purchaseGroup.PurchasePlatform, |
|
|
|
ConsigneeContactName = request.Consignee.ContactName, |
|
|
|
ConsigneeMobile = request.Consignee.Mobile, |
|
|
|
ConsigneeProvince = request.Consignee.Province, |
|
|
|
ConsigneeCity = request.Consignee.City, |
|
|
|
ConsigneeCounty = request.Consignee.County, |
|
|
|
ConsigneeTown = request.Consignee.Town, |
|
|
|
ConsigneeAddress = request.Consignee.Address, |
|
|
|
PurchaserId = purchaseGroup.PurchaserId, |
|
|
|
PurchaserName = purchaseGroup.PurchaserName, |
|
|
|
PurchaseAccountId = purchaseAccount.Id, |
|
|
|
PurchaseAmount = purchaseOrderSimpleInfo.TotalAmount, |
|
|
|
ProductAmount = purchaseOrderSimpleInfo.ProductAmount, |
|
|
|
PurchaseFreight = purchaseOrderSimpleInfo.FreightAmount, |
|
|
|
Remark = purchaseGroup.Remark, |
|
|
|
CreateTime = DateTime.Now, |
|
|
|
PurchaseMethod = Enums.PurchaseMethod.线上采购, |
|
|
|
PurchaseOrderMode = request.PurchaseOrderMode |
|
|
|
}; |
|
|
|
if (createOrderResponse.IsPay) |
|
|
|
purchaseOrderV2.PayTime = DateTime.Now; |
|
|
|
|
|
|
|
fsql.Transaction(() => |
|
|
|
{ |
|
|
|
fsql.Insert(purchaseOrderV2).ExecuteAffrows(); |
|
|
|
//fsql.Insert(purchaseOrderSku).ExecuteAffrows();
|
|
|
|
fsql.Insert(insertPurchaseOrderSkuList).ExecuteAffrows(); |
|
|
|
fsql.Update<PurchaseScheme>(updatePurchaseTimeSchemeIdList).Set(p => p.LastPurchaseTime, DateTime.Now).ExecuteAffrows(); |
|
|
|
}); |
|
|
|
successSkuIdList.AddRange(belongSkuGroups.Select(g => g.Key)); |
|
|
|
|
|
|
|
if (purchaseGroup.PurchasePlatform == Enums.Platform.拳探) |
|
|
|
{ |
|
|
|
qikuPackSkuConfigRequestList.Add(new |
|
|
|
{ |
|
|
|
orderId = purchaseOrderV2.Id, |
|
|
|
//shopId = request.ShopId.ToString(),
|
|
|
|
shopId = purchaseGroup.PurchaserId, //拳探店铺Id(商家Id)
|
|
|
|
originShopName = request.ShopName, |
|
|
|
userName = purchaseAccount.AccountName, |
|
|
|
platform = Enums.Platform.拳探, |
|
|
|
purchaseTaskModels = skuPackConfigList |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
failSkuList.AddRange(belongSkuGroups.Select(g => new BatchCreareOrderFailDetail() |
|
|
|
{ |
|
|
|
SkuId = g.Key, |
|
|
|
ErrorMsg = ex.Message |
|
|
|
})); |
|
|
|
//throw new BusinessException(errorBuilder.ToString());
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (qikuPackSkuConfigRequestList.Count() > 0) |
|
|
|
{ |
|
|
|
Task.Factory.StartNew(() => |
|
|
|
{ |
|
|
|
foreach (var qikuPackSkuConfigRequest in qikuPackSkuConfigRequestList) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
var qikuResponse = restApiService.SendRequest("http://qiku.qiyue666.com/", |
|
|
|
"api/PackPurchaseTask/BatchPublicPurchaseTask", |
|
|
|
qikuPackSkuConfigRequest, |
|
|
|
null, |
|
|
|
HttpMethod.Post); |
|
|
|
if (qikuResponse.StatusCode != System.Net.HttpStatusCode.OK) |
|
|
|
throw new Exception(qikuResponse.Content); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
nLogManager.GetLogger($"发布打包任务-{request.ShopName}").Error(ex, JsonConvert.SerializeObject(qikuPackSkuConfigRequest)); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
return new BatchCreareOrderResponse() |
|
|
|
{ |
|
|
|
FailSkuList = failSkuList, |
|
|
|
SuccessSkuIdList = successSkuIdList |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
private int GetQiKuWareType(Enums.StockType stockType) |
|
|
|
{ |
|
|
|
if (stockType == Enums.StockType.京仓) |
|
|
|