Browse Source

监控列表采集相邻的sku

master
С·æ 4 years ago
parent
commit
d73759fdd0
  1. 4
      JdShopListener/JdShopListener/App.xaml.cs
  2. 37
      JdShopListener/JdShopListener/ItemsWindow.xaml
  3. 53
      JdShopListener/JdShopListener/ItemsWindow.xaml.cs
  4. 22
      JdShopListener/JdShopListener/JdShopListener.csproj
  5. 8
      JdShopListener/JdShopListener/JdShopListener.csproj.user
  6. 2
      JdShopListener/JdShopListener/MainWindow.xaml
  7. 67
      JdShopListener/JdShopListener/MainWindowViewModel.cs
  8. 78
      JdShopListener/JdShopListener/Migrations/20210413085518_InitialCreate.Designer.cs
  9. 53
      JdShopListener/JdShopListener/Migrations/20210413085518_InitialCreate.cs
  10. 76
      JdShopListener/JdShopListener/Migrations/JdDBContextModelSnapshot.cs
  11. 103
      JdShopListener/JdShopListener/Models/ItemChangeModel.cs
  12. 141
      JdShopListener/JdShopListener/SqlHelpers/DbHelper.cs
  13. 29
      JdShopListener/JdShopListener/SqlHelpers/SkuModelSql.cs
  14. BIN
      JdShopListener/JdShopListener/db/items.db

4
JdShopListener/JdShopListener/App.xaml.cs

@ -13,5 +13,9 @@ namespace JdShopListener
/// </summary>
public partial class App : Application
{
public App()
{
}
}
}

37
JdShopListener/JdShopListener/ItemsWindow.xaml

@ -0,0 +1,37 @@
<Window x:Class="JdShopListener.ItemsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:JdShopListener"
mc:Ignorable="d"
Title="ItemsWindow" Height="450" Width="800">
<Grid>
<ListBox BorderBrush="Transparent" ItemsSource="{Binding SkuList}">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel>
<StackPanel Orientation="Horizontal" Width="380" DockPanel.Dock="Left">
<Image Width="50" Height="50" Source="{Binding ImgUrl}" Margin="0 0 10 0"></Image>
<StackPanel VerticalAlignment="Center">
<StackPanel Orientation="Horizontal">
<TextBlock Text="ID:"></TextBlock>
<TextBlock Text="{Binding SkuId}"></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0 10 0 0">
<TextBlock Text="备注:"></TextBlock>
<TextBlock Text="{Binding Desc}"></TextBlock>
</StackPanel>
</StackPanel>
</StackPanel>
<Button Content="删除" VerticalAlignment="Center" HorizontalAlignment="Center" Padding="15 3" Click="btn_Del_Click" Name="btn_Del">
</Button>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>

53
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
{
/// <summary>
/// ItemsWindow.xaml 的交互逻辑
/// </summary>
public partial class ItemsWindow : Window
{
private System.Collections.ObjectModel.ObservableCollection<SkuModel> _SkuList;
/// <summary>
/// sku监控列表
/// </summary>
public System.Collections.ObjectModel.ObservableCollection<SkuModel> SkuList
{
get { return _SkuList; }
set { _SkuList = value; }
}
public ItemsWindow(System.Collections.ObjectModel.ObservableCollection<SkuModel> 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);
}
}
}
}
}

22
JdShopListener/JdShopListener/JdShopListener.csproj

@ -7,8 +7,30 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="5.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MvvmLightLibsStd10" Version="5.4.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="SQLitePCLRaw.provider.dynamic_cdecl" Version="2.0.4" />
</ItemGroup>
<ItemGroup>
<Folder Include="db\" />
</ItemGroup>
<ItemGroup>
<None Update="db\items.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

8
JdShopListener/JdShopListener/JdShopListener.csproj.user

@ -7,6 +7,14 @@
</ApplicationDefinition>
</ItemGroup>
<ItemGroup>
<Compile Update="ItemsWindow.xaml.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Page Update="ItemsWindow.xaml">
<SubType>Designer</SubType>
</Page>
<Page Update="MainWindow.xaml">
<SubType>Designer</SubType>
</Page>

2
JdShopListener/JdShopListener/MainWindow.xaml

@ -234,7 +234,7 @@
<DataTemplate>
<DockPanel>
<StackPanel Orientation="Horizontal" Width="380" DockPanel.Dock="Left">
<Image Width="50" Height="50" Source="{Binding ImgUrl}"></Image>
<Image Width="50" Height="50" Source="{Binding ImgUrl}" Margin="0 0 10 0"></Image>
<StackPanel VerticalAlignment="Center">
<StackPanel Orientation="Horizontal">
<TextBlock Text="ID:"></TextBlock>

