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

184 lines
8.9 KiB

using BBWY.Common.Models;
using BBWY.Server.Model.Db;
using BBWY.Server.Model.Dto;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Yitter.IdGenerator;
namespace BBWY.Server.Business
{
public class BatchPurchaseBusiness : BaseBusiness, IDenpendency
{
private ProductBusiness productBusiness;
private IEnumerable<PlatformSDKBusiness> platformSDKBusinessList;
public BatchPurchaseBusiness(IFreeSql fsql,
NLogManager nLogManager,
IIdGenerator idGenerator,
ProductBusiness productBusiness,
IEnumerable<PlatformSDKBusiness> platformSDKBusinessList) : base(fsql, nLogManager, idGenerator)
{
this.productBusiness = productBusiness;
this.platformSDKBusinessList = platformSDKBusinessList;
}
/// <summary>
/// 获取包含对应平台采购方案的sku列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
/// <exception cref="BusinessException"></exception>
public IList<ProductSkuWithSchemeResponse> GetProductSkuAndSchemeList(SearchProductSkuAndSchemeRequest request)
{
if (string.IsNullOrEmpty(request.Spu) && string.IsNullOrEmpty(request.Sku))
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)
{
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
});
}
}
}
return list;
}
public PreviewOrderResponse PreviewOrderRequest(BatchPurchasePreviewOrderRequest request)
{
/*
记录报价日志
*/
if (request.ProductParamList == null || request.ProductParamList.Count() == 0)
throw new BusinessException("缺少商品参数");
if (request.ConsigneeRequest == null ||
string.IsNullOrEmpty(request.ConsigneeRequest.Address) ||
string.IsNullOrEmpty(request.ConsigneeRequest.Mobile) ||
string.IsNullOrEmpty(request.ConsigneeRequest.ContactName))
throw new BusinessException("缺少收货人信息");
if (request.PurchaseAccountList == null || request.PurchaseAccountList.Count() == 0)
throw new BusinessException("缺少采购账号");
var purchaserGroups = request.ProductParamList.GroupBy(p => p.PurchaserId);
var carIds = new List<object>();
var errorBuilder = new StringBuilder();
var freightAmount = 0M;
var productAmount = 0M;
var totalAmount = 0M;
foreach (var purchaserGroup in purchaserGroups)
{
var productParamList = purchaserGroup.ToList();
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,
Consignee = request.ConsigneeRequest,
Platform = purchasePlatform,
PurchaseOrderMode = request.PurchaseOrderMode,
CargoParamList = productParamList.Select(p => new CargoParamRequest()
{
ProductId = p.PurchaseProductId,
SkuId = p.PuchaseSkuId,
Quantity = p.Quantity,
SpecId = p.PurchaseSpecId
}).ToList()
});
if (purchasePlatform == Model.Enums.Platform.)
carIds.Add(new { PurchaserId = purchaserGroup.Key, CardId = previewOrderResponse.Extensions });
else if (purchasePlatform == Model.Enums.Platform.)
carIds.Add(new { PurchaserId = purchaserGroup.Key, OrderTradeTypeCode = previewOrderResponse.OrderTradeType?.Code });
freightAmount += previewOrderResponse.FreightAmount;
productAmount += previewOrderResponse.ProductAmount;
totalAmount += previewOrderResponse.TotalAmount;
}
catch (Exception ex)
{
errorBuilder.AppendLine($"采购商:{productParamList.FirstOrDefault().PurchaserName}");
errorBuilder.AppendLine($"店铺SkuId:{string.Join(",", productParamList.Select(p => p.BelongSkuId))}");
errorBuilder.AppendLine(ex.Message);
throw new BusinessException(errorBuilder.ToString());
}
}
return new PreviewOrderResponse()
{
Extensions = JsonConvert.SerializeObject(carIds),
FreightAmount = freightAmount,
ProductAmount = productAmount,
TotalAmount = totalAmount
};
}
}
}