Browse Source

新增数据sku变化结构

master
С·æ 4 years ago
parent
commit
a6ecc0f714
  1. 10
      JdShopListener/JdShopListener/Converters/ToStringConverter.cs
  2. 2
      JdShopListener/JdShopListener/JdShopListener.csproj
  3. 6
      JdShopListener/JdShopListener/MainWindow.xaml
  4. 173
      JdShopListener/JdShopListener/MainWindowViewModel.cs
  5. 27
      JdShopListener/JdShopListener/Migrations/20210418094901_InitialCreate.Designer.cs
  6. 17
      JdShopListener/JdShopListener/Migrations/20210418094901_InitialCreate.cs
  7. 25
      JdShopListener/JdShopListener/Migrations/JdDBContextModelSnapshot.cs
  8. 3
      JdShopListener/JdShopListener/Models/ItemChangeModel.cs
  9. 6
      JdShopListener/JdShopListener/Models/ProModel.cs
  10. 11
      JdShopListener/JdShopListener/Models/SkuModel.cs
  11. 15
      JdShopListener/JdShopListener/SqlHelpers/DbHelper.cs
  12. BIN
      JdShopListener/JdShopListener/db/items.db

10
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;
}

2
JdShopListener/JdShopListener/JdShopListener.csproj

@ -30,7 +30,7 @@
<ItemGroup>
<None Update="db\items.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

6
JdShopListener/JdShopListener/MainWindow.xaml

@ -214,7 +214,7 @@
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBox Tag="请输入备注" Text="{Binding Desc}" Width="350" Margin="0 10 0 0" HorizontalAlignment="Left"></TextBox>
<Button Content="开始监控" IsEnabled="{Binding IsStart,Converter={StaticResource toString},ConverterParameter=true----false|false----true}" Command="{Binding Btn_Start}" Margin="10 10 0 0"></Button>
<Button Content="{Binding IsStart,Converter={StaticResource toString},ConverterParameter=true----停止监控|Null----正在停止|false----开始监控}" Command="{Binding Btn_Start}" Margin="10 10 0 0"></Button>
</StackPanel>
</StackPanel>
@ -268,13 +268,13 @@
<StackPanel x:Name="spList" Orientation="Horizontal" DockPanel.Dock="Right">
<StackPanel VerticalAlignment="Center">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock Text="原:" VerticalAlignment="Center"></TextBlock>
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.SelectPro.Type,Converter={StaticResource toString},ConverterParameter=0----原:|sku----减:}" VerticalAlignment="Center"></TextBlock>
<ListBox Style="{StaticResource ListPro}" ItemsSource="{Binding OldProList}">
</ListBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="新:" VerticalAlignment="Center"></TextBlock>
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.SelectPro.Type,Converter={StaticResource toString},ConverterParameter=0----新:|sku----增:}" VerticalAlignment="Center"></TextBlock>
<ListBox Style="{StaticResource ListPro}" ItemsSource="{Binding NewProList}">
</ListBox>

