|
|
@ -46,6 +46,7 @@ namespace BBWY.Server.Business |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
var loggerName = $"库存预警-{shop.ShopName}"; |
|
|
|
long shopId = long.Parse(shop.ShopId); |
|
|
|
var yesterDayDate = DateTime.Now.Date.AddDays(-1); |
|
|
|
var ysterDayTime = DateTime.Now.Date.AddSeconds(-1); |
|
|
@ -105,6 +106,11 @@ namespace BBWY.Server.Business |
|
|
|
var _2Ratio = firstCycleItemTotal == 0 ? 0 : 1.0 * secondCycleItemTotal / firstCycleItemTotal - 1; |
|
|
|
var _3Ratio = secondCycleItemTotal == 0 ? 0 : 1.0 * thirdCycleItemTotal / secondCycleItemTotal - 1; |
|
|
|
|
|
|
|
|
|
|
|
var _7dAvgSaleList = skuSaleDailyList.Where(s => s.Sku == sku && s.Date >= _7dAvgStartDate && s.Date <= yesterDayDate); |
|
|
|
var _7dItemTotal = _7dAvgSaleList.Sum(s => s.ItemTotal - s.CancelItemTotal); |
|
|
|
var _7dAvgItemTotal = 1.0 * _7dItemTotal / 7; //近7天日均销量
|
|
|
|
|
|
|
|
Enums.SkuStockNumCycleType skuStockNumCycleType = Enums.SkuStockNumCycleType.暂无周期; |
|
|
|
if (_2Ratio >= 0.2 && _3Ratio >= 0.2) |
|
|
|
skuStockNumCycleType = Enums.SkuStockNumCycleType.增长期; |
|
|
@ -113,8 +119,27 @@ namespace BBWY.Server.Business |
|
|
|
else if (_2Ratio < -0.2 && _3Ratio < -0.2) |
|
|
|
skuStockNumCycleType = Enums.SkuStockNumCycleType.衰退期; |
|
|
|
|
|
|
|
var logContentBuilder = new StringBuilder(); |
|
|
|
logContentBuilder.AppendLine($"SKU:{sku}"); |
|
|
|
logContentBuilder.AppendLine($"商品状态:{skuStockNumCycleType}"); |
|
|
|
logContentBuilder.AppendLine($"第一周销量:{firstCycleItemTotal}"); |
|
|
|
logContentBuilder.AppendLine($"第二周期销量:{secondCycleItemTotal}, 相比第一周幅度:{_2Ratio}%"); |
|
|
|
logContentBuilder.AppendLine($"第三周期销量:{thirdCycleItemTotal}, 相比第二周幅度:{_3Ratio}%"); |
|
|
|
logContentBuilder.AppendLine($"近7天销量:{_7dItemTotal}"); |
|
|
|
logContentBuilder.AppendLine($"7天日均销量:{_7dAvgItemTotal}"); |
|
|
|
|
|
|
|
//dingdingContentBuilder.Append($"SKU:{sku}\n");
|
|
|
|
//dingdingContentBuilder.Append($"商品状态:{skuStockNumCycleType}\n");
|
|
|
|
//dingdingContentBuilder.Append($"近7天销量:{_7dItemTotal}\n");
|
|
|
|
//dingdingContentBuilder.Append($"7天日均销量:{_7dItemTotal}\n");
|
|
|
|
|
|
|
|
|
|
|
|
if (skuStockNumCycleType == Enums.SkuStockNumCycleType.暂无周期) |
|
|
|
{ |
|
|
|
logContentBuilder.AppendLine(); |
|
|
|
nLogManager.GetLogger(loggerName).Info(logContentBuilder); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
Thread.Sleep(1000); |
|
|
|
var restApiResult = restApiService.SendRequest(GetPlatformRelayAPIHost(shop.PlatformId), "api/platformsdk/GetStockNumBySku", new SearchProductSkuRequest() |
|
|
@ -129,7 +154,13 @@ namespace BBWY.Server.Business |
|
|
|
throw new Exception($"{sku} {restApiResult.Content}"); |
|
|
|
var response = JsonConvert.DeserializeObject<ApiResponse<JArray>>(restApiResult.Content); |
|
|
|
if (response.Data == null || response.Data.Count() == 0) |
|
|
|
{ |
|
|
|
logContentBuilder.AppendLine("未查询到JD库存数据"); |
|
|
|
logContentBuilder.AppendLine(); |
|
|
|
nLogManager.GetLogger(loggerName).Info(logContentBuilder); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
var skuStockNumList = response.Data.Select(j => new |
|
|
|
{ |
|
|
|
StockNum = j.Value<int>("stockNum"), |
|
|
@ -138,11 +169,10 @@ namespace BBWY.Server.Business |
|
|
|
SkuId = sku |
|
|
|
}); |
|
|
|
|
|
|
|
var _7dAvgSaleList = skuSaleDailyList.Where(s => s.Sku == sku && s.Date >= _7dAvgStartDate && s.Date <= yesterDayDate); |
|
|
|
var _7dItemTotal = _7dAvgSaleList.Sum(s => s.ItemTotal - s.CancelItemTotal); |
|
|
|
var _7dAvgItemTotal = 1.0 * _7dItemTotal / 7; //近7天日均销量
|
|
|
|
var totalStockNum = skuStockNumList.Sum(s => s.StockNum); //总库存
|
|
|
|
|
|
|
|
var skuStockNumGroups = skuStockNumList.GroupBy(s => s.Store?.Type ?? Enums.StockType.商家仓); //按仓库类型算库存
|
|
|
|
foreach (var skuStockNumGroup in skuStockNumGroups) |
|
|
|
{ |
|
|
|
var totalStockNum = skuStockNumGroup.Sum(s => s.StockNum); //总库存
|
|
|
|
var lessDay = _7dAvgItemTotal == 0 ? 0 : totalStockNum / _7dAvgItemTotal; //剩余天数
|
|
|
|
bool isWarning = false; //是否触发提醒
|
|
|
|
int suggestStockNum = 0; //建议备货量
|
|
|
@ -181,30 +211,33 @@ namespace BBWY.Server.Business |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#region 预警检查结果日志
|
|
|
|
nLogManager.GetLogger($"库存预警-{shop.ShopName}").Info($"sku {sku}, 周期 {skuStockNumCycleType}, 7天总销量 {_7dItemTotal}, 7天日均销量 {_7dAvgItemTotal}, 总库存 {totalStockNum}, 预估销售剩余天数 {lessDay}, 是否触发预警 {isWarning}"); |
|
|
|
#endregion
|
|
|
|
|
|
|
|
if (isWarning) |
|
|
|
{ |
|
|
|
isSendDingTalk = true; |
|
|
|
#region 拼接提醒内容
|
|
|
|
|
|
|
|
dingdingContentBuilder.Append($"SKU:{sku}\n"); |
|
|
|
dingdingContentBuilder.Append($"商品状态:{skuStockNumCycleType}\n"); |
|
|
|
dingdingContentBuilder.Append($"近7天销量:{_7dItemTotal}\n"); |
|
|
|
foreach (var stockNumInfo in skuStockNumList) |
|
|
|
{ |
|
|
|
if (stockNumInfo.Store != null) |
|
|
|
dingdingContentBuilder.Append($"{stockNumInfo.Store.Name}:{stockNumInfo.StockNum}件\n"); |
|
|
|
else |
|
|
|
dingdingContentBuilder.Append($"{stockNumInfo.StoreId}:{stockNumInfo.StockNum}件\n"); |
|
|
|
} |
|
|
|
foreach (var stockNumInfo in skuStockNumGroup) |
|
|
|
dingdingContentBuilder.Append($"{stockNumInfo.Store?.Name ?? stockNumInfo.StoreId}:{stockNumInfo.StockNum}件"); |
|
|
|
if (skuStockNumCycleType == Enums.SkuStockNumCycleType.增长期 || skuStockNumCycleType == Enums.SkuStockNumCycleType.稳定期) |
|
|
|
dingdingContentBuilder.Append($"低于安全周转天数,建议备货{suggestStockNum}件"); |
|
|
|
else if (skuStockNumCycleType == Enums.SkuStockNumCycleType.衰退期) |
|
|
|
dingdingContentBuilder.Append("商品进入衰退期,建议暂停备货,采购代发"); |
|
|
|
dingdingContentBuilder.AppendLine(); |
|
|
|
} |
|
|
|
|
|
|
|
#region 拼接sku在当前类型仓库的日志
|
|
|
|
logContentBuilder.AppendLine($"仓库类型:{skuStockNumGroup.Key}"); |
|
|
|
logContentBuilder.AppendLine($"总库存:{totalStockNum}"); |
|
|
|
logContentBuilder.AppendLine($"剩余天数:{lessDay}"); |
|
|
|
logContentBuilder.AppendLine($"建议备货量:{suggestStockNum}"); |
|
|
|
logContentBuilder.AppendLine($"是否触发预警:{isWarning}"); |
|
|
|
#endregion
|
|
|
|
} |
|
|
|
|
|
|
|
logContentBuilder.AppendLine(); |
|
|
|
nLogManager.GetLogger(loggerName).Info(logContentBuilder); |
|
|
|
} |
|
|
|
|
|
|
|
if (isSendDingTalk) |
|
|
|