Browse Source

修改储存Sku数据,增加查询必要参数

master
С·æ 4 years ago
parent
commit
7fa2ef9888
  1. 2
      JdShopListener/JdShopListener/JdShopListener.csproj
  2. 2
      JdShopListener/JdShopListener/MainWindow.xaml
  3. 360
      JdShopListener/JdShopListener/MainWindowViewModel.cs
  4. 29
      JdShopListener/JdShopListener/Migrations/20210414042219_InitialCreate.Designer.cs
  5. 11
      JdShopListener/JdShopListener/Migrations/20210414042219_InitialCreate.cs
  6. 27
      JdShopListener/JdShopListener/Migrations/JdDBContextModelSnapshot.cs
  7. 106
      JdShopListener/JdShopListener/Models/ItemChangeModel.cs
  8. 15
      JdShopListener/JdShopListener/Models/ItemData.cs
  9. 31
      JdShopListener/JdShopListener/Models/SkuModel.cs
  10. 13
      JdShopListener/JdShopListener/SqlHelpers/DbHelper.cs
  11. BIN
      JdShopListener/JdShopListener/db/items.db

2
JdShopListener/JdShopListener/JdShopListener.csproj

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

2
JdShopListener/JdShopListener/MainWindow.xaml

@ -167,7 +167,7 @@
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding}"></TextBlock>
<TextBlock Text="{Binding Title}"></TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>

360
JdShopListener/JdShopListener/MainWindowViewModel.cs

