9 changed files with 348 additions and 191 deletions
@ -0,0 +1,25 @@ |
|||||
|
using BBWYB.Server.Business; |
||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
|
||||
|
namespace BBWYB.Server.API.Controllers |
||||
|
{ |
||||
|
|
||||
|
public class AggregationPurchaserController : BaseApiController |
||||
|
{ |
||||
|
private AggregionPurchaserBusiness aggregionPurchaserBusiness; |
||||
|
|
||||
|
public AggregationPurchaserController(IHttpContextAccessor httpContextAccessor, AggregionPurchaserBusiness aggregionPurchaserBusiness) : base(httpContextAccessor) |
||||
|
{ |
||||
|
this.aggregionPurchaserBusiness = aggregionPurchaserBusiness; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 采购商聚合
|
||||
|
/// </summary>
|
||||
|
[HttpPost] |
||||
|
public void AutoAggregion() |
||||
|
{ |
||||
|
aggregionPurchaserBusiness.AutoAggregion(); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,209 @@ |
|||||
|
using BBWYB.Common.Log; |
||||
|
using BBWYB.Common.Models; |
||||
|
using BBWYB.Server.Model; |
||||
|
using BBWYB.Server.Model.Db; |
||||
|
using FreeSql; |
||||
|
using Yitter.IdGenerator; |
||||
|
|
||||
|
namespace BBWYB.Server.Business |
||||
|
{ |
||||
|
public class AggregionPurchaserBusiness : BaseBusiness, IDenpendency |
||||
|
{ |
||||
|
private TaskSchedulerManager taskSchedulerManager; |
||||
|
public AggregionPurchaserBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator, TaskSchedulerManager taskSchedulerManager) : base(fsql, nLogManager, idGenerator) |
||||
|
{ |
||||
|
this.taskSchedulerManager = taskSchedulerManager; |
||||
|
} |
||||
|
|
||||
|
public void AutoAggregion() |
||||
|
{ |
||||
|
var startTime = DateTime.Now.Date.AddDays(-90); |
||||
|
//查询最近有采购的采购商列表
|
||||
|
var purchaserIdList = fsql.Select<OrderPurchaseInfo, Order>() |
||||
|
.InnerJoin((opi, o) => opi.OrderId == o.Id) |
||||
|
.Where((opi, o) => opi.IsEnabled == true && |
||||
|
o.OrderState != Enums.OrderState.已取消 && |
||||
|
o.StartTime >= startTime && |
||||
|
!string.IsNullOrEmpty(opi.PurchaserId)) |
||||
|
.Distinct() |
||||
|
.ToList((opi, o) => opi.PurchaserId); |
||||
|
Task.Factory.StartNew(() => AggregionByGroup(purchaserIdList), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.AggregationPurchaserTaskScheduler); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
private void AggregionByGroup(IList<string> purchaserIdList) |
||||
|
{ |
||||
|
var tempPurchaserIdList = new List<string>(); |
||||
|
for (var i = 0; i < purchaserIdList.Count(); i++) |
||||
|
{ |
||||
|
tempPurchaserIdList.Add(purchaserIdList[i]); |
||||
|
if (i != 0 && i % 30 == 0) |
||||
|
{ |
||||
|
Console.WriteLine($"聚合{i + 1}/{purchaserIdList.Count()}"); |
||||
|
Aggregion(tempPurchaserIdList); |
||||
|
tempPurchaserIdList.Clear(); |
||||
|
} |
||||
|
} |
||||
|
if (tempPurchaserIdList.Count() > 0) |
||||
|
{ |
||||
|
Console.WriteLine($"最后聚合"); |
||||
|
Aggregion(tempPurchaserIdList); |
||||
|
tempPurchaserIdList.Clear(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void Aggregion(IList<string> purchaserIdList) |
||||
|
{ |
||||
|
Thread.Sleep(2000); |
||||
|
|
||||
|
List<IUpdate<Purchaser>> updatePurchaserList = new List<IUpdate<Purchaser>>(); |
||||
|
|
||||
|
#region 查询SPU绑定数/SKU绑定数
|
||||
|
var bindList = fsql.Select<PurchaseSchemeProduct, PurchaseScheme>() |
||||
|
.InnerJoin((psp, psc) => psp.SkuPurchaseSchemeId == psc.Id) |
||||
|
.Where((psp, psc) => purchaserIdList.Contains(psp.PurchaserId)) |
||||
|
.GroupBy((psp, psc) => new { psp.PurchaserId, psp.ProductId, psp.SkuId }) |
||||
|
.ToList(g => new |
||||
|
{ |
||||
|
g.Key.PurchaserId, |
||||
|
g.Key.ProductId, |
||||
|
g.Key.SkuId |
||||
|
}); |
||||
|
#endregion
|
||||
|
|
||||
|
#region 查询SPU采购数/SKU采购数
|
||||
|
var purchasedList = fsql.Select<ProductSku, OrderPurchaseRelationInfo, OrderPurchaseInfo, Order>() |
||||
|
.InnerJoin((ps, ori, opi, o) => ps.Id == ori.BelongSkuId) |
||||
|
.InnerJoin((ps, ori, opi, o) => ori.PurchaseOrderId == opi.PurchaseOrderId && |
||||
|
ori.OrderId == opi.OrderId) |
||||
|
.InnerJoin((ps, ori, opi, o) => opi.OrderId == o.Id) |
||||
|
.Where((ps, ori, opi, o) => o.OrderState != Enums.OrderState.已取消 && |
||||
|
opi.IsEnabled == true && |
||||
|
purchaserIdList.Contains(opi.PurchaserId)) |
||||
|
.GroupBy((ps, ori, opi, o) => new { opi.PurchaserId, ps.Id, ps.ProductId }) |
||||
|
.ToList(g => new |
||||
|
{ |
||||
|
g.Key.PurchaserId, |
||||
|
g.Key.ProductId, |
||||
|
SkuId = g.Key.Id |
||||
|
}); |
||||
|
|
||||
|
#endregion
|
||||
|
|
||||
|
#region 查询订单数/最近采购时间
|
||||
|
var poList = fsql.Select<OrderPurchaseInfo, Order>() |
||||
|
.InnerJoin((opi, o) => opi.OrderId == o.Id) |
||||
|
.Where((opi, o) => opi.IsEnabled == true && |
||||
|
o.OrderState != Enums.OrderState.已取消 && |
||||
|
purchaserIdList.Contains(opi.PurchaserId)) |
||||
|
.GroupBy((opi, o) => opi.PurchaserId) |
||||
|
.ToList(g => new |
||||
|
{ |
||||
|
PurchaserId = g.Key, |
||||
|
Count = g.Count(), |
||||
|
MaxPurchaseTime = g.Max(g.Value.Item1.CreateTime) |
||||
|
}); |
||||
|
|
||||
|
#endregion
|
||||
|
|
||||
|
#region 查询最近90天采购数量
|
||||
|
var recent90d = DateTime.Now.Date.AddDays(-90); |
||||
|
var recent90dPurchasedCountList = fsql.Select<OrderPurchaseInfo, Order>() |
||||
|
.InnerJoin((opi, o) => opi.OrderId == o.Id) |
||||
|
.Where((opi, o) => o.OrderState != Enums.OrderState.已取消 && |
||||
|
opi.IsEnabled == true && |
||||
|
opi.CreateTime >= recent90d && |
||||
|
purchaserIdList.Contains(opi.PurchaserId)) |
||||
|
.GroupBy((opi, o) => opi.PurchaserId) |
||||
|
.ToList(g => new |
||||
|
{ |
||||
|
PurchaserId = g.Key, |
||||
|
Count = g.Count(), |
||||
|
}); |
||||
|
#endregion
|
||||
|
|
||||
|
#region 查询采购金额
|
||||
|
var purchasedAmountList = fsql.Select<OrderCostDetail, Order, OrderPurchaseInfo>() |
||||
|
.InnerJoin((ocd, o, opi) => ocd.OrderId == o.Id) |
||||
|
.InnerJoin((ocd, o, opi) => o.Id == opi.OrderId) |
||||
|
.Where((ocd, o, opi) => o.OrderState != Enums.OrderState.已取消 && |
||||
|
ocd.IsEnabled == true && |
||||
|
opi.IsEnabled == true && |
||||
|
purchaserIdList.Contains(opi.PurchaserId)) |
||||
|
.GroupBy((ocd, o, opi) => opi.PurchaserId) |
||||
|
.ToList(g => new |
||||
|
{ |
||||
|
PurchaserId = g.Key, |
||||
|
PurchasedAmount = g.Sum(g.Value.Item1.SkuAmount + g.Value.Item1.PurchaseFreight) |
||||
|
}); |
||||
|
#endregion
|
||||
|
|
||||
|
#region 查询最近90天采购金额
|
||||
|
var recent90dPurchasedAmountList = fsql.Select<OrderCostDetail, Order, OrderPurchaseInfo>() |
||||
|
.InnerJoin((ocd, o, opi) => ocd.OrderId == o.Id) |
||||
|
.InnerJoin((ocd, o, opi) => o.Id == opi.OrderId) |
||||
|
.Where((ocd, o, opi) => o.OrderState != Enums.OrderState.已取消 && |
||||
|
ocd.IsEnabled == true && |
||||
|
opi.IsEnabled == true && |
||||
|
opi.CreateTime >= recent90d && |
||||
|
purchaserIdList.Contains(opi.PurchaserId)) |
||||
|
.GroupBy((ocd, o, opi) => opi.PurchaserId) |
||||
|
.ToList(g => new |
||||
|
{ |
||||
|
PurchaserId = g.Key, |
||||
|
PurchasedAmount = g.Sum(g.Value.Item1.SkuAmount + g.Value.Item1.PurchaseFreight) |
||||
|
}); |
||||
|
#endregion
|
||||
|
|
||||
|
foreach (var purchaserId in purchaserIdList) |
||||
|
{ |
||||
|
#region SPU绑定数/SKU绑定数
|
||||
|
var currentBindList = bindList.Where(x => x.PurchaserId == purchaserId).ToList(); |
||||
|
var bindingSpuCount = currentBindList.Select(x => x.ProductId).Distinct().Count(); |
||||
|
var bindingSkuCount = currentBindList.Select(x => x.SkuId).Count(); |
||||
|
#endregion
|
||||
|
|
||||
|
#region SPU采购数/SKU采购数
|
||||
|
var currentPurchasedList = purchasedList.Where(x => x.PurchaserId == purchaserId).ToList(); |
||||
|
var purchasedSpuCount = currentPurchasedList.Select(x => x.ProductId).Distinct().Count(); |
||||
|
var purchasedSkuCount = currentPurchasedList.Select(x => x.SkuId).Count(); |
||||
|
#endregion
|
||||
|
|
||||
|
#region 订单数/最近采购时间
|
||||
|
var purchasedCountAndTime = poList.FirstOrDefault(x => x.PurchaserId == purchaserId); |
||||
|
var purchasedCount = purchasedCountAndTime?.Count ?? 0; |
||||
|
var lastPurchaseTime = purchasedCountAndTime?.MaxPurchaseTime; |
||||
|
#endregion
|
||||
|
|
||||
|
#region 采购金额
|
||||
|
var purchasedAmount = purchasedAmountList.FirstOrDefault(x => x.PurchaserId == purchaserId)?.PurchasedAmount ?? 0; |
||||
|
#endregion
|
||||
|
|
||||
|
#region 最近90天采购金额
|
||||
|
var recent90dPurchasedAmount = recent90dPurchasedAmountList.FirstOrDefault(x => x.PurchaserId == purchaserId)?.PurchasedAmount ?? 0; |
||||
|
#endregion
|
||||
|
|
||||
|
#region 最近90天采购数量
|
||||
|
var recent90dPurchasedCount = recent90dPurchasedCountList.FirstOrDefault(x => x.PurchaserId == purchaserId)?.Count ?? 0; |
||||
|
#endregion
|
||||
|
|
||||
|
var update = fsql.Update<Purchaser>(purchaserId) |
||||
|
.Set(p => p.BindingSpuCount, bindingSpuCount) |
||||
|
.Set(p => p.BindingSkuCount, bindingSkuCount) |
||||
|
.Set(p => p.PurchasedSpuCount, purchasedSpuCount) |
||||
|
.Set(p => p.PurchasedSkuCount, purchasedSkuCount) |
||||
|
.Set(p => p.PurchasedCount, purchasedCount) |
||||
|
.Set(p => p.PurchasedAmount, purchasedAmount) |
||||
|
.Set(p => p.Recent90dPurchasedAmount, recent90dPurchasedAmount) |
||||
|
.Set(p => p.Recent90dPurchasedCount, recent90dPurchasedCount); |
||||
|
updatePurchaserList.Add(update); |
||||
|
} |
||||
|
|
||||
|
fsql.Transaction(() => |
||||
|
{ |
||||
|
foreach (var update in updatePurchaserList) |
||||
|
update.ExecuteAffrows(); |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue