步步为盈
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.

348 lines
18 KiB

using BBWY.Common.Models;
2 years ago
using BBWY.Server.Model;
2 years ago
using BBWY.Server.Model.Db;
using BBWY.Server.Model.Dto;
2 years ago
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
2 years ago
using System.Collections.Generic;
using System.Linq;
2 years ago
using System.Text;
2 years ago
using Yitter.IdGenerator;
namespace BBWY.Server.Business
{
public class BatchPurchaseBusiness : BaseBusiness, IDenpendency
{
private ProductBusiness productBusiness;
2 years ago
private IEnumerable<PlatformSDKBusiness> platformSDKBusinessList;
public BatchPurchaseBusiness(IFreeSql fsql,
NLogManager nLogManager,
IIdGenerator idGenerator,
ProductBusiness productBusiness,
IEnumerable<PlatformSDKBusiness> platformSDKBusinessList) : base(fsql, nLogManager, idGenerator)
2 years ago
{
this.productBusiness = productBusiness;
2 years ago
this.platformSDKBusinessList = platformSDKBusinessList;
2 years ago
}
/// <summary>
/// 获取包含对应平台采购方案的sku列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
/// <exception cref="BusinessException"></exception>
2 years ago
public IList<ProductSkuWithSchemeResponse> GetProductSkuAndSchemeList(SearchProductSkuAndSchemeRequest request)
{
2 years ago
if (string.IsNullOrEmpty(request.Spu) && string.IsNullOrEmpty(request.Sku))
2 years ago
throw new BusinessException("至少具备一个Sku或Spu条件");
var productSkuList = productBusiness.GetProductSkuList(new SearchProductSkuRequest()
{
AppKey = request.AppKey,
AppSecret = request.AppSecret,
AppToken = request.AppToken,
Platform = request.Platform,
Sku = request.Sku,
Spu = request.Spu
});
if (productSkuList == null || productSkuList.Count() == 0)
return null;
var skuIdList = productSkuList.Select(s => s.Id).ToList();
var schemeList = fsql.Select<PurchaseScheme, Purchaser>().InnerJoin((ps, p) => ps.PurchaserId == p.Id)
.Where((ps, p) => ps.ShopId == request.ShopId)
.Where((ps, p) => skuIdList.Contains(ps.SkuId))
.ToList((ps, p) => new
{
PurchaseSchemeId = ps.Id,
ps.PurchaserId,
PurchaserName = p.Name,
ps.PurchasePlatform,
ps.SkuId
});
var list = new List<ProductSkuWithSchemeResponse>();
foreach (var productSku in productSkuList)
2 years ago
{
var currentSchemeList = schemeList.Where(ps => ps.SkuId == productSku.Id).ToList();
if (currentSchemeList == null || currentSchemeList.Count() == 0)
{
list.Add(new ProductSkuWithSchemeResponse()
{
Id = productSku.Id,
SkuId = productSku.Id,
ProductId = productSku.ProductId,
CreateTime = productSku.CreateTime,
Logo = productSku.Logo,
Price = productSku.Price,
Title = productSku.Title,
State = productSku.State
});
}
else
{
foreach (var currentScheme in currentSchemeList)
{
list.Add(new ProductSkuWithSchemeResponse()
{
Id = $"{productSku.Id}_{currentScheme.PurchaseSchemeId}",
SkuId = productSku.Id,
ProductId = productSku.ProductId,
CreateTime = productSku.CreateTime,
Logo = productSku.Logo,
Price = productSku.Price,
Title = productSku.Title,
State = productSku.State,
PurchaserName = currentScheme.PurchaserName,
PurchasePlatform = currentScheme.PurchasePlatform,
PurchaserId = currentScheme.PurchaserId,
PurchaseSchemeId = currentScheme.PurchaseSchemeId
});
}
}
2 years ago
}
return list;
2 years ago
}
2 years ago
/// <summary>
/// 预览订单
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
/// <exception cref="BusinessException"></exception>
2 years ago
public PreviewOrderResponse PreviewOrder(BatchPurchasePreviewOrderRequest request)
2 years ago
{
/*
*/
if (request.ProductParamList == null || request.ProductParamList.Count() == 0)
throw new BusinessException("缺少商品参数");
2 years ago
if (request.Consignee == null ||
string.IsNullOrEmpty(request.Consignee.Address) ||
string.IsNullOrEmpty(request.Consignee.Mobile) ||
string.IsNullOrEmpty(request.Consignee.ContactName))
2 years ago
throw new BusinessException("缺少收货人信息");
if (request.PurchaseAccountList == null || request.PurchaseAccountList.Count() == 0)
throw new BusinessException("缺少采购账号");
2 years ago
var skuGroups = request.ProductParamList.GroupBy(p => p.BelongSkuId);
2 years ago
var extJArray = new List<object>();
2 years ago
var errorBuilder = new StringBuilder();
var freightAmount = 0M;
var productAmount = 0M;
var totalAmount = 0M;
2 years ago
foreach (var skuGroup in skuGroups)
2 years ago
{
2 years ago
var productParamList = skuGroup.ToList();
2 years ago
try
{
var purchasePlatform = productParamList.FirstOrDefault().PurchasePlatform;
var purchaseAccount = request.PurchaseAccountList.FirstOrDefault(pa => pa.PurchasePlatformId == purchasePlatform);
var platformSDKBusiness = platformSDKBusinessList.FirstOrDefault(p => p.Platform == purchasePlatform);
var previewOrderResponse = platformSDKBusiness.PreviewOrder(new PreviewOrderReuqest()
{
AppKey = purchaseAccount.AppKey,
AppSecret = purchaseAccount.AppSecret,
AppToken = purchaseAccount.AppToken,
2 years ago
Consignee = request.Consignee,
2 years ago
Platform = purchasePlatform,
PurchaseOrderMode = request.PurchaseOrderMode,
CargoParamList = productParamList.Select(p => new CargoParamRequest()
{
ProductId = p.PurchaseProductId,
2 years ago
SkuId = p.PurchaseSkuId,
2 years ago
Quantity = p.Quantity,
SpecId = p.PurchaseSpecId
}).ToList()
});
2 years ago
if (purchasePlatform == Enums.Platform.)
2 years ago
extJArray.Add(new { BelongSkuId = skuGroup.Key, CardId = previewOrderResponse.Extensions });
2 years ago
else if (purchasePlatform == Enums.Platform.)
2 years ago
extJArray.Add(new { BelongSkuId = skuGroup.Key, OrderTradeTypeCode = previewOrderResponse.OrderTradeType?.Code });
2 years ago
freightAmount += previewOrderResponse.FreightAmount;
productAmount += previewOrderResponse.ProductAmount;
totalAmount += previewOrderResponse.TotalAmount;
}
catch (Exception ex)
{
errorBuilder.AppendLine($"采购商:{productParamList.FirstOrDefault().PurchaserName}");
2 years ago
errorBuilder.AppendLine($"店铺SkuId:{skuGroup.Key}");
2 years ago
errorBuilder.AppendLine(ex.Message);
throw new BusinessException(errorBuilder.ToString());
}
}
2 years ago
return new PreviewOrderResponse()
{
2 years ago
Extensions = JsonConvert.SerializeObject(extJArray),
2 years ago
FreightAmount = freightAmount,
ProductAmount = productAmount,
TotalAmount = totalAmount
};
}
2 years ago
2 years ago
/// <summary>
/// 创建订单
/// </summary>
/// <param name="request"></param>
public BatchCreareOrderResponse BatchCreateOrder(BatchPurchaseCreateOrderRequest request)
{
/*
*/
if (request.ProductParamList == null || request.ProductParamList.Count() == 0)
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 successSkuIdList = new List<string>();
var failSkuList = new List<BatchCreareOrderFailDetail>();
var extJArray = JsonConvert.DeserializeObject<JArray>(request.Extensions);
var skuGroups = request.ProductParamList.GroupBy(p => p.BelongSkuId);
foreach (var skuGroup in skuGroups)
{
var productParamList = skuGroup.ToList();
try
{
var firstProductParam = productParamList.FirstOrDefault();
var purchaseAccount = request.PurchaseAccountList.FirstOrDefault(pa => pa.PurchasePlatformId == firstProductParam.PurchasePlatform);
2 years ago
var platformSDKBusiness = platformSDKBusinessList.FirstOrDefault(p => p.Platform == firstProductParam.PurchasePlatform);
2 years ago
string tradeMode = "", cardId = "";
var extJson = extJArray.FirstOrDefault(j => j.Value<string>("BelongSkuId") == skuGroup.Key);
if (firstProductParam.PurchasePlatform == Enums.Platform.)
cardId = extJson.Value<string>("CardId");
else if (firstProductParam.PurchasePlatform == Enums.Platform.)
tradeMode = extJson.Value<string>("OrderTradeTypeCode");
var createOrderResponse = platformSDKBusinessList.FirstOrDefault(p => p.Platform == firstProductParam.PurchasePlatform)
.FastCreateOrder(new CreateOnlinePurchaseOrderRequest()
{
AppKey = purchaseAccount.AppKey,
AppSecret = purchaseAccount.AppSecret,
AppToken = purchaseAccount.AppToken,
Platform = firstProductParam.PurchasePlatform,
Consignee = request.Consignee,
PurchaseOrderMode = request.PurchaseOrderMode,
Remark = request.Remark,
SourceShopName = request.ShopName,
SourceSku = skuGroup.Key,
CargoParamList = productParamList.Select(p => new CargoParamRequest()
{
ProductId = p.PurchaseProductId,
SkuId = p.PurchaseSkuId,
Quantity = p.Quantity,
SpecId = p.PurchaseSpecId
}).ToList(),
TradeMode = tradeMode,
Extensions = cardId
});
var purchaseOrderSimpleInfo = platformSDKBusinessList.FirstOrDefault(p => p.Platform == firstProductParam.PurchasePlatform).GetOrderSimpleInfo(new GetOrderInfoRequest()
{
AppKey = purchaseAccount.AppKey,
AppSecret = purchaseAccount.AppSecret,
AppToken = purchaseAccount.AppToken,
OrderId = createOrderResponse.PurchaseOrderId,
Platform = firstProductParam.PurchasePlatform
});
var purchaseOrderV2 = new PurchaseOrderV2()
{
Id = createOrderResponse.PurchaseOrderId,
ShopId = request.ShopId,
OrderState = createOrderResponse.IsPay ? Enums.PurchaseOrderState. : Enums.PurchaseOrderState.,
PurchasePlatform = firstProductParam.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 = firstProductParam.PurchaserId,
PurchaserName = firstProductParam.PurchaserName,
PurchaseAccountId = purchaseAccount.Id,
PurchaseAmount = purchaseOrderSimpleInfo.TotalAmount,
ProductAmount = purchaseOrderSimpleInfo.ProductAmount,
PurchaseFreight = purchaseOrderSimpleInfo.FreightAmount,
Remark = request.Remark,
CreateTime = DateTime.Now,
PurchaseMethod = Enums.PurchaseMethod.线,
PurchaseOrderMode = request.PurchaseOrderMode
};
if (createOrderResponse.IsPay)
purchaseOrderV2.PayTime = DateTime.Now;
var purchaseOrderSku = new PurchaseOrderSku()
{
Id = idGenerator.NewLong(),
ShopId = request.ShopId,
PurchaseOrderId = createOrderResponse.PurchaseOrderId,
ProductId = firstProductParam.BelongProductId,
SkuId = firstProductParam.BelongSkuId,
Price = firstProductParam.BelongPrice,
SkuTitle = firstProductParam.BelongSkuTitle,
Logo = firstProductParam.BelongLogo,
Quantity = firstProductParam.BelongQuantity,
PurchaseSchemeId = firstProductParam.BelongPurchaseSchemeId,
PurchaseSkuIds = string.Join(",", productParamList.Select(p => p.PurchaseSkuId).ToList()),
PurchaseAmount = purchaseOrderSimpleInfo.TotalAmount,
ProductAmount = purchaseOrderSimpleInfo.ProductAmount,
PurchaseFreight = purchaseOrderSimpleInfo.FreightAmount,
CreateTime = DateTime.Now
};
fsql.Transaction(() =>
{
fsql.Insert(purchaseOrderV2).ExecuteAffrows();
fsql.Insert(purchaseOrderSku).ExecuteAffrows();
});
successSkuIdList.Add(skuGroup.Key);
}
catch (Exception ex)
{
//var errorBuilder = new StringBuilder();
//errorBuilder.AppendLine($"采购商:{productParamList.FirstOrDefault().PurchaserName}");
//errorBuilder.AppendLine($"店铺SkuId:{skuGroup.Key}");
//errorBuilder.AppendLine(ex.Message);
failSkuList.Add(new BatchCreareOrderFailDetail()
{
SkuId = skuGroup.Key,
ErrorMsg = ex.Message
});
//throw new BusinessException(errorBuilder.ToString());
}
}
return new BatchCreareOrderResponse()
{
FailSkuList = failSkuList,
SuccessSkuIdList = successSkuIdList
};
2 years ago
}
2 years ago
}
}