@ -45,9 +45,11 @@ namespace JdShopListener
Btn_Add = new RelayCommand(Add);
Btn_ShowList = new RelayCommand(ShowList);
Btn_Start = new RelayCommand(Start);
Btn_ShowData = new RelayCommand(ShowData);
ItemList = new System.Collections.ObjectModel.ObservableCollection<ItemData>();
SkuList = new System.Collections.ObjectModel.ObservableCollection<SkuModel>();
SelectDate = DateList.FirstOrDefault();
SelectPro = ProList.FirstOrDefault();
Init();
}
@ -92,7 +94,7 @@ namespace JdShopListener
public ProModel SelectPro
{
get { return _SelectPro; }
set { Set(ref _SelectPro, value); }
set { Set(ref _SelectPro, value); ShowData(); }
}
@ -103,7 +105,7 @@ namespace JdShopListener
public DateModel SelectDate
{
get { return _SelectDate; }
set { Set(ref _SelectDate, value); }
set { Set(ref _SelectDate, value);ShowData(); }
}
private System.Collections.ObjectModel.ObservableCollection<ItemData> _ItemList;
@ -146,6 +148,10 @@ namespace JdShopListener
set { Set(ref _Desc, value); }
}
/// <summary>
/// 所有sku最后变化记录
/// </summary>
public List<ItemChangeModel> LastItemChangeList { get; set; } = new List<ItemChangeModel>();
/// <summary>
/// 添加事件
@ -162,6 +168,10 @@ namespace JdShopListener
/// </summary>
public RelayCommand Btn_Start { get; set; }
/// <summary>
/// 刷新数据
/// </summary>
public RelayCommand Btn_ShowData { get; set; }
/// <summary>
/// 添加
@ -171,7 +181,7 @@ namespace JdShopListener
Thread addThread=new Thread(()=>{
var detail= GetItemDetail("69920799280");
var detail= GetItemDetail(Sku);
var list = detail.product.colorSize;
@ -196,7 +206,9 @@ namespace JdShopListener
//加入本地数据库
skus.Add(model);
Application.Current.Dispatcher.Invoke(() => {
SkuList.Add(model);
});
}
//加载sku详情
@ -206,12 +218,29 @@ namespace JdShopListener
//主图
string src = detail.product.src;
var catIds = detail.product.cat;
List<int> cats = new List<int>();
foreach (var catId in catIds)
{
cats.Add((int)catId);
}
string cat = string.Join(",", cats);
//标题
string title = detail.product.name;
string shopId = detail.product.shopId;
string vid= detail.product.venderId;
sku.ImgUrl = "http://img11.360buyimg.com/n1/" + src;
sku.Title = title;
sku.Cat = cat;
sku.ShopId = shopId;
sku.VenderId = vid;
Thread.Sleep(3000);
});
@ -240,7 +269,158 @@ namespace JdShopListener
/// </summary>
public void Start()
{
Thread t = new Thread(() =>
{
LastItemChangeList.Clear();
LastItemChangeList = DbHelper.Db.GetLastItemChanges();
DoWork();
});
t.Start();
}
/// <summary>
/// 显示数据
/// </summary>
public void ShowData()
{
Application.Current.Dispatcher.Invoke(() =>
{
ItemList.Clear();
});
if (SelectPro == null||SelectDate==null)
return;
var list= DbHelper.Db.GetItemChanges(DateTime.Now.AddDays(SelectDate.Day));
switch (SelectPro.Type)
{
case ProType.Active:
{
var data = list.Where(c => c.NewActive != c.OldActive).ToList();
data.ForEach(item =>
{
ItemData change = new ItemData()
{
Date = item.Date,
Desc = item.Desc,
ImgUrl = item.ImgUrl,
SkuId = item.SkuId
};
if (item.OldActive == null)
{
item.OldActive = "[]";
}
if (item.NewActive == null)
{
item.NewActive = "[]";
}
change.OldProList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ProListModel>>(item.OldActive);
change.NewProList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ProListModel>>(item.NewActive);
Application.Current.Dispatcher.Invoke(() =>
{
ItemList.Add(change);
});
});
}
break;
case ProType.Coupons:
{
var data = list.Where(c => c.NewCoupons != c.OldCoupons).ToList();
data.ForEach(item =>
{
ItemData change = new ItemData()
{
Date = item.Date,
Desc = item.Desc,
ImgUrl = item.ImgUrl,
SkuId = item.SkuId
};
if (item.OldCoupons == null)
{
item.OldCoupons = "[]";
}
if (item.NewCoupons == null)
{
item.NewCoupons = "[]";
}
change.OldProList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ProListModel>>(item.OldCoupons);
change.NewProList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ProListModel>>(item.NewCoupons);
Application.Current.Dispatcher.Invoke(() =>
{
ItemList.Add(change);
});
});
}
break;
case ProType.Promotion:
{
var data = list.Where(c => c.NewPromotion != c.OldPromotion).ToList();
data.ForEach(item =>
{
ItemData change = new ItemData()
{
Date = item.Date,
Desc = item.Desc,
ImgUrl = item.ImgUrl,
SkuId = item.SkuId
};
if (item.OldPromotion == null)
{
item.OldPromotion = "[]";
}
if (item.NewPromotion == null)
{
item.NewPromotion = "[]";
}
change.OldProList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ProListModel>>(item.OldPromotion);
change.NewProList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ProListModel>>(item.NewPromotion);
Application.Current.Dispatcher.Invoke(() =>
{
ItemList.Add(change);
});
});
}
break;
case ProType.Price:
{
var data = list.Where(c => c.NewPrice != c.OldPrice).ToList();
data.ForEach(item =>
{
ItemData change = new ItemData()
{
Date = item.Date,
Desc = item.Desc,
ImgUrl = item.ImgUrl,
SkuId = item.SkuId
};
change.OldPro = item.OldPrice.ToString();
change.NewPro = item.NewPrice.ToString();
Application.Current.Dispatcher.Invoke(() =>
{
ItemList.Add(change);
});
});
}
break;
}
}
/// <summary>
@ -276,6 +456,176 @@ namespace JdShopListener
}
}
/// <summary>
/// 执行任务
/// </summary>
public void DoWork()
{
//开始监控全部
foreach (var sku in SkuList)
{
var lastSku = LastItemChangeList.FirstOrDefault(c => c.SkuId == sku.SkuId);
if(lastSku==null)
{
lastSku = new ItemChangeModel();
}
ItemChangeModel item = new ItemChangeModel()
{
Date = DateTime.Now,
Desc = sku.Desc,
ImgUrl = sku.ImgUrl,
SkuId = sku.SkuId
};
var result = GetWareBusiness(sku);
//价格
decimal? price = result.price.p;
if (price == null || price == 0)
{
price = result.price.op ?? 0;
}
item.NewPrice = (decimal)price;
#region 活动
var newActive = new List<object>();
//活动
var miaoshaInfo = result.miaoshaInfo;
//判断是否有活动
if (miaoshaInfo != null)
{
string miaoshaTitle = miaoshaInfo.title;
DateTime endDate = ToDateTime((long)miaoshaInfo.endTime);
decimal? msPrice = result.price.p;
newActive.Add(new { title= $"{miaoshaTitle} 价格:{msPrice} 结束时间:{endDate.ToString("yyyy-MM-dd")}", value=string.Empty });
}
//活动
var yuyueInfo = result.yuyueInfo;
if (yuyueInfo != null)
{
string title = "预约抢购";
string time = yuyueInfo.buyTime;
time = (time.Split("-")[3] + time.Split("-")[4] + time.Split("-")[5]);
//DateTime endDate = ToDateTime((long)yuyueInfo.endTime);
decimal? msPrice = result.price.p;
newActive.Add(new { title= $"{title} 价格:{msPrice} 结束时间:{time}", value=string.Empty });
}
item.NewActive = Newtonsoft.Json.JsonConvert.SerializeObject(newActive);
#endregion
#region 促销
var newPromotion = new List<object>();
//促销
var promotion = result.promotion;
if (promotion != null)
{
//促销活动列表
var activity = promotion.activity;
if (activity != null)
{
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)
{
value += gif.skuId + ",";
}
if (!string.IsNullOrEmpty(value))
{
newPromotion.Add(new { title, value });
}
}
item.NewPromotion = Newtonsoft.Json.JsonConvert.SerializeObject( newPromotion);
}
#endregion
#region 优惠券
var newCoupons = new List<object>();
//优惠券
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.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.OldCoupons;
item.OldPrice = lastSku.OldPrice;
item.OldPromotion = lastSku.OldPromotion;
DbHelper.Db.AddItemChangeModel(item);
}
Thread.Sleep(3000);
}
}
private dynamic GetWareBusiness(SkuModel model)
{
string url = $"https://item-soa.jd.com/getWareBusiness?callback=jQuery6834366&skuId={model.SkuId}&cat={model.Cat}&area=19_1666_1669_0&shopId={model.ShopId}&venderId={model.VenderId}&paramJson=%7B%22platform2%22%3A%221%22%2C%22colType%22%3A0%2C%22specialAttrStr%22%3A%22p0ppp1pppppp3ppppppppppp%22%2C%22skuMarkStr%22%3A%2200%22%7D&num=1";
HttpClient client = new HttpClient();
var json= client.GetStringAsync(url).Result;
json = json.Remove(0, "jQuery6834366(".Length);
json = json.Remove(json.Length - 1, 1);
var data= Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
return data;
}
/// <summary>
/// 将时间戳转换为时间
/// </summary>
/// <returns></returns>
public DateTime ToDateTime(long TimeStamps)
{
var date = new DateTime(1970, 1, 1).AddMilliseconds(TimeStamps);
//new DateTime().AddMilliseconds(621355968000000000/10000).AddMilliseconds(TimeStamps);//效果同上
return date;
}
}