173
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<DateModel>() {
@ -61,7 +62,10 @@ namespace JdShopListener
SkuList.Clear();
var list= DbHelper.Db.GetSkuList();
list.ForEach(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
/// </summary>
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();
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("开始停止监控!");
}
}
/// <summary>
@ -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<ProListModel> addSkus = new List<ProListModel>();
List<ProListModel> removeSkus = new List<ProListModel>();
var oldList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ProListModel>>(item.OldSkus).Select(c => c.Title);
var newList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ProListModel>>(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;
}
}
@ -531,10 +645,17 @@ namespace JdShopListener
{
//开始监控全部
foreach (var sku in SkuList)
{
if (IsStart != true)
{
break;
}
try
{
var lastSku = LastItemChangeList.FirstOrDefault(c => c.SkuId == sku.SkuId);
if(lastSku==null)
if (lastSku == null)
{
lastSku = new ItemChangeModel();
}
@ -571,7 +692,7 @@ namespace JdShopListener
decimal? msPrice = result.price.p;
newActive.Add(new { title= $"{miaoshaTitle} 价格:{msPrice} 结束时间:{endDate.ToString("yyyy-MM-dd")}", value=string.Empty });
newActive.Add(new { title = $"{miaoshaTitle} 价格:{msPrice} 结束时间:{endDate.ToString("yyyy-MM-dd")}", value = string.Empty });
}
//活动
@ -586,7 +707,7 @@ namespace JdShopListener
decimal? msPrice = result.price.p;
newActive.Add(new { title= $"{title} 价格:{msPrice} 结束时间:{time}", value=string.Empty });
newActive.Add(new { title = $"{title} 价格:{msPrice} 结束时间:{time}", value = string.Empty });
}
item.NewActive = Newtonsoft.Json.JsonConvert.SerializeObject(newActive);
@ -632,7 +753,7 @@ namespace JdShopListener
}
}
item.NewPromotion = Newtonsoft.Json.JsonConvert.SerializeObject( newPromotion);
item.NewPromotion = Newtonsoft.Json.JsonConvert.SerializeObject(newPromotion);
}
#endregion
@ -649,23 +770,53 @@ namespace JdShopListener
}
}
item.NewCoupons =Newtonsoft.Json.JsonConvert.SerializeObject( newCoupons);
item.NewCoupons = Newtonsoft.Json.JsonConvert.SerializeObject(newCoupons);
#endregion
#region sku
var newSkus = new List<object>();
var detail = GetItemDetail(item.SkuId);
//相邻的Sku列表
var nearSkus = detail.product.colorSize;
foreach (var near in nearSkus)
{
if (near != null)
{
newSkus.Add(new { title= near.skuId, value = string.Empty });
}
}
item.NewSkus = Newtonsoft.Json.JsonConvert.SerializeObject(newSkus);
#endregion
//判断是否有变化
if (lastSku.NewActive != item.NewActive||lastSku.NewCoupons!=item.NewCoupons||lastSku.NewPrice!=item.NewPrice||lastSku.NewPromotion!=item.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);
AddLog($"{sku.SkuId}监控完成!已产生变化记录");
}
else {
AddLog($"{sku.SkuId}监控完成!未发生变化");
}
Thread.Sleep(3000);
}
catch(Exception ex)
{
AddLog(sku.SkuId + "监控失败," + ex.Message);
}
}
}

27
JdShopListener/JdShopListener/Migrations/20210414042219_InitialCreate.Designer.cs → 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<string>("ImgUrl")
.HasColumnType("TEXT");
b.Property<string>("NewActive")
.HasColumnType("TEXT");
b.Property<string>("NewCoupons")
.HasColumnType("TEXT");
b.Property<decimal>("NewPrice")
.HasColumnType("TEXT");
b.Property<decimal>("OldPrice")
b.Property<string>("NewPromotion")
.HasColumnType("TEXT");
b.Property<string>("SkuId")
b.Property<string>("NewSkus")
.HasColumnType("TEXT");
b.Property<string>("newActive")
b.Property<string>("OldActive")
.HasColumnType("TEXT");
b.Property<string>("newCoupons")
b.Property<string>("OldCoupons")
.HasColumnType("TEXT");
b.Property<string>("newPromotion")
b.Property<decimal>("OldPrice")
.HasColumnType("TEXT");
b.Property<string>("oldActive")
b.Property<string>("OldPromotion")
.HasColumnType("TEXT");
b.Property<string>("oldCoupons")
b.Property<string>("OldSkus")
.HasColumnType("TEXT");
b.Property<string>("oldPromotion")
b.Property<string>("SkuId")
.HasColumnType("TEXT");
b.HasKey("Id");
@ -80,6 +86,9 @@ namespace JdShopListener.Migrations
b.Property<string>("ImgUrl")
.HasColumnType("TEXT");
b.Property<int>("IsShow")
.HasColumnType("INTEGER");
b.Property<string>("ShopId")
.HasColumnType("TEXT");

17
JdShopListener/JdShopListener/Migrations/20210414042219_InitialCreate.cs → JdShopListener/JdShopListener/Migrations/20210418094901_InitialCreate.cs