67
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<ProModel>() {
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<ItemData>() {
new ItemData(){ Desc="测试", OldPro="100",NewPro="120",SkuId="123123",OldProList=new List<string>(){ "满额返券","换购" } ,NewProList =new List<string>(){ "满额返券", "多买优惠" } },
new ItemData(){Desc="测试", OldPro="120",NewPro="111",SkuId="123123",OldProList=new List<string>(){ "满额返券","换购" } ,NewProList =new List<string>(){ "满额返券", "多买优惠", "换购" } }
};
SkuList = new System.Collections.ObjectModel.ObservableCollection<SkuModel>();
Init();
}
/// <summary>
/// 初始化
/// </summary>
public void Init()
{
SkuList.Clear();
var list= DbHelper.Db.GetSkuList();
list.ForEach(c => {
SkuList.Add(c);
});
}
private System.Collections.ObjectModel.ObservableCollection<ProModel> _ProList;
@ -152,6 +168,9 @@ namespace JdShopListener
/// </summary>
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<SkuModel> skus = new List<SkuModel>();
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();
}
/// <summary>
@ -187,7 +230,9 @@ namespace JdShopListener
/// </summary>
public void ShowList()
{
Init();
ItemsWindow window = new ItemsWindow(SkuList);
window.Show();
}
/// <summary>

78
JdShopListener/JdShopListener/Migrations/20210413085518_InitialCreate.Designer.cs

@ -0,0 +1,78 @@
// <auto-generated />
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<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<DateTime>("Date")
.HasColumnType("TEXT");
b.Property<string>("Desc")
.HasColumnType("TEXT");
b.Property<string>("ImgUrl")
.HasColumnType("TEXT");
b.Property<decimal>("NewPrice")
.HasColumnType("TEXT");
b.Property<decimal>("OldPrice")
.HasColumnType("TEXT");
b.Property<string>("SkuId")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("ItemChange");
});
modelBuilder.Entity("JdShopListener.Models.SkuModel", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<string>("Desc")
.HasColumnType("TEXT");
b.Property<string>("ImgUrl")
.HasColumnType("TEXT");
b.Property<string>("SkuId")
.HasColumnType("TEXT");
b.Property<string>("SpuId")
.HasColumnType("TEXT");
b.Property<string>("Title")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("SkuModel");
});
#pragma warning restore 612, 618
}
}
}

53
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<Guid>(type: "TEXT", nullable: false),
SkuId = table.Column<string>(type: "TEXT", nullable: true),
Desc = table.Column<string>(type: "TEXT", nullable: true),
ImgUrl = table.Column<string>(type: "TEXT", nullable: true),
OldPrice = table.Column<decimal>(type: "TEXT", nullable: false),
NewPrice = table.Column<decimal>(type: "TEXT", nullable: false),
Date = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ItemChange", x => x.Id);
});
migrationBuilder.CreateTable(
name: "SkuModel",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false),
SkuId = table.Column<string>(type: "TEXT", nullable: true),
ImgUrl = table.Column<string>(type: "TEXT", nullable: true),
SpuId = table.Column<string>(type: "TEXT", nullable: true),
Desc = table.Column<string>(type: "TEXT", nullable: true),
Title = table.Column<string>(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");
}
}
}

76
JdShopListener/JdShopListener/Migrations/JdDBContextModelSnapshot.cs

@ -0,0 +1,76 @@
// <auto-generated />
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<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<DateTime>("Date")
.HasColumnType("TEXT");
b.Property<string>("Desc")
.HasColumnType("TEXT");
b.Property<string>("ImgUrl")
.HasColumnType("TEXT");
b.Property<decimal>("NewPrice")
.HasColumnType("TEXT");
b.Property<decimal>("OldPrice")
.HasColumnType("TEXT");
b.Property<string>("SkuId")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("ItemChange");
});
modelBuilder.Entity("JdShopListener.Models.SkuModel", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<string>("Desc")
.HasColumnType("TEXT");
b.Property<string>("ImgUrl")
.HasColumnType("TEXT");
b.Property<string>("SkuId")
.HasColumnType("TEXT");
b.Property<string>("SpuId")
.HasColumnType("TEXT");
b.Property<string>("Title")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("SkuModel");
});
#pragma warning restore 612, 618
}
}
}

103
JdShopListener/JdShopListener/Models/ItemChangeModel.cs