29
JdShopListener/JdShopListener/Migrations/20210413085518_InitialCreate.Designer.cs → JdShopListener/JdShopListener/Migrations/20210414042219_InitialCreate.Designer.cs

@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace JdShopListener.Migrations
{
[DbContext(typeof(JdDBContext))]
[Migration("20210413085518_InitialCreate")]
[Migration("20210414042219_InitialCreate")]
partial class InitialCreate
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -42,6 +42,24 @@ namespace JdShopListener.Migrations
b.Property<string>("SkuId")
.HasColumnType("TEXT");
b.Property<string>("newActive")
.HasColumnType("TEXT");
b.Property<string>("newCoupons")
.HasColumnType("TEXT");
b.Property<string>("newPromotion")
.HasColumnType("TEXT");
b.Property<string>("oldActive")
.HasColumnType("TEXT");
b.Property<string>("oldCoupons")
.HasColumnType("TEXT");
b.Property<string>("oldPromotion")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("ItemChange");
@ -53,12 +71,18 @@ namespace JdShopListener.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<string>("Cat")
.HasColumnType("TEXT");
b.Property<string>("Desc")
.HasColumnType("TEXT");
b.Property<string>("ImgUrl")
.HasColumnType("TEXT");
b.Property<string>("ShopId")
.HasColumnType("TEXT");
b.Property<string>("SkuId")
.HasColumnType("TEXT");
@ -68,6 +92,9 @@ namespace JdShopListener.Migrations
b.Property<string>("Title")
.HasColumnType("TEXT");
b.Property<string>("VenderId")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("SkuModel");

11
JdShopListener/JdShopListener/Migrations/20210413085518_InitialCreate.cs → JdShopListener/JdShopListener/Migrations/20210414042219_InitialCreate.cs

@ -17,6 +17,12 @@ 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),
Date = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
@ -33,7 +39,10 @@ namespace JdShopListener.Migrations
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)
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)
},
constraints: table =>
{

27
JdShopListener/JdShopListener/Migrations/JdDBContextModelSnapshot.cs

@ -40,6 +40,24 @@ namespace JdShopListener.Migrations
b.Property<string>("SkuId")
.HasColumnType("TEXT");
b.Property<string>("newActive")
.HasColumnType("TEXT");
b.Property<string>("newCoupons")
.HasColumnType("TEXT");
b.Property<string>("newPromotion")
.HasColumnType("TEXT");
b.Property<string>("oldActive")
.HasColumnType("TEXT");
b.Property<string>("oldCoupons")
.HasColumnType("TEXT");
b.Property<string>("oldPromotion")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("ItemChange");
@ -51,12 +69,18 @@ namespace JdShopListener.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b.Property<string>("Cat")
.HasColumnType("TEXT");
b.Property<string>("Desc")
.HasColumnType("TEXT");
b.Property<string>("ImgUrl")
.HasColumnType("TEXT");
b.Property<string>("ShopId")
.HasColumnType("TEXT");
b.Property<string>("SkuId")
.HasColumnType("TEXT");
@ -66,6 +90,9 @@ namespace JdShopListener.Migrations
b.Property<string>("Title")
.HasColumnType("TEXT");
b.Property<string>("VenderId")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("SkuModel");

106
JdShopListener/JdShopListener/Models/ItemChangeModel.cs

@ -68,115 +68,21 @@ namespace JdShopListener.Models
}
private string oldActive;
/// <summary>
/// 旧活动
/// </summary>
[NotMapped]
public List<string> OldActive
{
get
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(oldActive);
}
set
{
oldActive= Newtonsoft.Json.JsonConvert.SerializeObject(value);
}
}
private string newActive;
/// <summary>
/// 新活动
/// </summary>
[NotMapped]
public List<string> NewActive
{
get
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(newActive);
}
public string OldActive { get; set; }
set
{
newActive = Newtonsoft.Json.JsonConvert.SerializeObject(value);
}
}
public string NewActive { get; set; }
private string oldPromotion;
/// <summary>
/// 旧促销
/// </summary>
[NotMapped]
public List<string> OldPromotion
{
get
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(oldPromotion);
}
set
{
oldPromotion = Newtonsoft.Json.JsonConvert.SerializeObject(value);
}
}
public string OldPromotion { get; set; }
private string newPromotion;
/// <summary>
/// 新促销
/// </summary>
[NotMapped]
public List<string> NewPromotion
{
get
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(newPromotion);
}
public string NewPromotion { get; set; }
set
{
newPromotion = Newtonsoft.Json.JsonConvert.SerializeObject(value);
}
}
private string oldCoupons;
/// <summary>
/// 旧优惠券
/// </summary>
[NotMapped]
public List<string> OldCoupons
{
get
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(oldCoupons);
}
public string OldCoupons { get; set; }
set
{
oldCoupons = Newtonsoft.Json.JsonConvert.SerializeObject(value);
}
}
private string newCoupons;
/// <summary>
/// 新优惠券
/// </summary>
[NotMapped]
public List<string> NewCoupons
{
get
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(newCoupons);
}
public string NewCoupons { get; set; }
set
{
newCoupons = Newtonsoft.Json.JsonConvert.SerializeObject(value);
}
}
private DateTime _Date;
/// <summary>