@ -17,12 +17,14 @@ namespace JdShopListener.Migrations
ImgUrl = table.Column<string>(type: "TEXT", nullable: true),
OldPrice = table.Column<decimal>(type: "TEXT", nullable: false),
NewPrice = table.Column<decimal>(type: "TEXT", nullable: false),
oldActive = table.Column<string>(type: "TEXT", nullable: true),
newActive = table.Column<string>(type: "TEXT", nullable: true),
oldPromotion = table.Column<string>(type: "TEXT", nullable: true),
newPromotion = table.Column<string>(type: "TEXT", nullable: true),
oldCoupons = table.Column<string>(type: "TEXT", nullable: true),
newCoupons = table.Column<string>(type: "TEXT", nullable: true),
OldActive = table.Column<string>(type: "TEXT", nullable: true),
NewActive = table.Column<string>(type: "TEXT", nullable: true),
OldPromotion = table.Column<string>(type: "TEXT", nullable: true),
NewPromotion = table.Column<string>(type: "TEXT", nullable: true),
OldCoupons = table.Column<string>(type: "TEXT", nullable: true),
NewCoupons = table.Column<string>(type: "TEXT", nullable: true),
NewSkus = table.Column<string>(type: "TEXT", nullable: true),
OldSkus = table.Column<string>(type: "TEXT", nullable: true),
Date = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
@ -42,7 +44,8 @@ namespace JdShopListener.Migrations
Cat = table.Column<string>(type: "TEXT", nullable: true),
Title = table.Column<string>(type: "TEXT", nullable: true),
ShopId = table.Column<string>(type: "TEXT", nullable: true),
VenderId = table.Column<string>(type: "TEXT", nullable: true)
VenderId = table.Column<string>(type: "TEXT", nullable: true),
IsShow = table.Column<int>(type: "INTEGER", nullable: false)
},
constraints: table =>
{

25
JdShopListener/JdShopListener/Migrations/JdDBContextModelSnapshot.cs

@ -31,31 +31,37 @@ namespace JdShopListener.Migrations
b.Property<string>("ImgUrl")
.HasColumnType("TEXT");
b.Property<string>("NewActive")
.HasColumnType("TEXT");
b.Property<string>("NewCoupons")
.HasColumnType("TEXT");
b.Property<decimal>("NewPrice")
.HasColumnType("TEXT");
b.Property<decimal>("OldPrice")
b.Property<string>("NewPromotion")
.HasColumnType("TEXT");
b.Property<string>("SkuId")
b.Property<string>("NewSkus")
.HasColumnType("TEXT");
b.Property<string>("newActive")
b.Property<string>("OldActive")
.HasColumnType("TEXT");
b.Property<string>("newCoupons")
b.Property<string>("OldCoupons")
.HasColumnType("TEXT");
b.Property<string>("newPromotion")
b.Property<decimal>("OldPrice")
.HasColumnType("TEXT");
b.Property<string>("oldActive")
b.Property<string>("OldPromotion")
.HasColumnType("TEXT");
b.Property<string>("oldCoupons")
b.Property<string>("OldSkus")
.HasColumnType("TEXT");
b.Property<string>("oldPromotion")
b.Property<string>("SkuId")
.HasColumnType("TEXT");
b.HasKey("Id");
@ -78,6 +84,9 @@ namespace JdShopListener.Migrations
b.Property<string>("ImgUrl")
.HasColumnType("TEXT");
b.Property<int>("IsShow")
.HasColumnType("INTEGER");
b.Property<string>("ShopId")
.HasColumnType("TEXT");

3
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;
/// <summary>

6
JdShopListener/JdShopListener/Models/ProModel.cs

@ -43,6 +43,10 @@ namespace JdShopListener.Models
/// <summary>
/// 优惠券
/// </summary>
Coupons=4
Coupons=4,
/// <summary>
/// 相邻的SKu
/// </summary>
Sku = 5,
}
}

11
JdShopListener/JdShopListener/Models/SkuModel.cs

@ -96,5 +96,16 @@ namespace JdShopListener.Models
set { Set(ref _VenderId, value); }
}
private int _IsShow;
/// <summary>
/// 是否显示
/// </summary>
public int IsShow
{
get { return _IsShow; }
set { Set(ref _IsShow, value); }
}
}
}

15
JdShopListener/JdShopListener/SqlHelpers/DbHelper.cs

@ -28,6 +28,21 @@ namespace JdShopListener.SqlHelpers
}
}
/// <summary>
/// 更新sku
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool UpdateSkuModel(SkuModel model)
{
using (var db = new JdDBContext())
{
db.SkuItems.Update(model);
var count = db.SaveChanges();
return count > 0;
}
}
/// <summary>
/// 批量添加sku监控
/// </summary>

BIN
JdShopListener/JdShopListener/db/items.db

Binary file not shown.
Loading…
Cancel
Save