From d73759fdd001dd7d645a9010352a1e709f4c19be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=C2=B7=C3=A6?= <279202647@qq.com> Date: Tue, 13 Apr 2021 17:28:42 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=91=E6=8E=A7=E5=88=97=E8=A1=A8=E9=87=87?= =?UTF-8?q?=E9=9B=86=E7=9B=B8=E9=82=BB=E7=9A=84sku?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JdShopListener/JdShopListener/App.xaml.cs | 4 + .../JdShopListener/ItemsWindow.xaml | 37 +++++ .../JdShopListener/ItemsWindow.xaml.cs | 53 +++++++ .../JdShopListener/JdShopListener.csproj | 22 +++ .../JdShopListener/JdShopListener.csproj.user | 8 + JdShopListener/JdShopListener/MainWindow.xaml | 2 +- .../JdShopListener/MainWindowViewModel.cs | 67 +++++++-- .../20210413085518_InitialCreate.Designer.cs | 78 ++++++++++ .../20210413085518_InitialCreate.cs | 53 +++++++ .../Migrations/JdDBContextModelSnapshot.cs | 76 ++++++++++ .../JdShopListener/Models/ItemChangeModel.cs | 103 ++++++++++--- .../JdShopListener/SqlHelpers/DbHelper.cs | 141 ++++++++++++++++++ .../JdShopListener/SqlHelpers/SkuModelSql.cs | 29 ---- JdShopListener/JdShopListener/db/items.db | Bin 0 -> 28672 bytes 14 files changed, 610 insertions(+), 63 deletions(-) create mode 100644 JdShopListener/JdShopListener/ItemsWindow.xaml create mode 100644 JdShopListener/JdShopListener/ItemsWindow.xaml.cs create mode 100644 JdShopListener/JdShopListener/Migrations/20210413085518_InitialCreate.Designer.cs create mode 100644 JdShopListener/JdShopListener/Migrations/20210413085518_InitialCreate.cs create mode 100644 JdShopListener/JdShopListener/Migrations/JdDBContextModelSnapshot.cs create mode 100644 JdShopListener/JdShopListener/SqlHelpers/DbHelper.cs delete mode 100644 JdShopListener/JdShopListener/SqlHelpers/SkuModelSql.cs create mode 100644 JdShopListener/JdShopListener/db/items.db diff --git a/JdShopListener/JdShopListener/App.xaml.cs b/JdShopListener/JdShopListener/App.xaml.cs index e9ec8ba..c0d1580 100644 --- a/JdShopListener/JdShopListener/App.xaml.cs +++ b/JdShopListener/JdShopListener/App.xaml.cs @@ -13,5 +13,9 @@ namespace JdShopListener /// public partial class App : Application { + public App() + { + + } } } diff --git a/JdShopListener/JdShopListener/ItemsWindow.xaml b/JdShopListener/JdShopListener/ItemsWindow.xaml new file mode 100644 index 0000000..dfacccd --- /dev/null +++ b/JdShopListener/JdShopListener/ItemsWindow.xaml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/JdShopListener/JdShopListener/ItemsWindow.xaml.cs b/JdShopListener/JdShopListener/ItemsWindow.xaml.cs new file mode 100644 index 0000000..e89e01b --- /dev/null +++ b/JdShopListener/JdShopListener/ItemsWindow.xaml.cs @@ -0,0 +1,53 @@ +using JdShopListener.Models; +using JdShopListener.SqlHelpers; +using System; +using System.Collections.Generic; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Shapes; + +namespace JdShopListener +{ + /// + /// ItemsWindow.xaml 的交互逻辑 + /// + public partial class ItemsWindow : Window + { + + private System.Collections.ObjectModel.ObservableCollection _SkuList; + /// + /// sku监控列表 + /// + public System.Collections.ObjectModel.ObservableCollection SkuList + { + get { return _SkuList; } + set { _SkuList = value; } + } + + public ItemsWindow(System.Collections.ObjectModel.ObservableCollection skuList) + { + InitializeComponent(); + SkuList = skuList; + this.DataContext = this; + } + + private void btn_Del_Click(object sender, RoutedEventArgs e) + { + var model = (e.Source as FrameworkElement).DataContext as SkuModel; + + if (MessageBox.Show($"是否删除{model.Title}?", "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes) + { + if (DbHelper.Db.RemoveSkuModel(model)) + { + SkuList.Remove(model); + } + } + } + } +} diff --git a/JdShopListener/JdShopListener/JdShopListener.csproj b/JdShopListener/JdShopListener/JdShopListener.csproj index a88495c..46ad476 100644 --- a/JdShopListener/JdShopListener/JdShopListener.csproj +++ b/JdShopListener/JdShopListener/JdShopListener.csproj @@ -7,8 +7,30 @@ + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + Always + diff --git a/JdShopListener/JdShopListener/JdShopListener.csproj.user b/JdShopListener/JdShopListener/JdShopListener.csproj.user index 644b0a6..2146a5c 100644 --- a/JdShopListener/JdShopListener/JdShopListener.csproj.user +++ b/JdShopListener/JdShopListener/JdShopListener.csproj.user @@ -7,6 +7,14 @@ + + Code + + + + + Designer + Designer diff --git a/JdShopListener/JdShopListener/MainWindow.xaml b/JdShopListener/JdShopListener/MainWindow.xaml index 9f3d4c1..c9cfc1c 100644 --- a/JdShopListener/JdShopListener/MainWindow.xaml +++ b/JdShopListener/JdShopListener/MainWindow.xaml @@ -234,7 +234,7 @@ - + diff --git a/JdShopListener/JdShopListener/MainWindowViewModel.cs b/JdShopListener/JdShopListener/MainWindowViewModel.cs index 5cddd4d..be02f67 100644 --- a/JdShopListener/JdShopListener/MainWindowViewModel.cs +++ b/JdShopListener/JdShopListener/MainWindowViewModel.cs @@ -6,6 +6,10 @@ using System.Collections.Generic; using System.Text; using System.Net; using System.Net.Http; +using JdShopListener.SqlHelpers; +using System.Windows; +using System.Linq; +using System.Threading; namespace JdShopListener { @@ -21,6 +25,9 @@ namespace JdShopListener } } + + + public MainWindowViewModel() { ProList = new System.Collections.ObjectModel.ObservableCollection() { new ProModel(){ Name="价格" , Type= ProType.Price}, @@ -39,12 +46,21 @@ namespace JdShopListener Btn_ShowList = new RelayCommand(ShowList); Btn_Start = new RelayCommand(Start); - ItemList = new System.Collections.ObjectModel.ObservableCollection() { - new ItemData(){ Desc="测试", OldPro="100",NewPro="120",SkuId="123123",OldProList=new List(){ "满额返券","换购" } ,NewProList =new List(){ "满额返券", "多买优惠" } }, - new ItemData(){Desc="测试", OldPro="120",NewPro="111",SkuId="123123",OldProList=new List(){ "满额返券","换购" } ,NewProList =new List(){ "满额返券", "多买优惠", "换购" } } - }; - SkuList = new System.Collections.ObjectModel.ObservableCollection(); + + Init(); + } + + /// + /// 初始化 + /// + public void Init() + { + SkuList.Clear(); + var list= DbHelper.Db.GetSkuList(); + list.ForEach(c => { + SkuList.Add(c); + }); } private System.Collections.ObjectModel.ObservableCollection _ProList; @@ -152,6 +168,9 @@ namespace JdShopListener /// public void Add() { + + Thread addThread=new Thread(()=>{ + var detail= GetItemDetail("69920799280"); var list = detail.product.colorSize; @@ -159,10 +178,8 @@ namespace JdShopListener //spuId string spuId = detail.product.mainSkuId; - //主图 - string src = detail.product.src; - //标题 - string title = detail.product.name; + + List skus = new List(); foreach (var sku in list) { @@ -173,13 +190,39 @@ namespace JdShopListener SpuId=spuId }; - //加入本地数据库 + //去除重复 + if (SkuList.Count(c => c.SkuId == model.SkuId) > 0) + continue; + //加入本地数据库 + skus.Add(model); SkuList.Add(model); } + //加载sku详情 + skus.ForEach(sku => + { + var detail = GetItemDetail(sku.SkuId); + + //主图 + string src = detail.product.src; + + //标题 + string title = detail.product.name; + sku.ImgUrl = "http://img11.360buyimg.com/n1/" + src; + sku.Title = title; + Thread.Sleep(3000); + }); + + if (DbHelper.Db.AddSkuModel(skus)) + { + MessageBox.Show("添加成功","提示"); + } + + }); + addThread.Start(); } /// @@ -187,7 +230,9 @@ namespace JdShopListener /// public void ShowList() { - + Init(); + ItemsWindow window = new ItemsWindow(SkuList); + window.Show(); } /// diff --git a/JdShopListener/JdShopListener/Migrations/20210413085518_InitialCreate.Designer.cs b/JdShopListener/JdShopListener/Migrations/20210413085518_InitialCreate.Designer.cs new file mode 100644 index 0000000..eca9ee9 --- /dev/null +++ b/JdShopListener/JdShopListener/Migrations/20210413085518_InitialCreate.Designer.cs @@ -0,0 +1,78 @@ +// +using System; +using JdShopListener; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace JdShopListener.Migrations +{ + [DbContext(typeof(JdDBContext))] + [Migration("20210413085518_InitialCreate")] + partial class InitialCreate + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "5.0.5"); + + modelBuilder.Entity("JdShopListener.Models.ItemChangeModel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("Date") + .HasColumnType("TEXT"); + + b.Property("Desc") + .HasColumnType("TEXT"); + + b.Property("ImgUrl") + .HasColumnType("TEXT"); + + b.Property("NewPrice") + .HasColumnType("TEXT"); + + b.Property("OldPrice") + .HasColumnType("TEXT"); + + b.Property("SkuId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("ItemChange"); + }); + + modelBuilder.Entity("JdShopListener.Models.SkuModel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("Desc") + .HasColumnType("TEXT"); + + b.Property("ImgUrl") + .HasColumnType("TEXT"); + + b.Property("SkuId") + .HasColumnType("TEXT"); + + b.Property("SpuId") + .HasColumnType("TEXT"); + + b.Property("Title") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("SkuModel"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/JdShopListener/JdShopListener/Migrations/20210413085518_InitialCreate.cs b/JdShopListener/JdShopListener/Migrations/20210413085518_InitialCreate.cs new file mode 100644 index 0000000..45de258 --- /dev/null +++ b/JdShopListener/JdShopListener/Migrations/20210413085518_InitialCreate.cs @@ -0,0 +1,53 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace JdShopListener.Migrations +{ + public partial class InitialCreate : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "ItemChange", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + SkuId = table.Column(type: "TEXT", nullable: true), + Desc = table.Column(type: "TEXT", nullable: true), + ImgUrl = table.Column(type: "TEXT", nullable: true), + OldPrice = table.Column(type: "TEXT", nullable: false), + NewPrice = table.Column(type: "TEXT", nullable: false), + Date = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ItemChange", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "SkuModel", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + SkuId = table.Column(type: "TEXT", nullable: true), + ImgUrl = table.Column(type: "TEXT", nullable: true), + SpuId = table.Column(type: "TEXT", nullable: true), + Desc = table.Column(type: "TEXT", nullable: true), + Title = table.Column(type: "TEXT", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_SkuModel", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ItemChange"); + + migrationBuilder.DropTable( + name: "SkuModel"); + } + } +} diff --git a/JdShopListener/JdShopListener/Migrations/JdDBContextModelSnapshot.cs b/JdShopListener/JdShopListener/Migrations/JdDBContextModelSnapshot.cs new file mode 100644 index 0000000..8348778 --- /dev/null +++ b/JdShopListener/JdShopListener/Migrations/JdDBContextModelSnapshot.cs @@ -0,0 +1,76 @@ +// +using System; +using JdShopListener; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace JdShopListener.Migrations +{ + [DbContext(typeof(JdDBContext))] + partial class JdDBContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "5.0.5"); + + modelBuilder.Entity("JdShopListener.Models.ItemChangeModel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("Date") + .HasColumnType("TEXT"); + + b.Property("Desc") + .HasColumnType("TEXT"); + + b.Property("ImgUrl") + .HasColumnType("TEXT"); + + b.Property("NewPrice") + .HasColumnType("TEXT"); + + b.Property("OldPrice") + .HasColumnType("TEXT"); + + b.Property("SkuId") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("ItemChange"); + }); + + modelBuilder.Entity("JdShopListener.Models.SkuModel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("Desc") + .HasColumnType("TEXT"); + + b.Property("ImgUrl") + .HasColumnType("TEXT"); + + b.Property("SkuId") + .HasColumnType("TEXT"); + + b.Property("SpuId") + .HasColumnType("TEXT"); + + b.Property("Title") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("SkuModel"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/JdShopListener/JdShopListener/Models/ItemChangeModel.cs b/JdShopListener/JdShopListener/Models/ItemChangeModel.cs index 9634eb2..493b940 100644 --- a/JdShopListener/JdShopListener/Models/ItemChangeModel.cs +++ b/JdShopListener/JdShopListener/Models/ItemChangeModel.cs @@ -14,15 +14,15 @@ namespace JdShopListener.Models { public Guid Id { get; set; } - private string _SpuId; + private string _SkuId; /// /// spu /// - public string SpuId + public string SkuId { - get { return _SpuId; } - set { Set(ref _SpuId, value); } + get { return _SkuId; } + set { Set(ref _SkuId, value); } } private string _Desc; @@ -35,6 +35,16 @@ namespace JdShopListener.Models set { Set(ref _Desc, value); } } + private string _ImgUrl; + /// + /// 主图 + /// + public string ImgUrl + { + get { return _ImgUrl; } + set { Set(ref _ImgUrl, value); } + } + private decimal _OldPrice; /// @@ -58,65 +68,114 @@ namespace JdShopListener.Models } - private List _OldActive; + private string oldActive; /// /// 旧活动 /// + [NotMapped] public List OldActive { - get { return _OldActive; } - set { Set(ref _OldActive, value); } + get + { + return Newtonsoft.Json.JsonConvert.DeserializeObject>(oldActive); + } + + set + { + oldActive= Newtonsoft.Json.JsonConvert.SerializeObject(value); + } } + - private List _NewActive; + private string newActive; /// /// 新活动 /// + [NotMapped] public List NewActive { - get { return _NewActive; } - set { Set(ref _NewActive, value); } + get + { + return Newtonsoft.Json.JsonConvert.DeserializeObject>(newActive); + } + + set + { + newActive = Newtonsoft.Json.JsonConvert.SerializeObject(value); + } } - private List _OldPromotion; + private string oldPromotion; /// /// 旧促销 /// + [NotMapped] public List OldPromotion { - get { return _OldPromotion; } - set { Set(ref _OldPromotion, value); } + get + { + return Newtonsoft.Json.JsonConvert.DeserializeObject>(oldPromotion); + } + + set + { + oldPromotion = Newtonsoft.Json.JsonConvert.SerializeObject(value); + } } - private List _NewPromotion; + private string newPromotion; /// /// 新促销 /// + [NotMapped] public List NewPromotion { - get { return _NewPromotion; } - set { Set(ref _NewPromotion, value); } + get + { + return Newtonsoft.Json.JsonConvert.DeserializeObject>(newPromotion); + } + + set + { + newPromotion = Newtonsoft.Json.JsonConvert.SerializeObject(value); + } } - private List _OldCoupons; + private string oldCoupons; /// /// 旧优惠券 /// + [NotMapped] public List OldCoupons { - get { return _OldCoupons; } - set { Set(ref _OldCoupons, value); } + get + { + return Newtonsoft.Json.JsonConvert.DeserializeObject>(oldCoupons); + } + + set + { + oldCoupons = Newtonsoft.Json.JsonConvert.SerializeObject(value); + } } - private List _NewCoupons; + private string newCoupons; /// /// 新优惠券 /// + [NotMapped] public List NewCoupons { - get { return _NewCoupons; } - set { Set(ref _NewCoupons, value); } + get + { + return Newtonsoft.Json.JsonConvert.DeserializeObject>(newCoupons); + } + + set + { + newCoupons = Newtonsoft.Json.JsonConvert.SerializeObject(value); + } } private DateTime _Date; diff --git a/JdShopListener/JdShopListener/SqlHelpers/DbHelper.cs b/JdShopListener/JdShopListener/SqlHelpers/DbHelper.cs new file mode 100644 index 0000000..09aa060 --- /dev/null +++ b/JdShopListener/JdShopListener/SqlHelpers/DbHelper.cs @@ -0,0 +1,141 @@ +using JdShopListener.Models; +using System; +using System.Collections.Generic; +using System.Text; +using System.Linq; +using SQLitePCL; +using System.Runtime.InteropServices; + +namespace JdShopListener.SqlHelpers +{ + public class DbHelper + { + public static DbHelper Db = new DbHelper(); + + /// + /// 添加sku监控 + /// + /// + /// + public bool AddSkuModel(SkuModel model) + { + using (var db = new JdDBContext()) + { + model.Id = Guid.NewGuid(); + db.SkuItems.Add(model); + var count = db.SaveChanges(); + return count > 0; + } + } + + /// + /// 批量添加sku监控 + /// + /// + /// + public bool AddSkuModel(List models) + { + using (var db = new JdDBContext()) + { + models.ForEach(model => + { + model.Id = Guid.NewGuid(); + db.SkuItems.Add(model); + }); + var count = db.SaveChanges(); + return count > 0; + } + } + + /// + /// 获取sku监控列表 + /// + /// + public List GetSkuList() + { + using (var db = new JdDBContext()) + { + return db.SkuItems.ToList(); + } + } + + + /// + /// 删除Sku + /// + /// + public bool RemoveSkuModel(SkuModel model) + { + using (var db = new JdDBContext()) + { + + db.SkuItems.Remove(model); + return db.SaveChanges()>0; + } + } + + /// + /// 添加变化记录 + /// + /// + /// + public bool AddItemChangeModel(ItemChangeModel model) + { + using (var db = new JdDBContext()) + { + model.Id = Guid.NewGuid(); + db.ItemChanges.Add(model); + var count = db.SaveChanges(); + return count > 0; + } + } + + /// + /// 批量添加变化记录 + /// + /// + /// + public bool AddItemChangeModel(List models) + { + using (var db = new JdDBContext()) + { + models.ForEach(model => + { + model.Id = Guid.NewGuid(); + db.ItemChanges.Add(model); + }); + var count = db.SaveChanges(); + return count > 0; + } + } + + + /// + /// 根据日期批量获取变化记录 + /// + /// + /// + public List GetItemChanges(DateTime date) + { + using (var db = new JdDBContext()) + { + return db.ItemChanges.Where(c => c.Date.Date == date.Date).ToList(); + } + } + + /// + /// 删除 + /// + /// + /// + public bool RemoveItemChanges(ItemChangeModel model) + { + using (var db = new JdDBContext()) + { + + db.ItemChanges.Remove(model); + return db.SaveChanges() > 0; + } + } + } +} diff --git a/JdShopListener/JdShopListener/SqlHelpers/SkuModelSql.cs b/JdShopListener/JdShopListener/SqlHelpers/SkuModelSql.cs deleted file mode 100644 index a1aed3d..0000000 --- a/JdShopListener/JdShopListener/SqlHelpers/SkuModelSql.cs +++ /dev/null @@ -1,29 +0,0 @@ -using JdShopListener.Models; -using System; -using System.Collections.Generic; -using System.Text; - -namespace JdShopListener.SqlHelpers -{ - public class SkuModelSql - { - /// - /// 添加sku监控 - /// - /// - /// - public bool AddSkuModel(SkuModel model) - { - using (var db = new JdDBContext()) - { - model.Id = Guid.NewGuid(); - db.SkuItems.Add(model); - var count = db.SaveChanges(); - return count > 0; - } - } - - - - } -} diff --git a/JdShopListener/JdShopListener/db/items.db b/JdShopListener/JdShopListener/db/items.db new file mode 100644 index 0000000000000000000000000000000000000000..bd2bdaa0c533690dd8b987184110e4804585abef GIT binary patch literal 28672 zcmeI&zi!h&9KdlqNfR114V$N`*N6cm)Fv%aI}%b;O!JS#Ma_UL)0kr6Cg8XN#MHuT zu(R+oj7&TRBbTbKQ)E}_nCN@T$>%%&F22v5BH5STi-sFG;?2lEu>-NC+*ML3<*^Wo zqNHWc$sDH1c_uuOM|G;4R7@-Ddp`=rze;wguPpy6{w#c1{=U>NJWmRs9svXpKmY** z5I_I{1ZGg+Y`BoqR#w!n8-e}Wb9(Pio1=l_g~x2YtJh3jn6)PjU6jJ=lDJ>U3z*I^(05%S|`%oT>Uk{=r5ztJPOj#T^cuPvZ}se5|tU(_nOQes#B2CA{z4 z`D!|+t*xow7B7U1z&WYEwTH(}RLWdeiW+X=G#zvcsf2+i0=B(D$9IpeP%dXJgl{vf8WdB#+qdu`03E zlV|xmlV6pB%xdwsB0nSq5I_I{1Q0*~0R#|0009ILm^p!aYFgJe%bS(*qsmr!r&_J- zSjNx|T-&Srj(p!zT`#X!^Kx8j@sA=uBm@vZ009ILKmY**5I_I{1Q3`}fz`CWc+*RO z$^O6m(?1CT1Q0*~0R#|0009ILKmY**=2&2||IhRP98WMqLjVB;5I_I{1Q0*~0R#|0 zKnk$`&oV#&0R#|0009ILKmY**5I|u51=#