15
JdShopListener/JdShopListener/Models/ItemData.cs

@ -5,24 +5,31 @@ using System.Text;
namespace JdShopListener.Models
{
public class ProListModel
{
public string Title { get; set; }
public string Value { get; set; }
}
public class ItemData : ViewModelBase
{
private List<string> _OldProList;
private List<ProListModel> _OldProList;
/// <summary>
/// 旧的数据变化集合
/// </summary>
public List<string> OldProList
public List<ProListModel> OldProList
{
get { return _OldProList; }
set { Set(ref _OldProList, value); }
}
private List<string> _NewProList;
private List<ProListModel> _NewProList;
/// <summary>
/// 新的数据变化集合
/// </summary>
public List<string> NewProList
public List<ProListModel> NewProList
{
get { return _NewProList; }
set { Set(ref _NewProList, value); }

31
JdShopListener/JdShopListener/Models/SkuModel.cs

@ -55,6 +55,17 @@ namespace JdShopListener.Models
set { Set(ref _Desc, value); }
}
private string _Cat;
/// <summary>
/// 类目
/// </summary>
public string Cat
{
get { return _Cat; }
set { Set(ref _Cat, value); }
}
private string _Title;
/// <summary>
/// 标题
@ -65,5 +76,25 @@ namespace JdShopListener.Models
set { Set(ref _Title, value); }
}
private string _ShopId;
/// <summary>
/// 店铺Id
/// </summary>
public string ShopId
{
get { return _ShopId; }
set { Set(ref _ShopId, value); }
}
private string _VenderId;
/// <summary>
/// VenderId
/// </summary>
public string VenderId
{
get { return _VenderId; }
set { Set(ref _VenderId, value); }
}
}
}

13
JdShopListener/JdShopListener/SqlHelpers/DbHelper.cs

@ -109,6 +109,19 @@ namespace JdShopListener.SqlHelpers
}
}
/// <summary>
/// 获取全部sku最后一条变化记录
/// </summary>
/// <returns></returns>
public List<ItemChangeModel> GetLastItemChanges()
{
using (var db = new JdDBContext())
{
return db.ItemChanges.ToList().GroupBy(c => c.SkuId).Select(c=>c.OrderByDescending(c => c.Date).FirstOrDefault()).ToList();
}
}
/// <summary>
/// 根据日期批量获取变化记录

BIN
JdShopListener/JdShopListener/db/items.db

Binary file not shown.
Loading…
Cancel
Save