|
|
|
using BBWYB.Common.Extensions;
|
|
|
|
using BBWYB.Common.Log;
|
|
|
|
using BBWYB.Common.Models;
|
|
|
|
using BBWYB.Server.Model;
|
|
|
|
using BBWYB.Server.Model.Db;
|
|
|
|
using BBWYB.Server.Model.Dto;
|
|
|
|
using FreeSql;
|
|
|
|
using SDKAdapter;
|
|
|
|
using SDKAdapter.OperationPlatform.Client;
|
|
|
|
using SDKAdapter.OperationPlatform.Models;
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
using Yitter.IdGenerator;
|
|
|
|
|
|
|
|
namespace BBWYB.Server.Business
|
|
|
|
{
|
|
|
|
public class OrderBusiness : BaseBusiness, IDenpendency
|
|
|
|
{
|
|
|
|
private OP_PlatformClientFactory opPlatformClientFactory;
|
|
|
|
|
|
|
|
public OrderBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator, OP_PlatformClientFactory opPlatformClientFactory) : base(fsql, nLogManager, idGenerator)
|
|
|
|
{
|
|
|
|
this.opPlatformClientFactory = opPlatformClientFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ISelect<Order, OrderConsignee, OrderCost> GetOrderListQueryConditions(QueryOrderRequest request)
|
|
|
|
{
|
|
|
|
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(request.OrderId))
|
|
|
|
{
|
|
|
|
select = select.Where((o, ocs, oct) => o.Id == request.OrderId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(request.Sku) || !string.IsNullOrEmpty(request.ProductId))
|
|
|
|
{
|
|
|
|
var childSelect = fsql.Select<OrderSku>().As("osku")
|
|
|
|
.WhereIf(!string.IsNullOrEmpty(request.Sku), osku => osku.SkuId == request.Sku)
|
|
|
|
.WhereIf(!string.IsNullOrEmpty(request.ProductId), osku => osku.ProductId == request.ProductId);
|
|
|
|
select = select.Where((o, ocs, oct) => childSelect.Where(osku => osku.OrderId == o.Id).Any());
|
|
|
|
}
|
|
|
|
|
|
|
|
select = select.WhereIf(request.OrderState != null, (o, ocs, oct) => o.OrderState == request.OrderState)
|
|
|
|
.WhereIf(request.StartDate != null, (o, ocs, oct) => o.StartTime >= request.StartDate)
|
|
|
|
.WhereIf(request.EndDate != null, (o, ocs, oct) => o.StartTime <= request.EndDate)
|
|
|
|
.WhereIf(!string.IsNullOrEmpty(request.ClientOrderId), (o, ocs, oct) => o.ClientOrderId == request.ClientOrderId)
|
|
|
|
.WhereIf(!string.IsNullOrEmpty(request.SourceShopName), (o, ocs, oct) => o.SourceShopName == request.SourceShopName)
|
|
|
|
.WhereIf(!string.IsNullOrEmpty(request.SourceSku), (o, ocs, oct) => o.SourceSku == request.SourceSku);
|
|
|
|
}
|
|
|
|
|
|
|
|
select = select.WhereIf(request.ShopId != null, (o, ocs, oct) => o.ShopId == request.ShopId);
|
|
|
|
|
|
|
|
//select = select.Where((o, ocs, oct) => o.ShopId == searchOrderRequest.ShopId);
|
|
|
|
|
|
|
|
return select;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Expression<Func<Order, OrderConsignee, OrderCost, Order>> GetOrderListField()
|
|
|
|
{
|
|
|
|
return (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,
|
|
|
|
VenderRemark = o.VenderRemark,
|
|
|
|
PurchaseRemark = o.PurchaseRemark,
|
|
|
|
WaybillNo = o.WaybillNo,
|
|
|
|
SellerPreferentialAmount = o.SellerPreferentialAmount,
|
|
|
|
PreferentialAmount = o.PreferentialAmount,
|
|
|
|
ClientOrderId = o.ClientOrderId,
|
|
|
|
SourceShopName = o.SourceShopName,
|
|
|
|
SourceSku = o.SourceSku,
|
|
|
|
ExpressName = o.ExpressName,
|
|
|
|
IsPurchased = o.IsPurchased,
|
|
|
|
BuyerAccount = o.BuyerAccount,
|
|
|
|
|
|
|
|
ContactName = ocs.ContactName,
|
|
|
|
Address = ocs.Address,
|
|
|
|
Province = ocs.Province,
|
|
|
|
County = ocs.County,
|
|
|
|
Town = ocs.Town,
|
|
|
|
City = ocs.City,
|
|
|
|
Mobile = ocs.Mobile,
|
|
|
|
TelePhone = ocs.TelePhone,
|
|
|
|
|
|
|
|
DeliveryExpressFreight = oct.DeliveryExpressFreight,
|
|
|
|
PlatformCommissionAmount = oct.PlatformCommissionAmount,
|
|
|
|
PlatformCommissionRatio = oct.PlatformCommissionRatio,
|
|
|
|
Profit = oct.Profit,
|
|
|
|
PurchaseAmount = oct.PurchaseAmount,
|
|
|
|
IsManualEdited = oct.IsManualEdited
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public OrderListResponse GetOrderList(QueryOrderRequest request)
|
|
|
|
{
|
|
|
|
if (request.EndDate != null)
|
|
|
|
request.EndDate = request.EndDate.Value.Date.AddDays(1).AddSeconds(-1);
|
|
|
|
|
|
|
|
var select = GetOrderListQueryConditions(request).OrderByDescending((o, ocs, oct) => o.StartTime)
|
|
|
|
.Count(out var total)
|
|
|
|
.Page(request.PageIndex, request.PageSize);
|
|
|
|
|
|
|
|
var orderSourceList = select.ToList(GetOrderListField());
|
|
|
|
var orderList = orderSourceList.Map<IList<OrderResponse>>();
|
|
|
|
var orderIdList = orderList.Select(o => o.Id).ToList();
|
|
|
|
|
|
|
|
if (orderList.Count() > 0)
|
|
|
|
{
|
|
|
|
#region 处理Sku
|
|
|
|
var orderSkuList = fsql.Select<OrderSku>().Where(osku => osku.Price != 0 &&
|
|
|
|
orderIdList.Contains(osku.OrderId)).ToList<OrderSkuResponse>();
|
|
|
|
foreach (var order in orderList)
|
|
|
|
{
|
|
|
|
order.ItemList = orderSkuList.Where(osku => osku.OrderId == order.Id).ToList();
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 处理订单成本明细
|
|
|
|
var orderCostDetailList = fsql.Select<OrderCostDetail>().Where(ocd => orderIdList.Contains(ocd.OrderId) && ocd.IsEnabled == true).ToList<OrderCostDetailResponse>();
|
|
|
|
foreach (var order in orderList)
|
|
|
|
order.OrderCostDetailList = orderCostDetailList.Where(ocd => ocd.OrderId == order.Id).ToList();
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region 处理采购信息
|
|
|
|
var orderPurchaseInfoList = fsql.Select<OrderPurchaseInfo>().Where(op => orderIdList.Contains(op.OrderId)).ToList<OrderPurchaseInfoResponse>();
|
|
|
|
foreach (var order in orderList)
|
|
|
|
order.OrderPurchaseInfoList = orderPurchaseInfoList.Where(op => op.OrderId == order.Id).ToList();
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
return new OrderListResponse()
|
|
|
|
{
|
|
|
|
Count = total,
|
|
|
|
Items = orderList
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OutStock(OutStockRequest request)
|
|
|
|
{
|
|
|
|
var dbOrder = fsql.Select<Order>(request.OrderId).ToOne();
|
|
|
|
if (dbOrder == null)
|
|
|
|
throw new BusinessException($"订单{request.OrderId}不存在");
|
|
|
|
if (dbOrder.OrderState != Enums.OrderState.待出库)
|
|
|
|
throw new BusinessException($"订单{request.OrderId} 只有在待出库时才允许出库");
|
|
|
|
|
|
|
|
opPlatformClientFactory.GetClient((AdapterEnums.PlatformType)request.Platform)
|
|
|
|
.OutStock(new OP_OutStockRequest()
|
|
|
|
{
|
|
|
|
AppKey = request.AppKey,
|
|
|
|
AppSecret = request.AppSecret,
|
|
|
|
AppToken = request.AppToken,
|
|
|
|
ExpressId = request.ExpressId,
|
|
|
|
ExpressName = request.ExpressName,
|
|
|
|
OrderId = request.OrderId,
|
|
|
|
Platform = (AdapterEnums.PlatformType)request.Platform,
|
|
|
|
WayBillNo = request.WayBillNo
|
|
|
|
});
|
|
|
|
|
|
|
|
fsql.Update<Order>(request.OrderId).Set(o => o.OrderState, Enums.OrderState.待收货)
|
|
|
|
.Set(o => o.WaybillNo, request.WayBillNo)
|
|
|
|
.Set(o => o.ExpressName, request.ExpressName)
|
|
|
|
.ExecuteAffrows();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|