@ -14,15 +14,15 @@ namespace JdShopListener.Models
{
public Guid Id { get; set; }
private string _SpuId;
private string _SkuId;
/// <summary>
/// spu
/// </summary>
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;
/// <summary>
/// 主图
/// </summary>
public string ImgUrl
{
get { return _ImgUrl; }
set { Set(ref _ImgUrl, value); }
}
private decimal _OldPrice;
/// <summary>
@ -58,65 +68,114 @@ namespace JdShopListener.Models
}
private List<string> _OldActive;
private string oldActive;
/// <summary>
/// 旧活动
/// </summary>
[NotMapped]
public List<string> OldActive
{
get { return _OldActive; }
set { Set(ref _OldActive, value); }
get
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(oldActive);
}
set
{
oldActive= Newtonsoft.Json.JsonConvert.SerializeObject(value);
}
}
private List<string> _NewActive;
private string newActive;
/// <summary>
/// 新活动
/// </summary>
[NotMapped]
public List<string> NewActive
{
get { return _NewActive; }
set { Set(ref _NewActive, value); }
get
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(newActive);
}
private List<string> _OldPromotion;
set
{
newActive = Newtonsoft.Json.JsonConvert.SerializeObject(value);
}
}
private string oldPromotion;
/// <summary>
/// 旧促销
/// </summary>
[NotMapped]
public List<string> OldPromotion
{
get { return _OldPromotion; }
set { Set(ref _OldPromotion, value); }
get
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(oldPromotion);
}
set
{
oldPromotion = Newtonsoft.Json.JsonConvert.SerializeObject(value);
}
}
private List<string> _NewPromotion;
private string newPromotion;
/// <summary>
/// 新促销
/// </summary>
[NotMapped]
public List<string> NewPromotion
{
get { return _NewPromotion; }
set { Set(ref _NewPromotion, value); }
get
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(newPromotion);
}
private List<string> _OldCoupons;
set
{
newPromotion = Newtonsoft.Json.JsonConvert.SerializeObject(value);
}
}
private string oldCoupons;
/// <summary>
/// 旧优惠券
/// </summary>
[NotMapped]
public List<string> OldCoupons
{
get { return _OldCoupons; }
set { Set(ref _OldCoupons, value); }
get
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(oldCoupons);
}
private List<string> _NewCoupons;
set
{
oldCoupons = Newtonsoft.Json.JsonConvert.SerializeObject(value);
}
}
private string newCoupons;
/// <summary>
/// 新优惠券
/// </summary>
[NotMapped]
public List<string> NewCoupons
{
get { return _NewCoupons; }
set { Set(ref _NewCoupons, value); }
get
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(newCoupons);
}
set
{
newCoupons = Newtonsoft.Json.JsonConvert.SerializeObject(value);
}
}
private DateTime _Date;

141
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();
/// <summary>
/// 添加sku监控
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 批量添加sku监控
/// </summary>
/// <param name="models"></param>
/// <returns></returns>
public bool AddSkuModel(List<SkuModel> models)
{
using (var db = new JdDBContext())
{
models.ForEach(model =>
{
model.Id = Guid.NewGuid();
db.SkuItems.Add(model);
});
var count = db.SaveChanges();
return count > 0;
}
}
/// <summary>
/// 获取sku监控列表
/// </summary>
/// <returns></returns>
public List<SkuModel> GetSkuList()
{
using (var db = new JdDBContext())
{
return db.SkuItems.ToList();
}
}
/// <summary>
/// 删除Sku
/// </summary>
/// <returns></returns>
public bool RemoveSkuModel(SkuModel model)
{
using (var db = new JdDBContext())
{
db.SkuItems.Remove(model);
return db.SaveChanges()>0;
}
}
/// <summary>
/// 添加变化记录
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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;
}
}
/// <summary>
/// 批量添加变化记录
/// </summary>
/// <param name="models"></param>
/// <returns></returns>
public bool AddItemChangeModel(List<ItemChangeModel> models)
{
using (var db = new JdDBContext())
{
models.ForEach(model =>
{
model.Id = Guid.NewGuid();
db.ItemChanges.Add(model);
});
var count = db.SaveChanges();
return count > 0;
}
}
/// <summary>
/// 根据日期批量获取变化记录
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public List<ItemChangeModel> GetItemChanges(DateTime date)
{
using (var db = new JdDBContext())
{
return db.ItemChanges.Where(c => c.Date.Date == date.Date).ToList();
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool RemoveItemChanges(ItemChangeModel model)
{
using (var db = new JdDBContext())
{
db.ItemChanges.Remove(model);
return db.SaveChanges() > 0;
}
}
}
}

29
JdShopListener/JdShopListener/SqlHelpers/SkuModelSql.cs

@ -1,29 +0,0 @@
using JdShopListener.Models;
using System;
using System.Collections.Generic;
using System.Text;
namespace JdShopListener.SqlHelpers
{
public class SkuModelSql
{
/// <summary>
/// 添加sku监控
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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;
}
}
}
}

BIN
JdShopListener/JdShopListener/db/items.db

Binary file not shown.
Loading…
Cancel
Save