|
|
|
using BBWY.Common.Extensions;
|
|
|
|
using BBWY.Common.Http;
|
|
|
|
using BBWY.Common.Models;
|
|
|
|
using BBWY.Server.Business.PlatformSDK.DataExtension;
|
|
|
|
using BBWY.Server.Model;
|
|
|
|
using BBWY.Server.Model.Db;
|
|
|
|
using BBWY.Server.Model.Dto;
|
|
|
|
using FreeSql;
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using NLog;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net.Http;
|
|
|
|
using Yitter.IdGenerator;
|
|
|
|
|
|
|
|
namespace BBWY.Server.Business
|
|
|
|
{
|
|
|
|
public class OrderBusiness : BasePlatformRelayBusiness, IDenpendency
|
|
|
|
{
|
|
|
|
private ILogger logger;
|
|
|
|
private IFreeSql fsql;
|
|
|
|
private IDictionary<Enums.Platform, Action<JArray, long, string, string, string, string>> syncOrderMethodDic;
|
|
|
|
private IIdGenerator idGenerator;
|
|
|
|
private IDictionary<string, string> mdsApiHeader;
|
|
|
|
|
|
|
|
public OrderBusiness(RestApiService restApiService, IConfiguration configuration, ILogger logger, IFreeSql fsql, IIdGenerator idGenerator, IOptions<GlobalConfig> options) : base(restApiService, options)
|
|
|
|
{
|
|
|
|
this.logger = logger;
|
|
|
|
this.fsql = fsql;
|
|
|
|
this.idGenerator = idGenerator;
|
|
|
|
syncOrderMethodDic = new Dictionary<Enums.Platform, Action<JArray, long, string, string, string, string>>()
|
|
|
|
{
|
|
|
|
{ Enums.Platform.京东, SyncJDOrder }
|
|
|
|
};
|
|
|
|
mdsApiHeader = new Dictionary<string, string>() {
|
|
|
|
{ "qy","qy"}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public OrderListResponse GetOrderList(SearchOrderRequest searchOrderRequest)
|
|
|
|
{
|
|
|
|
var select = fsql.Select<Order, OrderConsignee, OrderCost>().LeftJoin((o, ocs, oct) => o.Id == ocs.OrderId)
|
|
|
|
.LeftJoin((o, ocs, oct) => o.Id == oct.OrderId);
|
|
|
|
if (!string.IsNullOrEmpty(searchOrderRequest.OrderId))
|
|
|
|
{
|
|
|
|
select = select.Where((o, ocs, oct) => o.Id == searchOrderRequest.OrderId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(searchOrderRequest.Sku) || !string.IsNullOrEmpty(searchOrderRequest.ProductNo))
|
|
|
|
{
|
|
|
|
var childSelect = fsql.Select<OrderSku>().As("osku")
|
|
|
|
.WhereIf(string.IsNullOrEmpty(searchOrderRequest.Sku) == false, osku => osku.SkuId == searchOrderRequest.Sku)
|
|
|
|
.WhereIf(string.IsNullOrEmpty(searchOrderRequest.ProductNo) == false, osku => osku.ProductNo == searchOrderRequest.ProductNo);
|
|
|
|
select = select.Where((o, ocs, oct) => childSelect.Where(osku => osku.OrderId == o.Id).Any());
|
|
|
|
}
|
|
|
|
|
|
|
|
select = select.WhereIf(searchOrderRequest.OrderState != null, (o, ocs, oct) => o.OrderState == searchOrderRequest.OrderState)
|
|
|
|
.WhereIf(searchOrderRequest.StartDate != null, (o, ocs, oct) => o.StartTime >= searchOrderRequest.StartDate)
|
|
|
|
.WhereIf(searchOrderRequest.EndDate != null, (o, ocs, oct) => o.StartTime <= searchOrderRequest.EndDate)
|
|
|
|
.WhereIf(string.IsNullOrEmpty(searchOrderRequest.ContactName) == false, (o, ocs, oct) => ocs.ContactName == searchOrderRequest.ContactName)
|
|
|
|
.WhereIf(string.IsNullOrEmpty(searchOrderRequest.Waybill) == false, (o, ocs, oct) => o.WaybillNo.Contains(searchOrderRequest.Waybill)); //这一步可能比较慢
|
|
|
|
}
|
|
|
|
|
|
|
|
select = select.Where((o, ocs, oct) => o.ShopId == searchOrderRequest.ShopId)
|
|
|
|
.OrderByDescending((o, ocs, oct) => o.StartTime)
|
|
|
|
.Count(out var total)
|
|
|
|
.Page(searchOrderRequest.PageIndex, searchOrderRequest.PageSize);
|
|
|
|
|
|
|
|
var sql = select.ToSql();
|
|
|
|
|
|
|
|
var orderSourceList = select.ToList((o, ocs, oct) => new Order()
|
|
|
|
{
|
|
|
|
Id = o.Id,
|
|
|
|
BuyerRemark = o.BuyerRemark,
|
|
|
|
EndTime = o.EndTime,
|
|
|
|
FreightPrice = o.FreightPrice,
|
|
|
|
ModifyTime = o.ModifyTime,
|
|
|
|
OrderPayment = o.OrderPayment,
|
|
|
|
OrderSellerPrice = o.OrderSellerPrice,
|
|
|
|
OrderState = o.OrderState,
|
|
|
|
OrderTotalPrice = o.OrderTotalPrice,
|
|
|
|
OrderType = o.OrderType,
|
|
|
|
PayType = o.PayType,
|
|
|
|
Platform = o.Platform,
|
|
|
|
ShopId = o.ShopId,
|
|
|
|
StartTime = o.StartTime,
|
|
|
|
StorageType = o.StorageType,
|
|
|
|
StoreId = o.StoreId,
|
|
|
|
StoreOrder = o.StoreOrder,
|
|
|
|
VenderRemark = o.VenderRemark,
|
|
|
|
WaybillNo = o.WaybillNo,
|
|
|
|
Flag = o.Flag,
|
|
|
|
SDType = o.SDType,
|
|
|
|
|
|
|
|
ContactName = ocs.ContactName,
|
|
|
|
Address = ocs.Address,
|
|
|
|
Province = ocs.Province,
|
|
|
|
County = ocs.County,
|
|
|
|
Town = ocs.Town,
|
|
|
|
City = ocs.City,
|
|
|
|
IsDecode = ocs.IsDecode,
|
|
|
|
Mobile = ocs.Mobile,
|
|
|
|
TelePhone = ocs.TelePhone,
|
|
|
|
|
|
|
|
DeliveryExpressFreight = oct.DeliveryExpressFreight,
|
|
|
|
PlatformCommissionAmount = oct.PlatformCommissionAmount,
|
|
|
|
PlatformCommissionRatio = oct.PlatformCommissionRatio,
|
|
|
|
PreferentialAmount = oct.PreferentialAmount,
|
|
|
|
Profit = oct.Profit,
|
|
|
|
PurchaseAmount = oct.PurchaseAmount,
|
|
|
|
IsManualEdited = oct.IsManualEdited,
|
|
|
|
SDCommissionAmount = oct.SDCommissionAmount
|
|
|
|
});
|
|
|
|
var orderList = orderSourceList.Map<IList<OrderResponse>>();
|
|
|
|
|
|
|
|
if (orderList.Count > 0)
|
|
|
|
{
|
|
|
|
var orderIdList = orderList.Select(o => o.Id).ToList();
|
|
|
|
|
|
|
|
#region 处理Sku
|
|
|
|
var orderSkuList = fsql.Select<OrderSku>().Where(osku => orderIdList.Contains(osku.OrderId)).ToList().Map<IList<OrderSkuResponse>>();
|
|
|
|
foreach (var order in orderList)
|
|
|
|
order.ItemList = orderSkuList.Where(osku => osku.OrderId == order.Id).ToList();
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 处理优惠券
|
|
|
|
var orderCouponList = fsql.Select<OrderCoupon>().Where(oc => orderIdList.Contains(oc.OrderId)).ToList().Map<IList<OrderCouponResponse>>();
|
|
|
|
foreach (var order in orderList)
|
|
|
|
order.OrderCouponList = orderCouponList.Where(oc => oc.OrderId == order.Id).ToList();
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 处理订单成本明细
|
|
|
|
var orderCostDetailList = fsql.Select<OrderCostDetail>().Where(ocd => orderIdList.Contains(ocd.OrderId)).ToList().Map<IList<OrderCostDetailResponse>>();
|
|
|
|
foreach (var order in orderList)
|
|
|
|
order.OrderCostDetailList = orderCostDetailList.Where(ocd => ocd.OrderId == order.Id).ToList();
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 翻译仓库Id
|
|
|
|
foreach (var order in orderList)
|
|
|
|
order.StoreName = globalConfig.Stores.FirstOrDefault(s => s.StoreId == order.StoreId)?.StoreName ?? order.StoreId;
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
|
|
|
|
var response = new OrderListResponse()
|
|
|
|
{
|
|
|
|
Count = total,
|
|
|
|
Items = orderList
|
|
|
|
};
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 解密
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="decryptConsigneeRequest"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <exception cref="BusinessException"></exception>
|
|
|
|
public ConsigneeSimpleResponse DecryptConsignee(DecryptConsigneeRequest decryptConsigneeRequest)
|
|
|
|
{
|
|
|
|
var relayAPIHost = GetPlatformRelayAPIHost(decryptConsigneeRequest.Platform);
|
|
|
|
var sendResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/DecryptConsignee", decryptConsigneeRequest, null, HttpMethod.Post);
|
|
|
|
if (sendResult.StatusCode != System.Net.HttpStatusCode.OK)
|
|
|
|
throw new BusinessException(sendResult.Content) { Code = (int)sendResult.StatusCode };
|
|
|
|
var response = JsonConvert.DeserializeObject<ApiResponse<ConsigneeSimpleResponse>>(sendResult.Content);
|
|
|
|
if (!response.Success)
|
|
|
|
throw new BusinessException(response.Msg) { Code = response.Code };
|
|
|
|
|
|
|
|
//将解密后的收货人信息存至数据库
|
|
|
|
if (decryptConsigneeRequest.SaveDb)
|
|
|
|
fsql.Update<OrderConsignee>(decryptConsigneeRequest.OrderId).Set(oc => oc.ContactName, response.Data.ContactName)
|
|
|
|
.Set(oc => oc.Address, response.Data.Address)
|
|
|
|
.Set(oc => oc.Mobile, response.Data.Mobile)
|
|
|
|
.Set(oc => oc.TelePhone, response.Data.TelePhone)
|
|
|
|
.Set(oc => oc.IsDecode, true)
|
|
|
|
.ExecuteAffrows();
|
|
|
|
|
|
|
|
return response.Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 自动计算成本
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="autoCalculationCostRequest"></param>
|
|
|
|
public void AutoCalculationCost(AutoCalculationCostRequest autoCalculationCostRequest)
|
|
|
|
{
|
|
|
|
var dbOrder = fsql.Select<Order>(autoCalculationCostRequest.OrderId).ToOne();
|
|
|
|
if (dbOrder == null)
|
|
|
|
throw new BusinessException($"订单号{autoCalculationCostRequest.OrderId}不存在");
|
|
|
|
|
|
|
|
var orderSkus = fsql.Select<OrderSku>().Where(osku => osku.OrderId == autoCalculationCostRequest.OrderId).ToList();
|
|
|
|
var orderSkuIds = orderSkus.Select(osku => osku.SkuId).ToList();
|
|
|
|
var purchaserOrders = fsql.Select<PurchaseOrder>().Where(po => po.RemainingQuantity != 0 && orderSkuIds.Contains(po.SkuId)).ToList();
|
|
|
|
if (purchaserOrders.Count() == 0)
|
|
|
|
throw new BusinessException("库存为零不能自动计算成本");
|
|
|
|
|
|
|
|
var orderCost = fsql.Select<OrderCost>(autoCalculationCostRequest.OrderId).ToOne();
|
|
|
|
var orderCostDetails = fsql.Select<OrderCostDetail>().Where(ocd => ocd.OrderId == autoCalculationCostRequest.OrderId).ToList();
|
|
|
|
|
|
|
|
IUpdate<Order> orderUpdate = null;
|
|
|
|
IUpdate<OrderCost> updateOrderCost = null;
|
|
|
|
IInsert<OrderCost> insertOrderCost = null;
|
|
|
|
IList<IUpdate<PurchaseOrder>> updatePurchaseOrderList = new List<IUpdate<PurchaseOrder>>();
|
|
|
|
List<OrderCostDetail> insertOrderCostDetailList = new List<OrderCostDetail>();
|
|
|
|
|
|
|
|
if (autoCalculationCostRequest.IsSetStorageType)
|
|
|
|
orderUpdate = fsql.Update<Order>(autoCalculationCostRequest.OrderId).Set(o => o.StorageType, autoCalculationCostRequest.StorageType);
|
|
|
|
|
|
|
|
var orderCostPurchaseAmount = 0M;
|
|
|
|
var orderDeliveryExpressFreight = 0M; //发货总运费,sku购买数量第二个开始半价
|
|
|
|
foreach (var orderSku in orderSkus)
|
|
|
|
{
|
|
|
|
//查询该sku的扣减明细
|
|
|
|
var currentOrderSkuCostDetails = orderCostDetails.Where(ocd => ocd.SkuId == orderSku.SkuId);
|
|
|
|
//已扣减数量
|
|
|
|
var deductedQuantity = currentOrderSkuCostDetails.Count() == 0 ? 0 : currentOrderSkuCostDetails.Sum(ocd => ocd.DeductionQuantity);
|
|
|
|
//剩余扣减数量
|
|
|
|
var noDeductionQuantity = orderSku.ItemTotal.Value - deductedQuantity;
|
|
|
|
if (noDeductionQuantity == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//是否多次扣减库存
|
|
|
|
var isReduceMultiTimes = false;
|
|
|
|
if (currentOrderSkuCostDetails.Count() > 0)
|
|
|
|
isReduceMultiTimes = true; //之前有过扣减记录,发货运费按半价计算
|
|
|
|
|
|
|
|
while (noDeductionQuantity != 0)
|
|
|
|
{
|
|
|
|
var purchaseOrder = purchaserOrders.FirstOrDefault(po => po.StorageType == autoCalculationCostRequest.StorageType &&
|
|
|
|
po.RemainingQuantity != 0 &&
|
|
|
|
po.SkuId == orderSku.SkuId);
|
|
|
|
if (purchaseOrder == null)
|
|
|
|
break; //没有库存了
|
|
|
|
|
|
|
|
|
|
|
|
//本次扣减数量
|
|
|
|
var deductionQuantity = purchaseOrder.RemainingQuantity >= noDeductionQuantity ? noDeductionQuantity : purchaseOrder.RemainingQuantity;
|
|
|
|
//本次扣减量的采购成本
|
|
|
|
var currentPurchaseAmount = purchaseOrder.UnitCost * deductionQuantity;
|
|
|
|
//本次扣减量的发货运费
|
|
|
|
var currentSkuDeliveryFreight = isReduceMultiTimes ?
|
|
|
|
(purchaseOrder.SingleDeliveryFreight / 2 * deductionQuantity) :
|
|
|
|
(purchaseOrder.SingleDeliveryFreight + purchaseOrder.SingleDeliveryFreight / 2 * (deductionQuantity - 1));
|
|
|
|
isReduceMultiTimes = true;
|
|
|
|
|
|
|
|
noDeductionQuantity -= deductionQuantity;
|
|
|
|
purchaseOrder.RemainingQuantity -= deductionQuantity;
|
|
|
|
|
|
|
|
//累计采购成本
|
|
|
|
orderCostPurchaseAmount += currentPurchaseAmount;
|
|
|
|
//累计发货运费(销售运费)
|
|
|
|
orderDeliveryExpressFreight += currentSkuDeliveryFreight;
|
|
|
|
|
|
|
|
var updateSql = fsql.Update<PurchaseOrder>(purchaseOrder.Id).Set(po => po.RemainingQuantity - deductionQuantity);
|
|
|
|
updatePurchaseOrderList.Add(updateSql);
|
|
|
|
|
|
|
|
var orderCostDetail = new OrderCostDetail()
|
|
|
|
{
|
|
|
|
Id = idGenerator.NewLong(),
|
|
|
|
OrderId = autoCalculationCostRequest.OrderId,
|
|
|
|
ProductId = orderSku.ProductId,
|
|
|
|
SkuId = orderSku.SkuId,
|
|
|
|
CreateTime = DateTime.Now,
|
|
|
|
PurchaseOrderPKId = purchaseOrder.Id,
|
|
|
|
UnitCost = purchaseOrder.UnitCost,
|
|
|
|
DeductionQuantity = deductionQuantity,
|
|
|
|
DeliveryExpressFreight = currentSkuDeliveryFreight,
|
|
|
|
TotalCost = currentPurchaseAmount,
|
|
|
|
ConsumableAmount = purchaseOrder.SingleConsumableAmount * deductionQuantity,
|
|
|
|
FirstFreight = purchaseOrder.SingleFirstFreight * deductionQuantity,
|
|
|
|
OperationAmount = purchaseOrder.SingleOperationAmount * deductionQuantity,
|
|
|
|
PurchaseFreight = purchaseOrder.SingleFreight * deductionQuantity,
|
|
|
|
SkuAmount = purchaseOrder.SingleSkuAmount * deductionQuantity,
|
|
|
|
StorageAmount = purchaseOrder.SingleStorageAmount * deductionQuantity
|
|
|
|
};
|
|
|
|
insertOrderCostDetailList.Add(orderCostDetail);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (orderCost == null)
|
|
|
|
{
|
|
|
|
#region 计算成本
|
|
|
|
orderCost = new OrderCost()
|
|
|
|
{
|
|
|
|
OrderId = autoCalculationCostRequest.OrderId,
|
|
|
|
PlatformCommissionRatio = 0.05M,
|
|
|
|
PreferentialAmount = dbOrder.PreferentialAmount,
|
|
|
|
Profit = 0,
|
|
|
|
PurchaseAmount = orderCostPurchaseAmount,
|
|
|
|
DeliveryExpressFreight = orderDeliveryExpressFreight,
|
|
|
|
CreateTime = DateTime.Now
|
|
|
|
};
|
|
|
|
orderCost.PlatformCommissionAmount = dbOrder.OrderSellerPrice * orderCost.PlatformCommissionRatio;
|
|
|
|
orderCost.Profit = dbOrder.OrderSellerPrice +
|
|
|
|
dbOrder.FreightPrice -
|
|
|
|
orderCost.PurchaseAmount -
|
|
|
|
orderCost.DeliveryExpressFreight -
|
|
|
|
orderCost.PlatformCommissionAmount;
|
|
|
|
insertOrderCost = fsql.Insert(orderCost);
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
orderCost.PurchaseAmount += orderCostPurchaseAmount;
|
|
|
|
orderCost.DeliveryExpressFreight += orderDeliveryExpressFreight;
|
|
|
|
orderCost.Profit = dbOrder.OrderSellerPrice +
|
|
|
|
dbOrder.FreightPrice -
|
|
|
|
orderCost.PurchaseAmount -
|
|
|
|
orderCost.DeliveryExpressFreight -
|
|
|
|
orderCost.PlatformCommissionAmount;
|
|
|
|
updateOrderCost = fsql.Update<OrderCost>().SetSource(orderCost);
|
|
|
|
}
|
|
|
|
|
|
|
|
fsql.Transaction(() =>
|
|
|
|
{
|
|
|
|
orderUpdate?.ExecuteAffrows();
|
|
|
|
updateOrderCost?.ExecuteAffrows();
|
|
|
|
insertOrderCost?.ExecuteAffrows();
|
|
|
|
if (updatePurchaseOrderList.Count > 0)
|
|
|
|
{
|
|
|
|
foreach (var update in updatePurchaseOrderList)
|
|
|
|
update.ExecuteAffrows();
|
|
|
|
}
|
|
|
|
if (insertOrderCostDetailList.Count > 0)
|
|
|
|
fsql.Insert(insertOrderCostDetailList).ExecuteAffrows();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 手动计算成本
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="manualCalculationCostRequest"></param>
|
|
|
|
/// <exception cref="BusinessException"></exception>
|
|
|
|
public void ManualCalculationCost(ManualCalculationCostRequest manualCalculationCostRequest)
|
|
|
|
{
|
|
|
|
var dbOrder = fsql.Select<Order>(manualCalculationCostRequest.OrderId).ToOne();
|
|
|
|
if (dbOrder == null)
|
|
|
|
throw new BusinessException($"订单号{manualCalculationCostRequest.OrderId}不存在");
|
|
|
|
|
|
|
|
IUpdate<Order> orderUpdate = null;
|
|
|
|
IUpdate<OrderCost> updateOrderCost = null;
|
|
|
|
IInsert<OrderCost> insertOrderCost = null;
|
|
|
|
if (manualCalculationCostRequest.IsSetStorageType)
|
|
|
|
orderUpdate = fsql.Update<Order>(manualCalculationCostRequest.OrderId).Set(o => o.StorageType, manualCalculationCostRequest.StorageType);
|
|
|
|
|
|
|
|
|
|
|
|
var orderCost = fsql.Select<OrderCost>(manualCalculationCostRequest.OrderId).ToOne();
|
|
|
|
if (orderCost == null)
|
|
|
|
{
|
|
|
|
orderCost = new OrderCost()
|
|
|
|
{
|
|
|
|
OrderId = manualCalculationCostRequest.OrderId,
|
|
|
|
PlatformCommissionRatio = 0.05M,
|
|
|
|
PreferentialAmount = dbOrder.PreferentialAmount,
|
|
|
|
Profit = 0,
|
|
|
|
PurchaseAmount = manualCalculationCostRequest.PurchaseCost,
|
|
|
|
DeliveryExpressFreight = manualCalculationCostRequest.DeliveryExpressFreight,
|
|
|
|
CreateTime = DateTime.Now,
|
|
|
|
IsManualEdited = true
|
|
|
|
};
|
|
|
|
orderCost.PlatformCommissionAmount = dbOrder.OrderSellerPrice * orderCost.PlatformCommissionRatio;
|
|
|
|
orderCost.Profit = dbOrder.OrderSellerPrice +
|
|
|
|
dbOrder.FreightPrice -
|
|
|
|
orderCost.PurchaseAmount -
|
|
|
|
orderCost.DeliveryExpressFreight -
|
|
|
|
orderCost.PlatformCommissionAmount;
|
|
|
|
insertOrderCost = fsql.Insert(orderCost);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
orderCost.PurchaseAmount = manualCalculationCostRequest.PurchaseCost;
|
|
|
|
orderCost.DeliveryExpressFreight = manualCalculationCostRequest.DeliveryExpressFreight;
|
|
|
|
orderCost.Profit = dbOrder.OrderSellerPrice +
|
|
|
|
dbOrder.FreightPrice -
|
|
|
|
orderCost.PurchaseAmount -
|
|
|
|
orderCost.DeliveryExpressFreight -
|
|
|
|
orderCost.PlatformCommissionAmount;
|
|
|
|
orderCost.IsManualEdited = true;
|
|
|
|
updateOrderCost = fsql.Update<OrderCost>().SetSource(orderCost);
|
|
|
|
}
|
|
|
|
|
|
|
|
fsql.Transaction(() =>
|
|
|
|
{
|
|
|
|
orderUpdate?.ExecuteAffrows();
|
|
|
|
insertOrderCost?.ExecuteAffrows();
|
|
|
|
updateOrderCost?.ExecuteAffrows();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 刷单计算成本
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sdCalculationCostRequest"></param>
|
|
|
|
/// <exception cref="BusinessException"></exception>
|
|
|
|
public void SDCalculationCost(SDCalculationCostRequest sdCalculationCostRequest)
|
|
|
|
{
|
|
|
|
var dbOrder = fsql.Select<Order>(sdCalculationCostRequest.OrderId).ToOne();
|
|
|
|
if (dbOrder == null)
|
|
|
|
throw new BusinessException($"订单号{sdCalculationCostRequest.OrderId}不存在");
|
|
|
|
|
|
|
|
//修改平台订单备注
|
|
|
|
var relayAPIHost = GetPlatformRelayAPIHost(sdCalculationCostRequest.Platform);
|
|
|
|
var editApiResult = restApiService.SendRequest(relayAPIHost, "/Api/PlatformSDK/EditVenderRemark", new EditVenderRemarkRequest()
|
|
|
|
{
|
|
|
|
AppKey = sdCalculationCostRequest.AppKey,
|
|
|
|
AppSecret = sdCalculationCostRequest.AppSecret,
|
|
|
|
AppToken = sdCalculationCostRequest.AppToken,
|
|
|
|
Flag = sdCalculationCostRequest.Flag,
|
|
|
|
OrderId = sdCalculationCostRequest.OrderId,
|
|
|
|
Platform = sdCalculationCostRequest.Platform,
|
|
|
|
VenderRemark = sdCalculationCostRequest.VenderRemark
|
|
|
|
}, null, HttpMethod.Post);
|
|
|
|
if (editApiResult.StatusCode != System.Net.HttpStatusCode.OK)
|
|
|
|
throw new BusinessException($"修改商家备注失败 {editApiResult.Content}") { Code = (int)editApiResult.StatusCode };
|
|
|
|
var editResponse = JsonConvert.DeserializeObject<ApiResponse>(editApiResult.Content);
|
|
|
|
if (!editResponse.Success)
|
|
|
|
throw new BusinessException(editResponse.Msg);
|
|
|
|
|
|
|
|
IUpdate<Order> orderUpdate = null;
|
|
|
|
IUpdate<OrderCost> updateOrderCost = null;
|
|
|
|
IInsert<OrderCost> insertOrderCost = null;
|
|
|
|
|
|
|
|
orderUpdate = fsql.Update<Order>(sdCalculationCostRequest.OrderId).Set(o => o.SDType, sdCalculationCostRequest.SDType)
|
|
|
|
.Set(o => o.Flag, sdCalculationCostRequest.Flag)
|
|
|
|
.Set(o => o.VenderRemark, sdCalculationCostRequest.VenderRemark);
|
|
|
|
if (sdCalculationCostRequest.IsSetStorageType)
|
|
|
|
orderUpdate = orderUpdate.Set(o => o.StorageType, Enums.StorageType.SD);
|
|
|
|
|
|
|
|
var orderCost = fsql.Select<OrderCost>(sdCalculationCostRequest.OrderId).ToOne();
|
|
|
|
if (orderCost == null)
|
|
|
|
{
|
|
|
|
orderCost = new OrderCost()
|
|
|
|
{
|
|
|
|
OrderId = sdCalculationCostRequest.OrderId,
|
|
|
|
PlatformCommissionRatio = 0.05M,
|
|
|
|
PreferentialAmount = dbOrder.PreferentialAmount,
|
|
|
|
Profit = 0,
|
|
|
|
DeliveryExpressFreight = sdCalculationCostRequest.DeliveryExpressFreight,
|
|
|
|
CreateTime = DateTime.Now,
|
|
|
|
IsManualEdited = true,
|
|
|
|
SDCommissionAmount = sdCalculationCostRequest.SDCommissionAmount
|
|
|
|
};
|
|
|
|
orderCost.PlatformCommissionAmount = dbOrder.OrderSellerPrice * orderCost.PlatformCommissionRatio;
|
|
|
|
orderCost.Profit = (orderCost.SDCommissionAmount + orderCost.DeliveryExpressFreight + orderCost.PlatformCommissionAmount) * -1;
|
|
|
|
insertOrderCost = fsql.Insert(orderCost);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
orderCost.SDCommissionAmount = sdCalculationCostRequest.SDCommissionAmount;
|
|
|
|
orderCost.DeliveryExpressFreight = sdCalculationCostRequest.DeliveryExpressFreight;
|
|
|
|
orderCost.Profit = (orderCost.SDCommissionAmount + orderCost.DeliveryExpressFreight + orderCost.PlatformCommissionAmount) * -1;
|
|
|
|
orderCost.IsManualEdited = true;
|
|
|
|
updateOrderCost = fsql.Update<OrderCost>().SetSource(orderCost);
|
|
|
|
}
|
|
|
|
|
|
|
|
fsql.Transaction(() =>
|
|
|
|
{
|
|
|
|
orderUpdate?.ExecuteAffrows();
|
|
|
|
updateOrderCost?.ExecuteAffrows();
|
|
|
|
insertOrderCost?.ExecuteAffrows();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SyncOrder(long shopId, string orderId)
|
|
|
|
{
|
|
|
|
#region 获取店铺信息;
|
|
|
|
var shopResult = restApiService.SendRequest(globalConfig.MdsApi, "/TaskList/Shop/GetShopsByShopId", $"shopId={shopId}", mdsApiHeader, HttpMethod.Get, enableRandomTimeStamp: true);
|
|
|
|
if (shopResult.StatusCode != System.Net.HttpStatusCode.OK)
|
|
|
|
throw new Exception($"SyncOrder 获取店铺信息失败 shopId:{shopId} httpCode:{shopResult.StatusCode} httpContent:{shopResult.Content}");
|
|
|
|
|
|
|
|
var shopResponseJToken = JToken.Parse(shopResult.Content);
|
|
|
|
if (shopResponseJToken.Value<bool>("Success") != true)
|
|
|
|
throw new Exception($"SyncOrder 获取店铺信息失败 shopId:{shopId} ErrorMsg:{shopResponseJToken.Value<string>("Msg")}");
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
var shopJToken = shopResponseJToken["Data"].FirstOrDefault();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var appKey = shopJToken.Value<string>("AppKey");
|
|
|
|
var appSecret = shopJToken.Value<string>("AppSecret");
|
|
|
|
var appToken = shopJToken.Value<string>("AppToken");
|
|
|
|
var platformId = shopJToken.Value<int?>("PlatformId");
|
|
|
|
var shopType = shopJToken.Value<string>("ShopType");
|
|
|
|
if (string.IsNullOrEmpty(appKey) || string.IsNullOrEmpty(appSecret) || string.IsNullOrEmpty(appToken) || platformId == null)
|
|
|
|
throw new Exception("缺少店铺必要信息");
|
|
|
|
|
|
|
|
var platform = (Enums.Platform)platformId;
|
|
|
|
|
|
|
|
if (!syncOrderMethodDic.ContainsKey(platform))
|
|
|
|
throw new Exception("不支持的平台");
|
|
|
|
|
|
|
|
var relayAPIHost = GetPlatformRelayAPIHost((Enums.Platform)platformId);
|
|
|
|
var orderListApiResult = restApiService.SendRequest(relayAPIHost, "api/PlatformSDK/GetOrderList", new SearchPlatformOrderRequest()
|
|
|
|
{
|
|
|
|
StartDate = DateTime.Now.AddHours(-3),
|
|
|
|
EndDate = DateTime.Now,
|
|
|
|
AppKey = appKey,
|
|
|
|
AppSecret = appSecret,
|
|
|
|
AppToken = appToken,
|
|
|
|
PageIndex = 1,
|
|
|
|
PageSize = 100,
|
|
|
|
Platform = platform,
|
|
|
|
JDColType = string.IsNullOrEmpty(shopType) ? "0" : shopType,
|
|
|
|
SaveResponseLog = true,
|
|
|
|
OrderId = orderId
|
|
|
|
}, null, HttpMethod.Post);
|
|
|
|
if (orderListApiResult.StatusCode != System.Net.HttpStatusCode.OK)
|
|
|
|
throw new Exception($"获取订单失败 {orderListApiResult.Content}");
|
|
|
|
|
|
|
|
var orderListResponse = JsonConvert.DeserializeObject<ApiResponse<JArray>>(orderListApiResult.Content);
|
|
|
|
if (!orderListResponse.Success)
|
|
|
|
throw new Exception($"获取订单失败 {orderListApiResult.Content}");
|
|
|
|
|
|
|
|
if (orderListResponse.Data == null || orderListResponse.Data.Count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
syncOrderMethodDic[platform](orderListResponse.Data, shopId, relayAPIHost, appKey, appSecret, appToken);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
var shopData = JsonConvert.SerializeObject(shopJToken);
|
|
|
|
logger.Error(ex, $"SyncOrder ShopData:{shopData}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SyncJDOrder(JArray orderTokenJArray, long shopId, string relayAPIHost, string appKey, string appSecret, string appToken)
|
|
|
|
{
|
|
|
|
var orderTokenList = orderTokenJArray.Where(o => o.Value<decimal>("orderTotalPrice") != 0);
|
|
|
|
|
|
|
|
var interfaceOrderIdList = orderTokenList.Select(orderJToken => orderJToken.Value<string>("orderId"));
|
|
|
|
var dbOrderList = fsql.Select<Order>().Where(o => interfaceOrderIdList.Contains(o.Id)).ToList(o => new Order()
|
|
|
|
{
|
|
|
|
Id = o.Id,
|
|
|
|
OrderState = o.OrderState,
|
|
|
|
StorageType = o.StorageType
|
|
|
|
}); //数据库订单
|
|
|
|
|
|
|
|
var dbOrderConsigneeList = fsql.Select<OrderConsignee>().Where(oc => interfaceOrderIdList.Contains(oc.OrderId)).ToList(); //数据库订单收货信息
|
|
|
|
var dbOrderCostList = fsql.Select<OrderCost>().Where(oc => interfaceOrderIdList.Contains(oc.OrderId)).ToList(); //数据库订单成本信息
|
|
|
|
var dbOrderCouponList = fsql.Select<OrderCoupon>().Where(oc => interfaceOrderIdList.Contains(oc.OrderId)).ToList(); //数据库订单优惠信息
|
|
|
|
|
|
|
|
var orderSkuIds = new List<string>();
|
|
|
|
foreach (var orderJToken in orderTokenList)
|
|
|
|
{
|
|
|
|
var itemInfoList = orderJToken["itemInfoList"].Where(skuJToken => skuJToken.Value<decimal>("jdPrice") != 0M).Select(skuJToken => skuJToken.Value<string>("skuId"));
|
|
|
|
foreach (var skuId in itemInfoList)
|
|
|
|
{
|
|
|
|
if (!orderSkuIds.Contains(skuId))
|
|
|
|
orderSkuIds.Add(skuId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var dbPurchaseOrderList = fsql.Select<PurchaseOrder>().Where(po => po.RemainingQuantity != 0 && orderSkuIds.Contains(po.SkuId)).ToList(); //数据库采购单
|
|
|
|
orderSkuIds.Clear();
|
|
|
|
|
|
|
|
#region 数据库操作
|
|
|
|
List<Order> insertOrderList = new List<Order>();
|
|
|
|
List<OrderConsignee> insertOrderConsigneeList = new List<OrderConsignee>();
|
|
|
|
List<OrderCost> insertOrderCostList = new List<OrderCost>();
|
|
|
|
List<OrderCostDetail> insertOrderCostDetailList = new List<OrderCostDetail>();
|
|
|
|
List<OrderSku> insertOrderSkuList = new List<OrderSku>();
|
|
|
|
List<OrderCoupon> insertOrderCouponList = new List<OrderCoupon>();
|
|
|
|
|
|
|
|
IList<IUpdate<Order>> updateOrderList = new List<IUpdate<Order>>();
|
|
|
|
IList<IUpdate<PurchaseOrder>> updatePurchaseOrderList = new List<IUpdate<PurchaseOrder>>();
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
foreach (var orderJToken in orderTokenList)
|
|
|
|
{
|
|
|
|
var orderId = orderJToken.Value<string>("orderId");
|
|
|
|
var dbOrder = dbOrderList.FirstOrDefault(o => o.Id == orderId);
|
|
|
|
var isNewOrder = dbOrder == null;
|
|
|
|
|
|
|
|
#region 订单基本信息
|
|
|
|
var buyerRemark = orderJToken.Value<string>("orderRemark");
|
|
|
|
var venderRemark = orderJToken.Value<string>("venderRemark");
|
|
|
|
var modifyTime = orderJToken.Value<DateTime?>("modified");
|
|
|
|
var endTime = orderJToken.Value<DateTime?>("orderEndTime");
|
|
|
|
var waybillNo = orderJToken.Value<string>("waybill");
|
|
|
|
if (dbOrder == null)
|
|
|
|
{
|
|
|
|
dbOrder = new Order()
|
|
|
|
{
|
|
|
|
Id = orderId,
|
|
|
|
BuyerRemark = buyerRemark,
|
|
|
|
VenderRemark = venderRemark,
|
|
|
|
FreightPrice = orderJToken.Value<decimal>("freightPrice"),
|
|
|
|
EndTime = endTime,
|
|
|
|
StartTime = orderJToken.Value<DateTime>("orderStartTime"),
|
|
|
|
ModifyTime = modifyTime,
|
|
|
|
OrderPayment = orderJToken.Value<decimal>("orderPayment"),
|
|
|
|
OrderSellerPrice = orderJToken.Value<decimal>("orderSellerPrice"),
|
|
|
|
OrderTotalPrice = orderJToken.Value<decimal>("orderTotalPrice"),
|
|
|
|
OrderType = (Enums.OrderType)orderJToken.Value<int>("orderType"),
|
|
|
|
Platform = Enums.Platform.京东,
|
|
|
|
ShopId = shopId,
|
|
|
|
//VenderId = orderJToken.Value<long>("venderId"),
|
|
|
|
WaybillNo = waybillNo,
|
|
|
|
StoreOrder = orderJToken.Value<string>("storeOrder") ?? string.Empty,
|
|
|
|
StoreId = orderJToken.Value<string>("storeId")
|
|
|
|
};
|
|
|
|
|
|
|
|
if (dbOrder.StoreOrder.Contains("京仓"))
|
|
|
|
dbOrder.StorageType = Enums.StorageType.京仓;
|
|
|
|
else if (dbOrder.StoreOrder.Contains("云仓"))
|
|
|
|
dbOrder.StorageType = Enums.StorageType.云仓;
|
|
|
|
|
|
|
|
var payType = orderJToken.Value<string>("payType");
|
|
|
|
if (payType.Contains("-"))
|
|
|
|
dbOrder.PayType = (Enums.PayType)Convert.ToInt32(payType.Substring(0, 1));
|
|
|
|
|
|
|
|
insertOrderList.Add(dbOrder);
|
|
|
|
|
|
|
|
#region OrderSku
|
|
|
|
var orderSkuList = orderJToken["itemInfoList"].Where(skuJToken => skuJToken.Value<decimal>("jdPrice") != 0M).Select(skuToken => new OrderSku()
|
|
|
|
{
|
|
|
|
Id = idGenerator.NewLong(),
|
|
|
|
ItemTotal = skuToken.Value<int>("itemTotal"),
|
|
|
|
Price = skuToken.Value<decimal>("jdPrice"),
|
|
|
|
ProductId = skuToken.Value<string>("wareId"),
|
|
|
|
Title = skuToken.Value<string>("skuName").SimplifySkuName(),
|
|
|
|
ProductNo = skuToken.Value<string>("productNo"),
|
|
|
|
CreateTime = DateTime.Now,
|
|
|
|
OrderId = orderId,
|
|
|
|
SkuId = skuToken.Value<string>("skuId")
|
|
|
|
}).ToList();
|
|
|
|
insertOrderSkuList.AddRange(orderSkuList);
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 收货人信息
|
|
|
|
if (dbOrderConsigneeList.FirstOrDefault(oc => oc.OrderId == orderId) == null)
|
|
|
|
{
|
|
|
|
var orderConsignee = new OrderConsignee()
|
|
|
|
{
|
|
|
|
OrderId = orderId,
|
|
|
|
//Address = orderJToken["consigneeInfo"].Value<string>("fullAddress"),
|
|
|
|
//ContactName = orderJToken["consigneeInfo"].Value<string>("fullname"),
|
|
|
|
//Mobile = orderJToken["consigneeInfo"].Value<string>("mobile"),
|
|
|
|
//TelePhone = orderJToken["consigneeInfo"].Value<string>("telephone"),
|
|
|
|
City = orderJToken["consigneeInfo"].Value<string>("city"),
|
|
|
|
Province = orderJToken["consigneeInfo"].Value<string>("province"),
|
|
|
|
County = orderJToken["consigneeInfo"].Value<string>("county"),
|
|
|
|
Town = orderJToken["consigneeInfo"].Value<string>("town"),
|
|
|
|
IsDecode = false,
|
|
|
|
CreateTime = DateTime.Now
|
|
|
|
};
|
|
|
|
insertOrderConsigneeList.Add(orderConsignee);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 订单优惠
|
|
|
|
if (!dbOrderCouponList.Any(oc => oc.OrderId == orderId))
|
|
|
|
{
|
|
|
|
var orderCouponJArray = (JArray)orderJToken["couponDetailList"];
|
|
|
|
if (orderCouponJArray.HasValues)
|
|
|
|
{
|
|
|
|
foreach (var orderCouponJToken in orderCouponJArray)
|
|
|
|
{
|
|
|
|
var couponType = orderCouponJToken.Value<string>("couponType");
|
|
|
|
if (string.IsNullOrEmpty(couponType))
|
|
|
|
continue;
|
|
|
|
dbOrder.PreferentialAmount += orderCouponJToken.Value<decimal>("couponPrice");
|
|
|
|
insertOrderCouponList.Add(new OrderCoupon()
|
|
|
|
{
|
|
|
|
Id = idGenerator.NewLong(),
|
|
|
|
SkuId = orderCouponJToken.Value<string>("skuId"),
|
|
|
|
OrderId = orderId,
|
|
|
|
CreateTime = DateTime.Now,
|
|
|
|
CouponType = couponType,
|
|
|
|
CouponPrice = orderCouponJToken.Value<decimal>("couponPrice")
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 订单状态转换
|
|
|
|
var jdOrderState = orderJToken.Value<string>("orderState");
|
|
|
|
Enums.OrderState? orderState = null;
|
|
|
|
|
|
|
|
#region SOP状态翻译
|
|
|
|
if (jdOrderState.Equals("NOT_PAY")) //未付款
|
|
|
|
{
|
|
|
|
orderState = Enums.OrderState.待付款;
|
|
|
|
}
|
|
|
|
else if (jdOrderState.Equals("WAIT_SELLER_STOCK_OUT")) //等待出库
|
|
|
|
{
|
|
|
|
orderState = Enums.OrderState.等待采购;
|
|
|
|
if (dbOrder.StorageType != null)
|
|
|
|
orderState = Enums.OrderState.待出库;
|
|
|
|
}
|
|
|
|
else if (jdOrderState.Equals("WAIT_GOODS_RECEIVE_CONFIRM"))
|
|
|
|
{
|
|
|
|
orderState = Enums.OrderState.待收货;
|
|
|
|
}
|
|
|
|
else if (jdOrderState.Equals("FINISHED_L"))
|
|
|
|
{
|
|
|
|
orderState = Enums.OrderState.已完成;
|
|
|
|
}
|
|
|
|
else if (jdOrderState.Equals("LOCKED"))
|
|
|
|
{
|
|
|
|
orderState = Enums.OrderState.锁定;
|
|
|
|
}
|
|
|
|
else if (jdOrderState.Equals("TRADE_CANCELED"))
|
|
|
|
{
|
|
|
|
orderState = Enums.OrderState.已取消;
|
|
|
|
}
|
|
|
|
else if (jdOrderState.Equals("POP_ORDER_PAUSE") || jdOrderState.Equals("PAUSE"))
|
|
|
|
{
|
|
|
|
orderState = Enums.OrderState.暂停;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region FBP状态翻译
|
|
|
|
else if (jdOrderState.Equals("DengDaiDaYin") || jdOrderState.Equals("DengDaiChuKu") || jdOrderState.Equals("DengDaiDaBao") || jdOrderState.Equals("DengDaiFaHuo"))
|
|
|
|
{
|
|
|
|
orderState = Enums.OrderState.待出库;
|
|
|
|
}
|
|
|
|
else if (jdOrderState.Equals("DengDaiQueRenShouHuo"))
|
|
|
|
{
|
|
|
|
orderState = Enums.OrderState.待收货;
|
|
|
|
}
|
|
|
|
else if (jdOrderState.Equals("WanCheng"))
|
|
|
|
{
|
|
|
|
orderState = Enums.OrderState.已完成;
|
|
|
|
}
|
|
|
|
else if (jdOrderState.Equals("SuoDing") || jdOrderState.Equals("LOCKED"))
|
|
|
|
{
|
|
|
|
orderState = Enums.OrderState.锁定;
|
|
|
|
}
|
|
|
|
else if (jdOrderState.Equals("TRADE_CANCELED"))
|
|
|
|
{
|
|
|
|
orderState = Enums.OrderState.已取消;
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 扣减库存, 计算成本
|
|
|
|
if (dbOrder.StorageType != null &&
|
|
|
|
dbOrder.StorageType != Enums.StorageType.SD &&
|
|
|
|
orderState != null &&
|
|
|
|
orderState != Enums.OrderState.待付款 &&
|
|
|
|
orderState != Enums.OrderState.已取消)
|
|
|
|
{
|
|
|
|
var orderCost = dbOrderCostList.FirstOrDefault(oc => oc.OrderId == dbOrder.Id);
|
|
|
|
if (orderCost == null)
|
|
|
|
{
|
|
|
|
//再查询一次数据库,以防同步开始执行后被人为操作扣减库存,造成重复扣减库存
|
|
|
|
if (!fsql.Select<OrderCost>(dbOrder.Id).Any())
|
|
|
|
{
|
|
|
|
var orderSkuJArray = orderJToken["itemInfoList"].Where(skuJToken => skuJToken.Value<decimal>("jdPrice") != 0M);
|
|
|
|
if (orderSkuJArray != null && orderSkuJArray.Count() > 0)
|
|
|
|
{
|
|
|
|
var orderCostPurchaseAmount = 0M;
|
|
|
|
var orderDeliveryExpressFreight = 0M; //发货总运费,sku购买数量第二个开始半价
|
|
|
|
|
|
|
|
#region 扣减库存
|
|
|
|
foreach (var orderSkuJToken in orderSkuJArray)
|
|
|
|
{
|
|
|
|
var orderSkuId = orderSkuJToken.Value<string>("skuId");
|
|
|
|
var itemTotal = orderSkuJToken.Value<int>("itemTotal"); //sku购买数量
|
|
|
|
var isReduceMultiTimes = false; //是否多次扣减库存
|
|
|
|
while (itemTotal != 0)
|
|
|
|
{
|
|
|
|
var purchaseOrder = dbPurchaseOrderList.FirstOrDefault(po => po.StorageType == dbOrder.StorageType &&
|
|
|
|
po.RemainingQuantity != 0 &&
|
|
|
|
po.SkuId == orderSkuId);
|
|
|
|
if (purchaseOrder == null)
|
|
|
|
break; //没有库存了
|
|
|
|
|
|
|
|
//本次扣减量
|
|
|
|
var deductionQuantity = purchaseOrder.RemainingQuantity >= itemTotal ? itemTotal : purchaseOrder.RemainingQuantity;
|
|
|
|
//本次扣减量的采购成本
|
|
|
|
var currentPurchaseAmount = purchaseOrder.UnitCost * deductionQuantity;
|
|
|
|
//本次扣减量的发货运费
|
|
|
|
var currentSkuDeliveryFreight = isReduceMultiTimes ?
|
|
|
|
(purchaseOrder.SingleDeliveryFreight / 2 * deductionQuantity) :
|
|
|
|
(purchaseOrder.SingleDeliveryFreight + purchaseOrder.SingleDeliveryFreight / 2 * (deductionQuantity - 1));
|
|
|
|
|
|
|
|
purchaseOrder.RemainingQuantity -= deductionQuantity;
|
|
|
|
itemTotal -= deductionQuantity;
|
|
|
|
|
|
|
|
//累计采购成本
|
|
|
|
orderCostPurchaseAmount += currentPurchaseAmount;
|
|
|
|
//累计发货运费(销售运费)
|
|
|
|
orderDeliveryExpressFreight += currentSkuDeliveryFreight;
|
|
|
|
isReduceMultiTimes = true;
|
|
|
|
|
|
|
|
var updateSql = fsql.Update<PurchaseOrder>(purchaseOrder.Id).Set(po => po.RemainingQuantity - deductionQuantity);
|
|
|
|
updatePurchaseOrderList.Add(updateSql);
|
|
|
|
|
|
|
|
var orderCostDetail = new OrderCostDetail()
|
|
|
|
{
|
|
|
|
Id = idGenerator.NewLong(),
|
|
|
|
OrderId = orderId,
|
|
|
|
ProductId = orderSkuJToken.Value<string>("wareId"),
|
|
|
|
SkuId = orderSkuId,
|
|
|
|
CreateTime = DateTime.Now,
|
|
|
|
PurchaseOrderPKId = purchaseOrder.Id,
|
|
|
|
UnitCost = purchaseOrder.UnitCost,
|
|
|
|
DeductionQuantity = deductionQuantity,
|
|
|
|
DeliveryExpressFreight = currentSkuDeliveryFreight,
|
|
|
|
TotalCost = currentPurchaseAmount,
|
|
|
|
ConsumableAmount = purchaseOrder.SingleConsumableAmount * deductionQuantity,
|
|
|
|
FirstFreight = purchaseOrder.SingleFirstFreight * deductionQuantity,
|
|
|
|
OperationAmount = purchaseOrder.SingleOperationAmount * deductionQuantity,
|
|
|
|
PurchaseFreight = purchaseOrder.SingleFreight * deductionQuantity,
|
|
|
|
SkuAmount = purchaseOrder.SingleSkuAmount * deductionQuantity,
|
|
|
|
StorageAmount = purchaseOrder.SingleStorageAmount * deductionQuantity
|
|
|
|
};
|
|
|
|
insertOrderCostDetailList.Add(orderCostDetail);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 计算成本
|
|
|
|
orderCost = new OrderCost()
|
|
|
|
{
|
|
|
|
OrderId = orderId,
|
|
|
|
PlatformCommissionRatio = 0.05M,
|
|
|
|
PreferentialAmount = dbOrder.PreferentialAmount,
|
|
|
|
Profit = 0,
|
|
|
|
PurchaseAmount = orderCostPurchaseAmount,
|
|
|
|
DeliveryExpressFreight = orderDeliveryExpressFreight,
|
|
|
|
CreateTime = DateTime.Now
|
|
|
|
};
|
|
|
|
orderCost.PlatformCommissionAmount = dbOrder.OrderSellerPrice * orderCost.PlatformCommissionRatio;
|
|
|
|
orderCost.Profit = dbOrder.OrderSellerPrice +
|
|
|
|
dbOrder.FreightPrice -
|
|
|
|
orderCost.PurchaseAmount -
|
|
|
|
orderCost.DeliveryExpressFreight -
|
|
|
|
orderCost.PlatformCommissionAmount;
|
|
|
|
insertOrderCostList.Add(orderCost);
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 检查订单信息是否变化
|
|
|
|
if (isNewOrder)
|
|
|
|
{
|
|
|
|
dbOrder.OrderState = orderState;
|
|
|
|
}
|
|
|
|
else if ((orderState != null && orderState != dbOrder.OrderState) ||
|
|
|
|
buyerRemark != dbOrder.BuyerRemark ||
|
|
|
|
venderRemark != dbOrder.VenderRemark ||
|
|
|
|
modifyTime != dbOrder.ModifyTime ||
|
|
|
|
endTime != dbOrder.EndTime ||
|
|
|
|
waybillNo != dbOrder.WaybillNo)
|
|
|
|
{
|
|
|
|
var updateSql = fsql.Update<Order>(orderId).SetIf(orderState != null && orderState != dbOrder.OrderState, o => o.OrderState, orderState)
|
|
|
|
.SetIf(buyerRemark != dbOrder.BuyerRemark, o => o.BuyerRemark, buyerRemark)
|
|
|
|
.SetIf(venderRemark != dbOrder.VenderRemark, o => o.VenderRemark, venderRemark)
|
|
|
|
.SetIf(modifyTime != dbOrder.ModifyTime, o => o.ModifyTime, modifyTime)
|
|
|
|
.SetIf(endTime != dbOrder.EndTime, o => o.EndTime, modifyTime)
|
|
|
|
.SetIf(waybillNo != dbOrder.WaybillNo, o => o.WaybillNo, waybillNo);
|
|
|
|
updateOrderList.Add(updateSql);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
|
|
|
|
#region 补齐sku logo
|
|
|
|
if (insertOrderSkuList.Count() > 0)
|
|
|
|
{
|
|
|
|
foreach (var orderSku in insertOrderSkuList)
|
|
|
|
{
|
|
|
|
if (!orderSkuIds.Contains(orderSku.SkuId))
|
|
|
|
orderSkuIds.Add(orderSku.SkuId);
|
|
|
|
}
|
|
|
|
|
|
|
|
var orderSkuRequestCount = (orderSkuIds.Count() - 1) / 50 + 1; //sku最多一次请求50条
|
|
|
|
for (var i = 0; i < orderSkuRequestCount; i++)
|
|
|
|
{
|
|
|
|
var orderSkuIdString = string.Join(",", orderSkuIds.Skip(i * 50).Take(50));
|
|
|
|
var skuHttpResult = restApiService.SendRequest(relayAPIHost, "Api/PlatformSDK/GetSimpleProductSkuList", new SearchProductSkuRequest()
|
|
|
|
{
|
|
|
|
AppKey = appKey,
|
|
|
|
AppSecret = appSecret,
|
|
|
|
AppToken = appToken,
|
|
|
|
Platform = Enums.Platform.京东,
|
|
|
|
Sku = orderSkuIdString
|
|
|
|
}, null, HttpMethod.Post);
|
|
|
|
if (skuHttpResult.StatusCode != System.Net.HttpStatusCode.OK)
|
|
|
|
continue;
|
|
|
|
var skuResponse = JsonConvert.DeserializeObject<ApiResponse<IList<SimpleProductSkuResponse>>>(skuHttpResult.Content);
|
|
|
|
if (!skuResponse.Success)
|
|
|
|
continue;
|
|
|
|
foreach (var sku in skuResponse.Data)
|
|
|
|
{
|
|
|
|
var insertSkus = insertOrderSkuList.Where(orderSku => orderSku.SkuId == sku.Id);
|
|
|
|
foreach (var insertSku in insertSkus) { insertSku.Logo = sku.Logo; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
fsql.Transaction(() =>
|
|
|
|
{
|
|
|
|
if (insertOrderList.Count() > 0)
|
|
|
|
fsql.Insert(insertOrderList).ExecuteAffrows();
|
|
|
|
if (insertOrderSkuList.Count() > 0)
|
|
|
|
fsql.Insert(insertOrderSkuList).ExecuteAffrows();
|
|
|
|
if (insertOrderConsigneeList.Count() > 0)
|
|
|
|
fsql.Insert(insertOrderConsigneeList).ExecuteAffrows();
|
|
|
|
if (insertOrderCostList.Count() > 0)
|
|
|
|
fsql.Insert(insertOrderCostList).ExecuteAffrows();
|
|
|
|
if (insertOrderCostDetailList.Count() > 0)
|
|
|
|
fsql.Insert(insertOrderCostDetailList).ExecuteAffrows();
|
|
|
|
if (insertOrderCouponList.Count() > 0)
|
|
|
|
fsql.Insert(insertOrderCouponList).ExecuteAffrows();
|
|
|
|
|
|
|
|
if (updatePurchaseOrderList.Count() > 0)
|
|
|
|
{
|
|
|
|
foreach (var update in updatePurchaseOrderList)
|
|
|
|
update.ExecuteAffrows();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateOrderList.Count() > 0)
|
|
|
|
{
|
|
|
|
foreach (var update in updateOrderList)
|
|
|
|
update.ExecuteAffrows();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|