diff --git a/JdShopListener/JdShopListener/Converters/ToStringConverter.cs b/JdShopListener/JdShopListener/Converters/ToStringConverter.cs index c2ae626..cc0cc31 100644 --- a/JdShopListener/JdShopListener/Converters/ToStringConverter.cs +++ b/JdShopListener/JdShopListener/Converters/ToStringConverter.cs @@ -37,6 +37,8 @@ namespace WPF.Converters dic.Add(drr[0].ToLower(), drr[1]); } + string defa = dic.FirstOrDefault().Value.ToString(); + if (dic.ContainsKey(key)) { if (dic[key] == "Collapsed") @@ -46,15 +48,15 @@ namespace WPF.Converters return dic[key]; } - else - if (dic.FirstOrDefault().Value.ToString() == "Visible") + else if (defa == "Visible") { return System.Windows.Visibility.Collapsed; } else - if (dic.FirstOrDefault().Value.ToString() == "Collapsed") + if (defa == "Collapsed") return System.Windows.Visibility.Visible; - return value; + + return defa; } diff --git a/JdShopListener/JdShopListener/JdShopListener.csproj b/JdShopListener/JdShopListener/JdShopListener.csproj index 5bfc419..c9d9233 100644 --- a/JdShopListener/JdShopListener/JdShopListener.csproj +++ b/JdShopListener/JdShopListener/JdShopListener.csproj @@ -30,7 +30,7 @@ - Always + PreserveNewest diff --git a/JdShopListener/JdShopListener/MainWindow.xaml b/JdShopListener/JdShopListener/MainWindow.xaml index 6ea4902..daf6392 100644 --- a/JdShopListener/JdShopListener/MainWindow.xaml +++ b/JdShopListener/JdShopListener/MainWindow.xaml @@ -214,7 +214,7 @@ - + @@ -268,20 +268,20 @@ - + - + - + - + - + diff --git a/JdShopListener/JdShopListener/MainWindowViewModel.cs b/JdShopListener/JdShopListener/MainWindowViewModel.cs index 0267641..ca598e5 100644 --- a/JdShopListener/JdShopListener/MainWindowViewModel.cs +++ b/JdShopListener/JdShopListener/MainWindowViewModel.cs @@ -33,7 +33,8 @@ namespace JdShopListener new ProModel(){ Name="价格" , Type= ProType.Price}, new ProModel(){ Name="活动" , Type= ProType.Active}, new ProModel(){ Name="促销" , Type= ProType.Promotion}, - new ProModel(){ Name="优惠券" , Type= ProType.Coupons} + new ProModel(){ Name="优惠券" , Type= ProType.Coupons}, + new ProModel(){ Name="Sku" , Type= ProType.Sku}, }; DateList = new System.Collections.ObjectModel.ObservableCollection() { @@ -61,7 +62,10 @@ namespace JdShopListener SkuList.Clear(); var list= DbHelper.Db.GetSkuList(); list.ForEach(c => { - SkuList.Add(c); + if (c.IsShow == 1) + { + SkuList.Add(c); + } }); } @@ -191,9 +195,9 @@ namespace JdShopListener set { Set(ref _IsAdd, value); } } - private bool _IsStart; + private bool? _IsStart=false; - public bool IsStart + public bool? IsStart { get { return _IsStart; } set { Set(ref _IsStart, value); } @@ -245,12 +249,38 @@ namespace JdShopListener SpuId = spuId }; + if (model.SkuId == Sku) + { + model.IsShow = 1; + } + + + //去除重复 if (SkuList.Count(c => c.SkuId == model.SkuId) > 0) + { + + if (model.IsShow == 1) + { + var osku = SkuList.FirstOrDefault(c => c.SkuId == model.SkuId); + + if (osku.IsShow != 1) + { + + osku.IsShow = 1; + DbHelper.Db.UpdateSkuModel(osku); + } + + } + + continue; + } + //加入本地数据库 skus.Add(model); + Application.Current.Dispatcher.Invoke(() => { SkuList.Add(model); @@ -331,7 +361,7 @@ namespace JdShopListener /// public void Start() { - if (!IsStart) + if (IsStart==false) { IsStart = true; Thread t = new Thread(() => @@ -339,12 +369,36 @@ namespace JdShopListener LastItemChangeList.Clear(); LastItemChangeList = DbHelper.Db.GetLastItemChanges(); - DoWork(); + while (IsStart==true) + { + AddLog("开始sku定时监控!"); + + DoWork(); + + AddLog("全部sku监控执行完成,等待一小时后开始下一轮监控!"); + + for(int i=0;i<60;i++) + { + if (IsStart != true) + break; + for (int s = 0; s < 60; s++) + { + if (IsStart != true) + break; + //等待一小时 + Thread.Sleep(1000); + } + } + } IsStart = false; }); t.Start(); } + else { + IsStart = null; + AddLog("开始停止监控!"); + } } /// @@ -488,6 +542,66 @@ namespace JdShopListener }); } break; + + case ProType.Sku: + { + var data = list.Where(c => c.NewSkus != c.OldSkus).ToList(); + data.ForEach(item => + { + + ItemData change = new ItemData() + { + Date = item.Date, + Desc = item.Desc, + ImgUrl = item.ImgUrl, + SkuId = item.SkuId + }; + + if (item.OldSkus == null) + { + item.OldSkus = "[]"; + } + if (item.NewSkus == null) + { + item.NewSkus = "[]"; + } + + + List addSkus = new List(); + List removeSkus = new List(); + + var oldList = Newtonsoft.Json.JsonConvert.DeserializeObject>(item.OldSkus).Select(c => c.Title); + var newList = Newtonsoft.Json.JsonConvert.DeserializeObject>(item.NewSkus).Select(c => c.Title); + + foreach (var sku in newList) + { + //如果旧列表不存在就是新增 + if (!oldList.Contains(sku)) + { + addSkus.Add(new ProListModel() { Title=sku }); + } + } + + foreach (var sku in oldList) + { + //如果新列表没有就是删除 + if (!newList.Contains(sku)) + { + removeSkus.Add(new ProListModel() { Title = sku }); + } + } + + + change.OldProList = removeSkus; + change.NewProList = addSkus; + + Application.Current.Dispatcher.Invoke(() => + { + ItemList.Add(change); + }); + }); + } + break; } } @@ -532,139 +646,176 @@ namespace JdShopListener //开始监控全部 foreach (var sku in SkuList) { - var lastSku = LastItemChangeList.FirstOrDefault(c => c.SkuId == sku.SkuId); - - if(lastSku==null) + if (IsStart != true) { - lastSku = new ItemChangeModel(); + break; } - ItemChangeModel item = new ItemChangeModel() + try { - Date = DateTime.Now, - Desc = sku.Desc, - ImgUrl = sku.ImgUrl, - SkuId = sku.SkuId - }; + var lastSku = LastItemChangeList.FirstOrDefault(c => c.SkuId == sku.SkuId); - var result = GetWareBusiness(sku); + if (lastSku == null) + { + lastSku = new ItemChangeModel(); + } - //价格 - decimal? price = result.price.p; - if (price == null || price == 0) - { - price = result.price.op ?? 0; - } + ItemChangeModel item = new ItemChangeModel() + { + Date = DateTime.Now, + Desc = sku.Desc, + ImgUrl = sku.ImgUrl, + SkuId = sku.SkuId + }; - item.NewPrice = (decimal)price; + var result = GetWareBusiness(sku); - #region 活动 - var newActive = new List(); - //活动 - var miaoshaInfo = result.miaoshaInfo; - //判断是否有活动 - if (miaoshaInfo != null) - { - string miaoshaTitle = miaoshaInfo.title; + //价格 + decimal? price = result.price.p; + if (price == null || price == 0) + { + price = result.price.op ?? 0; + } - DateTime endDate = ToDateTime((long)miaoshaInfo.endTime); + item.NewPrice = (decimal)price; - decimal? msPrice = result.price.p; + #region 活动 + var newActive = new List(); + //活动 + var miaoshaInfo = result.miaoshaInfo; + //判断是否有活动 + if (miaoshaInfo != null) + { + string miaoshaTitle = miaoshaInfo.title; - newActive.Add(new { title= $"{miaoshaTitle} 价格:{msPrice} 结束时间:{endDate.ToString("yyyy-MM-dd")}", value=string.Empty }); - } + DateTime endDate = ToDateTime((long)miaoshaInfo.endTime); - //活动 - var yuyueInfo = result.yuyueInfo; - if (yuyueInfo != null) - { - string title = "预约抢购"; + decimal? msPrice = result.price.p; - string time = yuyueInfo.buyTime; - time = (time.Split("-")[3] + time.Split("-")[4] + time.Split("-")[5]); - //DateTime endDate = ToDateTime((long)yuyueInfo.endTime); + newActive.Add(new { title = $"{miaoshaTitle} 价格:{msPrice} 结束时间:{endDate.ToString("yyyy-MM-dd")}", value = string.Empty }); + } - decimal? msPrice = result.price.p; + //活动 + var yuyueInfo = result.yuyueInfo; + if (yuyueInfo != null) + { + string title = "预约抢购"; - newActive.Add(new { title= $"{title} 价格:{msPrice} 结束时间:{time}", value=string.Empty }); - } + string time = yuyueInfo.buyTime; + time = (time.Split("-")[3] + time.Split("-")[4] + time.Split("-")[5]); + //DateTime endDate = ToDateTime((long)yuyueInfo.endTime); - item.NewActive = Newtonsoft.Json.JsonConvert.SerializeObject(newActive); - #endregion + decimal? msPrice = result.price.p; - #region 促销 + newActive.Add(new { title = $"{title} 价格:{msPrice} 结束时间:{time}", value = string.Empty }); + } - var newPromotion = new List(); + item.NewActive = Newtonsoft.Json.JsonConvert.SerializeObject(newActive); + #endregion - //促销 - var promotion = result.promotion; - if (promotion != null) - { - //促销活动列表 - var activity = promotion.activity; - if (activity != null) + #region 促销 + + var newPromotion = new List(); + + //促销 + var promotion = result.promotion; + if (promotion != null) { - foreach (var active in activity) + //促销活动列表 + var activity = promotion.activity; + if (activity != null) { - string title = active.text; - string value = active.value; - newPromotion.Add(new { title, value }); + foreach (var active in activity) + { + string title = active.text; + string value = active.value; + newPromotion.Add(new { title, value }); + } } - } - //赠品列表 - var gift = promotion.gift; - if (gift != null) - { - - string title = "赠品"; - string value = string.Empty; - foreach (var gif in gift) + //赠品列表 + var gift = promotion.gift; + if (gift != null) { - value += gif.skuId + ","; + + string title = "赠品"; + string value = string.Empty; + foreach (var gif in gift) + { + value += gif.skuId + ","; + } + + if (!string.IsNullOrEmpty(value)) + { + + newPromotion.Add(new { title, value }); + } } - if (!string.IsNullOrEmpty(value)) - { + item.NewPromotion = Newtonsoft.Json.JsonConvert.SerializeObject(newPromotion); + } + #endregion - newPromotion.Add(new { title, value }); + #region 优惠券 + var newCoupons = new List(); + //优惠券 + var couponInfo = result.couponInfo; + if (couponInfo != null) + { + foreach (var coupon in couponInfo) + { + string title = $"满{coupon.quota}减{coupon.discount}"; + newCoupons.Add(new { title, value = string.Empty }); } } - item.NewPromotion = Newtonsoft.Json.JsonConvert.SerializeObject( newPromotion); - } - #endregion + item.NewCoupons = Newtonsoft.Json.JsonConvert.SerializeObject(newCoupons); + #endregion - #region 优惠券 - var newCoupons = new List(); - //优惠券 - var couponInfo = result.couponInfo; - if (couponInfo != null) - { - foreach (var coupon in couponInfo) + #region sku + + var newSkus = new List(); + + var detail = GetItemDetail(item.SkuId); + + //相邻的Sku列表 + var nearSkus = detail.product.colorSize; + + foreach (var near in nearSkus) { - string title = $"满{coupon.quota}减{coupon.discount}"; - newCoupons.Add(new { title, value = string.Empty }); + if (near != null) + { + newSkus.Add(new { title= near.skuId, value = string.Empty }); + } } - } + item.NewSkus = Newtonsoft.Json.JsonConvert.SerializeObject(newSkus); + #endregion - item.NewCoupons =Newtonsoft.Json.JsonConvert.SerializeObject( newCoupons); - #endregion - - //判断是否有变化 - if (lastSku.NewActive != item.NewActive||lastSku.NewCoupons!=item.NewCoupons||lastSku.NewPrice!=item.NewPrice||lastSku.NewPromotion!=item.NewPromotion) - { - item.OldActive = lastSku.NewActive; - item.OldCoupons = lastSku.NewCoupons; - item.OldPrice = lastSku.NewPrice; - item.OldPromotion = lastSku.NewPromotion; + //判断是否有变化 + if (lastSku.NewSkus!=item.NewSkus|| lastSku.NewActive != item.NewActive || lastSku.NewCoupons != item.NewCoupons || lastSku.NewPrice != item.NewPrice || lastSku.NewPromotion != item.NewPromotion) + { + item.OldActive = lastSku.NewActive; + item.OldCoupons = lastSku.NewCoupons; + item.OldPrice = lastSku.NewPrice; + item.OldPromotion = lastSku.NewPromotion; + item.OldSkus = lastSku.NewSkus; - DbHelper.Db.AddItemChangeModel(item); + DbHelper.Db.AddItemChangeModel(item); + AddLog($"{sku.SkuId}监控完成!已产生变化记录"); + } + else { + AddLog($"{sku.SkuId}监控完成!未发生变化"); + } + Thread.Sleep(3000); + } + catch(Exception ex) + { + AddLog(sku.SkuId + "监控失败," + ex.Message); } + - Thread.Sleep(3000); } } diff --git a/JdShopListener/JdShopListener/Migrations/20210414042219_InitialCreate.Designer.cs b/JdShopListener/JdShopListener/Migrations/20210418094901_InitialCreate.Designer.cs similarity index 81% rename from JdShopListener/JdShopListener/Migrations/20210414042219_InitialCreate.Designer.cs rename to JdShopListener/JdShopListener/Migrations/20210418094901_InitialCreate.Designer.cs index 7938acf..de7123b 100644 --- a/JdShopListener/JdShopListener/Migrations/20210414042219_InitialCreate.Designer.cs +++ b/JdShopListener/JdShopListener/Migrations/20210418094901_InitialCreate.Designer.cs @@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace JdShopListener.Migrations { [DbContext(typeof(JdDBContext))] - [Migration("20210414042219_InitialCreate")] + [Migration("20210418094901_InitialCreate")] partial class InitialCreate { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -33,31 +33,37 @@ namespace JdShopListener.Migrations b.Property("ImgUrl") .HasColumnType("TEXT"); + b.Property("NewActive") + .HasColumnType("TEXT"); + + b.Property("NewCoupons") + .HasColumnType("TEXT"); + b.Property("NewPrice") .HasColumnType("TEXT"); - b.Property("OldPrice") + b.Property("NewPromotion") .HasColumnType("TEXT"); - b.Property("SkuId") + b.Property("NewSkus") .HasColumnType("TEXT"); - b.Property("newActive") + b.Property("OldActive") .HasColumnType("TEXT"); - b.Property("newCoupons") + b.Property("OldCoupons") .HasColumnType("TEXT"); - b.Property("newPromotion") + b.Property("OldPrice") .HasColumnType("TEXT"); - b.Property("oldActive") + b.Property("OldPromotion") .HasColumnType("TEXT"); - b.Property("oldCoupons") + b.Property("OldSkus") .HasColumnType("TEXT"); - b.Property("oldPromotion") + b.Property("SkuId") .HasColumnType("TEXT"); b.HasKey("Id"); @@ -80,6 +86,9 @@ namespace JdShopListener.Migrations b.Property("ImgUrl") .HasColumnType("TEXT"); + b.Property("IsShow") + .HasColumnType("INTEGER"); + b.Property("ShopId") .HasColumnType("TEXT"); diff --git a/JdShopListener/JdShopListener/Migrations/20210414042219_InitialCreate.cs b/JdShopListener/JdShopListener/Migrations/20210418094901_InitialCreate.cs similarity index 79% rename from JdShopListener/JdShopListener/Migrations/20210414042219_InitialCreate.cs rename to JdShopListener/JdShopListener/Migrations/20210418094901_InitialCreate.cs index 9252c0b..1ffac18 100644 --- a/JdShopListener/JdShopListener/Migrations/20210414042219_InitialCreate.cs +++ b/JdShopListener/JdShopListener/Migrations/20210418094901_InitialCreate.cs @@ -17,12 +17,14 @@ namespace JdShopListener.Migrations ImgUrl = table.Column(type: "TEXT", nullable: true), OldPrice = table.Column(type: "TEXT", nullable: false), NewPrice = table.Column(type: "TEXT", nullable: false), - oldActive = table.Column(type: "TEXT", nullable: true), - newActive = table.Column(type: "TEXT", nullable: true), - oldPromotion = table.Column(type: "TEXT", nullable: true), - newPromotion = table.Column(type: "TEXT", nullable: true), - oldCoupons = table.Column(type: "TEXT", nullable: true), - newCoupons = table.Column(type: "TEXT", nullable: true), + OldActive = table.Column(type: "TEXT", nullable: true), + NewActive = table.Column(type: "TEXT", nullable: true), + OldPromotion = table.Column(type: "TEXT", nullable: true), + NewPromotion = table.Column(type: "TEXT", nullable: true), + OldCoupons = table.Column(type: "TEXT", nullable: true), + NewCoupons = table.Column(type: "TEXT", nullable: true), + NewSkus = table.Column(type: "TEXT", nullable: true), + OldSkus = table.Column(type: "TEXT", nullable: true), Date = table.Column(type: "TEXT", nullable: false) }, constraints: table => @@ -42,7 +44,8 @@ namespace JdShopListener.Migrations Cat = table.Column(type: "TEXT", nullable: true), Title = table.Column(type: "TEXT", nullable: true), ShopId = table.Column(type: "TEXT", nullable: true), - VenderId = table.Column(type: "TEXT", nullable: true) + VenderId = table.Column(type: "TEXT", nullable: true), + IsShow = table.Column(type: "INTEGER", nullable: false) }, constraints: table => { diff --git a/JdShopListener/JdShopListener/Migrations/JdDBContextModelSnapshot.cs b/JdShopListener/JdShopListener/Migrations/JdDBContextModelSnapshot.cs index 36ed136..99e6fc1 100644 --- a/JdShopListener/JdShopListener/Migrations/JdDBContextModelSnapshot.cs +++ b/JdShopListener/JdShopListener/Migrations/JdDBContextModelSnapshot.cs @@ -31,31 +31,37 @@ namespace JdShopListener.Migrations b.Property("ImgUrl") .HasColumnType("TEXT"); + b.Property("NewActive") + .HasColumnType("TEXT"); + + b.Property("NewCoupons") + .HasColumnType("TEXT"); + b.Property("NewPrice") .HasColumnType("TEXT"); - b.Property("OldPrice") + b.Property("NewPromotion") .HasColumnType("TEXT"); - b.Property("SkuId") + b.Property("NewSkus") .HasColumnType("TEXT"); - b.Property("newActive") + b.Property("OldActive") .HasColumnType("TEXT"); - b.Property("newCoupons") + b.Property("OldCoupons") .HasColumnType("TEXT"); - b.Property("newPromotion") + b.Property("OldPrice") .HasColumnType("TEXT"); - b.Property("oldActive") + b.Property("OldPromotion") .HasColumnType("TEXT"); - b.Property("oldCoupons") + b.Property("OldSkus") .HasColumnType("TEXT"); - b.Property("oldPromotion") + b.Property("SkuId") .HasColumnType("TEXT"); b.HasKey("Id"); @@ -78,6 +84,9 @@ namespace JdShopListener.Migrations b.Property("ImgUrl") .HasColumnType("TEXT"); + b.Property("IsShow") + .HasColumnType("INTEGER"); + b.Property("ShopId") .HasColumnType("TEXT"); diff --git a/JdShopListener/JdShopListener/Models/ItemChangeModel.cs b/JdShopListener/JdShopListener/Models/ItemChangeModel.cs index d9c0ea7..51fdace 100644 --- a/JdShopListener/JdShopListener/Models/ItemChangeModel.cs +++ b/JdShopListener/JdShopListener/Models/ItemChangeModel.cs @@ -67,7 +67,6 @@ namespace JdShopListener.Models set { Set(ref _NewPrice, value); } } - public string OldActive { get; set; } public string NewActive { get; set; } @@ -83,6 +82,8 @@ namespace JdShopListener.Models public string NewCoupons { get; set; } + public string NewSkus { get; set; } + public string OldSkus { get; set; } private DateTime _Date; /// diff --git a/JdShopListener/JdShopListener/Models/ProModel.cs b/JdShopListener/JdShopListener/Models/ProModel.cs index 6258f7a..da8dd75 100644 --- a/JdShopListener/JdShopListener/Models/ProModel.cs +++ b/JdShopListener/JdShopListener/Models/ProModel.cs @@ -43,6 +43,10 @@ namespace JdShopListener.Models /// /// 优惠券 /// - Coupons=4 + Coupons=4, + /// + /// 相邻的SKu + /// + Sku = 5, } } diff --git a/JdShopListener/JdShopListener/Models/SkuModel.cs b/JdShopListener/JdShopListener/Models/SkuModel.cs index 7c9738f..2647635 100644 --- a/JdShopListener/JdShopListener/Models/SkuModel.cs +++ b/JdShopListener/JdShopListener/Models/SkuModel.cs @@ -96,5 +96,16 @@ namespace JdShopListener.Models set { Set(ref _VenderId, value); } } + + private int _IsShow; + /// + /// 是否显示 + /// + public int IsShow + { + get { return _IsShow; } + set { Set(ref _IsShow, value); } + } + } } diff --git a/JdShopListener/JdShopListener/SqlHelpers/DbHelper.cs b/JdShopListener/JdShopListener/SqlHelpers/DbHelper.cs index ba58f7a..be0d482 100644 --- a/JdShopListener/JdShopListener/SqlHelpers/DbHelper.cs +++ b/JdShopListener/JdShopListener/SqlHelpers/DbHelper.cs @@ -28,6 +28,21 @@ namespace JdShopListener.SqlHelpers } } + /// + /// 更新sku + /// + /// + /// + public bool UpdateSkuModel(SkuModel model) + { + using (var db = new JdDBContext()) + { + db.SkuItems.Update(model); + var count = db.SaveChanges(); + return count > 0; + } + } + /// /// 批量添加sku监控 /// diff --git a/JdShopListener/JdShopListener/db/items.db b/JdShopListener/JdShopListener/db/items.db index 5752b3a..633c7b1 100644 Binary files a/JdShopListener/JdShopListener/db/items.db and b/JdShopListener/JdShopListener/db/items.db differ