using BBWY.Common.Http; using BBWY.Common.Models; using BBWY.Server.Model; using BBWY.Server.Model.Db; using BBWY.Server.Model.Dto; using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Linq; using Yitter.IdGenerator; namespace BBWY.Server.Business { public class JDStoreHouseWarningBusiness : BaseSyncBusiness, IDenpendency { private IList validStorageTypeList; public JDStoreHouseWarningBusiness(RestApiService restApiService, IOptions options, NLogManager nLogManager, IFreeSql fsql, IIdGenerator idGenerator, TaskSchedulerManager taskSchedulerManager, VenderBusiness venderBusiness, YunDingBusiness yunDingBusiness) : base(restApiService, options, nLogManager, fsql, idGenerator, taskSchedulerManager, venderBusiness, yunDingBusiness) { validStorageTypeList = new List() { Enums.StorageType.云仓, Enums.StorageType.京仓, Enums.StorageType.本地自发 }; } public void StartCheckStockNum() { var shopList = venderBusiness.GetShopList(platform: Enums.Platform.京东, filterTurnoverDays: true); foreach (var shop in shopList) { CheckStockNum(shop); } } private void CheckStockNum(ShopResponse shop) { long shopId = long.Parse(shop.ShopId); var yesterDayDate = DateTime.Now.Date.AddDays(-1); var ysterDayTime = DateTime.Now.Date.AddSeconds(-1); var yesterDaySkuIds = fsql.Select() .InnerJoin((s, osku, o) => s.Sku == osku.SkuId) .InnerJoin((s, osku, o) => osku.OrderId == o.Id) .Where((s, osku, o) => s.ShopId == shopId && s.Date == yesterDayDate && s.IsGift == false && s.ItemTotal > s.CancelItemTotal && o.StartTime >= yesterDayDate && o.StartTime <= ysterDayTime && validStorageTypeList.Contains(o.StorageType.Value)) .Distinct() .ToList((s, osku, o) => s.Sku); var queryStartDate = yesterDayDate.AddDays(-8); var skuSaleDailyList = fsql.Select() .Where(s => s.Date >= queryStartDate && s.Date <= yesterDayDate) .Where(s => yesterDaySkuIds.Contains(s.Sku)) .ToList(); var firstCycleStartDate = queryStartDate; var firstCycleEndDate = queryStartDate.AddDays(2); var secondCycleStartDate = firstCycleEndDate.AddDays(1); var secondCycleEndDate = secondCycleStartDate.AddDays(2); var thirdCycleStartDate = secondCycleEndDate.AddDays(1); var thirdCycleEndDate = thirdCycleStartDate.AddDays(2); foreach (var sku in yesterDaySkuIds) { //第一周期销量 var firstCycleSaleList = skuSaleDailyList.Where(s => s.Sku == sku && s.Date >= firstCycleStartDate && s.Date <= firstCycleEndDate); var firstCycleItemTotal = firstCycleSaleList.Count() > 0 ? firstCycleSaleList.Sum(s => s.ItemTotal - s.CancelItemTotal) : 0; //第二周期销量 var secondCycleSaleList = skuSaleDailyList.Where(s => s.Sku == sku && s.Date >= secondCycleStartDate && s.Date <= secondCycleEndDate); var secondCycleItemTotal = secondCycleSaleList.Count() > 0 ? secondCycleSaleList.Sum(s => s.ItemTotal - s.CancelItemTotal) : 0; //第三周期销量 var thirdCycleSaleList = skuSaleDailyList.Where(s => s.Sku == sku && s.Date >= thirdCycleStartDate && s.Date <= thirdCycleEndDate); var thirdCycleItemTotal = thirdCycleSaleList.Count() > 0 ? thirdCycleSaleList.Sum(s => s.ItemTotal - s.CancelItemTotal) : 0; //判断周期 } } } }